Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

Commit

Permalink
reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
lzjluzijie committed Jul 15, 2018
1 parent 0ce186c commit fbd76b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion client/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
24 changes: 24 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
32 changes: 15 additions & 17 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions websocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
return
}

err = wc.Listen()
err = wc.Run()
return
},
},
Expand Down Expand Up @@ -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",
},
Expand All @@ -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)
Expand All @@ -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
}
Expand Down

0 comments on commit fbd76b8

Please sign in to comment.