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.
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package httpreverse
|
|
|
|
import (
|
|
"b612.me/starlog"
|
|
"github.com/spf13/cobra"
|
|
"net/url"
|
|
"os"
|
|
"os/signal"
|
|
)
|
|
|
|
var remote, config string
|
|
var addr, key, cert, log string
|
|
var port int
|
|
var enablessl bool
|
|
var host string
|
|
|
|
func init() {
|
|
Cmd.Flags().StringVarP(&host, "host", "H", "", "host字段")
|
|
Cmd.Flags().StringVarP(&remote, "remote", "r", "", "反向代理地址")
|
|
Cmd.Flags().StringVarP(&config, "config", "C", "", "配置文件地址")
|
|
Cmd.Flags().StringVarP(&addr, "addr", "a", "0.0.0.0", "监听地址")
|
|
Cmd.Flags().StringVarP(&key, "key", "k", "", "ssl key地址")
|
|
Cmd.Flags().StringVarP(&cert, "cert", "c", "", "ssl 证书地址")
|
|
Cmd.Flags().StringVarP(&log, "log", "l", "", "log日志地址")
|
|
Cmd.Flags().BoolVarP(&enablessl, "enable-ssl", "s", false, "启用ssl")
|
|
Cmd.Flags().IntVarP(&port, "port", "p", 8080, "监听端口")
|
|
}
|
|
|
|
var Cmd = &cobra.Command{
|
|
Use: "hproxy",
|
|
Short: "Http Reverse Proxy(Http反向代理)",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if log != "" {
|
|
starlog.SetLogFile(log, starlog.Std, true)
|
|
}
|
|
if config != "" {
|
|
r, err := Parse(config)
|
|
if err != nil {
|
|
starlog.Errorln(err)
|
|
os.Exit(1)
|
|
}
|
|
go func() {
|
|
sig := make(chan os.Signal)
|
|
signal.Notify(sig, os.Kill, os.Interrupt)
|
|
starlog.Noticeln("Stop Due to Recv Siganl", <-sig)
|
|
r.Close()
|
|
}()
|
|
err = r.Run()
|
|
if err != nil {
|
|
starlog.Errorln("Http Reverse Proxy Exit Error", err)
|
|
os.Exit(2)
|
|
}
|
|
starlog.Infoln("Http Reverse Proxy Exit Normally")
|
|
return
|
|
}
|
|
if remote == "" {
|
|
starlog.Errorln("please enter the reverse url")
|
|
os.Exit(4)
|
|
}
|
|
u, err := url.Parse(remote)
|
|
if err != nil {
|
|
starlog.Errorln(err)
|
|
os.Exit(3)
|
|
}
|
|
reverse := ReverseConfig{
|
|
Name: "web",
|
|
Addr: addr,
|
|
Host: host,
|
|
ReverseURL: map[string]*url.URL{
|
|
"/": u,
|
|
},
|
|
Port: port,
|
|
UsingSSL: enablessl,
|
|
Key: key,
|
|
Cert: cert,
|
|
XForwardMode: 1,
|
|
}
|
|
go func() {
|
|
sig := make(chan os.Signal)
|
|
signal.Notify(sig, os.Kill, os.Interrupt)
|
|
starlog.Noticeln("Stop Due to Recv Siganl", <-sig)
|
|
reverse.Close()
|
|
}()
|
|
err = reverse.Run()
|
|
if err != nil {
|
|
starlog.Errorln("Http Reverse Proxy Exit Error", err)
|
|
os.Exit(2)
|
|
}
|
|
starlog.Infoln("Http Reverse Proxy Exit Normally")
|
|
return
|
|
},
|
|
}
|