| 59 | } |
| 60 | |
| 61 | void VpnPlugin::ConnectCore(VpnChannel const& channel) |
| 62 | { |
| 63 | const auto localhost = HostName{ L"127.0.0.1" }; |
| 64 | DatagramSocket transport{}, backTransport{}; |
| 65 | channel.AssociateTransport(transport, nullptr); |
| 66 | transport.BindEndpointAsync(localhost, L"").get(); |
| 67 | backTransport.BindEndpointAsync(localhost, L"").get(); |
| 68 | transport.ConnectAsync(localhost, backTransport.Information().LocalPort()).get(); |
| 69 | backTransport.ConnectAsync(localhost, transport.Information().LocalPort()).get(); |
| 70 | |
| 71 | VpnRouteAssignment routeScope{}; |
| 72 | routeScope.ExcludeLocalSubnets(true); |
| 73 | routeScope.Ipv4InclusionRoutes(std::vector{ |
| 74 | // 直接写 0.0.0.0/0 哪怕绑了接口也会绕回环 |
| 75 | // VpnRoute(HostName{ L"0.0.0.0" }, 0) |
| 76 | VpnRoute(HostName{ L"0.0.0.0" }, 1), |
| 77 | VpnRoute(HostName{ L"128.0.0.0" }, 1), |
| 78 | }); |
| 79 | routeScope.Ipv6InclusionRoutes(std::vector{ |
| 80 | VpnRoute(HostName{L"::"}, 1), |
| 81 | VpnRoute(HostName{L"8000::"}, 1) |
| 82 | }); |
| 83 | // 排除代理服务器的话就会 os 10023 以一种访问权限不允许的方式做了一个访问套接字的尝试 |
| 84 | // routeScope.Ipv4ExclusionRoutes(std::vector<VpnRoute>{ |
| 85 | // VpnRoute(HostName{ L"172.25.0.0" }, 16) |
| 86 | // }); |
| 87 | |
| 88 | const auto outputStreamAbi = winrt::detach_abi(backTransport.OutputStream()); |
| 89 | StopLeaf(); |
| 90 | { |
| 91 | std::lock_guard _guard{ m_decapQueueLock }; |
| 92 | while (!m_decapQueue.empty()) { |
| 93 | m_decapQueue.pop(); |
| 94 | } |
| 95 | } |
| 96 | m_backTransport = backTransport; |
| 97 | |
| 98 | m_netStackHandle = netstack_register(cb, outputStreamAbi); |
| 99 | if (m_netStackHandle == nullptr) { |
| 100 | channel.TerminateConnection(L"Error initializing Leaf netstack."); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | const auto& appData = ApplicationData::Current(); |
| 105 | const auto& configFolderPath = appData.LocalFolder().CreateFolderAsync(L"config", CreationCollisionOption::OpenIfExists).get().Path(); |
| 106 | const auto& localProperties = appData.LocalSettings().Values(); |
| 107 | const auto& outNetif = localProperties.TryLookup(NETIF_SETTING_KEY).try_as<hstring>().value_or(L""); |
| 108 | const auto& cacheDir = appData.LocalCacheFolder().Path(); |
| 109 | if ( |
| 110 | !SetEnvironmentVariable(L"ASSET_LOCATION", configFolderPath.data()) |
| 111 | || !SetEnvironmentVariable(L"LOG_NO_COLOR", L"true") |
| 112 | || !SetEnvironmentVariable(L"OUTBOUND_INTERFACE", outNetif.data()) |
| 113 | || !SetEnvironmentVariable(L"CACHE_LOCATION", cacheDir.data()) |
| 114 | || !SetEnvironmentVariable(L"ENABLE_IPV6", L"true") |
| 115 | ) { |
| 116 | channel.TerminateConnection(L"Failed to set environment variables: " + winrt::to_hstring((uint32_t)GetLastError())); |
| 117 | return; |
| 118 | } |