Cloak/internal/client/state.go

152 lines
3.7 KiB
Go
Raw Normal View History

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/base64"
"encoding/json"
"errors"
"io/ioutil"
"strings"
"time"
2018-10-14 19:32:54 +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
UID string
PublicKey string
BrowserSig string
2019-08-12 21:43:16 +00:00
NumConn int
2019-08-19 22:23:41 +00:00
StreamTimeout 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 {
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
SessionID uint32
UID []byte
2019-08-16 23:18:19 +00:00
staticPub crypto.PublicKey
2019-08-20 21:43:04 +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
}
2019-08-20 21:43:04 +00:00
// TODO: remove this and let the caller declare it directly
2019-01-22 21:51:57 +00:00
func InitState(localHost, localPort, remoteHost, remotePort string, nowFunc func() time.Time) *State {
2018-10-14 19:32:54 +00:00
ret := &State{
2019-08-02 00:01:19 +00:00
LocalHost: localHost,
LocalPort: localPort,
RemoteHost: remoteHost,
RemotePort: remotePort,
2019-08-16 23:18:19 +00:00
now: nowFunc,
2018-10-14 19:32:54 +00:00
}
return ret
}
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
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")
}
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
2019-08-19 22:23:41 +00:00
sta.Timeout = time.Duration(preParse.StreamTimeout) * time.Second
2019-06-09 11:05:41 +00:00
2018-12-03 20:30:06 +00:00
uid, err := base64.StdEncoding.DecodeString(preParse.UID)
2018-10-07 17:09:45 +00:00
if err != nil {
2018-12-03 20:30:06 +00:00
return errors.New("Failed to parse UID: " + err.Error())
2018-10-07 17:09:45 +00:00
}
2018-11-07 21:16:13 +00:00
sta.UID = uid
2018-10-07 17:09:45 +00:00
2018-12-03 20:30:06 +00:00
pubBytes, err := base64.StdEncoding.DecodeString(preParse.PublicKey)
2018-10-07 17:09:45 +00:00
if err != nil {
2018-12-03 20:30:06 +00:00
return errors.New("Failed to parse Public key: " + err.Error())
2018-10-07 17:09:45 +00:00
}
pub, ok := ecdh.Unmarshal(pubBytes)
2018-12-03 20:30:06 +00:00
if !ok {
return errors.New("Failed to unmarshal Public key")
}
sta.staticPub = pub
return nil
2018-10-14 19:32:54 +00:00
}