-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
222 lines (192 loc) · 5.38 KB
/
main.go
File metadata and controls
222 lines (192 loc) · 5.38 KB
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
213
214
215
216
217
218
219
220
221
222
package main
import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"github.com/astaxie/beego/logs"
"github.com/easymesh/autoproxy/engin"
)
var (
Help bool
Timeout int
LocalAddr string
LocalAuth string
RemoteAddr string
RemoteAuth string
RunMode string // local、proxy、domain、auto
DomainFile string
CertFile string
KeyFile string
LogFile string
Stat bool
)
func init() {
flag.StringVar(&KeyFile, "key-file", "", "tls key file pem format, if not set, the program will automatically generate")
flag.StringVar(&CertFile, "cert-file", "", "tls cert file pem format, if not set, the program will automatically generate")
flag.IntVar(&Timeout, "timeout", 30, "connect timeout (unit second)")
flag.StringVar(&LocalAddr, "local-address", "http://0.0.0.0:8080", "Local proxy listening address")
flag.StringVar(&LocalAuth, "local-auth", "", "Local proxy auth username and password")
flag.StringVar(&RemoteAddr, "remote-address", "https://your.vps:8080", "Remote proxy listening address")
flag.StringVar(&RemoteAuth, "remote-auth", "", "Remote proxy auth username and password")
flag.StringVar(&RunMode, "mode", "proxy", "proxy mode(local/proxy/domain/auto)")
flag.StringVar(&DomainFile, "domain", "domain.json", "match domain list file(domain mode requires)")
flag.StringVar(&LogFile, "logfile", "", "the logging file, using the stdout as default.")
flag.BoolVar(&Help, "help", false, "usage help")
flag.BoolVar(&Stat, "stat", false, "display the traffic statistics in a loop timer")
}
func parseAuth(auth string) (*engin.AuthInfo, error) {
if auth == "" {
return nil, nil
}
list := strings.Split(auth, ":")
if len(list) != 2 {
return nil, fmt.Errorf("authentication information '%s' is incorrect", auth)
}
return &engin.AuthInfo{User: list[0], Token: list[1]}, nil
}
func parseAddress(addr string) (string, string, error) {
ul, err := url.Parse(addr)
if err != nil {
return "", "", err
}
return strings.ToLower(ul.Scheme), engin.Address(ul), nil
}
func LocalAccessInit(scheme string, address string, auth *engin.AuthInfo) (engin.Access, error) {
var tlsEnable bool
if scheme == "https" {
tlsEnable = true
}
access, err := engin.NewHttpsAccess(address, Timeout, tlsEnable, CertFile, KeyFile)
if err != nil {
logs.Error(err.Error())
return nil, err
}
if auth != nil {
logs.Info("local service enable auth [%s:%s]", auth.User, auth.Token)
access.AuthHandlerSet(func(info *engin.AuthInfo) bool {
if info == nil {
logs.Info("auth request auth not exist")
return false
}
logs.Info("auth request auth [%s:%s]", info.User, info.Token)
if info.User == auth.User && info.Token == auth.Token {
logs.Info("auth passed")
return true
}
logs.Info("auth fail, not match")
return false
})
}
return access, nil
}
func RemoteForwardInit(scheme string, address string, auth *engin.AuthInfo) (engin.Forward, error) {
var tlsEnable bool
if scheme == "https" {
tlsEnable = true
}
forward, err := engin.NewHttpProxyForward(address, Timeout, auth, tlsEnable, CertFile, KeyFile)
if err != nil {
logs.Error(err.Error())
return nil, err
}
return forward, nil
}
func main() {
flag.Parse()
if Help {
flag.Usage()
return
}
LogInit(LogFile)
scheme, address, err := parseAddress(LocalAddr)
if err != nil {
panic(err.Error())
}
auth, err := parseAuth(LocalAuth)
if err != nil {
panic(err.Error())
}
var acc engin.Access
acc, err = LocalAccessInit(scheme, address, auth)
if err != nil {
panic(err.Error())
}
var local, proxy engin.Forward
local = engin.NewLocalForward(Timeout)
if strings.ToLower(RunMode) != "local" {
scheme, address, err = parseAddress(RemoteAddr)
if err != nil {
panic(err.Error())
}
auth, err = parseAuth(RemoteAuth)
if err != nil {
panic(err.Error())
}
proxy, err = RemoteForwardInit(scheme, address, auth)
if err != nil {
panic(err.Error())
}
}
switch strings.ToLower(RunMode) {
case "domain":
{
DomainInit(DomainFile)
acc.ForwardHandlerSet(func(address string, r *http.Request) engin.Forward {
if DomainCheck(address) {
logs.Info("%s auto forward to remote proxy", address)
return proxy
}
return local
})
}
case "auto":
{
AutoInit()
acc.ForwardHandlerSet(func(address string, r *http.Request) engin.Forward {
if AutoCheck(address) {
logs.Info("%s auto forward to local network", address)
return local
}
logs.Info("%s auto forward to remote proxy", address)
return proxy
})
acc.ForwardUpdateHandlerSet(func(address string, forward engin.Forward) {
if forward == local {
AutoCheckUpdate(address, false)
}
if forward == proxy {
AutoCheckUpdate(address, true)
}
})
}
case "proxy":
acc.ForwardHandlerSet(func(address string, r *http.Request) engin.Forward {
return proxy
})
case "local":
acc.ForwardHandlerSet(func(address string, r *http.Request) engin.Forward {
return local
})
default:
panic(fmt.Sprintf("running mode(%s) not support", RunMode))
}
logs.Info("autoproxy %s instance %s running mode %s success", VersionGet(), engin.GetUUID(), RunMode)
if Stat {
go engin.Display()
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-signalChan
logs.Info("recv signal %s, ready to exit", sig.String())
acc.Shutdown()
local.Close()
if proxy != nil {
proxy.Close()
}
os.Exit(-1)
}