Skip to content

Commit

Permalink
build static library for the dart
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-eques committed Dec 1, 2022
1 parent 4a023f6 commit c53286b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cmd/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
)

//export RunProxy
func RunProxy() {
func RunProxy(port int, user *C.char, pass *C.char) {
logger := logging.DefaultLogger()

proxy := goproxy.NewProxyHttpServer()
proxyConfig := util.ProxyConfig{
Port: 8080,
Port: uint(port),
Username: C.GoString(user),
Password: C.GoString(pass),
CACertPath: "",
CAKeyPath: "",
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ func main() {
proxy := goproxy.NewProxyHttpServer()
proxyConfig := util.ProxyConfig{
Port: 8080,
Addr: "127.0.0.1",
Username: "admin",
Password: "123456",
CACertPath: "",
CAKeyPath: "",
}
Expand All @@ -20,7 +23,7 @@ func main() {
logger.Errorw("Faild to configure proxy server", "config", proxyConfig)
return
} else {
logger.Infof("Start to proxy server :%d", proxyConfig.Port)
logger.Infof("Start to proxy server %s:%d", proxyConfig.Addr, proxyConfig.Port)
srvProxy.Serve(httpsListener)
}
}
21 changes: 14 additions & 7 deletions pkg/proxy/util/httpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ import (

type ProxyConfig struct {
Port uint `mapstructure:"PROXY_PORT"`
Addr string `mapstructure:"PROXY_ADDR"`
Username string
Password string
CACertPath string `mapstructure:"PROXY_CA_CERT_PATH"`
CAKeyPath string `mapstructure:"PROXY_CA_KEY_PATH"`
}

func HttpServer(proxy *goproxy.ProxyHttpServer, cfg *ProxyConfig) (server *http.Server, listener net.Listener) {
logger := logging.DefaultLogger()
verbose := flag.Bool("v", true, "should every proxy request be logged to stdout")
addr := flag.String("addr", fmt.Sprintf(":%d", cfg.Port), "proxy listen address")
addr := flag.String("addr", fmt.Sprintf("%s:%d", cfg.Addr, cfg.Port), "proxy listen address")
flag.Parse()

// Bandwidth counter
Expand All @@ -34,21 +37,25 @@ func HttpServer(proxy *goproxy.ProxyHttpServer, cfg *ProxyConfig) (server *http.
proxy.Verbose = *verbose

// Authenticate middleware
proxy.OnRequest().Do(auth.Basic("auth", authHandler(httpsConns)))
proxy.OnRequest().HandleConnect(auth.BasicConnect("auth", authHandler(httpsConns)))
proxy.OnRequest().Do(auth.Basic("auth", authHandler(httpsConns, cfg.Username, cfg.Password)))
proxy.OnRequest().HandleConnect(auth.BasicConnect("auth", authHandler(httpsConns, cfg.Username, cfg.Password)))

proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
return req, req.Response
})

httpServer := http.Server{Handler: proxy, Addr: *addr}
return &httpServer, httpListener
}

// authenticate user and initiate the bandwidth counter.
func authHandler(httpsConns *bandwidth.ConnMap) func(req *http.Request, user, passwd string) bool {
func authHandler(httpsConns *bandwidth.ConnMap, username, password string) func(req *http.Request, user, passwd string) bool {
logger := logging.DefaultLogger()
return func(req *http.Request, user, passwd string) (authorized bool) {
// Set Username to interceptCon
authorized = false
// authenticate
authorized = authenticate(user, passwd)
authorized = authenticate(user, passwd, username, password)
// initiate the bandWidthCounter
remoteAddr := req.RemoteAddr
conn, ok := httpsConns.Find(remoteAddr)
Expand All @@ -69,8 +76,8 @@ func authHandler(httpsConns *bandwidth.ConnMap) func(req *http.Request, user, pa
}

// Authenticate with username and password
func authenticate(user, passwd string) bool {
return user == "test" && passwd == "1234"
func authenticate(user, passwd, username, password string) bool {
return user == username && passwd == password
}

// Handle the counted bandwidth
Expand Down

0 comments on commit c53286b

Please sign in to comment.