From ce0b05232ca4fad5478bc227cdf22431cc942afb Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 7 Oct 2021 17:11:18 -0700 Subject: [PATCH] adjustments --- app/main.go | 2 +- go.mod | 6 ++++-- go.sum | 4 ++++ server.go | 37 ++++++++++++++++++------------------- serverConfig.go | 46 ++++++++++++++++++++++++++++++++++++++++------ servers.go | 10 +++++----- 6 files changed, 72 insertions(+), 33 deletions(-) diff --git a/app/main.go b/app/main.go index 42eed2f..7d28600 100644 --- a/app/main.go +++ b/app/main.go @@ -95,8 +95,8 @@ func main() { signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c + server.Shutdown() acn.Close() - server.Close() os.Exit(1) }() diff --git a/go.mod b/go.mod index b158b25..8ff9ecf 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,13 @@ go 1.14 require ( cwtch.im/cwtch v0.8.5 - git.openprivacy.ca/cwtch.im/tapir v0.4.2 - git.openprivacy.ca/openprivacy/connectivity v1.4.3 + git.openprivacy.ca/cwtch.im/tapir v0.4.4 + git.openprivacy.ca/openprivacy/connectivity v1.4.5 git.openprivacy.ca/openprivacy/log v1.0.2 github.com/gtank/ristretto255 v0.1.2 github.com/mattn/go-sqlite3 v1.14.7 github.com/struCoder/pidusage v0.2.1 golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee ) + +replace cwtch.im/cwtch => /home/dan/src/go/src/cwtch.im/cwtch diff --git a/go.sum b/go.sum index a08604c..d70cc96 100644 --- a/go.sum +++ b/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.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.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/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/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.2 h1:HLP4wsw4ljczFAelYnbObIs821z+jgMPCe8uODPnGQM= git.openprivacy.ca/openprivacy/log v1.0.2/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw= diff --git a/server.go b/server.go index 8755327..bd789e5 100644 --- a/server.go +++ b/server.go @@ -37,13 +37,13 @@ type Server interface { CheckStatus() (bool, error) Shutdown() GetStatistics() Statistics - ConfigureAutostart(autostart bool) - Close() Delete(password string) error Onion() string Server() string TofuBundle() string HashName() string + GetAttribute(string) string + SetAttribute(string, string) } type server struct { @@ -122,6 +122,7 @@ func (s *server) Run(acn connectivity.ACN) error { }() s.running = true + s.SetAttribute(AttrEnabled, "true") return nil } @@ -148,13 +149,16 @@ func (s *server) CheckStatus() (bool, error) { // Shutdown kills the app closing all connections and freeing all goroutines func (s *server) Shutdown() { + log.Infof("Shutting down server") s.lock.Lock() defer s.lock.Unlock() s.service.Shutdown() s.tokenTapirService.Shutdown() + log.Infof("Closing Token server Database...") + s.tokenServer.Close() s.metricsPack.Stop() 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. @@ -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 { s.lock.Lock() defer s.lock.Unlock() @@ -202,7 +191,7 @@ func (s *server) Delete(password string) error { } func (s *server) Onion() string { - return tor.GetTorV3Hostname(s.config.PublicKey) + ".onion" + return s.config.Onion() } func (s *server) Server() string { @@ -234,3 +223,13 @@ func (s *server) HashName() string { } 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) +} diff --git a/serverConfig.go b/serverConfig.go index 7b3834b..3f4f5cd 100644 --- a/serverConfig.go +++ b/serverConfig.go @@ -5,17 +5,28 @@ import ( v1 "cwtch.im/cwtch/storage/v1" "encoding/json" "git.openprivacy.ca/cwtch.im/tapir/primitives" + "git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/log" "github.com/gtank/ristretto255" "golang.org/x/crypto/ed25519" "io/ioutil" "os" "path" + "sync" ) const ( // SaltFile is the standard filename to store an encrypted config's SALT under beside it 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 @@ -42,7 +53,9 @@ type Config struct { TokenServiceK ristretto255.Scalar `json:"tokenServiceK"` ServerReporting Reporting `json:"serverReporting"` - AutoStart bool `json:"autostart"` + + attributes map[string]string + lock sync.Mutex } // 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) } -func initDefaultConfig(configDir, filename string, encrypted bool) Config { - config := Config{Encrypted: encrypted, ConfigDir: configDir, FilePath: filename} +func initDefaultConfig(configDir, filename string, encrypted bool) *Config { + config := &Config{Encrypted: encrypted, ConfigDir: configDir, FilePath: filename} id, pk := primitives.InitializeEphemeralIdentity() tid, tpk := primitives.InitializeEphemeralIdentity() @@ -70,7 +83,8 @@ func initDefaultConfig(configDir, filename string, encrypted bool) Config { ReportingGroupID: "", ReportingServerAddr: "", } - config.AutoStart = false + config.attributes[AttrAutostart] = "false" + config.attributes[AttrEnabled] = "true" k := new(ristretto255.Scalar) b := make([]byte, 64) @@ -108,7 +122,7 @@ func CreateConfig(configDir, filename string, encrypted bool, password string) ( } config.Save() - return &config, nil + return config, nil } // 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) config.Save() - return &config, nil + return config, nil } // 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[:]) 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] +} diff --git a/servers.go b/servers.go index 5849245..f8e4f74 100644 --- a/servers.go +++ b/servers.go @@ -22,7 +22,7 @@ type Servers interface { ListServers() []string DeleteServer(onion string, currentPassword string) error - LaunchServers() + LaunchServer(string) ShutdownServer(string) Shutdown() } @@ -54,7 +54,7 @@ func (s *servers) LoadServers(password string) ([]string, error) { loadedServers := []string{} for _, dir := range dirs { 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) s.servers[server.Onion()] = server loadedServers = append(loadedServers, server.Onion()) @@ -110,11 +110,11 @@ func (s *servers) DeleteServer(onion string, password string) error { return errors.New("Server not found") } -// LaunchServers Run() all loaded servers -func (s *servers) LaunchServers() { +// LaunchServer Run() the specified server +func (s *servers) LaunchServer(onion string) { s.lock.Lock() defer s.lock.Unlock() - for _, server := range s.servers { + if server, exists := s.servers[onion]; exists { server.Run(s.acn) } }