Skip to content

Commit

Permalink
v1.4.1
Browse files Browse the repository at this point in the history
update cache for auto mode
  • Loading branch information
linimbus committed Dec 12, 2022
1 parent b45a398 commit eb83a69
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 46 deletions.
23 changes: 0 additions & 23 deletions Dockerfile

This file was deleted.

13 changes: 13 additions & 0 deletions auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,16 @@ func AutoCheck(address string) bool {

return result.Access
}

func AutoCheckUpdate(address string, access bool) {
autoCtrl.RLock()
result, ok := autoCtrl.cache[address]
autoCtrl.RUnlock()

if !ok || result.Access != access {
autoCtrl.Lock()
autoCtrl.cache[address] = LocalAccessInfo{address, access}
syncToFile()
autoCtrl.Unlock()
}
}
17 changes: 12 additions & 5 deletions engin/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type HttpAccess struct {
httpserver *http.Server
sync.WaitGroup

authHandler func(auth *AuthInfo) bool
forwardHandler func(address string, r *http.Request) Forward
defaultForward Forward
authHandler func(auth *AuthInfo) bool
forwardHandler func(address string, r *http.Request) Forward
forwardUpdateHandler func(address string, forward Forward)
defaultForward Forward

session int32
}
Expand All @@ -33,6 +34,7 @@ type Access interface {
Shutdown() error
AuthHandlerSet(func(*AuthInfo) bool)
ForwardHandlerSet(func(address string, r *http.Request) Forward)
ForwardUpdateHandlerSet(func(address string, forward Forward))
}

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

func (acc *HttpAccess) ForwardUpdateHandlerSet(handler func(address string, forward Forward)) {
acc.forwardUpdateHandler = handler
}

func (acc *HttpAccess) AuthHttp(r *http.Request) bool {
if acc.authHandler == nil {
return true
Expand Down Expand Up @@ -131,20 +137,21 @@ func (acc *HttpAccess) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

r.Header.Add("AUTOPROXY", GetUUID())

if r.Method == "CONNECT" {
acc.HttpsRoundTripper(w, r)
return
}

r.Header.Add("AUTOPROXY", GetUUID())

acc.SessionAdd()
defer acc.SessionDel()

var rsp *http.Response
var err error

if !r.URL.IsAbs() {
logs.Info("the request is not proxy request, transport to local network")
r.URL.Host = r.Host
r.URL.Scheme = "http"
rsp, err = acc.defaultForward.Http(r)
Expand Down
42 changes: 28 additions & 14 deletions engin/protocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,34 @@ import (
"github.com/astaxie/beego/logs"
)

func (acc *HttpAccess) ForwardUpdate(address string, forward Forward) {
if acc.forwardUpdateHandler != nil {
acc.forwardUpdateHandler(address, forward)
}
}

func (acc *HttpAccess) HttpsForward(address string, r *http.Request) (net.Conn, error) {
if acc.forwardHandler != nil {
forward := acc.forwardHandler(address, r)
return forward.Https(address, r)
if acc.forwardHandler == nil {
return nil, fmt.Errorf("forward handler is null")
}
forward := acc.forwardHandler(address, r)
conn, err := forward.Https(address, r)
if err != nil {
acc.ForwardUpdate(address, forward)
}
return nil, fmt.Errorf("forward handler is null")
return conn, err
}

func (acc *HttpAccess) HttpForward(address string, r *http.Request) (*http.Response, error) {
if acc.forwardHandler != nil {
forward := acc.forwardHandler(address, r)
return forward.Http(r)
if acc.forwardHandler == nil {
return nil, fmt.Errorf("forward handler is null")
}
return nil, fmt.Errorf("forward handler is null")
forward := acc.forwardHandler(address, r)
conn, err := forward.Http(r)
if err != nil {
acc.ForwardUpdate(address, forward)
}
return conn, err
}

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

address := Address(r.URL)

connection := fmt.Sprintf("HTTP/1.1 200 Connection Established\r\nAUTOPROXY:%s\r\n\r\n", GetUUID())

err = WriteFull(client, []byte(connection))
server, err := acc.HttpsForward(address, r)
if err != nil {
errstr := fmt.Sprintf("client connect %s fail", client.RemoteAddr())
errstr := fmt.Sprintf("can't forward hostname %s", address)
logs.Error(errstr, err.Error())
HttpError(w, errstr, http.StatusInternalServerError)

client.Close()
return
}

server, err := acc.HttpsForward(address, r)
connection := fmt.Sprintf("HTTP/1.1 200 Connection Established\r\n\r\n")

err = WriteFull(client, []byte(connection))
if err != nil {
errstr := fmt.Sprintf("can't forward hostname %s", address)
errstr := fmt.Sprintf("client connect %s fail", client.RemoteAddr())
logs.Error(errstr, err.Error())
HttpError(w, errstr, http.StatusInternalServerError)

Expand Down
2 changes: 1 addition & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

func LogInit(debug bool) error {
logs.SetLogger(logs.AdapterConsole)
if debug {
logs.SetLogger(logs.AdapterConsole)
logs.EnableFuncCallDepth(true)
logs.SetLogFuncCallDepth(3)
}
Expand Down
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ func init() {
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://you.domain.com:8080", "Remote proxy listening address")
flag.StringVar(&RemoteAddr, "remote-address", "https://my.domain: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.BoolVar(&Debug, "debug", false, "enable debug")
flag.BoolVar(&Debug, "debug", false, "enable enhanced logger")
flag.BoolVar(&Help, "help", false, "usage help")
}

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

func AutoForwardUpdate(address string, forward engin.Forward) {
if forward == LocalForward {
AutoCheckUpdate(address, false)
}
if forward == RemoteForward {
AutoCheckUpdate(address, true)
}
}

func AutoForwardFunc(address string, r *http.Request) engin.Forward {
if AutoCheck(address) {
logs.Info("%s auto forward to local network", address)
return LocalForward
}
logs.Info("%s auto forward to remote proxy", address)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package main

func VersionGet() string {
return "v1.4.0"
return "v1.4.1"
}

0 comments on commit eb83a69

Please sign in to comment.