2018-10-07 17:09:45 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2018-10-14 19:32:54 +00:00
|
|
|
"crypto"
|
2018-10-07 17:09:45 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
2020-02-12 06:56:25 +00:00
|
|
|
"strconv"
|
2018-10-07 17:09:45 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2018-10-14 19:32:54 +00:00
|
|
|
|
2019-01-25 00:24:47 +00:00
|
|
|
"github.com/cbeuw/Cloak/internal/ecdh"
|
2019-08-20 21:43:04 +00:00
|
|
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
2018-10-07 17:09:45 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 21:43:04 +00:00
|
|
|
// rawConfig represents the fields in the config json file
|
2018-10-07 17:09:45 +00:00
|
|
|
type rawConfig struct {
|
2019-06-09 11:05:41 +00:00
|
|
|
ServerName string
|
|
|
|
ProxyMethod string
|
|
|
|
EncryptionMethod string
|
2019-12-29 16:55:21 +00:00
|
|
|
UID []byte
|
|
|
|
PublicKey []byte
|
2019-06-09 11:05:41 +00:00
|
|
|
BrowserSig string
|
2019-08-31 17:01:39 +00:00
|
|
|
Transport string
|
2019-08-12 21:43:16 +00:00
|
|
|
NumConn int
|
2019-08-19 22:23:41 +00:00
|
|
|
StreamTimeout int
|
2020-02-12 06:56:25 +00:00
|
|
|
RemotePort int
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 21:43:04 +00:00
|
|
|
// State stores the parsed configuration fields
|
2018-10-07 17:09:45 +00:00
|
|
|
type State struct {
|
2019-06-09 06:10:22 +00:00
|
|
|
LocalHost string
|
|
|
|
LocalPort string
|
|
|
|
RemoteHost string
|
|
|
|
RemotePort string
|
2019-08-16 22:20:24 +00:00
|
|
|
Unordered bool
|
2018-10-14 19:32:54 +00:00
|
|
|
|
2019-08-31 17:01:39 +00:00
|
|
|
Transport Transport
|
|
|
|
|
2019-07-26 16:05:46 +00:00
|
|
|
SessionID uint32
|
|
|
|
UID []byte
|
|
|
|
|
2019-08-16 23:18:19 +00:00
|
|
|
staticPub crypto.PublicKey
|
2019-08-20 21:48:01 +00:00
|
|
|
Now func() time.Time // for easier testing
|
2019-08-16 23:18:19 +00:00
|
|
|
browser browser
|
2019-08-02 15:02:25 +00:00
|
|
|
|
2019-06-09 11:05:41 +00:00
|
|
|
ProxyMethod string
|
|
|
|
EncryptionMethod byte
|
|
|
|
ServerName string
|
|
|
|
NumConn int
|
2019-08-19 22:23:41 +00:00
|
|
|
Timeout time.Duration
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// semi-colon separated value. This is for Android plugin options
|
|
|
|
func ssvToJson(ssv string) (ret []byte) {
|
|
|
|
unescape := func(s string) string {
|
2018-12-17 22:12:38 +00:00
|
|
|
r := strings.Replace(s, `\\`, `\`, -1)
|
|
|
|
r = strings.Replace(r, `\=`, `=`, -1)
|
|
|
|
r = strings.Replace(r, `\;`, `;`, -1)
|
2018-10-07 17:09:45 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
lines := strings.Split(unescape(ssv), ";")
|
|
|
|
ret = []byte("{")
|
|
|
|
for _, ln := range lines {
|
|
|
|
if ln == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
sp := strings.SplitN(ln, "=", 2)
|
|
|
|
key := sp[0]
|
|
|
|
value := sp[1]
|
2019-08-20 21:43:04 +00:00
|
|
|
// JSON doesn't like quotation marks around int and bool
|
|
|
|
// This is extremely ugly but it's still better than writing a tokeniser
|
2019-08-19 22:23:41 +00:00
|
|
|
if key == "NumConn" || key == "Unordered" || key == "StreamTimeout" {
|
2018-12-17 22:12:38 +00:00
|
|
|
ret = append(ret, []byte(`"`+key+`":`+value+`,`)...)
|
2018-10-07 17:09:45 +00:00
|
|
|
} else {
|
2018-12-17 22:12:38 +00:00
|
|
|
ret = append(ret, []byte(`"`+key+`":"`+value+`",`)...)
|
2018-10-07 17:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = ret[:len(ret)-1] // remove the last comma
|
|
|
|
ret = append(ret, '}')
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the config (either a path to json or Android config) into a State variable
|
|
|
|
func (sta *State) ParseConfig(conf string) (err error) {
|
|
|
|
var content []byte
|
2019-08-20 21:43:04 +00:00
|
|
|
// Checking if it's a path to json or a ssv string
|
2018-10-07 17:09:45 +00:00
|
|
|
if strings.Contains(conf, ";") && strings.Contains(conf, "=") {
|
|
|
|
content = ssvToJson(conf)
|
|
|
|
} else {
|
|
|
|
content, err = ioutil.ReadFile(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var preParse rawConfig
|
|
|
|
err = json.Unmarshal(content, &preParse)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-09 11:05:41 +00:00
|
|
|
|
2019-08-02 15:02:25 +00:00
|
|
|
switch strings.ToLower(preParse.EncryptionMethod) {
|
2019-06-09 11:05:41 +00:00
|
|
|
case "plain":
|
2019-08-20 21:43:04 +00:00
|
|
|
sta.EncryptionMethod = mux.E_METHOD_PLAIN
|
2019-06-14 12:28:14 +00:00
|
|
|
case "aes-gcm":
|
2019-08-20 21:43:04 +00:00
|
|
|
sta.EncryptionMethod = mux.E_METHOD_AES_GCM
|
2019-06-14 10:26:26 +00:00
|
|
|
case "chacha20-poly1305":
|
2019-08-20 21:43:04 +00:00
|
|
|
sta.EncryptionMethod = mux.E_METHOD_CHACHA20_POLY1305
|
2019-06-09 11:05:41 +00:00
|
|
|
default:
|
|
|
|
return errors.New("Unknown encryption method")
|
|
|
|
}
|
|
|
|
|
2019-08-02 15:02:25 +00:00
|
|
|
switch strings.ToLower(preParse.BrowserSig) {
|
|
|
|
case "chrome":
|
2019-08-16 23:18:19 +00:00
|
|
|
sta.browser = &Chrome{}
|
2019-08-02 15:02:25 +00:00
|
|
|
case "firefox":
|
2019-08-16 23:18:19 +00:00
|
|
|
sta.browser = &Firefox{}
|
2019-08-02 15:02:25 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unsupported browser signature")
|
|
|
|
}
|
|
|
|
|
2019-08-31 17:01:39 +00:00
|
|
|
switch strings.ToLower(preParse.Transport) {
|
2019-09-02 13:03:10 +00:00
|
|
|
case "direct":
|
|
|
|
sta.Transport = DirectTLS{}
|
|
|
|
case "cdn":
|
|
|
|
sta.Transport = WSOverTLS{}
|
2019-08-31 17:01:39 +00:00
|
|
|
default:
|
2019-09-15 20:33:35 +00:00
|
|
|
sta.Transport = DirectTLS{}
|
2019-08-31 17:01:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 06:10:22 +00:00
|
|
|
sta.ProxyMethod = preParse.ProxyMethod
|
2018-10-07 17:09:45 +00:00
|
|
|
sta.ServerName = preParse.ServerName
|
2018-10-09 20:53:55 +00:00
|
|
|
sta.NumConn = preParse.NumConn
|
2020-01-23 20:31:08 +00:00
|
|
|
if preParse.StreamTimeout == 0 {
|
|
|
|
sta.Timeout = 300 * time.Second
|
|
|
|
} else {
|
|
|
|
sta.Timeout = time.Duration(preParse.StreamTimeout) * time.Second
|
|
|
|
}
|
2019-12-29 16:55:21 +00:00
|
|
|
sta.UID = preParse.UID
|
2019-06-09 11:05:41 +00:00
|
|
|
|
2019-12-29 16:55:21 +00:00
|
|
|
pub, ok := ecdh.Unmarshal(preParse.PublicKey)
|
2018-12-03 20:30:06 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Failed to unmarshal Public key")
|
|
|
|
}
|
|
|
|
sta.staticPub = pub
|
2019-12-29 16:55:21 +00:00
|
|
|
|
2020-02-12 06:56:25 +00:00
|
|
|
// OPTIONAL: set RemotePort via JSON
|
|
|
|
// if RemotePort is specified in the JSON we overwrite sta.RemotePort
|
|
|
|
// if not, don't do anything, since sta.RemotePort is already initialised in ck-client.go
|
|
|
|
if preParse.RemotePort != 0 {
|
|
|
|
// basic validity check
|
|
|
|
if preParse.RemotePort >= 1 && preParse.RemotePort <= 65535 {
|
|
|
|
sta.RemotePort = strconv.Itoa(preParse.RemotePort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 20:30:06 +00:00
|
|
|
return nil
|
2018-10-14 19:32:54 +00:00
|
|
|
}
|