@@ -165,10 +165,24 @@ func (api *API) registerClient(req tunnelsdk.ClientRegisterRequest) (tunnelsdk.C
165165
166166 ip , urls := api .WireguardPublicKeyToIPAndURLs (req .PublicKey , req .Version )
167167
168+ api .pkeyCacheMu .Lock ()
169+ api .pkeyCache [ip ] = cachedPeer {
170+ key : req .PublicKey ,
171+ lastHandshake : time .Now (),
172+ }
173+ api .pkeyCacheMu .Unlock ()
174+
168175 exists := true
169176 if api .wgDevice .LookupPeer (req .PublicKey ) == nil {
170177 exists = false
171178
179+ api .pkeyCacheMu .Lock ()
180+ api .pkeyCache [ip ] = cachedPeer {
181+ key : req .PublicKey ,
182+ lastHandshake : time .Now (),
183+ }
184+ api .pkeyCacheMu .Unlock ()
185+
172186 err := api .wgDevice .IpcSet (fmt .Sprintf (`public_key=%x
173187allowed_ip=%s/128` ,
174188 req .PublicKey ,
@@ -186,6 +200,7 @@ allowed_ip=%s/128`,
186200
187201 return tunnelsdk.ClientRegisterResponse {
188202 Version : req .Version ,
203+ ReregisterWait : api .PeerRegisterInterval ,
189204 TunnelURLs : urlsStr ,
190205 ClientIP : ip ,
191206 ServerEndpoint : api .WireguardEndpoint ,
@@ -205,6 +220,12 @@ func (api *API) handleTunnel(rw http.ResponseWriter, r *http.Request) {
205220 subdomainParts := strings .Split (subdomain , "-" )
206221 user := subdomainParts [len (subdomainParts )- 1 ]
207222
223+ span := trace .SpanFromContext (ctx )
224+ span .SetAttributes (
225+ attribute .Bool ("proxy_request" , true ),
226+ attribute .String ("user" , user ),
227+ )
228+
208229 ip , err := api .HostnameToWireguardIP (user )
209230 if err != nil {
210231 httpapi .Write (ctx , rw , http .StatusBadRequest , tunnelsdk.Response {
@@ -214,11 +235,17 @@ func (api *API) handleTunnel(rw http.ResponseWriter, r *http.Request) {
214235 return
215236 }
216237
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+ }
222249
223250 // The transport on the reverse proxy uses this ctx value to know which
224251 // IP to dial. See tunneld.go.
0 commit comments