forked from m-messiah/ip2geo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtor.go
More file actions
126 lines (119 loc) · 2.61 KB
/
tor.go
File metadata and controls
126 lines (119 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bufio"
"errors"
"fmt"
"io"
"net/http"
"sort"
"strings"
)
func torGenerate(outputDir string, errors_chan chan Error) {
torLists := make(chan map[string]bool, 2)
go torBlutmagieDownload(torLists)
go torTorProjectDownload(torLists)
torlist := torMerge(torLists)
if torlist != nil && len(torlist) > 0 {
printMessage("TOR", "Merge", "OK")
} else {
errors_chan <- Error{errors.New("torlist empty"), "TOR", "Merge"}
return
}
if err := torWriteMap(outputDir, torlist); err != nil {
errors_chan <- Error{err, "TOR", "nginx"}
return
} else {
printMessage("TOR", "Write nginx maps", "OK")
}
errors_chan <- Error{err: nil}
}
func torBlutmagieDownload(ch chan map[string]bool) {
resp, err := http.Get("https://torstatus.blutmagie.de/ip_list_exit.php/Tor_ip_list_EXIT.csv")
if err != nil {
printMessage("TOR", "Blutmagie Download", "FAIL")
ch <- nil
return
}
defer resp.Body.Close()
torlist := make(map[string]bool)
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadString('\n')
// Stop at EOF.
if err == io.EOF {
break
}
if err != nil {
printMessage("TOR", "can't read line from blutmagie", "WARN")
continue
}
if len(line) < 1 {
continue
}
torlist[strings.TrimSpace(line)] = true
}
printMessage("TOR", "Blutmagie Download", "OK")
ch <- torlist
}
func torTorProjectDownload(ch chan map[string]bool) {
resp, err := http.Get("https://check.torproject.org/exit-addresses")
if err != nil {
printMessage("TOR", "Torproject Download", "FAIL")
ch <- nil
return
}
defer resp.Body.Close()
torproject := make(map[string]bool)
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
printMessage("TOR", "Can't read line from torproject", "WARN")
continue
}
if len(line) < 1 {
continue
}
if !strings.Contains(line, "ExitAddress") {
continue
}
fields := strings.Fields(line)
torproject[fields[1]] = true
}
printMessage("TOR", "Torproject Download", "OK")
ch <- torproject
}
func torMerge(ch chan map[string]bool) IPList {
result := make(map[string]bool)
for i := 0; i < 2; i++ {
m := <-ch
if m == nil {
continue
}
for k, v := range m {
result[k] = v
}
}
ipList := make(IPList, len(result))
i := 0
for ip := range result {
ipList[i] = ip
i++
}
sort.Sort(ipList)
return ipList
}
func torWriteMap(outputDir string, torlist IPList) error {
tor, err := openMapFile(outputDir, "tor.txt")
if err != nil {
return err
}
defer tor.Close()
for _, ip := range torlist {
fmt.Fprintf(tor, "%s-%s 1;\n", ip, ip)
}
return nil
}