-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
tor.go
140 lines (131 loc) · 2.95 KB
/
tor.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bufio"
"errors"
"fmt"
"io"
"net/http"
"sort"
"strings"
"time"
)
const torListLength = 1
// Tor network lists DB
type Tor struct {
OutputDir string
ErrorsChan chan Error
list IPList
tempLists chan map[string]bool
}
// Generate Tor maps for nginx (download, parse, merge, write)
func (tor *Tor) Generate() {
tor.tempLists = make(chan map[string]bool, torListLength)
// go tor.blutmagieDownload()
go tor.torProjectDownload()
if err := tor.merge(); err != nil {
tor.ErrorsChan <- Error{err, "TOR", "Merge"}
return
}
printMessage("TOR", "Merge", "OK")
if err := tor.writeMap(); err != nil {
tor.ErrorsChan <- Error{err, "TOR", "nginx"}
return
}
printMessage("TOR", "Write nginx maps", "OK")
tor.ErrorsChan <- Error{err: nil}
}
// func (tor *Tor) blutmagieDownload() {
// 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")
// tor.tempLists <- 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")
// tor.tempLists <- torlist
// }
func (tor *Tor) torProjectDownload() {
client := &http.Client{Timeout: time.Second * 30}
resp, err := client.Get("https://check.torproject.org/exit-addresses")
if err != nil {
printMessage("TOR", "Torproject Download", "FAIL")
tor.tempLists <- 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")
tor.tempLists <- torproject
}
func (tor *Tor) merge() error {
result := make(map[string]bool)
for i := 0; i < torListLength; i++ {
m := <-tor.tempLists
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)
tor.list = ipList
if len(tor.list) > 0 {
return nil
}
return errors.New("torlist empty")
}
func (tor *Tor) writeMap() error {
torFile, err := openMapFile(tor.OutputDir, "tor.txt")
if err != nil {
return err
}
defer torFile.Close()
for _, ip := range tor.list {
fmt.Fprintf(torFile, "%s-%s 1;\n", ip, ip)
}
return nil
}