-
Notifications
You must be signed in to change notification settings - Fork 10
/
easyss_server.go
201 lines (166 loc) · 4.4 KB
/
easyss_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
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
package easyss
import (
"crypto/tls"
"net"
"net/url"
"strconv"
"sync"
"time"
"github.com/nange/easyss/v2/httptunnel"
"github.com/nange/easyss/v2/log"
"github.com/nange/easyss/v2/util"
)
type EasyServer struct {
config *ServerConfig
mu sync.Mutex
ln net.Listener
httpTunnelServer *httptunnel.Server
tlsConfig *tls.Config
nextProxyIPs map[string]struct{}
nextProxyCIDRIPs []*net.IPNet
nextProxyDomains map[string]struct{}
// only used for testing
disableValidateAddr bool
}
func NewServer(config *ServerConfig) (*EasyServer, error) {
es := &EasyServer{config: config}
if u := es.NextProxyURL(); u != nil {
log.Info("[EASYSS_SERVER] next proxy is enabled",
"next_proxy_url", u.String(),
"enable_next_proxy_udp", es.EnableNextProxyUDP(),
"enable_next_proxy_all_host", es.EnableNextProxyALLHost())
if err := es.loadNextProxyIPDomains(); err != nil {
return nil, err
}
es.printNextProxyInfo()
}
return es, nil
}
func (es *EasyServer) loadNextProxyIPDomains() error {
es.nextProxyIPs = make(map[string]struct{})
es.nextProxyDomains = make(map[string]struct{})
nextIPs, err := util.ReadFileLinesMap(es.config.NextProxyIPsFile)
if err != nil {
return err
}
if len(nextIPs) > 0 {
log.Info("[EASYSS_SERVER] load next proxy ips success", "len", len(nextIPs))
for k := range nextIPs {
_, ipnet, err := net.ParseCIDR(k)
if err != nil {
continue
}
if ipnet != nil {
es.nextProxyCIDRIPs = append(es.nextProxyCIDRIPs, ipnet)
delete(nextIPs, k)
}
}
es.nextProxyIPs = nextIPs
}
nextDomains, err := util.ReadFileLinesMap(es.config.NextProxyDomainsFile)
if err != nil {
return err
}
if len(nextDomains) > 0 {
log.Info("[EASYSS_SERVER] load next proxy domains success", "len", len(nextDomains))
es.nextProxyDomains = nextDomains
// not only proxy the domains but also the ips of domains
for domain := range nextDomains {
ips, err := util.LookupIPV4From(DefaultDNSServer, domain)
if err != nil {
log.Warn("[EASYSS_SERVER] lookup ip for", "domain", domain, "err", err)
continue
}
for _, ip := range ips {
es.nextProxyIPs[ip.String()] = struct{}{}
}
}
}
return nil
}
func (es *EasyServer) printNextProxyInfo() {
keys := make([]string, 0, len(es.nextProxyDomains))
for k := range es.nextProxyDomains {
keys = append(keys, k)
}
log.Info("[EASYSS_SERVER] next proxy domains", "domains", keys)
keys = keys[:0]
for k := range es.nextProxyIPs {
keys = append(keys, k)
}
for _, v := range es.nextProxyCIDRIPs {
keys = append(keys, v.String())
}
log.Info("[EASYSS_SERVER] next proxy ips", "ips", keys)
}
func (es *EasyServer) Server() string {
return es.config.Server
}
func (es *EasyServer) ListenAddr() string {
addr := ":" + strconv.Itoa(es.ServerPort())
return addr
}
func (es *EasyServer) ListenHTTPTunnelAddr() string {
addr := ":" + strconv.Itoa(es.HTTPInboundPort())
return addr
}
func (es *EasyServer) DisableTLS() bool {
return es.config.DisableTLS
}
func (es *EasyServer) ServerPort() int {
return es.config.ServerPort
}
func (es *EasyServer) Password() string {
return es.config.Password
}
func (es *EasyServer) Timeout() time.Duration {
return time.Duration(es.config.Timeout) * time.Second
}
func (es *EasyServer) MaxConnWaitTimeout() time.Duration {
return 10 * es.Timeout()
}
func (es *EasyServer) Email() string {
return es.config.Email
}
func (es *EasyServer) CertPath() string {
return es.config.CertPath
}
func (es *EasyServer) KeyPath() string {
return es.config.KeyPath
}
func (es *EasyServer) EnabledHTTPInbound() bool {
return es.config.EnableHTTPInbound
}
func (es *EasyServer) HTTPInboundPort() int {
return es.config.HTTPInboundPort
}
func (es *EasyServer) NextProxyURL() *url.URL {
if es.config.NextProxyURL == "" {
return nil
}
u, _ := url.Parse(es.config.NextProxyURL)
return u
}
func (es *EasyServer) EnableNextProxyUDP() bool {
return es.config.EnableNextProxyUDP
}
func (es *EasyServer) EnableNextProxyALLHost() bool {
return es.config.EnableNextProxyALLHost
}
func (es *EasyServer) NextProxyDomainsFile() string {
return es.config.NextProxyDomainsFile
}
func (es *EasyServer) NextProxyIPsFile() string {
return es.config.NextProxyIPsFile
}
func (es *EasyServer) Close() (err error) {
es.mu.Lock()
defer es.mu.Unlock()
if es.ln != nil {
err = es.ln.Close()
}
if es.httpTunnelServer != nil {
err = es.httpTunnelServer.Close()
}
return
}