Cloak/internal/client/state.go

136 lines
3.1 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"
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
NumConn int
2018-10-07 17:09:45 +00:00
}
// State stores global variables
type State struct {
LocalHost string
LocalPort string
RemoteHost string
RemotePort string
2018-10-14 19:32:54 +00:00
Now func() time.Time
SessionID uint32
UID []byte
staticPub crypto.PublicKey
2019-07-27 18:55:53 +00:00
IsAdmin bool
2019-06-09 11:05:41 +00:00
ProxyMethod string
EncryptionMethod byte
ServerName string
BrowserSig string
NumConn int
2018-10-07 17:09:45 +00:00
}
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,
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]
// JSON doesn't like quotation marks around int
// Yes this is extremely ugly but it's still better than writing a tokeniser
if key == "TicketTimeHint" || key == "NumConn" {
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
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
switch preParse.EncryptionMethod {
case "plain":
sta.EncryptionMethod = 0x00
case "aes-gcm":
2019-06-09 11:05:41 +00:00
sta.EncryptionMethod = 0x01
2019-06-14 10:26:26 +00:00
case "chacha20-poly1305":
sta.EncryptionMethod = 0x02
2019-06-09 11:05:41 +00:00
default:
return errors.New("Unknown encryption method")
}
sta.ProxyMethod = preParse.ProxyMethod
2018-10-07 17:09:45 +00:00
sta.ServerName = preParse.ServerName
sta.BrowserSig = preParse.BrowserSig
2018-10-09 20:53:55 +00:00
sta.NumConn = preParse.NumConn
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
}