You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cloak/internal/server/state.go

124 lines
2.8 KiB
Go

6 years ago
package server
import (
"crypto"
6 years ago
"encoding/base64"
"encoding/json"
6 years ago
"errors"
6 years ago
"io/ioutil"
"sync"
"time"
gmux "github.com/gorilla/mux"
6 years ago
)
type rawConfig struct {
ProxyBook map[string]string
RedirAddr string
PrivateKey string
AdminUID string
DatabasePath string
CncMode bool
6 years ago
}
// State type stores the global state of the program
type State struct {
ProxyBook map[string]string
BindHost string
BindPort string
6 years ago
Now func() time.Time
AdminUID []byte
staticPv crypto.PrivateKey
RedirAddr string
usedRandomM sync.RWMutex
usedRandom map[[32]byte]int
Panel *userPanel
LocalAPIRouter *gmux.Router
}
func InitState(bindHost, bindPort string, nowFunc func() time.Time) (*State, error) {
ret := &State{
BindHost: bindHost,
BindPort: bindPort,
Now: nowFunc,
}
ret.usedRandom = make(map[[32]byte]int)
go ret.UsedRandomCleaner()
return ret, nil
6 years ago
}
// 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())
}
6 years ago
} else {
errJson := json.Unmarshal(content, &preParse)
if errJson != nil {
return errors.New("Failed to read configuration file: " + errJson.Error())
6 years ago
}
}
if preParse.CncMode {
//TODO: implement command & control mode
} 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
pvBytes, err := base64.StdEncoding.DecodeString(preParse.PrivateKey)
if err != nil {
6 years ago
return errors.New("Failed to decode private key: " + err.Error())
}
var pv [32]byte
copy(pv[:], pvBytes)
sta.staticPv = &pv
6 years ago
adminUID, err := base64.StdEncoding.DecodeString(preParse.AdminUID)
if err != nil {
6 years ago
return errors.New("Failed to decode AdminUID: " + err.Error())
}
sta.AdminUID = adminUID
6 years ago
return nil
}
5 years ago
// This is the accepting window of the encrypted timestamp from client
// we reject the client if the timestamp is outside of this window.
// This is for replay prevention so that we don't have to save unlimited amount of
// random
const TIMESTAMP_WINDOW = 12 * time.Hour
6 years ago
// UsedRandomCleaner clears the cache of used random fields every 12 hours
func (sta *State) UsedRandomCleaner() {
for {
5 years ago
time.Sleep(TIMESTAMP_WINDOW)
6 years ago
now := int(sta.Now().Unix())
sta.usedRandomM.Lock()
for key, t := range sta.usedRandom {
5 years ago
if now-t > int(TIMESTAMP_WINDOW.Seconds()) {
delete(sta.usedRandom, key)
6 years ago
}
}
sta.usedRandomM.Unlock()
6 years ago
}
}