MCPcopy Index your code
hub / github.com/coder/coder / NewConn

Function NewConn

tailnet/conn.go:155–383  ·  view source on GitHub ↗

NewConn constructs a new Wireguard server that will accept connections from the addresses provided.

(options *Options)

Source from the content-addressed store, hash-verified

153
154// NewConn constructs a new Wireguard server that will accept connections from the addresses provided.
155func NewConn(options *Options) (conn *Conn, err error) {
156 if options == nil {
157 options = &Options{}
158 }
159 if len(options.Addresses) == 0 {
160 return nil, xerrors.New("At least one IP range must be provided")
161 }
162
163 useNetNS := options.TUNDev != nil
164 options.Logger.Debug(context.Background(), "network isolation configuration", slog.F("use_netns", useNetNS))
165 netns.SetEnabled(useNetNS)
166 // The Coder soft isolation mode is a workaround to allow Coder Connect to
167 // connect to Coder servers behind corporate VPNs, and relaxes some of the
168 // loop protections that come with Tailscale.
169 // See the comment above the netns function for more details.
170 netns.SetCoderSoftIsolation(useNetNS)
171
172 var telemetryStore *TelemetryStore
173 if options.TelemetrySink != nil {
174 var err error
175 telemetryStore, err = newTelemetryStore()
176 if err != nil {
177 return nil, xerrors.Errorf("create telemetry store: %w", err)
178 }
179 }
180
181 nodePrivateKey := key.NewNode()
182 var nodeID tailcfg.NodeID
183
184 // If we're provided with a UUID, use it to populate our node ID.
185 if options.ID != uuid.Nil {
186 nodeID = NodeID(options.ID)
187 } else {
188 uid, err := cryptorand.Int63()
189 if err != nil {
190 return nil, xerrors.Errorf("generate node id: %w", err)
191 }
192 nodeID = tailcfg.NodeID(uid)
193 }
194
195 if options.WireguardMonitor == nil {
196 options.WireguardMonitor, err = netmon.New(Logger(options.Logger.Named("net.wgmonitor")))
197 if err != nil {
198 return nil, xerrors.Errorf("create wireguard link monitor: %w", err)
199 }
200 }
201 defer func() {
202 if err != nil {
203 options.WireguardMonitor.Close()
204 }
205 }()
206
207 dialer := &tsdial.Dialer{
208 Logf: Logger(options.Logger.Named("net.tsdial")),
209 }
210 sys := new(tsd.System)
211 wireguardEngine, err := wgengine.NewUserspaceEngine(Logger(options.Logger.Named("net.wgengine")), wgengine.Config{
212 NetMon: options.WireguardMonitor,

Callers 14

TestDERPFunction · 0.92
NewServerTailnetFunction · 0.92
NewConnMethod · 0.92
newTailnetConnFunction · 0.92
DialAgentMethod · 0.92
TestTailnetFunction · 0.92
TestConn_PreferredDERPFunction · 0.92
TestConn_UpdateDERPFunction · 0.92
TestConn_BlockEndpointsFunction · 0.92
startClientOptionsFunction · 0.92
TestAgent_UpdatedDERPFunction · 0.92

Calls 15

CloseMethod · 0.95
watchConnChangeMethod · 0.95
Int63Function · 0.92
newTelemetryStoreFunction · 0.85
NodeIDFunction · 0.85
newConfigMapsFunction · 0.85
newNodeUpdaterFunction · 0.85
NamedMethod · 0.80
InstallCaptureHookMethod · 0.80
SetBlockEndpointsMethod · 0.80
DialContextTCPMethod · 0.80
setDERPMapMethod · 0.80

Tested by 9

TestDERPFunction · 0.74
newTailnetConnFunction · 0.74
TestTailnetFunction · 0.74
TestConn_PreferredDERPFunction · 0.74
TestConn_UpdateDERPFunction · 0.74
TestConn_BlockEndpointsFunction · 0.74
TestAgent_UpdatedDERPFunction · 0.74
setupAgentWithSecretsFunction · 0.74