-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtor.go
212 lines (178 loc) · 4.11 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/cretz/bine/tor"
log "github.com/sirupsen/logrus"
"golang.org/x/net/proxy"
)
type TorStruct struct {
TorList *tor.Tor
Port string
IPAddr string
Country string
City string
Load int
}
func (p *TorStruct) AddCountry(new string) *TorStruct {
p.Country = new
return p
}
func (p *TorStruct) AddIP(new string) *TorStruct {
p.IPAddr = new
return p
}
func (i *TorStruct) TorStructLoad() *TorStruct {
i.Load++
return i
}
func initTor(n int) ([]TorStruct, error) {
var TorList []TorStruct
var wg sync.WaitGroup
for i := PortUsage; i < PortUsage+n; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup, j int) {
defer wg.Done()
Port := strconv.Itoa(j)
Time := *renewIP * 60
Args := []string{"SOCKSPort", Port, "MaxCircuitDirtiness", strconv.Itoa(Time)}
if *exitNode != "" {
exitNodeSlice := []string{}
for _, v := range strings.Split(*exitNode, ",") {
exitNodeSlice = append(exitNodeSlice, fmt.Sprintf("{%s}", v))
}
Args = append(Args, "StrictNodes", "1", "ExitNodes", strings.Join(exitNodeSlice, ","), "Log", "debug")
}
t, err := tor.Start(context.Background(), &tor.StartConf{
ExePath: *torPath,
ExtraArgs: Args,
EnableNetwork: true,
RetainTempDataDir: false,
})
if err != nil {
log.Fatal(err)
}
log.WithFields(log.Fields{
"Args": Args,
}).Info("Successfully create new tor circuit")
dialSocksProxy, err := proxy.SOCKS5("tcp", *hostNode+":"+Port, nil, proxy.Direct)
if err != nil {
log.Fatal(err)
}
body, _, err := Curl(&http.Client{
Transport: &http.Transport{Dial: dialSocksProxy.Dial},
})
if err != nil {
log.Error(err)
t.Close()
return
}
var ipInfo IpinfoIo
err = json.Unmarshal(body, &ipInfo)
if err != nil {
log.Error(err)
}
log.WithFields(log.Fields{
"RealIP": ipInfoOri.IP,
"TorIP": ipInfo.IP,
"Country": ipInfo.Country,
}).Info("Testing tor circuit")
TorList = append(TorList, TorStruct{
TorList: t,
Port: Port,
Country: ipInfo.Country,
IPAddr: ipInfo.IP,
City: ipInfo.City,
})
}(&wg, i)
}
wg.Wait()
PortUsage = PortUsage + n
return TorList, nil
}
func CurlTor(s string, p TorStruct) (*http.Response, error) {
dialSocksProxy, err := proxy.SOCKS5("tcp", *hostNode+":"+p.Port, nil, proxy.Direct)
if err != nil {
return nil, err
}
tr := &http.Transport{Dial: dialSocksProxy.Dial}
// Create client
myClient := &http.Client{
Transport: tr,
Timeout: 2 * time.Minute,
}
res, err := myClient.Get(s)
if err != nil {
return nil, err
}
return res, nil
}
func TortoMap(p []TorStruct) map[int]interface{} {
A := make(map[int]interface{})
for i, v := range p {
A[i] = map[string]interface{}{
"IP Address": v.IPAddr,
"Socks5": *hostNode + ":" + v.Port,
"Country": v.Country,
"City": v.City,
}
}
return A
}
func (p TorStruct) DeleteCircuit() {
p.TorList.Close()
}
func RemoveTorList(s []TorStruct, index int) []TorStruct {
return append(s[:index], s[index+1:]...)
}
var Counter = 0
//GetTorLB Get one tor slice
func GetTorLB(i []TorStruct) *TorStruct {
Circuit := i[Counter].TorStructLoad()
Counter++
if Counter >= (len(i)) {
Counter = 0
}
return Circuit
}
func GetTorLBWeight(i []TorStruct) *TorStruct {
Circuit := TorStruct{}
for j := 0; j < len(i); j++ {
currenCir := i[j]
nextCir := i[j+1].TorStructLoad()
if nextCir.Load <= currenCir.Load {
Circuit = *nextCir
break
}
}
return &Circuit
}
type IpinfoIo struct {
IP string `json:"ip"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Loc string `json:"loc"`
Org string `json:"org"`
Timezone string `json:"timezone"`
Readme string `json:"readme"`
}
func Curl(c *http.Client) ([]byte, *http.Response, error) {
res, err := c.Get(ifconfig)
if err != nil {
return nil, nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, nil, err
}
return body, res, nil
}