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

api: add methods to list and update socks5 proxy #141

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 7 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions api/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
73 changes: 73 additions & 0 deletions api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
28 changes: 25 additions & 3 deletions entity/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -84,6 +98,14 @@ type (
PeerID string
protocol.AuthPeer
}

ListAvailableProxiesResponse struct {
Proxies []AvailableProxy
}
AvailableProxy struct {
PeerID string
PeerName string
}
)

type (
Expand Down
Loading