Skip to content

Commit

Permalink
Merge pull request #915 from mrpalide/feature/public_autoconnect-api-…
Browse files Browse the repository at this point in the history
…endpoints

Public Autoconnect [API | Summary]
  • Loading branch information
jdknives authored Oct 12, 2021
2 parents 4b3aa10 + 43bbd5e commit bb755a4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -167,6 +168,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 @@ -219,6 +221,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 @@ -771,3 +774,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}/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())
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 @@ -667,7 +667,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 @@ -288,6 +288,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 @@ -884,6 +891,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

0 comments on commit bb755a4

Please sign in to comment.