forked from anywherelan/awl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.go
More file actions
192 lines (168 loc) · 5.45 KB
/
Copy pathsettings.go
File metadata and controls
192 lines (168 loc) · 5.45 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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()
// Snapshot the runtime-mutable scalar config fields under the read lock.
// P2pNode.Name is mutated by UpdateMySettings; VPNConfig fields may change
// once runtime reconfiguration lands — read them under the lock too.
h.conf.RLock()
p2pNode := h.conf.P2pNode
vpnConfig := h.conf.VPNConfig
h.conf.RUnlock()
peerInfo := entity.PeerInfo{
PeerID: p2pNode.PeerID,
Name: 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: vpnConfig.InterfaceName,
IPNet: vpnConfig.IPNet,
},
SOCKS5: func() entity.SOCKS5Info {
h.conf.RLock()
socks5 := h.conf.SOCKS5
h.conf.RUnlock()
info := entity.SOCKS5Info{
ListenAddress: socks5.ListenAddress,
ProxyingEnabled: socks5.ProxyingEnabled,
ListenerEnabled: socks5.ListenerEnabled,
UsingPeerID: socks5.UsingPeerID,
}
if socks5.UsingPeerID != "" {
if peer, ok := h.conf.GetPeer(socks5.UsingPeerID); ok {
peerID := peer.PeerId()
info.Connected = h.p2p.IsConnected(peerID)
info.UsingPeerName = peer.DisplayName()
info.UsingPeerPublicIP = h.p2p.PeerPublicIP(peerID)
if info.Connected {
info.UsingPeerPing = h.p2p.GetPeerLatency(peerID)
info.UsingPeerThroughRelay = !h.p2p.HasDirectConnection(peerID)
}
}
}
return info
}(),
VPNGateway: func() entity.VPNGatewayInfo {
h.conf.RLock()
gw := h.conf.VPNGateway
h.conf.RUnlock()
info := entity.VPNGatewayInfo{
ClientEnabled: gw.ClientEnabled,
GatewayPeerID: gw.GatewayPeerID,
ServerEnabled: gw.ServerEnabled,
}
if gw.GatewayPeerID != "" {
if peer, ok := h.conf.GetPeer(gw.GatewayPeerID); ok {
peerID := peer.PeerId()
info.GatewayPeerName = peer.DisplayName()
info.Connected = h.p2p.IsConnected(peerID)
info.GatewayPublicIP = h.p2p.PeerPublicIP(peerID)
if info.Connected {
info.GatewayPing = h.p2p.GetPeerLatency(peerID)
info.GatewayThroughRelay = !h.p2p.HasDirectConnection(peerID)
}
}
}
return info
}(),
}
if ipV6, _ := h.conf.VPNLocalIPMaskV6(); ipV6 != nil {
peerInfo.VPN.IPv6Addr = ipV6.String()
}
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)
}