/
libproxyrotator.go
100 lines (80 loc) · 1.78 KB
/
libproxyrotator.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
package libproxyrotator
import (
"context"
"errors"
"io/ioutil"
"log"
"net"
"os"
"github.com/armon/go-socks5"
"github.com/aztecrabbit/liblog"
"github.com/aztecrabbit/libutils"
"golang.org/x/net/proxy"
)
var (
DefaultConfig = &Config{
Port: "3080",
}
)
type Config struct {
Port string
}
type ProxyRotator struct {
Config *Config
Proxies []string
}
func (p *ProxyRotator) AddProxy(address string) {
p.Proxies = append(p.Proxies, address)
}
func (p *ProxyRotator) GetProxy() (string, error) {
libutils.Lock.Lock()
defer libutils.Lock.Unlock()
if len(p.Proxies) == 0 {
return "", errors.New("Proxy Address not found")
}
proxyAddress := p.Proxies[0]
if len(p.Proxies) > 1 {
p.Proxies = append(p.Proxies[1:], p.Proxies[0])
}
return proxyAddress, nil
}
func (p *ProxyRotator) DeleteProxy(address string) {
for i, proxyAddress := range p.Proxies {
if proxyAddress == address {
p.Proxies = append(p.Proxies[:i], p.Proxies[i+1:]...)
break
}
}
}
func (p *ProxyRotator) Start() {
config := &socks5.Config{
Logger: log.New(os.Stdout, "", log.LstdFlags),
Dial: func(ctx context.Context, net_, addr string) (net.Conn, error) {
for i := 0; i < len(p.Proxies); i++ {
proxyAddress, err := p.GetProxy()
if err != nil {
break
}
dialer, err := proxy.SOCKS5("tcp", proxyAddress, nil, proxy.Direct)
if err != nil {
panic(err)
}
data, err := dialer.Dial(net_, addr)
if err != nil {
continue
}
return data, nil
}
return nil, errors.New("proxies not available")
},
}
config.Logger.SetOutput(ioutil.Discard)
server, err := socks5.New(config)
if err != nil {
panic(err)
}
if err := server.ListenAndServe("tcp", "0.0.0.0:"+p.Config.Port); err != nil {
liblog.LogException(err, "INFO")
os.Exit(0)
}
}