From 5639e36bb9537f4a83c559cc87e979e855f828c1 Mon Sep 17 00:00:00 2001 From: Maxim Merzhanov Date: Sat, 12 Oct 2024 20:06:00 +0300 Subject: [PATCH] api: add methods to list and update socks5 proxy --- api/api.go | 11 +++++--- api/const.go | 8 ++++-- api/settings.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ entity/api.go | 28 +++++++++++++++++-- 4 files changed, 110 insertions(+), 10 deletions(-) diff --git a/api/api.go b/api/api.go index f06d224..f69ae3f 100644 --- a/api/api.go +++ b/api/api.go @@ -10,14 +10,15 @@ import ( "runtime/pprof" "strings" - "github.com/anywherelan/awl/config" - "github.com/anywherelan/awl/p2p" - "github.com/anywherelan/awl/ringbuffer" - "github.com/anywherelan/awl/service" "github.com/go-playground/validator/v10" "github.com/ipfs/go-log/v2" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + + "github.com/anywherelan/awl/config" + "github.com/anywherelan/awl/p2p" + "github.com/anywherelan/awl/ringbuffer" + "github.com/anywherelan/awl/service" ) type DNSService interface { @@ -108,6 +109,8 @@ func (h *Handler) setupRouter(address string) (*echo.Echo, error) { // Settings e.GET(GetMyPeerInfoPath, h.GetMyPeerInfo) e.POST(UpdateMyInfoPath, h.UpdateMySettings) + e.GET(ListAvailableProxiesPath, h.ListAvailableProxies) + e.POST(UpdateProxySettingsPath, h.UpdateProxySettings) e.GET(ExportServerConfigPath, h.ExportServerConfiguration) // Debug diff --git a/api/const.go b/api/const.go index 88fc569..96180ab 100644 --- a/api/const.go +++ b/api/const.go @@ -16,9 +16,11 @@ const ( GetAuthRequestsPath = V0Prefix + "peers/auth_requests" // Settings - GetMyPeerInfoPath = V0Prefix + "settings/peer_info" - UpdateMyInfoPath = V0Prefix + "settings/update" - ExportServerConfigPath = V0Prefix + "settings/export_server_config" + GetMyPeerInfoPath = V0Prefix + "settings/peer_info" + UpdateMyInfoPath = V0Prefix + "settings/update" + ListAvailableProxiesPath = V0Prefix + "settings/list_proxies" + UpdateProxySettingsPath = V0Prefix + "settings/set_proxy" + ExportServerConfigPath = V0Prefix + "settings/export_server_config" // Debug GetP2pDebugInfoPath = V0Prefix + "debug/p2p_info" diff --git a/api/settings.go b/api/settings.go index 461db09..3c581bb 100644 --- a/api/settings.go +++ b/api/settings.go @@ -31,6 +31,16 @@ func (h *Handler) GetMyPeerInfo(c echo.Context) (err error) { Reachability: h.p2p.Reachability().String(), AwlDNSAddress: h.dns.AwlDNSAddress(), IsAwlDNSSetAsSystem: h.dns.IsAwlDNSSetAsSystem(), + SOCKS5: entity.SOCKS5Info{ + ListenAddress: h.conf.SOCKS5.ListenAddress, + ProxyingEnabled: h.conf.SOCKS5.ProxyingEnabled, + ListenerEnabled: h.conf.SOCKS5.ListenerEnabled, + UsingPeerID: h.conf.SOCKS5.UsingPeerID, + UsingPeerName: func() string { + peer, _ := h.conf.GetPeer(h.conf.SOCKS5.UsingPeerID) + return peer.DisplayName() + }(), + }, } return c.JSON(http.StatusOK, peerInfo) @@ -76,3 +86,66 @@ func (h *Handler) ExportServerConfiguration(c echo.Context) (err error) { return c.Blob(http.StatusOK, echo.MIMEApplicationJSON, data) } + +// @Tags Settings +// @Summary List available socks5 proxies +// @Accept json +// @Produce json +// @Success 200 {object} entity.ListAvailableProxiesResponse +// @Router /settings/list_proxies [GET] +func (h *Handler) ListAvailableProxies(c echo.Context) (err error) { + h.conf.RLock() + proxies := []entity.AvailableProxy{} + for _, peer := range h.conf.KnownPeers { + if !peer.AllowedUsingAsExitNode { + continue + } + + proxy := entity.AvailableProxy{ + PeerID: peer.PeerID, + PeerName: peer.DisplayName(), + } + proxies = append(proxies, proxy) + } + h.conf.RUnlock() + + response := entity.ListAvailableProxiesResponse{ + Proxies: proxies, + } + + return c.JSON(http.StatusOK, response) +} + +// @Tags Settings +// @Summary Update current proxy settings +// @Accept json +// @Produce json +// @Param body body entity.UpdateProxySettingsRequest true "Params" +// @Success 200 "OK" +// @Router /settings/set_proxy [POST] +func (h *Handler) UpdateProxySettings(c echo.Context) (err error) { + req := entity.UpdateProxySettingsRequest{} + err = c.Bind(&req) + if err != nil { + return c.JSON(http.StatusBadRequest, ErrorMessage(err.Error())) + } + if err = c.Validate(req); err != nil { + return c.JSON(http.StatusBadRequest, ErrorMessage(err.Error())) + } + + peer, ok := h.conf.GetPeer(req.UsingPeerID) + if req.UsingPeerID == "" { + // ok + } else if !ok { + return c.JSON(http.StatusNotFound, ErrorMessage("peer not found")) + } else if !peer.AllowedUsingAsExitNode { + return c.JSON(http.StatusBadRequest, ErrorMessage("peer doesn't allow using as exit node")) + } + + h.conf.Lock() + h.conf.SOCKS5.UsingPeerID = req.UsingPeerID + h.conf.Unlock() + h.conf.Save() + + return c.NoContent(http.StatusOK) +} diff --git a/entity/api.go b/entity/api.go index 14ff3b5..29c9fdd 100644 --- a/entity/api.go +++ b/entity/api.go @@ -3,10 +3,11 @@ package entity import ( "time" - "github.com/anywherelan/awl/p2p" - "github.com/anywherelan/awl/protocol" kbucket "github.com/libp2p/go-libp2p-kbucket" "github.com/libp2p/go-libp2p/core/metrics" + + "github.com/anywherelan/awl/p2p" + "github.com/anywherelan/awl/protocol" ) // Requests @@ -30,12 +31,16 @@ type ( UpdatePeerSettingsRequest struct { PeerID string `validate:"required"` Alias string `validate:"required,trimmed_str_not_empty"` - DomainName string + DomainName string `validate:"required,trimmed_str_not_empty"` AllowUsingAsExitNode bool } UpdateMySettingsRequest struct { Name string } + + UpdateProxySettingsRequest struct { + UsingPeerID string + } ) // Responses @@ -71,6 +76,15 @@ type ( Reachability string `enums:"Unknown,Public,Private"` AwlDNSAddress string IsAwlDNSSetAsSystem bool + SOCKS5 SOCKS5Info + } + + SOCKS5Info struct { + ListenAddress string + ProxyingEnabled bool + ListenerEnabled bool + UsingPeerID string + UsingPeerName string } StatsInUnits struct { @@ -84,6 +98,14 @@ type ( PeerID string protocol.AuthPeer } + + ListAvailableProxiesResponse struct { + Proxies []AvailableProxy + } + AvailableProxy struct { + PeerID string + PeerName string + } ) type (