This repository has been archived on 2023-06-16. You can view files and clone it, but cannot push or open issues or pull requests.
libcwtch-go/features/servers/servers_functionality.go

146 lines
3.6 KiB
Go
Raw Normal View History

2021-11-01 16:10:56 +00:00
package servers
import (
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/model/attr"
2021-11-01 16:10:56 +00:00
"fmt"
"git.openprivacy.ca/cwtch.im/libcwtch-go/constants"
"git.openprivacy.ca/cwtch.im/server"
2021-11-01 16:10:56 +00:00
"git.openprivacy.ca/openprivacy/connectivity"
2021-11-05 20:21:48 +00:00
"git.openprivacy.ca/openprivacy/log"
"os"
2021-11-05 20:21:48 +00:00
"path/filepath"
2021-11-01 16:10:56 +00:00
"sync"
)
const serversExperiment = "servers-experiment"
const (
2021-11-17 20:33:51 +00:00
ZeroServersLoaded = event.Type("ZeroServersLoaded")
NewServer = event.Type("NewServer")
2021-11-01 16:10:56 +00:00
ServerIntentUpdate = event.Type("ServerIntentUpdate")
2021-11-18 23:44:21 +00:00
ServerDeleted = event.Type("ServerDeleted")
2021-11-01 16:10:56 +00:00
)
const (
Intent = event.Field("Intent")
)
const (
IntentRunning = "running"
IntentStopped = "stopped"
2021-11-01 16:10:56 +00:00
)
// TODO: move into Cwtch model/attr
const ServerZone = attr.Zone("server")
type ServerInfo struct {
2021-11-17 20:33:51 +00:00
Onion string
ServerBundle string
2021-11-17 20:33:51 +00:00
Autostart bool
Running bool
Description string
StorageType string
}
2021-11-01 16:10:56 +00:00
var lock sync.Mutex
var appServers server.Servers
func InitServers(acn connectivity.ACN, appdir string) {
lock.Lock()
defer lock.Unlock()
if appServers == nil {
2021-11-05 20:21:48 +00:00
serversDir := filepath.Join(appdir, "servers")
2021-11-05 20:16:13 +00:00
err := os.MkdirAll(serversDir, 0700)
if err != nil {
log.Errorf("Could not init servers directory: %s", err)
}
appServers = server.NewServers(acn, serversDir)
appServers.LoadServers(constants.DefactoPasswordForUnencryptedProfiles)
2021-11-01 16:10:56 +00:00
}
}
func DeactivateServers() {
lock.Lock()
defer lock.Unlock()
if appServers != nil {
appServers.Stop()
}
2021-11-01 16:10:56 +00:00
}
// ServersFunctionality provides experiment gated server functionality
type ServersFunctionality struct {
}
// ExperimentGate returns ServersFunctionality if the experiment is enabled, and an error otherwise.
func ExperimentGate(experimentMap map[string]bool) (*ServersFunctionality, error) {
if experimentMap[serversExperiment] {
lock.Lock()
2021-11-17 20:33:51 +00:00
defer lock.Unlock()
2021-11-01 16:10:56 +00:00
return &ServersFunctionality{}, nil
}
return nil, fmt.Errorf("gated by %v", serversExperiment)
}
func (sf *ServersFunctionality) LoadServers(password string) ([]string, error) {
return appServers.LoadServers(password)
}
func (sf *ServersFunctionality) CreateServer(password string) (server.Server, error) {
return appServers.CreateServer(password)
}
func (sf *ServersFunctionality) GetServer(onion string) server.Server {
return appServers.GetServer(onion)
}
func (sf *ServersFunctionality) ListServers() []string {
return appServers.ListServers()
}
func (sf *ServersFunctionality) DeleteServer(onion string, currentPassword string) error {
return appServers.DeleteServer(onion, currentPassword)
}
func (sf *ServersFunctionality) LaunchServer(onion string) {
appServers.LaunchServer(onion)
}
func (sf *ServersFunctionality) StopServer(onion string) {
appServers.StopServer(onion)
}
func (sf *ServersFunctionality) DestroyServers() {
appServers.Destroy()
2021-11-01 16:10:56 +00:00
}
func (sf *ServersFunctionality) GetServerInfo(onion string) *ServerInfo {
2021-11-01 16:10:56 +00:00
s := sf.GetServer(onion)
var serverInfo ServerInfo
serverInfo.Onion = s.Onion()
serverInfo.ServerBundle = s.ServerBundle()
serverInfo.Autostart = s.GetAttribute(server.AttrAutostart) == "true"
running, _ := s.CheckStatus()
serverInfo.Running = running
serverInfo.Description = s.GetAttribute(server.AttrDescription)
serverInfo.StorageType = s.GetAttribute(server.AttrStorageType)
return &serverInfo
}
func (si *ServerInfo) EnrichEvent(e *event.Event) {
e.Data["Onion"] = si.Onion
e.Data["ServerBundle"] = si.ServerBundle
e.Data["Description"] = si.Description
e.Data["StorageType"] = si.StorageType
if si.Autostart {
e.Data["Autostart"] = "true"
} else {
e.Data["Autostart"] = "false"
}
if si.Running {
e.Data["Running"] = "true"
} else {
e.Data["Running"] = "false"
}
2021-11-05 20:16:13 +00:00
}