@@ -49,8 +49,8 @@ type Options struct {
49
49
Logger slog.Logger
50
50
}
51
51
52
- // New constructs a new Wireguard server that will accept connections from the addresses provided.
53
- func New (options * Options ) (* Server , error ) {
52
+ // NewConn constructs a new Wireguard server that will accept connections from the addresses provided.
53
+ func NewConn (options * Options ) (* Conn , error ) {
54
54
if options == nil {
55
55
options = & Options {}
56
56
}
@@ -186,7 +186,7 @@ func New(options *Options) (*Server, error) {
186
186
logIPSet := netipx.IPSetBuilder {}
187
187
logIPs , _ := logIPSet .IPSet ()
188
188
wireguardEngine .SetFilter (filter .New (netMap .PacketFilter , localIPs , logIPs , nil , Logger (options .Logger .Named ("packet-filter" ))))
189
- server := & Server {
189
+ server := & Conn {
190
190
logger : options .Logger ,
191
191
magicConn : magicConn ,
192
192
dialer : dialer ,
@@ -218,8 +218,8 @@ func IP() netip.Addr {
218
218
return netip .AddrFrom16 (uid )
219
219
}
220
220
221
- // Server is an actively listening Wireguard connection.
222
- type Server struct {
221
+ // Conn is an actively listening Wireguard connection.
222
+ type Conn struct {
223
223
mutex sync.Mutex
224
224
logger slog.Logger
225
225
@@ -237,15 +237,15 @@ type Server struct {
237
237
// SetNodeCallback is triggered when a network change occurs and peer
238
238
// renegotiation may be required. Clients should constantly be emitting
239
239
// node changes.
240
- func (s * Server ) SetNodeCallback (callback func (node * Node )) {
241
- s .magicConn .SetNetInfoCallback (func (ni * tailcfg.NetInfo ) {
242
- s .logger .Info (context .Background (), "latency" , slog .F ("latency" , ni .DERPLatency ))
240
+ func (c * Conn ) SetNodeCallback (callback func (node * Node )) {
241
+ c .magicConn .SetNetInfoCallback (func (ni * tailcfg.NetInfo ) {
242
+ c .logger .Info (context .Background (), "latency" , slog .F ("latency" , ni .DERPLatency ))
243
243
callback (& Node {
244
- ID : s .netMap .SelfNode .ID ,
245
- Key : s .netMap .SelfNode .Key ,
246
- Addresses : s .netMap .SelfNode .Addresses ,
247
- AllowedIPs : s .netMap .SelfNode .AllowedIPs ,
248
- DiscoKey : s .magicConn .DiscoPublicKey (),
244
+ ID : c .netMap .SelfNode .ID ,
245
+ Key : c .netMap .SelfNode .Key ,
246
+ Addresses : c .netMap .SelfNode .Addresses ,
247
+ AllowedIPs : c .netMap .SelfNode .AllowedIPs ,
248
+ DiscoKey : c .magicConn .DiscoPublicKey (),
249
249
PreferredDERP : ni .PreferredDERP ,
250
250
DERPLatency : ni .DERPLatency ,
251
251
})
@@ -254,11 +254,11 @@ func (s *Server) SetNodeCallback(callback func(node *Node)) {
254
254
255
255
// UpdateNodes connects with a set of peers. This can be constantly updated,
256
256
// and peers will continually be reconnected as necessary.
257
- func (s * Server ) UpdateNodes (nodes []* Node ) error {
258
- s .mutex .Lock ()
259
- defer s .mutex .Unlock ()
257
+ func (c * Conn ) UpdateNodes (nodes []* Node ) error {
258
+ c .mutex .Lock ()
259
+ defer c .mutex .Unlock ()
260
260
peerMap := map [tailcfg.NodeID ]* tailcfg.Node {}
261
- for _ , peer := range s .netMap .Peers {
261
+ for _ , peer := range c .netMap .Peers {
262
262
peerMap [peer .ID ] = peer
263
263
}
264
264
for _ , node := range nodes {
@@ -272,41 +272,41 @@ func (s *Server) UpdateNodes(nodes []*Node) error {
272
272
Hostinfo : hostinfo .New ().View (),
273
273
}
274
274
}
275
- s .netMap .Peers = make ([]* tailcfg.Node , 0 , len (peerMap ))
275
+ c .netMap .Peers = make ([]* tailcfg.Node , 0 , len (peerMap ))
276
276
for _ , peer := range peerMap {
277
- s .netMap .Peers = append (s .netMap .Peers , peer )
277
+ c .netMap .Peers = append (c .netMap .Peers , peer )
278
278
}
279
- cfg , err := nmcfg .WGCfg (s .netMap , Logger (s .logger .Named ("wgconfig" )), netmap .AllowSingleHosts , "" )
279
+ cfg , err := nmcfg .WGCfg (c .netMap , Logger (c .logger .Named ("wgconfig" )), netmap .AllowSingleHosts , "" )
280
280
if err != nil {
281
281
return xerrors .Errorf ("update wireguard config: %w" , err )
282
282
}
283
- err = s .wireguardEngine .Reconfig (cfg , s .wireguardRouter , & dns.Config {}, & tailcfg.Debug {})
283
+ err = c .wireguardEngine .Reconfig (cfg , c .wireguardRouter , & dns.Config {}, & tailcfg.Debug {})
284
284
if err != nil {
285
285
return xerrors .Errorf ("reconfig: %w" , err )
286
286
}
287
- netMapCopy := * s .netMap
288
- s .wireguardEngine .SetNetworkMap (& netMapCopy )
287
+ netMapCopy := * c .netMap
288
+ c .wireguardEngine .SetNetworkMap (& netMapCopy )
289
289
return nil
290
290
}
291
291
292
292
// Ping sends a ping to the Wireguard engine.
293
- func (s * Server ) Ping (ip netip.Addr , pingType tailcfg.PingType , cb func (* ipnstate.PingResult )) {
294
- s .wireguardEngine .Ping (ip , pingType , cb )
293
+ func (c * Conn ) Ping (ip netip.Addr , pingType tailcfg.PingType , cb func (* ipnstate.PingResult )) {
294
+ c .wireguardEngine .Ping (ip , pingType , cb )
295
295
}
296
296
297
297
// Close shuts down the Wireguard connection.
298
- func (s * Server ) Close () error {
299
- s .mutex .Lock ()
300
- defer s .mutex .Unlock ()
301
- for _ , l := range s .listeners {
298
+ func (c * Conn ) Close () error {
299
+ c .mutex .Lock ()
300
+ defer c .mutex .Unlock ()
301
+ for _ , l := range c .listeners {
302
302
_ = l .Close ()
303
303
}
304
- _ = s .dialer .Close ()
305
- _ = s .magicConn .Close ()
306
- _ = s .netStack .Close ()
307
- _ = s .wireguardMonitor .Close ()
308
- _ = s .tunDevice .Close ()
309
- s .wireguardEngine .Close ()
304
+ _ = c .dialer .Close ()
305
+ _ = c .magicConn .Close ()
306
+ _ = c .netStack .Close ()
307
+ _ = c .wireguardMonitor .Close ()
308
+ _ = c .tunDevice .Close ()
309
+ c .wireguardEngine .Close ()
310
310
return nil
311
311
}
312
312
@@ -326,54 +326,54 @@ type Node struct {
326
326
327
327
// Listen announces only on the Tailscale network.
328
328
// It will start the server if it has not been started yet.
329
- func (s * Server ) Listen (network , addr string ) (net.Listener , error ) {
329
+ func (c * Conn ) Listen (network , addr string ) (net.Listener , error ) {
330
330
host , port , err := net .SplitHostPort (addr )
331
331
if err != nil {
332
332
return nil , xerrors .Errorf ("wgnet: %w" , err )
333
333
}
334
334
lk := listenKey {network , host , port }
335
335
ln := & listener {
336
- s : s ,
336
+ s : c ,
337
337
key : lk ,
338
338
addr : addr ,
339
339
340
340
conn : make (chan net.Conn ),
341
341
}
342
- s .mutex .Lock ()
343
- if s .listeners == nil {
344
- s .listeners = map [listenKey ]* listener {}
342
+ c .mutex .Lock ()
343
+ if c .listeners == nil {
344
+ c .listeners = map [listenKey ]* listener {}
345
345
}
346
- if _ , ok := s .listeners [lk ]; ok {
347
- s .mutex .Unlock ()
346
+ if _ , ok := c .listeners [lk ]; ok {
347
+ c .mutex .Unlock ()
348
348
return nil , xerrors .Errorf ("wgnet: listener already open for %s, %s" , network , addr )
349
349
}
350
- s .listeners [lk ] = ln
351
- s .mutex .Unlock ()
350
+ c .listeners [lk ] = ln
351
+ c .mutex .Unlock ()
352
352
return ln , nil
353
353
}
354
354
355
- func (s * Server ) DialContextTCP (ctx context.Context , ipp netip.AddrPort ) (* gonet.TCPConn , error ) {
356
- return s .netStack .DialContextTCP (ctx , ipp )
355
+ func (c * Conn ) DialContextTCP (ctx context.Context , ipp netip.AddrPort ) (* gonet.TCPConn , error ) {
356
+ return c .netStack .DialContextTCP (ctx , ipp )
357
357
}
358
358
359
- func (s * Server ) DialContextUDP (ctx context.Context , ipp netip.AddrPort ) (* gonet.UDPConn , error ) {
360
- return s .netStack .DialContextUDP (ctx , ipp )
359
+ func (c * Conn ) DialContextUDP (ctx context.Context , ipp netip.AddrPort ) (* gonet.UDPConn , error ) {
360
+ return c .netStack .DialContextUDP (ctx , ipp )
361
361
}
362
362
363
- func (s * Server ) forwardTCP (c net.Conn , port uint16 ) {
364
- s .mutex .Lock ()
365
- ln , ok := s .listeners [listenKey {"tcp" , "" , fmt .Sprint (port )}]
366
- s .mutex .Unlock ()
363
+ func (c * Conn ) forwardTCP (conn net.Conn , port uint16 ) {
364
+ c .mutex .Lock ()
365
+ ln , ok := c .listeners [listenKey {"tcp" , "" , fmt .Sprint (port )}]
366
+ c .mutex .Unlock ()
367
367
if ! ok {
368
- _ = c .Close ()
368
+ _ = conn .Close ()
369
369
return
370
370
}
371
371
t := time .NewTimer (time .Second )
372
372
defer t .Stop ()
373
373
select {
374
- case ln .conn <- c :
374
+ case ln .conn <- conn :
375
375
case <- t .C :
376
- _ = c .Close ()
376
+ _ = conn .Close ()
377
377
}
378
378
}
379
379
@@ -384,7 +384,7 @@ type listenKey struct {
384
384
}
385
385
386
386
type listener struct {
387
- s * Server
387
+ s * Conn
388
388
key listenKey
389
389
addr string
390
390
conn chan net.Conn
@@ -420,12 +420,3 @@ func Logger(logger slog.Logger) tslogger.Logf {
420
420
logger .Debug (context .Background (), fmt .Sprintf (format , args ... ))
421
421
})
422
422
}
423
-
424
- // The exchanger is entirely in-memory and works based on connected nodes.
425
- // It uses a PubSub system to dynamically add/remove nodes from the network
426
- // and build a netmap based on connection ID.
427
- //
428
- // Each node is allocated it's own internal connection ID.
429
- //
430
- // The connecting node *just* requires information about the other node.
431
- // The other node needs connection information of all the others.
0 commit comments