Skip to content

Commit eb83a69

Browse files
committed
v1.4.1
update cache for auto mode
1 parent b45a398 commit eb83a69

File tree

7 files changed

+67
-46
lines changed

7 files changed

+67
-46
lines changed

Dockerfile

Lines changed: 0 additions & 23 deletions
This file was deleted.

auto.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,16 @@ func AutoCheck(address string) bool {
113113

114114
return result.Access
115115
}
116+
117+
func AutoCheckUpdate(address string, access bool) {
118+
autoCtrl.RLock()
119+
result, ok := autoCtrl.cache[address]
120+
autoCtrl.RUnlock()
121+
122+
if !ok || result.Access != access {
123+
autoCtrl.Lock()
124+
autoCtrl.cache[address] = LocalAccessInfo{address, access}
125+
syncToFile()
126+
autoCtrl.Unlock()
127+
}
128+
}

engin/access.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ type HttpAccess struct {
2222
httpserver *http.Server
2323
sync.WaitGroup
2424

25-
authHandler func(auth *AuthInfo) bool
26-
forwardHandler func(address string, r *http.Request) Forward
27-
defaultForward Forward
25+
authHandler func(auth *AuthInfo) bool
26+
forwardHandler func(address string, r *http.Request) Forward
27+
forwardUpdateHandler func(address string, forward Forward)
28+
defaultForward Forward
2829

2930
session int32
3031
}
@@ -33,6 +34,7 @@ type Access interface {
3334
Shutdown() error
3435
AuthHandlerSet(func(*AuthInfo) bool)
3536
ForwardHandlerSet(func(address string, r *http.Request) Forward)
37+
ForwardUpdateHandlerSet(func(address string, forward Forward))
3638
}
3739

3840
func HttpError(w http.ResponseWriter, err string, code int) {
@@ -77,6 +79,10 @@ func (acc *HttpAccess) ForwardHandlerSet(handler func(address string, r *http.Re
7779
acc.forwardHandler = handler
7880
}
7981

82+
func (acc *HttpAccess) ForwardUpdateHandlerSet(handler func(address string, forward Forward)) {
83+
acc.forwardUpdateHandler = handler
84+
}
85+
8086
func (acc *HttpAccess) AuthHttp(r *http.Request) bool {
8187
if acc.authHandler == nil {
8288
return true
@@ -131,20 +137,21 @@ func (acc *HttpAccess) ServeHTTP(w http.ResponseWriter, r *http.Request) {
131137
return
132138
}
133139

140+
r.Header.Add("AUTOPROXY", GetUUID())
141+
134142
if r.Method == "CONNECT" {
135143
acc.HttpsRoundTripper(w, r)
136144
return
137145
}
138146

139-
r.Header.Add("AUTOPROXY", GetUUID())
140-
141147
acc.SessionAdd()
142148
defer acc.SessionDel()
143149

144150
var rsp *http.Response
145151
var err error
146152

147153
if !r.URL.IsAbs() {
154+
logs.Info("the request is not proxy request, transport to local network")
148155
r.URL.Host = r.Host
149156
r.URL.Scheme = "http"
150157
rsp, err = acc.defaultForward.Http(r)

engin/protocal.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,34 @@ import (
99
"github.com/astaxie/beego/logs"
1010
)
1111

12+
func (acc *HttpAccess) ForwardUpdate(address string, forward Forward) {
13+
if acc.forwardUpdateHandler != nil {
14+
acc.forwardUpdateHandler(address, forward)
15+
}
16+
}
17+
1218
func (acc *HttpAccess) HttpsForward(address string, r *http.Request) (net.Conn, error) {
13-
if acc.forwardHandler != nil {
14-
forward := acc.forwardHandler(address, r)
15-
return forward.Https(address, r)
19+
if acc.forwardHandler == nil {
20+
return nil, fmt.Errorf("forward handler is null")
21+
}
22+
forward := acc.forwardHandler(address, r)
23+
conn, err := forward.Https(address, r)
24+
if err != nil {
25+
acc.ForwardUpdate(address, forward)
1626
}
17-
return nil, fmt.Errorf("forward handler is null")
27+
return conn, err
1828
}
1929

2030
func (acc *HttpAccess) HttpForward(address string, r *http.Request) (*http.Response, error) {
21-
if acc.forwardHandler != nil {
22-
forward := acc.forwardHandler(address, r)
23-
return forward.Http(r)
31+
if acc.forwardHandler == nil {
32+
return nil, fmt.Errorf("forward handler is null")
2433
}
25-
return nil, fmt.Errorf("forward handler is null")
34+
forward := acc.forwardHandler(address, r)
35+
conn, err := forward.Http(r)
36+
if err != nil {
37+
acc.ForwardUpdate(address, forward)
38+
}
39+
return conn, err
2640
}
2741

2842
func (acc *HttpAccess) HttpsRoundTripper(w http.ResponseWriter, r *http.Request) {
@@ -39,21 +53,21 @@ func (acc *HttpAccess) HttpsRoundTripper(w http.ResponseWriter, r *http.Request)
3953

4054
address := Address(r.URL)
4155

42-
connection := fmt.Sprintf("HTTP/1.1 200 Connection Established\r\nAUTOPROXY:%s\r\n\r\n", GetUUID())
43-
44-
err = WriteFull(client, []byte(connection))
56+
server, err := acc.HttpsForward(address, r)
4557
if err != nil {
46-
errstr := fmt.Sprintf("client connect %s fail", client.RemoteAddr())
58+
errstr := fmt.Sprintf("can't forward hostname %s", address)
4759
logs.Error(errstr, err.Error())
4860
HttpError(w, errstr, http.StatusInternalServerError)
4961

5062
client.Close()
5163
return
5264
}
5365

54-
server, err := acc.HttpsForward(address, r)
66+
connection := fmt.Sprintf("HTTP/1.1 200 Connection Established\r\n\r\n")
67+
68+
err = WriteFull(client, []byte(connection))
5569
if err != nil {
56-
errstr := fmt.Sprintf("can't forward hostname %s", address)
70+
errstr := fmt.Sprintf("client connect %s fail", client.RemoteAddr())
5771
logs.Error(errstr, err.Error())
5872
HttpError(w, errstr, http.StatusInternalServerError)
5973

log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
)
66

77
func LogInit(debug bool) error {
8+
logs.SetLogger(logs.AdapterConsole)
89
if debug {
9-
logs.SetLogger(logs.AdapterConsole)
1010
logs.EnableFuncCallDepth(true)
1111
logs.SetLogFuncCallDepth(3)
1212
}

main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ func init() {
4242
flag.StringVar(&LocalAddr, "local-address", "http://0.0.0.0:8080", "Local proxy listening address")
4343
flag.StringVar(&LocalAuth, "local-auth", "", "Local proxy auth username and password")
4444

45-
flag.StringVar(&RemoteAddr, "remote-address", "https://you.domain.com:8080", "Remote proxy listening address")
45+
flag.StringVar(&RemoteAddr, "remote-address", "https://my.domain:8080", "Remote proxy listening address")
4646
flag.StringVar(&RemoteAuth, "remote-auth", "", "Remote proxy auth username and password")
4747

4848
flag.StringVar(&RunMode, "mode", "proxy", "proxy mode(local/proxy/domain/auto)")
4949
flag.StringVar(&DomainFile, "domain", "domain.json", "match domain list file(domain mode requires)")
5050

51-
flag.BoolVar(&Debug, "debug", false, "enable debug")
51+
flag.BoolVar(&Debug, "debug", false, "enable enhanced logger")
5252
flag.BoolVar(&Help, "help", false, "usage help")
5353
}
5454

@@ -166,8 +166,18 @@ func DomainForwardFunc(address string, r *http.Request) engin.Forward {
166166
return LocalForward
167167
}
168168

169+
func AutoForwardUpdate(address string, forward engin.Forward) {
170+
if forward == LocalForward {
171+
AutoCheckUpdate(address, false)
172+
}
173+
if forward == RemoteForward {
174+
AutoCheckUpdate(address, true)
175+
}
176+
}
177+
169178
func AutoForwardFunc(address string, r *http.Request) engin.Forward {
170179
if AutoCheck(address) {
180+
logs.Info("%s auto forward to local network", address)
171181
return LocalForward
172182
}
173183
logs.Info("%s auto forward to remote proxy", address)

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package main
22

33
func VersionGet() string {
4-
return "v1.4.0"
4+
return "v1.4.1"
55
}

0 commit comments

Comments
 (0)