From c50d34d3723f9280ff4ba267b3668a95c752d124 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Thu, 7 Oct 2021 17:19:48 +0330 Subject: [PATCH 1/5] add public_autoconnect field to summary response --- pkg/visor/api.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/visor/api.go b/pkg/visor/api.go index 3099f1faca..ecae589996 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -167,6 +167,7 @@ type Summary struct { MinHops uint16 `json:"min_hops"` PersistentTransports []transport.PersistentTransports `json:"persistent_transports"` SkybianBuildVersion string `json:"skybian_build_version"` + AutoconnectPublic bool `json:"public_autoconnect"` } // Summary implements API. @@ -215,6 +216,7 @@ func (v *Visor) Summary() (*Summary, error) { MinHops: v.conf.Routing.MinHops, PersistentTransports: pts, SkybianBuildVersion: skybianBuildVersion, + AutoconnectPublic: v.conf.Transport.AutoconnectPublic, } return summary, nil From d3fe0b44894b6d185e95a6e8b102e881505ca337 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Thu, 7 Oct 2021 19:36:56 +0330 Subject: [PATCH 2/5] add public_autoconnect endpoint for update its value --- pkg/visor/api.go | 10 ++++++++-- pkg/visor/hypervisor.go | 25 +++++++++++++++++++++++++ pkg/visor/init.go | 2 +- pkg/visor/rpc_client.go | 12 ++++++++++++ pkg/visor/visorconfig/v1.go | 11 ++++++++++- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/pkg/visor/api.go b/pkg/visor/api.go index ecae589996..f6fa1ba793 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -56,6 +56,7 @@ type API interface { Transport(tid uuid.UUID) (*TransportSummary, error) AddTransport(remote cipher.PubKey, tpType string, timeout time.Duration) (*TransportSummary, error) RemoveTransport(tid uuid.UUID) error + SetPublicAutoconnect(pAc bool) error DiscoverTransportsByPK(pk cipher.PubKey) ([]*transport.Entry, error) DiscoverTransportByID(id uuid.UUID) (*transport.Entry, error) @@ -167,7 +168,7 @@ type Summary struct { MinHops uint16 `json:"min_hops"` PersistentTransports []transport.PersistentTransports `json:"persistent_transports"` SkybianBuildVersion string `json:"skybian_build_version"` - AutoconnectPublic bool `json:"public_autoconnect"` + PublicAutoconnect bool `json:"public_autoconnect"` } // Summary implements API. @@ -216,7 +217,7 @@ func (v *Visor) Summary() (*Summary, error) { MinHops: v.conf.Routing.MinHops, PersistentTransports: pts, SkybianBuildVersion: skybianBuildVersion, - AutoconnectPublic: v.conf.Transport.AutoconnectPublic, + PublicAutoconnect: v.conf.Transport.PublicAutoconnect, } return summary, nil @@ -814,3 +815,8 @@ func (v *Visor) SetPersistentTransports(pTps []transport.PersistentTransports) e func (v *Visor) GetPersistentTransports() ([]transport.PersistentTransports, error) { return v.conf.GetPersistentTransports() } + +// SetPublicAutoconnect sets public_autoconnect config of visor +func (v *Visor) SetPublicAutoconnect(pAc bool) error { + return v.conf.UpdatePublicAutoconnect(pAc) +} diff --git a/pkg/visor/hypervisor.go b/pkg/visor/hypervisor.go index f3d9bbd734..bccc986870 100644 --- a/pkg/visor/hypervisor.go +++ b/pkg/visor/hypervisor.go @@ -246,6 +246,7 @@ func (hv *Hypervisor) makeMux() chi.Router { r.Get("/visors/{pk}/transports/{tid}", hv.getTransport()) r.Delete("/visors/{pk}/transports/{tid}", hv.deleteTransport()) r.Delete("/visors/{pk}/transports/", hv.deleteTransports()) + r.Put("/visors/{pk}/transport-public-autoconnect", hv.putPublicAutoconnect()) r.Get("/visors/{pk}/routes", hv.getRoutes()) r.Post("/visors/{pk}/routes", hv.postRoute()) r.Get("/visors/{pk}/routes/{rid}", hv.getRoute()) @@ -864,6 +865,30 @@ func (hv *Hypervisor) deleteTransports() http.HandlerFunc { }) } +func (hv *Hypervisor) putPublicAutoconnect() http.HandlerFunc { + return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) { + var reqBody publicAutoconnectReq + + if err := httputil.ReadJSON(r, &reqBody); err != nil { + if err != io.EOF { + hv.log(r).Warnf("putPublicAutoconnect request: %v", err) + } + httputil.WriteJSON(w, r, http.StatusBadRequest, usermanager.ErrMalformedRequest) + return + } + + if err := ctx.API.SetPublicAutoconnect(reqBody.PublicAutoconnect); err != nil { + httputil.WriteJSON(w, r, http.StatusInternalServerError, err) + return + } + httputil.WriteJSON(w, r, http.StatusOK, struct{}{}) + }) +} + +type publicAutoconnectReq struct { + PublicAutoconnect bool `json:"public_autoconnect"` +} + type routingRuleResp struct { Key routing.RouteID `json:"key"` Rule string `json:"rule"` diff --git a/pkg/visor/init.go b/pkg/visor/init.go index bf59dbbb8f..07d8888eca 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -665,7 +665,7 @@ func initPublicVisor(_ context.Context, v *Visor, log *logging.Logger) error { } func initPublicVisors(ctx context.Context, v *Visor, log *logging.Logger) error { - if !v.conf.Transport.AutoconnectPublic { + if !v.conf.Transport.PublicAutoconnect { return nil } serviceDisc := v.conf.Launcher.ServiceDisc diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 51cc9fce12..01775fa734 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -289,6 +289,13 @@ func (rc *rpcClient) DiscoverTransportByID(id uuid.UUID) (*transport.Entry, erro return &entry, err } +// SetPublicAutoconnect implements API. +func (rc *rpcClient) SetPublicAutoconnect(pAc bool) error { + return rc.Call("SetPublicAutoconnect", &publicAutoconnectReq{ + PublicAutoconnect: pAc, + }, &struct{}{}) +} + // RoutingRules calls RoutingRules. func (rc *rpcClient) RoutingRules() ([]routing.Rule, error) { entries := make([]routing.Rule, 0) @@ -889,6 +896,11 @@ func (mc *mockRPCClient) DiscoverTransportByID(uuid.UUID) (*transport.Entry, err return nil, ErrNotImplemented } +// SetPublicAutoconnect implements API. +func (mc *mockRPCClient) SetPublicAutoconnect(_ bool) error { + return nil +} + // RoutingRules implements API. func (mc *mockRPCClient) RoutingRules() ([]routing.Rule, error) { return mc.rt.AllRules(), nil diff --git a/pkg/visor/visorconfig/v1.go b/pkg/visor/visorconfig/v1.go index c18f373a4d..97cea665fa 100644 --- a/pkg/visor/visorconfig/v1.go +++ b/pkg/visor/visorconfig/v1.go @@ -79,7 +79,7 @@ type V1Dmsgpty struct { type V1Transport struct { Discovery string `json:"discovery"` AddressResolver string `json:"address_resolver"` - AutoconnectPublic bool `json:"public_autoconnect"` + PublicAutoconnect bool `json:"public_autoconnect"` TransportSetup []cipher.PubKey `json:"transport_setup_nodes"` } @@ -204,6 +204,15 @@ func (v1 *V1) GetPersistentTransports() ([]transport.PersistentTransports, error return v1.PersistentTransports, nil } +// UpdatePublicAutoconnect updates public_autoconnect in config +func (v1 *V1) UpdatePublicAutoconnect(pAc bool) error { + v1.mu.Lock() + v1.Transport.PublicAutoconnect = pAc + v1.mu.Unlock() + + return v1.flush(v1) +} + // updateStringArg updates the cli non-boolean flag of the specified app config and also within the launcher. // It removes argName from app args if value is an empty string. // The updated config gets flushed to file if there are any changes. From 8990ae7bed10d4760628b1ccf7dbfc22b34aed83 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Thu, 7 Oct 2021 19:39:38 +0330 Subject: [PATCH 3/5] make check && make format --- cmd/apps/skychat/chat.go | 4 ++-- cmd/skywire-visor/commands/root.go | 4 ++-- internal/gui/gui.go | 2 +- pkg/visor/hypervisorconfig/config.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index 1ff04fb7ef..e1aba8fc25 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -4,19 +4,19 @@ skychat app for skywire visor package main import ( - "embed" "encoding/json" "flag" "fmt" - "io/fs" "net" "net/http" "os" "sync" "time" + "embed" "github.com/skycoin/dmsg/buildinfo" "github.com/skycoin/dmsg/cipher" + "io/fs" "github.com/skycoin/skywire/internal/netutil" "github.com/skycoin/skywire/pkg/app" diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index 36d937d687..5f378e3260 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -2,10 +2,8 @@ package commands import ( "context" - "embed" "fmt" "io" - "io/fs" "io/ioutil" "net/http" _ "net/http/pprof" // nolint:gosec // https://golang.org/doc/diagnostics.html#profiling @@ -17,12 +15,14 @@ import ( "syscall" "time" + "embed" "github.com/pkg/profile" "github.com/skycoin/dmsg/buildinfo" "github.com/skycoin/dmsg/cmdutil" "github.com/skycoin/skycoin/src/util/logging" "github.com/spf13/cobra" "github.com/toqueteos/webbrowser" + "io/fs" "github.com/skycoin/skywire/pkg/restart" "github.com/skycoin/skywire/pkg/syslog" diff --git a/internal/gui/gui.go b/internal/gui/gui.go index 575934132d..e6b259c9c7 100644 --- a/internal/gui/gui.go +++ b/internal/gui/gui.go @@ -5,7 +5,6 @@ package gui import ( "context" - "embed" "fmt" "io" "io/ioutil" @@ -15,6 +14,7 @@ import ( "sync/atomic" "time" + "embed" "github.com/gen2brain/dlgs" "github.com/getlantern/systray" "github.com/skycoin/skycoin/src/util/logging" diff --git a/pkg/visor/hypervisorconfig/config.go b/pkg/visor/hypervisorconfig/config.go index 637e815a89..706f2a162d 100644 --- a/pkg/visor/hypervisorconfig/config.go +++ b/pkg/visor/hypervisorconfig/config.go @@ -3,7 +3,6 @@ package hypervisorconfig import ( "encoding/hex" "encoding/json" - "io/fs" "log" "net/http" "os" @@ -11,6 +10,7 @@ import ( "time" "github.com/skycoin/dmsg/cipher" + "io/fs" "github.com/skycoin/skywire/pkg/skyenv" "github.com/skycoin/skywire/pkg/util/pathutil" From ad00cdd389873411677ea1ec42bfa790427a5b99 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Mon, 11 Oct 2021 09:17:51 +0330 Subject: [PATCH 4/5] make format --- cmd/apps/skychat/chat.go | 4 ++-- cmd/skywire-visor/commands/root.go | 5 ++--- internal/gui/gui.go | 2 +- pkg/visor/hypervisorconfig/config.go | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index e1aba8fc25..1ff04fb7ef 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -4,19 +4,19 @@ skychat app for skywire visor package main import ( + "embed" "encoding/json" "flag" "fmt" + "io/fs" "net" "net/http" "os" "sync" "time" - "embed" "github.com/skycoin/dmsg/buildinfo" "github.com/skycoin/dmsg/cipher" - "io/fs" "github.com/skycoin/skywire/internal/netutil" "github.com/skycoin/skywire/pkg/app" diff --git a/cmd/skywire-visor/commands/root.go b/cmd/skywire-visor/commands/root.go index d6783b978d..be4062370a 100644 --- a/cmd/skywire-visor/commands/root.go +++ b/cmd/skywire-visor/commands/root.go @@ -2,8 +2,10 @@ package commands import ( "context" + "embed" "fmt" "io" + "io/fs" "io/ioutil" "net/http" _ "net/http/pprof" // nolint:gosec // https://golang.org/doc/diagnostics.html#profiling @@ -15,9 +17,6 @@ import ( "syscall" "time" - "embed" - "io/fs" - "github.com/pkg/profile" "github.com/skycoin/dmsg/buildinfo" "github.com/skycoin/dmsg/cmdutil" diff --git a/internal/gui/gui.go b/internal/gui/gui.go index e6b259c9c7..575934132d 100644 --- a/internal/gui/gui.go +++ b/internal/gui/gui.go @@ -5,6 +5,7 @@ package gui import ( "context" + "embed" "fmt" "io" "io/ioutil" @@ -14,7 +15,6 @@ import ( "sync/atomic" "time" - "embed" "github.com/gen2brain/dlgs" "github.com/getlantern/systray" "github.com/skycoin/skycoin/src/util/logging" diff --git a/pkg/visor/hypervisorconfig/config.go b/pkg/visor/hypervisorconfig/config.go index 955011e997..801304b160 100644 --- a/pkg/visor/hypervisorconfig/config.go +++ b/pkg/visor/hypervisorconfig/config.go @@ -3,6 +3,7 @@ package hypervisorconfig import ( "encoding/hex" "encoding/json" + "io/fs" "log" "net/http" "os" @@ -10,7 +11,6 @@ import ( "time" "github.com/skycoin/dmsg/cipher" - "io/fs" "github.com/skycoin/skywire/pkg/skyenv" "github.com/skycoin/skywire/pkg/util/pathutil" From 43bbd5eae618f54512fc8490e74c1cb03c538325 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Tue, 12 Oct 2021 12:18:07 +0330 Subject: [PATCH 5/5] fix review suggestions --- pkg/visor/hypervisor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/visor/hypervisor.go b/pkg/visor/hypervisor.go index bccc986870..956d208e8e 100644 --- a/pkg/visor/hypervisor.go +++ b/pkg/visor/hypervisor.go @@ -246,7 +246,7 @@ func (hv *Hypervisor) makeMux() chi.Router { r.Get("/visors/{pk}/transports/{tid}", hv.getTransport()) r.Delete("/visors/{pk}/transports/{tid}", hv.deleteTransport()) r.Delete("/visors/{pk}/transports/", hv.deleteTransports()) - r.Put("/visors/{pk}/transport-public-autoconnect", hv.putPublicAutoconnect()) + r.Put("/visors/{pk}/public-autoconnect", hv.putPublicAutoconnect()) r.Get("/visors/{pk}/routes", hv.getRoutes()) r.Post("/visors/{pk}/routes", hv.postRoute()) r.Get("/visors/{pk}/routes/{rid}", hv.getRoute())