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"
|
|
|
|
|
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 {
|
2019-06-09 06:10:22 +00:00
|
|
|
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
|
2018-12-30 00:18:50 +00:00
|
|
|
DatabasePath string
|
|
|
|
BackupDirPath string
|
2018-10-09 15:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// State type stores the global state of the program
|
|
|
|
type State struct {
|
2019-06-09 06:10:22 +00:00
|
|
|
ProxyBook map[string]string
|
|
|
|
|
|
|
|
BindHost string
|
|
|
|
BindPort string
|
2018-10-09 15:07:54 +00:00
|
|
|
|
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
|
|
|
|
|
2019-06-09 06:10:22 +00:00
|
|
|
RedirAddr string
|
2018-10-14 19:32:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 06:10:22 +00:00
|
|
|
func InitState(bindHost, bindPort string, nowFunc func() time.Time) (*State, error) {
|
2018-10-14 19:32:54 +00:00
|
|
|
ret := &State{
|
2019-06-09 06:10:22 +00:00
|
|
|
BindHost: bindHost,
|
|
|
|
BindPort: bindPort,
|
|
|
|
Now: nowFunc,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-06-09 06:10:22 +00:00
|
|
|
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 {
|
2019-06-09 06:10:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 00:18:50 +00:00
|
|
|
up, err := usermanager.MakeUserpanel(preParse.DatabasePath, preParse.BackupDirPath)
|
|
|
|
if err != nil {
|
2019-01-12 17:05:55 +00:00
|
|
|
return errors.New("Attempting to open database: " + err.Error())
|
2018-12-30 00:18:50 +00:00
|
|
|
}
|
|
|
|
sta.Userpanel = up
|
|
|
|
|
2019-06-09 06:10:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|