From 6733a719e06dc79d046ab099dc942f8052f73fcb Mon Sep 17 00:00:00 2001 From: Daniel Ding Date: Wed, 1 Jan 2025 11:17:02 +0800 Subject: [PATCH] fea: add user for http proxy. --- cmd/switch/main.go | 3 +-- pkg/config/proxy.go | 10 ++++--- pkg/libol/utils_test.go | 4 +-- pkg/proxy/http.go | 60 +++++++++++++++++++++++++++++++++++------ 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/cmd/switch/main.go b/cmd/switch/main.go index 2196e1b..d33b8e5 100755 --- a/cmd/switch/main.go +++ b/cmd/switch/main.go @@ -4,7 +4,7 @@ import ( "github.com/luscis/openlan/pkg/cache" "github.com/luscis/openlan/pkg/config" "github.com/luscis/openlan/pkg/libol" - "github.com/luscis/openlan/pkg/switch" + cswitch "github.com/luscis/openlan/pkg/switch" ) func main() { @@ -12,7 +12,6 @@ func main() { config.Update(c) libol.SetLogger(c.Log.File, c.Log.Verbose) - libol.Debug("main %s", c) cache.Init(&c.Perf) s := cswitch.NewSwitch(c) libol.PreNotify() diff --git a/pkg/config/proxy.go b/pkg/config/proxy.go index 5212dee..b6068b2 100755 --- a/pkg/config/proxy.go +++ b/pkg/config/proxy.go @@ -67,12 +67,14 @@ func (h *HttpProxy) Correct() { if h.Cert != nil { h.Cert.Correct() } - if h.Password != "" { - h.Password = path.Join(h.ConfDir, h.Password) + if h.Password == "" { + h.Password = h.Listen + ".pass" } - if h.CaCert != "" { - h.CaCert = path.Join(h.ConfDir, h.CaCert) + h.Password = path.Join(h.ConfDir, h.Password) + if h.CaCert == "" { + h.CaCert = "ca.crt" } + h.CaCert = path.Join(h.ConfDir, h.CaCert) } func (h *HttpProxy) FindMatch(domain string, to *HttpForward) int { diff --git a/pkg/libol/utils_test.go b/pkg/libol/utils_test.go index 09660d3..085f587 100755 --- a/pkg/libol/utils_test.go +++ b/pkg/libol/utils_test.go @@ -1,9 +1,9 @@ package libol import ( - "bytes" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestPrettyTime(t *testing.T) { diff --git a/pkg/proxy/http.go b/pkg/proxy/http.go index 5fd04d0..476a8fd 100755 --- a/pkg/proxy/http.go +++ b/pkg/proxy/http.go @@ -9,10 +9,10 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httputil" + "os" "regexp" "sort" "strings" @@ -146,6 +146,8 @@ func (t *HttpProxy) loadUrl() { t.api.HandleFunc("/api/config", t.GetConfig).Methods("GET") t.api.HandleFunc("/api/match/{domain}/to/{backend}", t.AddMatch).Methods("POST") t.api.HandleFunc("/api/match/{domain}/to/{backend}", t.DelMatch).Methods("DELETE") + t.api.HandleFunc("/api/user/{user}/{pass}", t.AddUser).Methods("POST") + t.api.HandleFunc("/api/user/{user}", t.DelUser).Methods("DELETE") t.api.HandleFunc("/pac", t.GetPac).Methods("GET") } t.api.NotFoundHandler = http.HandlerFunc(NotFound) @@ -153,7 +155,7 @@ func (t *HttpProxy) loadUrl() { func (t *HttpProxy) loadPass() { file := t.cfg.Password - if file == "" { + if file == "" || libol.FileExist(file) != nil { return } reader, err := libol.OpenRead(file) @@ -178,6 +180,19 @@ func (t *HttpProxy) loadPass() { } } +func (t *HttpProxy) savePass() error { + file := t.cfg.Password + writer, err := libol.OpenTrunk(file) + if err != nil { + return err + } + for user, pass := range t.pass { + line := user + ":" + pass + _, _ = writer.WriteString(line + "\n") + } + return nil +} + func (t *HttpProxy) isAuth(username, password string) bool { if p, ok := t.pass[username]; ok { return p == password @@ -254,10 +269,10 @@ func (t *HttpProxy) openConn(protocol, remote string, insecure bool) (net.Conn, InsecureSkipVerify: insecure, } caFile := t.cfg.CaCert - if caFile != "" { + if caFile != "" && libol.FileExist(caFile) == nil { caCertPool := x509.NewCertPool() // Load CA cert - caCert, err := ioutil.ReadFile(caFile) + caCert, err := os.ReadFile(caFile) if err != nil { t.out.Warn("HttpProxy.openConn %s", err) } else { @@ -457,7 +472,7 @@ var httpTmpl = map[string]string{ - OpenLAN Proxy + OpenLAN Ceci