forked from cwtch.im/server
adjustments
This commit is contained in:
parent
d361d71a2a
commit
ce0b05232c
|
@ -95,8 +95,8 @@ func main() {
|
||||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
go func() {
|
go func() {
|
||||||
<-c
|
<-c
|
||||||
|
server.Shutdown()
|
||||||
acn.Close()
|
acn.Close()
|
||||||
server.Close()
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
6
go.mod
6
go.mod
|
@ -4,11 +4,13 @@ go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
cwtch.im/cwtch v0.8.5
|
cwtch.im/cwtch v0.8.5
|
||||||
git.openprivacy.ca/cwtch.im/tapir v0.4.2
|
git.openprivacy.ca/cwtch.im/tapir v0.4.4
|
||||||
git.openprivacy.ca/openprivacy/connectivity v1.4.3
|
git.openprivacy.ca/openprivacy/connectivity v1.4.5
|
||||||
git.openprivacy.ca/openprivacy/log v1.0.2
|
git.openprivacy.ca/openprivacy/log v1.0.2
|
||||||
github.com/gtank/ristretto255 v0.1.2
|
github.com/gtank/ristretto255 v0.1.2
|
||||||
github.com/mattn/go-sqlite3 v1.14.7
|
github.com/mattn/go-sqlite3 v1.14.7
|
||||||
github.com/struCoder/pidusage v0.2.1
|
github.com/struCoder/pidusage v0.2.1
|
||||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
|
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
|
||||||
)
|
)
|
||||||
|
|
||||||
|
replace cwtch.im/cwtch => /home/dan/src/go/src/cwtch.im/cwtch
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -10,10 +10,14 @@ git.openprivacy.ca/cwtch.im/tapir v0.4.1 h1:9LMpQX41IzecNNlRc1FZKXHg6wlFss679tFs
|
||||||
git.openprivacy.ca/cwtch.im/tapir v0.4.1/go.mod h1:eH6dZxXrhW0C4KZX18ksUa6XJCrEvtg8cJJ/Fy6gv+E=
|
git.openprivacy.ca/cwtch.im/tapir v0.4.1/go.mod h1:eH6dZxXrhW0C4KZX18ksUa6XJCrEvtg8cJJ/Fy6gv+E=
|
||||||
git.openprivacy.ca/cwtch.im/tapir v0.4.2 h1:bxMWZnVJXX4dqqOFS7ELW4iFkVL4GS8wiRkjRv5rJe8=
|
git.openprivacy.ca/cwtch.im/tapir v0.4.2 h1:bxMWZnVJXX4dqqOFS7ELW4iFkVL4GS8wiRkjRv5rJe8=
|
||||||
git.openprivacy.ca/cwtch.im/tapir v0.4.2/go.mod h1:eH6dZxXrhW0C4KZX18ksUa6XJCrEvtg8cJJ/Fy6gv+E=
|
git.openprivacy.ca/cwtch.im/tapir v0.4.2/go.mod h1:eH6dZxXrhW0C4KZX18ksUa6XJCrEvtg8cJJ/Fy6gv+E=
|
||||||
|
git.openprivacy.ca/cwtch.im/tapir v0.4.4 h1:KyuTVmr9GYptTCeR7JDODjmhBBbnIBf9V3NSC4+6bHc=
|
||||||
|
git.openprivacy.ca/cwtch.im/tapir v0.4.4/go.mod h1:qMFTdmDZITc1BLP1jSW0gVpLmvpg+Zjsh5ek8StwbFE=
|
||||||
git.openprivacy.ca/openprivacy/bine v0.0.4 h1:CO7EkGyz+jegZ4ap8g5NWRuDHA/56KKvGySR6OBPW+c=
|
git.openprivacy.ca/openprivacy/bine v0.0.4 h1:CO7EkGyz+jegZ4ap8g5NWRuDHA/56KKvGySR6OBPW+c=
|
||||||
git.openprivacy.ca/openprivacy/bine v0.0.4/go.mod h1:13ZqhKyqakDsN/ZkQkIGNULsmLyqtXc46XBcnuXm/mU=
|
git.openprivacy.ca/openprivacy/bine v0.0.4/go.mod h1:13ZqhKyqakDsN/ZkQkIGNULsmLyqtXc46XBcnuXm/mU=
|
||||||
git.openprivacy.ca/openprivacy/connectivity v1.4.3 h1:i2Ad/U9FlL9dKr2bhRck7lJ8NoWyGtoEfUwoCyMT0fU=
|
git.openprivacy.ca/openprivacy/connectivity v1.4.3 h1:i2Ad/U9FlL9dKr2bhRck7lJ8NoWyGtoEfUwoCyMT0fU=
|
||||||
git.openprivacy.ca/openprivacy/connectivity v1.4.3/go.mod h1:bR0Myx9nm2YzWtsThRelkNMV4Pp7sPDa123O1qsAbVo=
|
git.openprivacy.ca/openprivacy/connectivity v1.4.3/go.mod h1:bR0Myx9nm2YzWtsThRelkNMV4Pp7sPDa123O1qsAbVo=
|
||||||
|
git.openprivacy.ca/openprivacy/connectivity v1.4.5 h1:UYMdCWPzEAP7LbqdMXGNXmfKjWlvfnKdmewBtnbgQRI=
|
||||||
|
git.openprivacy.ca/openprivacy/connectivity v1.4.5/go.mod h1:JVRCIdL+lAG6ohBFWiKeC/MN42nnC0sfFszR9XG6vPQ=
|
||||||
git.openprivacy.ca/openprivacy/log v1.0.1/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw=
|
git.openprivacy.ca/openprivacy/log v1.0.1/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw=
|
||||||
git.openprivacy.ca/openprivacy/log v1.0.2 h1:HLP4wsw4ljczFAelYnbObIs821z+jgMPCe8uODPnGQM=
|
git.openprivacy.ca/openprivacy/log v1.0.2 h1:HLP4wsw4ljczFAelYnbObIs821z+jgMPCe8uODPnGQM=
|
||||||
git.openprivacy.ca/openprivacy/log v1.0.2/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw=
|
git.openprivacy.ca/openprivacy/log v1.0.2/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw=
|
||||||
|
|
37
server.go
37
server.go
|
@ -37,13 +37,13 @@ type Server interface {
|
||||||
CheckStatus() (bool, error)
|
CheckStatus() (bool, error)
|
||||||
Shutdown()
|
Shutdown()
|
||||||
GetStatistics() Statistics
|
GetStatistics() Statistics
|
||||||
ConfigureAutostart(autostart bool)
|
|
||||||
Close()
|
|
||||||
Delete(password string) error
|
Delete(password string) error
|
||||||
Onion() string
|
Onion() string
|
||||||
Server() string
|
Server() string
|
||||||
TofuBundle() string
|
TofuBundle() string
|
||||||
HashName() string
|
HashName() string
|
||||||
|
GetAttribute(string) string
|
||||||
|
SetAttribute(string, string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
|
@ -122,6 +122,7 @@ func (s *server) Run(acn connectivity.ACN) error {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.running = true
|
s.running = true
|
||||||
|
s.SetAttribute(AttrEnabled, "true")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,13 +149,16 @@ func (s *server) CheckStatus() (bool, error) {
|
||||||
|
|
||||||
// Shutdown kills the app closing all connections and freeing all goroutines
|
// Shutdown kills the app closing all connections and freeing all goroutines
|
||||||
func (s *server) Shutdown() {
|
func (s *server) Shutdown() {
|
||||||
|
log.Infof("Shutting down server")
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
s.service.Shutdown()
|
s.service.Shutdown()
|
||||||
s.tokenTapirService.Shutdown()
|
s.tokenTapirService.Shutdown()
|
||||||
|
log.Infof("Closing Token server Database...")
|
||||||
|
s.tokenServer.Close()
|
||||||
s.metricsPack.Stop()
|
s.metricsPack.Stop()
|
||||||
s.running = true
|
s.running = true
|
||||||
|
s.SetAttribute(AttrEnabled, "false")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Statistics is an encapsulation of information about the server that an operator might want to know at a glance.
|
// Statistics is an encapsulation of information about the server that an operator might want to know at a glance.
|
||||||
|
@ -176,21 +180,6 @@ func (s *server) GetStatistics() Statistics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigureAutostart sets whether this server should autostart (in the Cwtch UI/bundling application)
|
|
||||||
func (s *server) ConfigureAutostart(autostart bool) {
|
|
||||||
s.config.AutoStart = autostart
|
|
||||||
s.config.Save()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close shuts down the cwtch server in a safe way.
|
|
||||||
func (s *server) Close() {
|
|
||||||
log.Infof("Shutting down server")
|
|
||||||
s.lock.Lock()
|
|
||||||
defer s.lock.Unlock()
|
|
||||||
log.Infof("Closing Token server Database...")
|
|
||||||
s.tokenServer.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *server) Delete(password string) error {
|
func (s *server) Delete(password string) error {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
|
@ -202,7 +191,7 @@ func (s *server) Delete(password string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Onion() string {
|
func (s *server) Onion() string {
|
||||||
return tor.GetTorV3Hostname(s.config.PublicKey) + ".onion"
|
return s.config.Onion()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Server() string {
|
func (s *server) Server() string {
|
||||||
|
@ -234,3 +223,13 @@ func (s *server) HashName() string {
|
||||||
}
|
}
|
||||||
return strings.Join(words, "-")
|
return strings.Join(words, "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAttribute gets a server attribute
|
||||||
|
func (s *server) GetAttribute(key string) string {
|
||||||
|
return s.config.GetAttribute(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAttribute sets a server attribute
|
||||||
|
func (s *server) SetAttribute(key, val string) {
|
||||||
|
s.config.SetAttribute(key, val)
|
||||||
|
}
|
||||||
|
|
|
@ -5,17 +5,28 @@ import (
|
||||||
v1 "cwtch.im/cwtch/storage/v1"
|
v1 "cwtch.im/cwtch/storage/v1"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
||||||
|
"git.openprivacy.ca/openprivacy/connectivity/tor"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
"git.openprivacy.ca/openprivacy/log"
|
||||||
"github.com/gtank/ristretto255"
|
"github.com/gtank/ristretto255"
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// SaltFile is the standard filename to store an encrypted config's SALT under beside it
|
// SaltFile is the standard filename to store an encrypted config's SALT under beside it
|
||||||
SaltFile = "SALT"
|
SaltFile = "SALT"
|
||||||
|
|
||||||
|
// AttrAutostart is the attribute key for autostart setting
|
||||||
|
AttrAutostart = "autostart"
|
||||||
|
|
||||||
|
// AttrDescription is the attribute key for a user set server description
|
||||||
|
AttrDescription = "description"
|
||||||
|
|
||||||
|
// AttrEnabled is the attribute key for user toggle of server being enabled
|
||||||
|
AttrEnabled = "enabled"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
|
// Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
|
||||||
|
@ -42,7 +53,9 @@ type Config struct {
|
||||||
TokenServiceK ristretto255.Scalar `json:"tokenServiceK"`
|
TokenServiceK ristretto255.Scalar `json:"tokenServiceK"`
|
||||||
|
|
||||||
ServerReporting Reporting `json:"serverReporting"`
|
ServerReporting Reporting `json:"serverReporting"`
|
||||||
AutoStart bool `json:"autostart"`
|
|
||||||
|
attributes map[string]string
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identity returns an encapsulation of the servers keys
|
// Identity returns an encapsulation of the servers keys
|
||||||
|
@ -55,8 +68,8 @@ func (config *Config) TokenServiceIdentity() primitives.Identity {
|
||||||
return primitives.InitializeIdentity("", &config.TokenServerPrivateKey, &config.TokenServerPublicKey)
|
return primitives.InitializeIdentity("", &config.TokenServerPrivateKey, &config.TokenServerPublicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDefaultConfig(configDir, filename string, encrypted bool) Config {
|
func initDefaultConfig(configDir, filename string, encrypted bool) *Config {
|
||||||
config := Config{Encrypted: encrypted, ConfigDir: configDir, FilePath: filename}
|
config := &Config{Encrypted: encrypted, ConfigDir: configDir, FilePath: filename}
|
||||||
|
|
||||||
id, pk := primitives.InitializeEphemeralIdentity()
|
id, pk := primitives.InitializeEphemeralIdentity()
|
||||||
tid, tpk := primitives.InitializeEphemeralIdentity()
|
tid, tpk := primitives.InitializeEphemeralIdentity()
|
||||||
|
@ -70,7 +83,8 @@ func initDefaultConfig(configDir, filename string, encrypted bool) Config {
|
||||||
ReportingGroupID: "",
|
ReportingGroupID: "",
|
||||||
ReportingServerAddr: "",
|
ReportingServerAddr: "",
|
||||||
}
|
}
|
||||||
config.AutoStart = false
|
config.attributes[AttrAutostart] = "false"
|
||||||
|
config.attributes[AttrEnabled] = "true"
|
||||||
|
|
||||||
k := new(ristretto255.Scalar)
|
k := new(ristretto255.Scalar)
|
||||||
b := make([]byte, 64)
|
b := make([]byte, 64)
|
||||||
|
@ -108,7 +122,7 @@ func CreateConfig(configDir, filename string, encrypted bool, password string) (
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Save()
|
config.Save()
|
||||||
return &config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig loads a Config from a json file specified by filename
|
// LoadConfig loads a Config from a json file specified by filename
|
||||||
|
@ -142,7 +156,7 @@ func LoadConfig(configDir, filename string, encrypted bool, password string) (*C
|
||||||
|
|
||||||
// Always save (first time generation, new version with new variables populated)
|
// Always save (first time generation, new version with new variables populated)
|
||||||
config.Save()
|
config.Save()
|
||||||
return &config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save dumps the latest version of the config to a json file given by filename
|
// Save dumps the latest version of the config to a json file given by filename
|
||||||
|
@ -165,3 +179,23 @@ func (config *Config) CheckPassword(checkpass string) bool {
|
||||||
oldkey := v1.CreateKey(checkpass, salt[:])
|
oldkey := v1.CreateKey(checkpass, salt[:])
|
||||||
return oldkey == config.key
|
return oldkey == config.key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Onion returns the .onion url for the server
|
||||||
|
func (config *Config) Onion() string {
|
||||||
|
return tor.GetTorV3Hostname(config.PublicKey) + ".onion"
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAttribute sets a server attribute
|
||||||
|
func (config *Config) SetAttribute(key, val string) {
|
||||||
|
config.lock.Lock()
|
||||||
|
defer config.lock.Unlock()
|
||||||
|
config.attributes[key] = val
|
||||||
|
config.Save()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAttribute gets a server attribute
|
||||||
|
func (config *Config) GetAttribute(key string) string {
|
||||||
|
config.lock.Lock()
|
||||||
|
defer config.lock.Unlock()
|
||||||
|
return config.attributes[key]
|
||||||
|
}
|
||||||
|
|
10
servers.go
10
servers.go
|
@ -22,7 +22,7 @@ type Servers interface {
|
||||||
ListServers() []string
|
ListServers() []string
|
||||||
DeleteServer(onion string, currentPassword string) error
|
DeleteServer(onion string, currentPassword string) error
|
||||||
|
|
||||||
LaunchServers()
|
LaunchServer(string)
|
||||||
ShutdownServer(string)
|
ShutdownServer(string)
|
||||||
Shutdown()
|
Shutdown()
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ func (s *servers) LoadServers(password string) ([]string, error) {
|
||||||
loadedServers := []string{}
|
loadedServers := []string{}
|
||||||
for _, dir := range dirs {
|
for _, dir := range dirs {
|
||||||
newConfig, err := LoadConfig(path.Join(s.directory, dir.Name()), ServerConfigFile, true, password)
|
newConfig, err := LoadConfig(path.Join(s.directory, dir.Name()), ServerConfigFile, true, password)
|
||||||
if err == nil {
|
if _, exists := s.servers[newConfig.Onion()]; err == nil && !exists {
|
||||||
server := NewServer(newConfig)
|
server := NewServer(newConfig)
|
||||||
s.servers[server.Onion()] = server
|
s.servers[server.Onion()] = server
|
||||||
loadedServers = append(loadedServers, server.Onion())
|
loadedServers = append(loadedServers, server.Onion())
|
||||||
|
@ -110,11 +110,11 @@ func (s *servers) DeleteServer(onion string, password string) error {
|
||||||
return errors.New("Server not found")
|
return errors.New("Server not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// LaunchServers Run() all loaded servers
|
// LaunchServer Run() the specified server
|
||||||
func (s *servers) LaunchServers() {
|
func (s *servers) LaunchServer(onion string) {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
for _, server := range s.servers {
|
if server, exists := s.servers[onion]; exists {
|
||||||
server.Run(s.acn)
|
server.Run(s.acn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue