From fbd76b8c2783b89c51cd36a309d496145ad5211d Mon Sep 17 00:00:00 2001 From: halulu Date: Sun, 15 Jul 2018 14:41:27 +0800 Subject: [PATCH] reverse proxy --- client/client.go | 2 +- client/web.go | 2 +- server/config.go | 24 ++++++++++++++++++++++++ server/server.go | 32 +++++++++++++++----------------- websocks.go | 19 ++++++++++--------- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/client/client.go b/client/client.go index 19a09da..58f6828 100644 --- a/client/client.go +++ b/client/client.go @@ -32,7 +32,7 @@ type WebSocksClient struct { Stats *core.Stats } -func (client *WebSocksClient) Listen() (err error) { +func (client *WebSocksClient) Run() (err error) { listener, err := net.ListenTCP("tcp", client.ListenAddr) if err != nil { return err diff --git a/client/web.go b/client/web.go index b004169..d488673 100644 --- a/client/web.go +++ b/client/web.go @@ -89,7 +89,7 @@ func (app *WebSocksClientApp) StartClient(ctx *macaron.Context) { app.running = true go func() { - err = websocksClient.Listen() + err = websocksClient.Run() if err != nil { log.Error(err.Error()) } diff --git a/server/config.go b/server/config.go index 5d45e2c..f41980c 100644 --- a/server/config.go +++ b/server/config.go @@ -1,5 +1,13 @@ package server +import ( + "time" + + "github.com/gorilla/websocket" + "github.com/juju/loggo" + "github.com/lzjluzijie/websocks/core" +) + type Config struct { ListenAddr string Pattern string @@ -8,3 +16,19 @@ type Config struct { KeyPath string ReverseProxy string } + +func (config *Config) GetServer() (server *WebSocksServer) { + server = &WebSocksServer{ + Config: config, + Upgrader: &websocket.Upgrader{ + ReadBufferSize: 4 * 1024, + WriteBufferSize: 4 * 1024, + HandshakeTimeout: 10 * time.Second, + }, + CreatedAt: time.Now(), + Stats: core.NewStats(), + } + + logger.SetLogLevel(loggo.DEBUG) + return +} diff --git a/server/server.go b/server/server.go index 13fe41f..a88e63e 100644 --- a/server/server.go +++ b/server/server.go @@ -7,6 +7,9 @@ import ( "sync" "time" + "net/http/httputil" + "net/url" + "github.com/gin-gonic/gin" "github.com/gorilla/websocket" "github.com/juju/loggo" @@ -28,22 +31,6 @@ type WebSocksServer struct { Stats *core.Stats } -func (config *Config) GetServer() (server *WebSocksServer) { - server = &WebSocksServer{ - Config: config, - Upgrader: &websocket.Upgrader{ - ReadBufferSize: 4 * 1024, - WriteBufferSize: 4 * 1024, - HandshakeTimeout: 10 * time.Second, - }, - CreatedAt: time.Now(), - Stats: core.NewStats(), - } - - logger.SetLogLevel(loggo.DEBUG) - return -} - func (server *WebSocksServer) HandleWebSocket(c *gin.Context) { w := c.Writer r := c.Request @@ -97,10 +84,21 @@ func (server *WebSocksServer) DialRemote(host string) (conn net.Conn, err error) return } -func (server *WebSocksServer) Listen() (err error) { +func (server *WebSocksServer) Run() (err error) { r := gin.Default() r.GET(server.Pattern, server.HandleWebSocket) + if server.ReverseProxy != "" { + remote, err := url.Parse(server.ReverseProxy) + if err != nil { + panic(err) + } + proxy := httputil.NewSingleHostReverseProxy(remote) + r.NoRoute(func(c *gin.Context) { + proxy.ServeHTTP(c.Writer, c.Request) + }) + } + s := http.Server{ Addr: server.ListenAddr, Handler: r, diff --git a/websocks.go b/websocks.go index 29086da..fa652ba 100644 --- a/websocks.go +++ b/websocks.go @@ -85,7 +85,7 @@ func main() { return } - err = wc.Listen() + err = wc.Run() return }, }, @@ -119,7 +119,7 @@ func main() { Usage: "tls key path", }, cli.StringFlag{ - Name: "proxy", + Name: "reverse-proxy", Value: "", Usage: "reverse proxy url, leave blank to disable", }, @@ -131,7 +131,7 @@ func main() { tls := c.Bool("tls") certPath := c.String("cert") keyPath := c.String("key") - //proxy := c.String("proxy") + reverseProxy := c.String("reverse-proxy") if debug { logger.SetLogLevel(loggo.DEBUG) @@ -140,16 +140,17 @@ func main() { logger.Infof("Log level %s", logger.LogLevel().String()) config := server.Config{ - Pattern: pattern, - ListenAddr: listenAddr, - TLS: tls, - CertPath: certPath, - KeyPath: keyPath, + Pattern: pattern, + ListenAddr: listenAddr, + TLS: tls, + CertPath: certPath, + KeyPath: keyPath, + ReverseProxy: reverseProxy, } websocksServer := config.GetServer() logger.Infof("Listening at %s", listenAddr) - err = websocksServer.Listen() + err = websocksServer.Run() if err != nil { return }