Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public Autoconnect [API | Summary] #915

Merged
merged 6 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,9 +17,6 @@ import (
"syscall"
"time"

"embed"
"io/fs"

"github.com/pkg/profile"
"github.com/skycoin/dmsg/buildinfo"
"github.com/skycoin/dmsg/cmdutil"
Expand Down
8 changes: 8 additions & 0 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -168,6 +169,7 @@ type Summary struct {
PersistentTransports []transport.PersistentTransports `json:"persistent_transports"`
SkybianBuildVersion string `json:"skybian_build_version"`
BuildTag string `json:"build_tag"`
PublicAutoconnect bool `json:"public_autoconnect"`
}

// BuildTag variable that will set when building binary
Expand Down Expand Up @@ -220,6 +222,7 @@ func (v *Visor) Summary() (*Summary, error) {
PersistentTransports: pts,
SkybianBuildVersion: skybianBuildVersion,
BuildTag: BuildTag,
PublicAutoconnect: v.conf.Transport.PublicAutoconnect,
}

return summary, nil
Expand Down Expand Up @@ -817,3 +820,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)
}
25 changes: 25 additions & 0 deletions pkg/visor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
mrpalide marked this conversation as resolved.
Show resolved Hide resolved
r.Get("/visors/{pk}/routes", hv.getRoutes())
r.Post("/visors/{pk}/routes", hv.postRoute())
r.Get("/visors/{pk}/routes/{rid}", hv.getRoute())
Expand Down Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/visor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion pkg/visor/visorconfig/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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"`
}

Expand Down Expand Up @@ -205,6 +205,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.
Expand Down