Cloak/internal/server/state.go

118 lines
2.5 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"
"sync"
"time"
gmux "github.com/gorilla/mux"
2018-10-09 15:07:54 +00:00
)
type rawConfig struct {
ProxyBook map[string]string
RedirAddr 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
CncMode bool
2018-10-09 15:07:54 +00:00
}
// State type stores the global state of the program
type State struct {
ProxyBook map[string]string
BindHost string
BindPort string
2018-10-09 15:07:54 +00:00
Now func() time.Time
AdminUID []byte
staticPv crypto.PrivateKey
RedirAddr string
2018-10-14 19:32:54 +00:00
usedRandomM sync.RWMutex
usedRandom map[[32]byte]int
Panel *userPanel
LocalAPIRouter *gmux.Router
2018-10-14 19:32:54 +00:00
}
func InitState(bindHost, bindPort string, nowFunc func() time.Time) (*State, error) {
2018-10-14 19:32:54 +00:00
ret := &State{
BindHost: bindHost,
BindPort: bindPort,
Now: nowFunc,
2018-10-14 19:32:54 +00:00
}
ret.usedRandom = make(map[[32]byte]int)
go ret.UsedRandomCleaner()
2018-11-07 21:16:13 +00:00
return ret, nil
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
var preParse rawConfig
content, errPath := ioutil.ReadFile(conf)
if errPath != nil {
errJson := json.Unmarshal(content, &preParse)
if errJson != nil {
return errors.New("Failed to read/unmarshal configuration, path is invalid or " + errJson.Error())
}
2018-10-09 15:07:54 +00:00
} else {
errJson := json.Unmarshal(content, &preParse)
if errJson != nil {
return errors.New("Failed to read configuration file: " + errJson.Error())
2018-10-09 15:07:54 +00:00
}
}
if preParse.CncMode {
} else {
manager, err := MakeLocalManager(preParse.DatabasePath)
if err != nil {
return err
}
sta.Panel = MakeUserPanel(manager)
sta.LocalAPIRouter = manager.Router
}
sta.RedirAddr = preParse.RedirAddr
sta.ProxyBook = preParse.ProxyBook
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
}
}