You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
10 months ago
|
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)
|
||
|
}
|
||
|
}
|