diff --git a/features/servers/servers_functionality.go b/features/servers/servers_functionality.go new file mode 100644 index 0000000..3b93611 --- /dev/null +++ b/features/servers/servers_functionality.go @@ -0,0 +1,98 @@ +package servers + +import ( + "cwtch.im/cwtch/event" + "encoding/json" + "fmt" + "git.openprivacy.ca/openprivacy/connectivity" + "path" + "sync" +) +import "git.openprivacy.ca/cwtch.im/server" + +const serversExperiment = "servers-experiment" + +const ( + ZeroServersLoaded = event.Type("ZeroServersLoaded") + NewServer = event.Type("NewServer") + ServerIntentUpdate = event.Type("ServerIntentUpdate") +) + +const ( + Intent = event.Field("Intent") +) + +const ( + IntentEnabled = "enabled" + IntentDisabled = "disabled" +) + +var lock sync.Mutex +var appServers server.Servers + +func InitServers(acn connectivity.ACN, appdir string) { + lock.Lock() + defer lock.Unlock() + if appServers == nil { + appServers = server.NewServers(acn, path.Join(appdir, "servers")) + } +} + +func DeactivateServers() { + appServers.Shutdown() +} + +// 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() + defer lock.Unlock() + 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) ShutdownServer(onion string) { + appServers.ShutdownServer(onion) +} + +func (sf *ServersFunctionality) SeverToJson(onion string) string { + s := sf.GetServer(onion) + hash := make(map[string]string) + hash["onion"] = s.Onion() + hash["serverbundle"] = s.ServerBundle() + hash[server.AttrAutostart] = s.GetAttribute(server.AttrAutostart) + hash[server.AttrEnabled] = s.GetAttribute(server.AttrEnabled) + hash[server.AttrDescription] = s.GetAttribute(server.AttrDescription) + hash[server.AttrStorageType] = s.GetAttribute(server.AttrStorageType) + json, _ := json.Marshal(hash) + return string(json) +} \ No newline at end of file diff --git a/go.mod b/go.mod index 75f508f..2168528 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,7 @@ require ( git.openprivacy.ca/cwtch.im/server v1.3.1 git.openprivacy.ca/openprivacy/connectivity v1.5.0 git.openprivacy.ca/openprivacy/log v1.0.3 + golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 // indirect + golang.org/x/mod v0.5.0 // indirect golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect ) diff --git a/go.sum b/go.sum index 7f78350..425ba71 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,13 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI= golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 h1:3In5TnfvnuXTF/uflgpYxSCEGP2NdYT37KsPh3VjZYU= +golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554/go.mod h1:jFTmtFYCV0MFtXBU+J5V/+5AUeVS0ON/0WkE/KSrl6E= +golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.5.0 h1:UG21uOlmZabA4fW5i7ZX6bjw1xELEGg/ZLgZq9auk/Q= +golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= diff --git a/lib.go b/lib.go index 9f9721b..324f735 100644 --- a/lib.go +++ b/lib.go @@ -10,25 +10,26 @@ import ( "crypto/rand" "encoding/json" "fmt" - "git.openprivacy.ca/cwtch.im/libcwtch-go/features/servers" - "git.openprivacy.ca/cwtch.im/server" "os/user" "runtime" "strconv" "strings" "unsafe" + "git.openprivacy.ca/openprivacy/connectivity" "cwtch.im/cwtch/app" "cwtch.im/cwtch/event" "cwtch.im/cwtch/functionality/filesharing" "cwtch.im/cwtch/model" "cwtch.im/cwtch/model/attr" "cwtch.im/cwtch/peer" + "git.openprivacy.ca/cwtch.im/server" "git.openprivacy.ca/cwtch.im/libcwtch-go/constants" contact "git.openprivacy.ca/cwtch.im/libcwtch-go/features/contacts" "git.openprivacy.ca/cwtch.im/libcwtch-go/features/groups" + "git.openprivacy.ca/cwtch.im/libcwtch-go/features/servers" + "git.openprivacy.ca/cwtch.im/libcwtch-go/utils" - "git.openprivacy.ca/openprivacy/connectivity" "encoding/base64" mrand "math/rand"