This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
364 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ caddy.log | |
dist | ||
websocks.cer | ||
websocks.key | ||
*.config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.