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

Commit

Permalink
v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lzjluzijie committed Jun 6, 2018
2 parents fb0661e + e2ee265 commit 2368125
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 179 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ caddy.log
dist
websocks.cer
websocks.key
*.config.json
86 changes: 86 additions & 0 deletions config/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package config

import (
"crypto/tls"
"net"
"net/url"

"encoding/json"
"io/ioutil"
"time"

"github.com/gorilla/websocket"
"github.com/lzjluzijie/websocks/core"
"github.com/urfave/cli"
)

//GetClient return client from path
func GetClientConfig(path string) (client *core.Client, err error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return
}

//read config
config := &core.ClientConfig{}
err = json.Unmarshal(data, config)
if err != nil {
return
}

//tackle config
serverURL, err := url.Parse(config.ServerURL)
if err != nil {
return
}

laddr, err := net.ResolveTCPAddr("tcp", config.ListenAddr)
if err != nil {
return
}

tlsConfig := &tls.Config{
InsecureSkipVerify: config.InsecureCert,
ServerName: config.SNI,
}

client = &core.Client{
ClientConfig: config,

ServerURL: serverURL,
ListenAddr: laddr,
Dialer: &websocket.Dialer{
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
HandshakeTimeout: 10 * time.Second,
TLSClientConfig: tlsConfig,
},

CreatedAt: time.Now(),
}
return
}

//GenerateClientConfig create a client config from cli.Context
func GenerateClientConfig(c *cli.Context) (err error) {
path := c.String("path")

config := &core.ClientConfig{
ListenAddr: c.String("l"),
ServerURL: c.String("s"),
SNI: c.String("sni"),
InsecureCert: c.Bool("insecure"),
Mux: c.Bool("mux"),
}

data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return
}

err = ioutil.WriteFile(path, data, 600)
if err != nil {
return
}
return
}
91 changes: 91 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package config

import (
"github.com/urfave/cli"
)

var Command = cli.Command{
Name: "config",
Aliases: []string{"config"},
Usage: "generate configuration",
Subcommands: []cli.Command{
{
Name: "client",
Aliases: []string{"c"},
Usage: "generate client config",
Flags: []cli.Flag{
cli.StringFlag{
Name: "path",
Value: "client.config.json",
Usage: "client config output path",
},
cli.StringFlag{
Name: "l",
Value: "127.0.0.1:10801",
Usage: "local listening port",
},
cli.StringFlag{
Name: "s",
Value: "ws://localhost:23333/websocks",
Usage: "server url",
},
cli.BoolFlag{
Name: "mux",
Usage: "mux mode",
},
cli.StringFlag{
Name: "n",
Value: "",
Usage: "fake server name for tls client hello, leave blank to disable",
},
cli.BoolFlag{
Name: "insecure",
Usage: "InsecureSkipVerify: true",
},
},
Action: GenerateClientConfig,
},
{
Name: "server",
Aliases: []string{"s"},
Usage: "generate server config",
Flags: []cli.Flag{
cli.StringFlag{
Name: "path",
Value: "server.config.json",
Usage: "server config output path",
},
cli.StringFlag{
Name: "l",
Value: "0.0.0.0:23333",
Usage: "local listening port",
},
cli.StringFlag{
Name: "p",
Value: "/websocks",
Usage: "server.com/pattern, like password, start with '/'",
},
cli.BoolFlag{
Name: "tls",
Usage: "enable built-in tls",
},
cli.StringFlag{
Name: "cert",
Value: "websocks.cer",
Usage: "tls cert path",
},
cli.StringFlag{
Name: "key",
Value: "websocks.key",
Usage: "tls key path",
},
cli.StringFlag{
Name: "reverse-proxy",
Value: "",
Usage: "reverse proxy url, leave blank to disable",
},
},
Action: GenerateServerConfig,
},
},
}
68 changes: 68 additions & 0 deletions config/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package config

import (
"encoding/json"
"io/ioutil"
"time"

"github.com/gorilla/websocket"
"github.com/lzjluzijie/websocks/core"
"github.com/urfave/cli"
)

//GenerateServerConfig create a client config from path
func GetServerConfig(path string) (server *core.Server, err error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return
}

