请参阅下面的使用Visual Studio 2017社区版在Windows 10桌面上尝试的UWP代码段。
该代码实现了Custom IVpnPlugin模块。将系统的VPN配置选择为此应用程序并完成连接后,将触发应用程序的任务,并调用VPN插件的“ Connect()”方法。
但是,以下代码步骤在执行StartWithMainTransport(…)时会遇到异常。 (“该操作已被用户取消”)。 在系统的VPN设置上,看到以下错误-“远程访问服务ip配置不可用”
我认为我正确地将v4和v6地址传递给已绑定到我的m / c网络I / f的channel-> StartWithMainTransport(…)API。还有哪些其他验证可能导致了此问题。我不想为VpnChannel配置证书等,因为我打算在VpnPlugin中实现封装和解封装。
// Sample Plugin's connect implementation
void TunnelPlugin::Connect(Windows::Networking::Vpn::VpnChannel^ channel)
{
this->dSock = ref new DatagramSocket();
channel->AssociateTransport(this->dSock, nullptr); // No difference even if this statement is moved after ConnectAsync().
Platform::String^ svcName = "22111";
auto result = create_task(dSock->BindServiceNameAsync(svcName));
result.get();
// Connect to the destination tunnel address on UDP socket.
HostName^ remoteTunnelIP = ref new HostName("192.168.1.137");
Platform::String^ remoteTunnelPort = "22112";
result = create_task(this->dSock->ConnectAsync(remoteTunnelIP, remoteTunnelPort));
result.get();
VpnChannelConfiguration^ chanCfg = channel->Configuration;
// IP destinations to be routed via VPN
VpnRouteAssignment^ routeScope = ref new VpnRouteAssignment();
routeScope->Ipv4InclusionRoutes->Append(ref new VpnRoute(ref new HostName("192.168.1.111"), 32));
Vector<HostName^>^ localV4Addrs = ref new Vector<HostName^>;
localV4Addrs->Append(ref new HostName("192.168.1.133")); // Local host name to be bound.
Vector<HostName^>^ localV6Addrs = ref new Vector<HostName^>;
localV6Addrs->Append(ref new HostName("fc00::44fd:d3ed:b02a:a05e"));
Vector<HostName^>^ dnsServers = ref new Vector<HostName^>;
dnsServers->Append(ref new HostName("1.1.1.1"));
VpnDomainNameInfo^ dnsInfo = ref new VpnDomainNameInfo(".", VpnDomainNameType::Suffix, dnsServers, ref new Vector<HostName^>);
VpnDomainNameAssignment^ dnsAssignment = ref new VpnDomainNameAssignment;
dnsAssignment->DomainNameList->Append(dnsInfo);
try
{
// Throws exception here.
channel->StartWithMainTransport(localV4Addrs->GetView(), localV6Addrs->GetView(), nullptr, routeScope, dnsAssignment, 1400, 1412, false, this->dSock);
}
catch (Exception^ exc)
{
auto type = exc->GetType();
Platform::String^ str = exc->ToString();
}
}