Cloak/internal/server/state.go

160 lines
3.4 KiB
Go
Raw Normal View History

2018-10-09 15:07:54 +00:00
package server
import (
2018-10-14 19:32:54 +00:00
"crypto"
2018-10-09 15:07:54 +00:00
"encoding/base64"
"encoding/json"
"io/ioutil"
"strings"
"sync"
"time"
2018-11-07 21:16:13 +00:00
"github.com/cbeuw/Cloak/internal/server/usermanager"
2018-10-09 15:07:54 +00:00
)
type rawConfig struct {
WebServerAddr string
2018-12-03 20:30:06 +00:00
PrivateKey string
2018-11-22 21:55:23 +00:00
AdminUID string
2018-10-09 15:07:54 +00:00
}
type stateManager interface {
ParseConfig(string) error
SetAESKey(string)
PutUsedRandom([32]byte)
}
// State type stores the global state of the program
type State struct {
SS_LOCAL_HOST string
SS_LOCAL_PORT string
SS_REMOTE_HOST string
SS_REMOTE_PORT string
2018-10-14 19:32:54 +00:00
Now func() time.Time
2018-11-22 21:55:23 +00:00
AdminUID []byte
2018-10-14 19:32:54 +00:00
staticPv crypto.PrivateKey
2018-11-07 21:16:13 +00:00
Userpanel *usermanager.Userpanel
2018-10-14 19:32:54 +00:00
usedRandomM sync.RWMutex
usedRandom map[[32]byte]int
WebServerAddr string
}
2018-11-07 21:16:13 +00:00
func InitState(localHost, localPort, remoteHost, remotePort string, nowFunc func() time.Time, dbPath string) (*State, error) {
up, err := usermanager.MakeUserpanel(dbPath)
if err != nil {
return nil, err
}
2018-10-14 19:32:54 +00:00
ret := &State{
SS_LOCAL_HOST: localHost,
SS_LOCAL_PORT: localPort,
SS_REMOTE_HOST: remoteHost,
SS_REMOTE_PORT: remotePort,
Now: nowFunc,
2018-11-07 21:16:13 +00:00
Userpanel: up,
2018-10-14 19:32:54 +00:00
}
ret.usedRandom = make(map[[32]byte]int)
2018-11-07 21:16:13 +00:00
return ret, nil
2018-10-09 15:07:54 +00:00
}
// semi-colon separated value.
func ssvToJson(ssv string) (ret []byte) {
unescape := func(s string) string {
r := strings.Replace(s, "\\\\", "\\", -1)
r = strings.Replace(r, "\\=", "=", -1)
r = strings.Replace(r, "\\;", ";", -1)
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]
ret = append(ret, []byte("\""+key+"\":\""+value+"\",")...)
}
ret = ret[:len(ret)-1] // remove the last comma
ret = append(ret, '}')
return ret
}
2018-11-22 21:55:23 +00:00
// base64 encoded 32 byte adminUID
func parseAdminUID(b64 string) ([]byte, error) {
uid, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return nil, err
}
return uid, nil
}
// TODO: specify which parse fails
2018-10-09 15:07:54 +00:00
// ParseConfig parses the config (either a path to json or in-line ssv 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
}
sta.WebServerAddr = preParse.WebServerAddr
2018-12-03 20:30:06 +00:00
pvBytes, err := base64.StdEncoding.DecodeString(preParse.PrivateKey)
2018-10-14 19:32:54 +00:00
if err != nil {
return err
}
2018-12-03 20:30:06 +00:00
var pv [32]byte
copy(pv[:], pvBytes)
sta.staticPv = &pv
2018-11-22 21:55:23 +00:00
adminUID, err := parseAdminUID(preParse.AdminUID)
if err != nil {
return err
}
sta.AdminUID = adminUID
2018-10-09 15:07:54 +00:00
return nil
}
func (sta *State) getUsedRandom(random [32]byte) int {
2018-10-14 19:32:54 +00:00
sta.usedRandomM.Lock()
defer sta.usedRandomM.Unlock()
return sta.usedRandom[random]
2018-10-09 15:07:54 +00:00
}
2018-10-14 19:32:54 +00:00
// PutUsedRandom adds a random field into map usedRandom
2018-10-09 15:07:54 +00:00
func (sta *State) putUsedRandom(random [32]byte) {
2018-10-14 19:32:54 +00:00
sta.usedRandomM.Lock()
sta.usedRandom[random] = int(sta.Now().Unix())
sta.usedRandomM.Unlock()
2018-10-09 15:07:54 +00:00
}
// UsedRandomCleaner clears the cache of used random fields every 12 hours
func (sta *State) UsedRandomCleaner() {
for {
time.Sleep(12 * time.Hour)
now := int(sta.Now().Unix())
2018-10-14 19:32:54 +00:00
sta.usedRandomM.Lock()
for key, t := range sta.usedRandom {
2018-10-09 15:07:54 +00:00
if now-t > 12*3600 {
2018-10-14 19:32:54 +00:00
delete(sta.usedRandom, key)
2018-10-09 15:07:54 +00:00
}
}
2018-10-14 19:32:54 +00:00
sta.usedRandomM.Unlock()
2018-10-09 15:07:54 +00:00
}
}