//read config
config := &core.ServerConfig{}
err = json.Unmarshal(data, config)
if err != nil {
return
}

server = &core.Server{
ServerConfig: config,
Upgrader: &websocket.Upgrader{
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
HandshakeTimeout: 10 * time.Second,
},
CreatedAt: time.Now(),
}
return
}

//GenerateServerConfig create a server config from cli.Context
func GenerateServerConfig(c *cli.Context) (err error) {
path := c.String("path")
listenAddr := c.String("l")
pattern := c.String("p")
tls := c.Bool("tls")
certPath := c.String("cert")
keyPath := c.String("key")
proxy := c.String("proxy")

config := &core.ServerConfig{
Pattern: pattern,
ListenAddr: listenAddr,
TLS: tls,
CertPath: certPath,
KeyPath: keyPath,
ReverseProxy: proxy,
}

data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return
}

err = ioutil.WriteFile(path, data, 600)
if err != nil {
return
}
return
}
38 changes: 24 additions & 14 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@ package core
import (
"io"
"net"
"net/url"
"time"

"net/url"

"github.com/gorilla/websocket"
"github.com/juju/loggo"
)

var logger = loggo.GetLogger("core")
type ClientConfig struct {
ListenAddr string
ServerURL string

type Client struct {
LogLevel loggo.Level
ListenAddr *net.TCPAddr
URL *url.URL
SNI string
InsecureCert bool

Mux bool
}

Mux bool
MuxWS *MuxWebSocket
type Client struct {
*ClientConfig
LogLevel loggo.Level

Dialer *websocket.Dialer
ServerURL *url.URL
ListenAddr *net.TCPAddr
Dialer *websocket.Dialer
muxWS *MuxWebSocket

//statistics
CreatedAt time.Time
}

Expand All @@ -44,7 +53,7 @@ func (client *Client) Listen() (err error) {
return err
}

go client.MuxWS.ClientListen()
go client.muxWS.ClientListen()
}

for {
Expand All @@ -67,19 +76,19 @@ func (client *Client) handleConn(conn *net.TCPConn) {

err := handShake(conn)
if err != nil {
logger.Errorf(err.Error())
logger.Debugf(err.Error())
return
}

_, host, err := getRequest(conn)
if err != nil {
logger.Errorf(err.Error())
logger.Debugf(err.Error())
return
}

_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
if err != nil {
logger.Errorf(err.Error())
logger.Debugf(err.Error())
return
}

Expand All @@ -93,13 +102,14 @@ func (client *Client) handleConn(conn *net.TCPConn) {
}

func (client *Client) DialWSConn(host string, conn *net.TCPConn) {
wsConn, _, err := client.Dialer.Dial(client.URL.String(), map[string][]string{
wsConn, _, err := client.Dialer.Dial(client.ServerURL.String(), map[string][]string{
"WebSocks-Host": {host},
})

if err != nil {
return
}
defer wsConn.Close()

logger.Debugf("dialed ws for %s", host)

Expand Down
2 changes: 1 addition & 1 deletion core/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type MuxConn struct {
sendMessageID *uint64
}

//client use
//NewMuxConn create new mux connection for client
func NewMuxConn(muxWS *MuxWebSocket) (conn *MuxConn) {
return &MuxConn{
ID: rand.Uint64(),
Expand Down
6 changes: 3 additions & 3 deletions core/muxclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (muxWS *MuxWebSocket) ClientListen() {
}

func (client *Client) OpenMux() (err error) {
wsConn, _, err := client.Dialer.Dial(client.URL.String(), map[string][]string{
wsConn, _, err := client.Dialer.Dial(client.ServerURL.String(), map[string][]string{
"WebSocks-Mux": {"mux"},
})

Expand All @@ -36,11 +36,11 @@ func (client *Client) OpenMux() (err error) {
}

muxWS := NewMuxWebSocket(ws)
client.MuxWS = muxWS
client.muxWS = muxWS
return
}
func (client *Client) DialMuxConn(host string, conn *net.TCPConn) {
muxConn := NewMuxConn(client.MuxWS)
muxConn := NewMuxConn(client.muxWS)

err := muxConn.DialMessage(host)
if err != nil {
Expand Down
Loading

0 comments on commit 2368125

Please sign in to comment.