@@ -165,10 +165,24 @@ func (api *API) registerClient(req tunnelsdk.ClientRegisterRequest) (tunnelsdk.C
165
165
166
166
ip , urls := api .WireguardPublicKeyToIPAndURLs (req .PublicKey , req .Version )
167
167
168
+ api .pkeyCacheMu .Lock ()
169
+ api .pkeyCache [ip ] = cachedPeer {
170
+ key : req .PublicKey ,
171
+ lastHandshake : time .Now (),
172
+ }
173
+ api .pkeyCacheMu .Unlock ()
174
+
168
175
exists := true
169
176
if api .wgDevice .LookupPeer (req .PublicKey ) == nil {
170
177
exists = false
171
178
179
+ api .pkeyCacheMu .Lock ()
180
+ api .pkeyCache [ip ] = cachedPeer {
181
+ key : req .PublicKey ,
182
+ lastHandshake : time .Now (),
183
+ }
184
+ api .pkeyCacheMu .Unlock ()
185
+
172
186
err := api .wgDevice .IpcSet (fmt .Sprintf (`public_key=%x
173
187
allowed_ip=%s/128` ,
174
188
req .PublicKey ,
@@ -186,6 +200,7 @@ allowed_ip=%s/128`,
186
200
187
201
return tunnelsdk.ClientRegisterResponse {
188
202
Version : req .Version ,
203
+ ReregisterWait : api .PeerRegisterInterval ,
189
204
TunnelURLs : urlsStr ,
190
205
ClientIP : ip ,
191
206
ServerEndpoint : api .WireguardEndpoint ,
@@ -205,6 +220,12 @@ func (api *API) handleTunnel(rw http.ResponseWriter, r *http.Request) {
205
220
subdomainParts := strings .Split (subdomain , "-" )
206
221
user := subdomainParts [len (subdomainParts )- 1 ]
207
222
223
+ span := trace .SpanFromContext (ctx )
224
+ span .SetAttributes (
225
+ attribute .Bool ("proxy_request" , true ),
226
+ attribute .String ("user" , user ),
227
+ )
228
+
208
229
ip , err := api .HostnameToWireguardIP (user )
209
230
if err != nil {
210
231
httpapi .Write (ctx , rw , http .StatusBadRequest , tunnelsdk.Response {
@@ -214,11 +235,17 @@ func (api *API) handleTunnel(rw http.ResponseWriter, r *http.Request) {
214
235
return
215
236
}
216
237
217
- span := trace .SpanFromContext (ctx )
218
- span .SetAttributes (
219
- attribute .Bool ("proxy_request" , true ),
220
- attribute .String ("user" , user ),
221
- )
238
+ api .pkeyCacheMu .RLock ()
239
+ pkey , ok := api .pkeyCache [ip ]
240
+ api .pkeyCacheMu .RUnlock ()
241
+
242
+ if ! ok || time .Since (pkey .lastHandshake ) > api .PeerTimeout {
243
+ httpapi .Write (ctx , rw , http .StatusBadGateway , tunnelsdk.Response {
244
+ Message : "Peer is not connected." ,
245
+ Detail : "" ,
246
+ })
247
+ return
248
+ }
222
249
223
250
// The transport on the reverse proxy uses this ctx value to know which
224
251
// IP to dial. See tunneld.go.
0 commit comments