From 063ad61e363f8bed05fa35bdd9daa45bd7b37f68 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 1 Nov 2021 18:48:00 -0700 Subject: [PATCH] move from Shutdown to Stop/Destroy, remove enabled in favor of running from CheckStatus --- features/servers/servers_functionality.go | 63 +++++++++++++++------ go.sum | 23 +++++++- lib.go | 68 ++++++++++++++--------- utils/eventHandler.go | 1 + 4 files changed, 111 insertions(+), 44 deletions(-) diff --git a/features/servers/servers_functionality.go b/features/servers/servers_functionality.go index 3b93611..0f679d1 100644 --- a/features/servers/servers_functionality.go +++ b/features/servers/servers_functionality.go @@ -2,13 +2,12 @@ package servers import ( "cwtch.im/cwtch/event" - "encoding/json" "fmt" + "git.openprivacy.ca/cwtch.im/server" "git.openprivacy.ca/openprivacy/connectivity" "path" "sync" ) -import "git.openprivacy.ca/cwtch.im/server" const serversExperiment = "servers-experiment" @@ -23,10 +22,19 @@ const ( ) const ( - IntentEnabled = "enabled" - IntentDisabled = "disabled" + IntentRunning = "running" + IntentStopped = "stopped" ) +type ServerInfo struct { + Onion string + ServerBundle string + Autostart bool + Running bool + Description string + StorageType string +} + var lock sync.Mutex var appServers server.Servers @@ -39,7 +47,7 @@ func InitServers(acn connectivity.ACN, appdir string) { } func DeactivateServers() { - appServers.Shutdown() + appServers.Stop() } // ServersFunctionality provides experiment gated server functionality @@ -80,19 +88,40 @@ func (sf *ServersFunctionality) LaunchServer(onion string) { appServers.LaunchServer(onion) } -func (sf *ServersFunctionality) ShutdownServer(onion string) { - appServers.ShutdownServer(onion) +func (sf *ServersFunctionality) StopServer(onion string) { + appServers.StopServer(onion) } -func (sf *ServersFunctionality) SeverToJson(onion string) string { +func (sf *ServersFunctionality) DestroyServers() { + appServers.Destroy() +} + +func (sf *ServersFunctionality) GetServerInfo(onion string) *ServerInfo { 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) + 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" + } } \ No newline at end of file diff --git a/go.sum b/go.sum index 425ba71..678cfdb 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,11 @@ cwtch.im/cwtch v0.12.2 h1:I+ndKadCRCITw4SPbd+1cpRv+z/7iHjjTUv8OzRwTrE= cwtch.im/cwtch v0.12.2/go.mod h1:QpTkQK7MqNt0dQK9/pBk5VpkvFhy6xuoxJIn401B8fM= +cwtch.im/cwtch v0.13.0 h1:9WhLG9czfyRceZnHSqfTAq0vfmDC/E20mziJb9+Skrg= +cwtch.im/cwtch v0.13.0/go.mod h1:QpTkQK7MqNt0dQK9/pBk5VpkvFhy6xuoxJIn401B8fM= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= -git.openprivacy.ca/cwtch.im/server v1.2.0 h1:0upP6XkGXoQW6J+jXyzPqjdEvtjaZEgVQgmucPBH/i8= -git.openprivacy.ca/cwtch.im/server v1.2.0/go.mod h1:gps6glXDt/ra66Do491csrm0TwatAc2lMLOAKCkh5Vw= +git.openprivacy.ca/cwtch.im/server v1.3.1 h1:Kt4TnlGxGPk1KTjvs1cXtnFWDx+hYqu8w+2eIaqt+F4= +git.openprivacy.ca/cwtch.im/server v1.3.1/go.mod h1:gps6glXDt/ra66Do491csrm0TwatAc2lMLOAKCkh5Vw= git.openprivacy.ca/cwtch.im/tapir v0.4.9 h1:LXonlztwvI1F1++0IyomIcDH1/Bxzo+oN8YjGonNvjM= git.openprivacy.ca/cwtch.im/tapir v0.4.9/go.mod h1:p4bHo3DAO8wwimU6JAeZXbfPQ4jnoA2bV+4YvknWTNQ= git.openprivacy.ca/openprivacy/bine v0.0.4 h1:CO7EkGyz+jegZ4ap8g5NWRuDHA/56KKvGySR6OBPW+c= @@ -13,6 +15,7 @@ git.openprivacy.ca/openprivacy/connectivity v1.5.0/go.mod h1:UjQiGBnWbotmBzIw59B git.openprivacy.ca/openprivacy/log v1.0.2/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw= git.openprivacy.ca/openprivacy/log v1.0.3 h1:E/PMm4LY+Q9s3aDpfySfEDq/vYQontlvNj/scrPaga0= git.openprivacy.ca/openprivacy/log v1.0.3/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -40,12 +43,18 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/struCoder/pidusage v0.2.1 h1:dFiEgUDkubeIj0XA1NpQ6+8LQmKrLi7NiIQl86E6BoY= github.com/struCoder/pidusage v0.2.1/go.mod h1:bewtP2KUA1TBUyza5+/PCpSQ6sc/H6jJbIKAzqW86BA= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg= go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 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/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= +golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= 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= @@ -53,10 +62,14 @@ 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-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 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= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -72,6 +85,12 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= diff --git a/lib.go b/lib.go index 324f735..6e0616c 100644 --- a/lib.go +++ b/lib.go @@ -16,18 +16,18 @@ import ( "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/server" + "git.openprivacy.ca/openprivacy/connectivity" "git.openprivacy.ca/cwtch.im/libcwtch-go/utils" @@ -282,8 +282,10 @@ func ReconnectCwtchForeground() { if err == nil { serversList := serversHandler.ListServers() for _, server := range serversList { - json := serversHandler.SeverToJson(server) - application.GetPrimaryBus().Publish(event.NewEventList(servers.NewServer, event.Data, json)) + serverInfo := serversHandler.GetServerInfo(server) + ev := event.NewEvent(servers.NewServer, make(map[event.Field]string)) + serverInfo.EnrichEvent(&ev) + application.GetPrimaryBus().Publish(ev) } } @@ -984,9 +986,11 @@ func LoadServers(password string) { application.GetPrimaryBus().Publish(event.NewEventList(servers.ZeroServersLoaded)) } else { for _, serverOnion := range serversList { - json := serversHandler.SeverToJson(serverOnion) - log.Debugf("Load Server NewServer event: %s", json) - application.GetPrimaryBus().Publish(event.NewEventList(servers.NewServer, event.Data, json)) + serverInfo := serversHandler.GetServerInfo(serverOnion) + log.Debugf("Load Server NewServer event: %s", serverInfo) + ev := event.NewEvent(servers.NewServer, make(map[event.Field]string)) + serverInfo.EnrichEvent(&ev) + application.GetPrimaryBus().Publish(ev) } } } @@ -1016,9 +1020,11 @@ func CreateServer(password string, description string, autostart bool) { } else { s.SetAttribute(server.AttrStorageType, server.StorageTypePassword) } - json := serversHandler.SeverToJson(s.Onion()) - log.Debugf("Creating Server NewServer event: %s", json) - application.GetPrimaryBus().Publish(event.NewEventList(servers.NewServer, event.Data, json)) + serverInfo := serversHandler.GetServerInfo(s.Onion()) + log.Debugf("Creating Server NewServer event: %s", serverInfo) + ev := event.NewEvent(servers.NewServer, make(map[event.Field]string)) + serverInfo.EnrichEvent(&ev) + application.GetPrimaryBus().Publish(ev) } } } @@ -1031,10 +1037,10 @@ func c_DeleteServer(onionPtr *C.char, onionLen C.int, currentPasswordPtr *C.char func DeleteServer(onion string, currentPassword string) { serversHandler, err := servers.ExperimentGate(utils.ReadGlobalSettings().Experiments) if err == nil { - serversHandler.ShutdownServer(onion) - application.GetPrimaryBus().Publish(event.NewEventList(servers.ServerIntentUpdate, servers.Intent, servers.IntentDisabled)) + serversHandler.StopServer(onion) + application.GetPrimaryBus().Publish(event.NewEventList(servers.ServerIntentUpdate, event.Identity, onion, servers.Intent, servers.IntentStopped)) serversHandler.DeleteServer(onion, currentPassword) - // TODO HANDLE err from DeletServer? + // TODO HANDLE err from DeleteServer? } } @@ -1067,38 +1073,50 @@ func LaunchServer(onion string) { serversHandler, err := servers.ExperimentGate(utils.ReadGlobalSettings().Experiments) if err == nil { serversHandler.LaunchServer(onion) - application.GetPrimaryBus().Publish(event.NewEventList(servers.ServerIntentUpdate, servers.Intent, servers.IntentEnabled)) + application.GetPrimaryBus().Publish(event.NewEventList(servers.ServerIntentUpdate, event.Identity, onion, servers.Intent, servers.IntentRunning)) } } -//export c_ShutdownServer -func c_ShutdownServer(onionPtr *C.char, onionLen C.int) { - ShutdownServer(C.GoStringN(onionPtr, onionLen)) +//export c_StopServer +func c_StopServer(onionPtr *C.char, onionLen C.int) { + StopServer(C.GoStringN(onionPtr, onionLen)) } -func ShutdownServer(onion string) { +func StopServer(onion string) { serversHandler, err := servers.ExperimentGate(utils.ReadGlobalSettings().Experiments) if err == nil { - serversHandler.ShutdownServer(onion) - application.GetPrimaryBus().Publish(event.NewEventList(servers.ServerIntentUpdate, servers.Intent, servers.IntentDisabled)) + serversHandler.StopServer(onion) + application.GetPrimaryBus().Publish(event.NewEventList(servers.ServerIntentUpdate, event.Identity, onion, servers.Intent, servers.IntentStopped)) } } -//export c_ShutdownServers -func c_ShutdownServers() { - ShutdownServers() +//export c_StopServers +func c_StopServers() { + StopServers() } -func ShutdownServers() { +func StopServers() { serversHandler, err := servers.ExperimentGate(utils.ReadGlobalSettings().Experiments) if err == nil { for _, onion := range serversHandler.ListServers() { - ShutdownServer(onion) + StopServer(onion) } } } +//export c_DestroyServers +func c_DestroyServers() { + DestroyServers() +} + +func DestroyServers() { + serversHandler, err := servers.ExperimentGate(utils.ReadGlobalSettings().Experiments) + if err == nil { + serversHandler.DestroyServers() + } +} + //export c_SetServerAttribute func c_SetServerAttribute(onionPtr *C.char, onionLen C.int, keyPtr *C.char, keyLen C.int, valPtr *C.char, valLen C.int) { SetServerAttribute(C.GoStringN(onionPtr, onionLen), C.GoStringN(keyPtr, keyLen), C.GoStringN(valPtr, valLen)) diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 34b2392..8657202 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -51,6 +51,7 @@ func (eh *EventHandler) HandleApp(application app.Application) { application.GetPrimaryBus().Subscribe(UpdateGlobalSettings, eh.appBusQueue) application.GetPrimaryBus().Subscribe(CwtchStarted, eh.appBusQueue) application.GetPrimaryBus().Subscribe(servers.NewServer, eh.appBusQueue) + application.GetPrimaryBus().Subscribe(servers.ServerIntentUpdate, eh.appBusQueue) } func (eh *EventHandler) GetNextEvent() string {