Cloak/internal/server/state.go

134 lines
3.0 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"
2018-12-22 23:58:03 +00:00
"errors"
2018-10-09 15:07:54 +00:00
"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
DatabasePath string
BackupDirPath string
2018-10-09 15:07:54 +00:00
}
// 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
}
func InitState(localHost, localPort, remoteHost, remotePort string, nowFunc func() time.Time) (*State, error) {
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,
}
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 {
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-09 15:07:54 +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]
2018-12-17 22:12:38 +00:00
ret = append(ret, []byte(`"`+key+`":"`+value+`",`)...)
2018-10-09 15:07:54 +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 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 {
2018-12-22 23:58:03 +00:00
return errors.New("Failed to unmarshal: " + err.Error())
2018-10-09 15:07:54 +00:00
}
up, err := usermanager.MakeUserpanel(preParse.DatabasePath, preParse.BackupDirPath)
if err != nil {
return errors.New("Attempting to open database: " + err.Error())
}
sta.Userpanel = up
2018-10-09 15:07:54 +00:00
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 {
2018-12-22 23:58:03 +00:00
return errors.New("Failed to decode private key: " + err.Error())
2018-10-14 19:32:54 +00:00
}
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
2018-12-22 23:58:03 +00:00
adminUID, err := base64.StdEncoding.DecodeString(preParse.AdminUID)
2018-11-22 21:55:23 +00:00
if err != nil {
2018-12-22 23:58:03 +00:00
return errors.New("Failed to decode AdminUID: " + err.Error())
2018-11-22 21:55:23 +00:00
}
sta.AdminUID = adminUID
2018-10-09 15:07:54 +00:00
return nil
}
// 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
}
}