addAddress begins meshing with a new address. It returns false if the address is already being meshed with. It's expected that this is a full HTTP address with a path. e.g. http://127.0.0.1:8080/derp nolint:revive
(address string, connect bool)
| 90 | // e.g. http://127.0.0.1:8080/derp |
| 91 | // nolint:revive |
| 92 | func (m *Mesh) addAddress(address string, connect bool) (bool, error) { |
| 93 | m.mutex.Lock() |
| 94 | defer m.mutex.Unlock() |
| 95 | if m.isClosed() { |
| 96 | return false, nil |
| 97 | } |
| 98 | _, isActive := m.active[address] |
| 99 | if isActive { |
| 100 | return false, nil |
| 101 | } |
| 102 | client, err := derphttp.NewClient(m.server.PrivateKey(), address, tailnet.Logger(m.logger.Named("client"))) |
| 103 | if err != nil { |
| 104 | return false, xerrors.Errorf("create derp client: %w", err) |
| 105 | } |
| 106 | client.TLSConfig = m.tlsConfig |
| 107 | client.MeshKey = m.server.MeshKey() |
| 108 | client.SetURLDialer(func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 109 | var dialer net.Dialer |
| 110 | return dialer.DialContext(ctx, network, addr) |
| 111 | }) |
| 112 | if connect { |
| 113 | _ = client.Connect(m.ctx) |
| 114 | } |
| 115 | ctx, cancelFunc := context.WithCancel(m.ctx) |
| 116 | closed := make(chan struct{}) |
| 117 | closeFunc := func() { |
| 118 | cancelFunc() |
| 119 | _ = client.Close() |
| 120 | <-closed |
| 121 | } |
| 122 | m.active[address] = closeFunc |
| 123 | go func() { |
| 124 | defer close(closed) |
| 125 | client.RunWatchConnectionLoop(ctx, m.server.PublicKey(), tailnet.Logger(m.logger.Named("loop")), func(np key.NodePublic) { |
| 126 | m.server.AddPacketForwarder(np, client) |
| 127 | }, func(np key.NodePublic) { |
| 128 | m.server.RemovePacketForwarder(np, client) |
| 129 | }) |
| 130 | }() |
| 131 | return true, nil |
| 132 | } |
| 133 | |
| 134 | // removeAddress stops meshing with a given address. |
| 135 | func (m *Mesh) removeAddress(address string) bool { |
no test coverage detected