Cloak/internal/server/state.go

181 lines
4.1 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"
2019-08-12 22:13:13 +00:00
"fmt"
2019-08-03 10:17:09 +00:00
"github.com/cbeuw/Cloak/internal/server/usermanager"
2018-10-09 15:07:54 +00:00
"io/ioutil"
2019-08-12 22:13:13 +00:00
"net"
"strings"
2018-10-09 15:07:54 +00:00
"sync"
"time"
gmux "github.com/gorilla/mux"
2018-10-09 15:07:54 +00:00
)
type rawConfig struct {
2019-08-19 22:23:41 +00:00
ProxyBook map[string][]string
BypassUID [][]byte
RedirAddr string
PrivateKey string
AdminUID string
DatabasePath string
StreamTimeout int
CncMode bool
2018-10-09 15:07:54 +00:00
}
// State type stores the global state of the program
type State struct {
2019-08-12 22:13:13 +00:00
ProxyBook map[string]net.Addr
BindHost string
BindPort string
2018-10-09 15:07:54 +00:00
Now func() time.Time
AdminUID []byte
2019-08-19 22:23:41 +00:00
Timeout time.Duration
2019-08-04 20:10:59 +00:00
BypassUID map[[16]byte]struct{}
staticPv crypto.PrivateKey
RedirAddr string
2018-10-14 19:32:54 +00:00
usedRandomM sync.RWMutex
2019-08-03 10:49:05 +00:00
usedRandom map[[32]byte]int64
2018-10-14 19:32:54 +00:00
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{
2019-08-12 22:13:13 +00:00
BindHost: bindHost,
BindPort: bindPort,
Now: nowFunc,
BypassUID: make(map[[16]byte]struct{}),
ProxyBook: map[string]net.Addr{},
usedRandom: map[[32]byte]int64{},
2018-10-14 19:32:54 +00:00
}
go ret.UsedRandomCleaner()
2018-11-07 21:16:13 +00:00
return ret, nil
2018-10-09 15:07:54 +00:00
}
2019-08-20 21:43:04 +00:00
// ParseConfig parses the config (either a path to json or the json itself as argument) into a State variable
2018-10-09 15:07:54 +00:00
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 {
2019-07-31 13:44:34 +00:00
//TODO: implement command & control mode
} else {
2019-08-03 10:17:09 +00:00
manager, err := usermanager.MakeLocalManager(preParse.DatabasePath)
if err != nil {
return err
}
sta.Panel = MakeUserPanel(manager)
sta.LocalAPIRouter = manager.Router
}
sta.RedirAddr = preParse.RedirAddr
2019-08-19 22:23:41 +00:00
sta.Timeout = time.Duration(preParse.StreamTimeout) * time.Second
2019-08-12 22:13:13 +00:00
for name, pair := range preParse.ProxyBook {
if len(pair) != 2 {
return fmt.Errorf("invalid protocol and address pair for %v: %v", name, pair)
}
network := strings.ToLower(pair[0])
switch network {
case "tcp":
addr, err := net.ResolveTCPAddr("tcp", pair[1])
if err != nil {
return err
}
sta.ProxyBook[name] = addr
continue
case "udp":
addr, err := net.ResolveUDPAddr("udp", pair[1])
if err != nil {
return err
}
sta.ProxyBook[name] = addr
continue
}
}
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
2019-08-04 20:10:59 +00:00
var arrUID [16]byte
for _, UID := range preParse.BypassUID {
copy(arrUID[:], UID)
sta.BypassUID[arrUID] = struct{}{}
}
copy(arrUID[:], adminUID)
sta.BypassUID[arrUID] = struct{}{}
2018-10-09 15:07:54 +00:00
return nil
}
2019-08-20 21:43:04 +00:00
// IsBypass checks if a UID is a bypass user
2019-08-04 20:10:59 +00:00
func (sta *State) IsBypass(UID []byte) bool {
var arrUID [16]byte
copy(arrUID[:], UID)
_, exist := sta.BypassUID[arrUID]
return exist
}
const TIMESTAMP_TOLERANCE = 180 * time.Second
const CACHE_CLEAN_INTERVAL = 12 * time.Hour
2019-08-02 00:01:19 +00:00
2019-08-20 21:43:04 +00:00
// UsedRandomCleaner clears the cache of used random fields every CACHE_CLEAN_INTERVAL
2018-10-09 15:07:54 +00:00
func (sta *State) UsedRandomCleaner() {
for {
time.Sleep(CACHE_CLEAN_INTERVAL)
now := sta.Now()
2018-10-14 19:32:54 +00:00
sta.usedRandomM.Lock()
for key, t := range sta.usedRandom {
if time.Unix(t, 0).Before(now.Add(TIMESTAMP_TOLERANCE)) {
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
}
}
2019-08-03 10:49:05 +00:00
func (sta *State) registerRandom(r []byte) bool {
var random [32]byte
copy(random[:], r)
sta.usedRandomM.Lock()
_, used := sta.usedRandom[random]
sta.usedRandom[random] = sta.Now().Unix()
sta.usedRandomM.Unlock()
return used
}