-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsettings.go
More file actions
140 lines (121 loc) · 3.96 KB
/
Copy pathsettings.go
File metadata and controls
140 lines (121 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package api
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/anywherelan/awl/config"
"github.com/anywherelan/awl/entity"
)
// @Tags Settings
// @Summary Get my peer info
// @Accept json
// @Produce json
// @Success 200 {object} entity.PeerInfo
// @Router /settings/peer_info [GET]
func (h *Handler) GetMyPeerInfo(c echo.Context) (err error) {
totalBootstraps, connectedBootstraps := h.p2p.BootstrapPeersStats()
netStats := h.p2p.NetworkStats()
peerInfo := entity.PeerInfo{
PeerID: h.conf.P2pNode.PeerID,
Name: h.conf.P2pNode.Name,
Uptime: h.p2p.Uptime(),
ServerVersion: config.Version,
NetworkStats: netStats,
NetworkStatsInIECUnits: getStatsInIECUnits(netStats),
TotalBootstrapPeers: totalBootstraps,
ConnectedBootstrapPeers: connectedBootstraps,
Reachability: h.p2p.Reachability().String(),
AwlDNSAddress: h.dns.AwlDNSAddress(),
IsAwlDNSSetAsSystem: h.dns.IsAwlDNSSetAsSystem(),
VPN: entity.VPNInfo{
VPNInterfaceEnabled: h.tunnel != nil,
InterfaceName: h.conf.VPNConfig.InterfaceName,
IPNet: h.conf.VPNConfig.IPNet,
},
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)
}
// @Tags Settings
// @Summary Update my peer info
// @Accept json
// @Produce json
// @Param body body entity.UpdateMySettingsRequest true "Params"
// @Success 200 "OK"
// @Router /settings/update [POST]
func (h *Handler) UpdateMySettings(c echo.Context) (err error) {
req := entity.UpdateMySettingsRequest{}
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()))
}
h.conf.Lock()
h.conf.P2pNode.Name = req.Name
h.conf.Unlock()
h.conf.Save()
go func() {
h.authStatus.ExchangeStatusInfoWithAllKnownPeers(h.ctx)
}()
return c.NoContent(http.StatusOK)
}
// @Tags Settings
// @Summary Export server configuration
// @Accept json
// @Produce json
// @Success 200 {object} config.Config
// @Router /settings/export_server_config [GET]
func (h *Handler) ExportServerConfiguration(c echo.Context) (err error) {
data := h.conf.Export()
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) {
proxies := h.socks5.ListAvailableProxies()
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.socks5.SetProxyPeerID(req.UsingPeerID)
return c.NoContent(http.StatusOK)
}