| 33 | ) |
| 34 | |
| 35 | func GetNetworkingStack(t *Tunnel, _ *StartRequest, logger slog.Logger) (NetworkStack, error) { |
| 36 | // Initialize COM process-wide so Tailscale can make calls to the windows |
| 37 | // network APIs to read/write adapter state. |
| 38 | comProcessType := com.ConsoleApp |
| 39 | isSvc, err := svc.IsWindowsService() |
| 40 | if err != nil { |
| 41 | return NetworkStack{}, xerrors.Errorf("svc.IsWindowsService failed: %w", err) |
| 42 | } |
| 43 | if isSvc { |
| 44 | comProcessType = com.Service |
| 45 | } |
| 46 | if err := com.StartRuntime(comProcessType); err != nil { |
| 47 | return NetworkStack{}, xerrors.Errorf("could not initialize COM: com.StartRuntime(%d): %w", comProcessType, err) |
| 48 | } |
| 49 | |
| 50 | // Set the name and GUID for the TUN interface. |
| 51 | tun.WintunTunnelType = tunName |
| 52 | guid, err := windows.GUIDFromString(tunGUID) |
| 53 | if err != nil { |
| 54 | return NetworkStack{}, xerrors.Errorf("could not parse GUID %q: %w", tunGUID, err) |
| 55 | } |
| 56 | tun.WintunStaticRequestedGUID = &guid |
| 57 | |
| 58 | // Ensure wintun.dll is available, and fail early if it's not to avoid |
| 59 | // hanging for 5 minutes in tstunNewWithWindowsRetries. |
| 60 | // |
| 61 | // First, we call wintun.Version() to make the wintun package attempt to |
| 62 | // load wintun.dll. This allows the wintun package to set the logging |
| 63 | // callback in the DLL before we load it ourselves. |
| 64 | _ = wintun.Version() |
| 65 | |
| 66 | // Then, we try to load wintun.dll ourselves so we get a better error |
| 67 | // message if there was a problem. This call matches the wintun package, so |
| 68 | // we're loading it in the same way. |
| 69 | // |
| 70 | // Note: this leaks the handle to wintun.dll, but since it's already loaded |
| 71 | // it wouldn't be freed anyways. |
| 72 | const ( |
| 73 | LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200 |
| 74 | LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800 |
| 75 | ) |
| 76 | _, err = windows.LoadLibraryEx(wintunDLL, 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR|LOAD_LIBRARY_SEARCH_SYSTEM32) |
| 77 | if err != nil { |
| 78 | return NetworkStack{}, xerrors.Errorf("could not load %q, it should be in the same directory as the executable (in Coder Desktop, this should have been installed automatically): %w", wintunDLL, err) |
| 79 | } |
| 80 | |
| 81 | tunDev, tunName, err := tstunNewWithWindowsRetries(tailnet.Logger(logger.Named("net.tun.device")), tunName) |
| 82 | if err != nil { |
| 83 | return NetworkStack{}, xerrors.Errorf("create tun device: %w", err) |
| 84 | } |
| 85 | logger.Info(context.Background(), "tun created", slog.F("name", tunName)) |
| 86 | |
| 87 | wireguardMonitor, err := netmon.New(tailnet.Logger(logger.Named("net.wgmonitor"))) |
| 88 | |
| 89 | coderRouter, err := router.New(tailnet.Logger(logger.Named("net.router")), tunDev, wireguardMonitor) |
| 90 | if err != nil { |
| 91 | return NetworkStack{}, xerrors.Errorf("create router: %w", err) |
| 92 | } |