package httproxy import ( "b612.me/starlog" "github.com/elazarl/goproxy" "github.com/elazarl/goproxy/ext/auth" "github.com/spf13/cobra" "net/http" ) var username, password string var listen string func init() { Cmd.Flags().StringVarP(&username, "username", "u", "", "用户名") Cmd.Flags().StringVarP(&password, "password", "p", "", "密码") Cmd.Flags().StringVarP(&listen, "listen", "l", ":8000", "监听地址") } var Cmd = &cobra.Command{ Use: "httproxy", Short: "http代理", Long: "http代理", Run: func(cmd *cobra.Command, args []string) { run() }, } func run() { // Create a http server p := goproxy.NewProxyHttpServer() p.Verbose = true starlog.Infof("start http proxy server on %s username %s password %s \n", listen, username, password) if username != "" && password != "" { auth.ProxyBasic(p, "B612 Http Proxy Need Password", func(user, pwd string) bool { return user == username && pwd == password }) } err := http.ListenAndServe(listen, p) if err != nil { starlog.Errorln("http proxy server error:", err) } }