-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
152 lines (122 loc) · 3.51 KB
/
server.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
package ip_proxy_pool
import (
"fmt"
"net"
"sync"
"time"
"github.com/huizhang-open-source/ip-proxy-pool/source"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
type Server struct {
sources map[string]source.Source
proxyPool sync.Map
}
var server *Server
func init() {
server = &Server{
sources: map[string]source.Source{},
proxyPool: sync.Map{},
}
sourceIp66 := source.Ip66{}
server.sources[sourceIp66.Config().Name] = sourceIp66
sourceIp89 := source.Ip89{}
server.sources[sourceIp89.Config().Name] = sourceIp89
}
func GetServer() *Server {
return server
}
func (s *Server) RegisterSources(sources []source.Source) *Server {
for _, item := range sources {
s.sources[item.Config().Name] = item
}
return s
}
func (s *Server) Start() {
for _, sourceItem := range s.sources {
s.registeredProxy(sourceItem)
s.monitorProxyHealth(sourceItem)
}
}
func (s *Server) registeredProxy(sourceItem source.Source) {
go func(sourceItem source.Source) {
for true {
proxyItems := sourceItem.Exec()
s.registeredProxyToPool(sourceItem, proxyItems)
time.Sleep(time.Duration(sourceItem.Config().ExecRate) * time.Second)
}
}(sourceItem)
}
func (s *Server) monitorProxyHealth(sourceItem source.Source) {
go func(sourceItem source.Source) {
for true {
s.proxyPool.Range(func(key, value interface{}) bool {
proxy := value.(source.Proxy)
proxyKey := fmt.Sprintf("%s_%s_%d", sourceItem.Config().Name, proxy.Ip, proxy.Port)
if cast.ToString(key) == proxyKey {
if !s.ipPortIsEffect(proxy.Ip, proxy.Port, sourceItem.Config().CheckIpPortTimeout) {
s.proxyPool.Delete(proxyKey)
logrus.WithFields(logrus.Fields{
"source_name": sourceItem.Config().Name,
"ip": proxy.Ip,
"port": proxy.Port,
}).Infof("[Server.monitorProxyHealth] Ip:port invalid")
}
}
return true
})
time.Sleep(time.Duration(sourceItem.Config().CheckIpPortRate) * time.Second)
}
}(sourceItem)
}
func (s *Server) registeredProxyToPool(sourceItem source.Source, proxyItems []source.Proxy) {
for _, proxyItem := range proxyItems {
proxyKey := fmt.Sprintf("%s_%s_%d", sourceItem.Config().Name, proxyItem.Ip, proxyItem.Port)
if _, ok := s.proxyPool.Load(proxyKey); ok {
continue
}
if !s.ipPortIsEffect(proxyItem.Ip, proxyItem.Port, sourceItem.Config().CheckIpPortTimeout) {
s.proxyPool.Delete(proxyKey)
logrus.WithFields(logrus.Fields{
"source_name": sourceItem.Config().Name,
"ip": proxyItem.Ip,
"port": proxyItem.Port,
}).Infof("[Server.registeredProxyToPool] Ip:port invalid")
continue
}
if _, ok := s.proxyPool.LoadOrStore(proxyKey, proxyItem); !ok {
logrus.WithFields(logrus.Fields{
"source_name": sourceItem.Config().Name,
"ip": proxyItem.Ip,
"port": proxyItem.Port,
}).Infof("[Server.registeredProxyToPool] Registered proxy pool success")
}
}
}
func (s *Server) GetProxyPoolTotal() int {
var result int
s.proxyPool.Range(func(key, value interface{}) bool {
result++
return true
})
return result
}
func (s *Server) RandomGetOneProxy() source.Proxy {
var result source.Proxy
s.proxyPool.Range(func(key, value interface{}) bool {
result = value.(source.Proxy)
return false
})
return result
}
func (*Server) ipPortIsEffect(ip string, port int, timeout int) bool {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Duration(timeout)*time.Second)
if err != nil {
return false
}
if conn == nil {
return false
}
defer conn.Close()
return true
}