示例#1
0
 private void Tun_PacketPoped(object sender, byte[] e)
 {
     // s.OutputStream.WriteAsync(e.AsBuffer()).AsTask();
     channel.LogDiagnosticMessage("Poped one packet");
     PendingPackets.Enqueue(e);
     CheckPendingPacket();
 }
示例#2
0
        public void Connect(VpnChannel channel)
        {
            //VpnCustomPromptTextInput i = new VpnCustomPromptTextInput();
            //i.Compulsory = true;
            //i.DisplayName = "aaaa";

            //await channel.RequestCustomPromptAsync(new IVpnCustomPromptElement[] {i});

            //channel.RequestCredentials(VpnCredentialType.UsernamePassword, false, false, null);
            channel.LogDiagnosticMessage("asdasdasd");
        }
示例#3
0
        public void Connect(VpnChannel channel)
        {
            Debug.WriteLine("UniPacketConnect++");
            try
            {
                channel.LogDiagnosticMessage("Start");

                foreach (var shn in channel.Configuration.ServerHostNameList)
                {
                    Debug.WriteLine("serverHostName->{0}", shn);
                    remoteHost = shn.ToString();
                }
                foreach (var ssn in channel.Configuration.ServerServiceName)
                {
                    Debug.WriteLine("ServerServiceName->{0}", ssn);
                }


                tunnelSocket.ConnectAsync(new HostName(remoteHost), "2312").AsTask().Wait();
                channel.AssociateTransport(tunnelSocket, null);
                channel.Start(
                    new[] { new HostName("66.32.12.12") }, //此处可从服务端获取ip配置 或者直接通过dhcp来设置,此处需注意随机IP生成不得与本地网络中所有的网段碰撞
                    null,                                  //此处为V6地址指派,用于设置虚拟nic的v6地址,也可通过远端dhcp配置
                    null,                                  //未弄清,此处为绑定的网卡id还是创建的虚拟nic id
                    new VpnRouteAssignment
                {
                    ExcludeLocalSubnets = true,
                    Ipv4InclusionRoutes = new[] { new VpnRoute(new HostName("66.32.12.0"), 24) }
                },         //路由表可在此处设置
                    new VpnNamespaceAssignment()
                {
                },                //值得研究,此处似乎可以筛选来源和目标APP,此处是支持ProxyAutoConfigUri,可考虑使用js代理脚本
                    512,          //NIC设置, MTU
                    1024,         //NIC设置, maxFrame 用于ip层拆包策略
                    false,
                    tunnelSocket, //main tunnel
                    null
                    );
            }
            catch (System.Exception e)
            {
                channel.SetErrorMessage(e.Message);
            }
            Debug.WriteLine("UniPacketConnect--");
        }
示例#4
0
 public void Decapsulate(VpnChannel channel, VpnPacketBuffer encapBuffer, VpnPacketBufferList decapsulatedPackets,
                         VpnPacketBufferList controlPacketsToSend)
 {
     channel.LogDiagnosticMessage("asdasdasd");
 }
示例#5
0
 public void Encapsulate(VpnChannel channel, VpnPacketBufferList packets, VpnPacketBufferList encapulatedPackets)
 {
     channel.LogDiagnosticMessage("asdasdasd");
 }
示例#6
0
 public void GetKeepAlivePayload(VpnChannel channel, out VpnPacketBuffer keepAlivePacket)
 {
     keepAlivePacket = null;
     channel.LogDiagnosticMessage("asdasdasd");
 }
示例#7
0
 public void Disconnect(VpnChannel channel)
 {
     channel.LogDiagnosticMessage("asdasdasd");
 }
示例#8
0
 private void LogLine(string text, VpnChannel channel)
 {
     //Debug.WriteLine(text);
     channel.LogDiagnosticMessage(text);
 }
示例#9
0
        public void Connect(VpnChannel channel)
        {
            State = VpnPluginState.Connecting;
            DebugLogger.Logger = s =>
            {
                channel.LogDiagnosticMessage(s);
            };
            DebugLogger.Log("Starting connection to VPN tunnel");
            try
            {
                var transport = new DatagramSocket();
                channel.AssociateTransport(transport, null);

                DebugLogger.Log("Initializing context");
#if !YT_MOCK
                var configPath = AdapterConfig.GetDefaultConfigFilePath();
                if (string.IsNullOrEmpty(configPath))
                {
                    channel.TerminateConnection("Default server configuration not set. Please launch YtFlow app and select a server configuration as default.");
                    return;
                }
                try
                {
                    var config = AdapterConfig.GetConfigFromFilePath(configPath);
                    if (config == null)
                    {
                        channel.TerminateConnection("Could not read server configuration.");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    channel.TerminateConnection("Error reading server configuration: " + ex.ToString());
                    return;
                }
#endif
                DebugLogger.Log("Config read, binding endpoint");
                if (!transport.BindEndpointAsync(new HostName("127.0.0.1"), string.Empty).AsTask().ContinueWith(t =>
                {
                    if (t.IsFaulted || t.IsCanceled)
                    {
                        DebugLogger.Log("Error binding endpoint: " + t.Exception.ToString());
                        return(false);
                    }
                    else
                    {
                        DebugLogger.Log("Binded");
                        return(true);
                    }
                }).Result)
                {
                    return;
                }
                DebugLogger.Log("Endpoint binded, init context");
                var rport = context.Init(int.Parse(transport.Information.LocalPort)).ToString();
                DebugLogger.Log("Context initialized");

                /* var rport = context.Init(transport.Information.LocalPort, str =>
                 * {
                 *  LogLine(str, channel);
                 *  return null;
                 * }); */
                DebugLogger.Log("Connecting to local packet processor");
                if (!transport.ConnectAsync(new HostName("127.0.0.1"), rport).AsTask().ContinueWith(t =>
                {
                    if (t.IsFaulted || t.IsCanceled)
                    {
                        channel.TerminateConnection("Error connecting to local packet processor: " + t.Exception.ToString());
                        DebugLogger.Log("Local packet processor connected");
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }).Result)
                {
                    return;
                }
                DebugLogger.Log("Connected to local packet processor");

                VpnRouteAssignment routeScope = new VpnRouteAssignment()
                {
                    ExcludeLocalSubnets = true
                };

                var inclusionRoutes = routeScope.Ipv4InclusionRoutes;
                // DNS server
                inclusionRoutes.Add(new VpnRoute(new HostName("1.1.1.1"), 32));
                // main CIDR
                inclusionRoutes.Add(new VpnRoute(new HostName("11.17.0.0"), 16));
                // proxy
                inclusionRoutes.Add(new VpnRoute(new HostName("172.17.255.0"), 24));

                var assignment = new VpnDomainNameAssignment();
                assignment.DomainNameList.Add(new VpnDomainNameInfo(
                                                  ".",
                                                  VpnDomainNameType.Suffix,
                                                  new[] { new HostName("1.1.1.1") },
                                                  Array.Empty <HostName>()));

                var now = DateTime.Now;
                DebugLogger.Log("Starting transport");
                channel.StartWithMainTransport(
                    new[] { new HostName("192.168.3.1") },
                    null,
                    null,
                    routeScope,
                    assignment,
                    1500u,
                    1512u,
                    false,
                    transport
                    );
                var delta = DateTime.Now - now;
                _ = context.u.SendAsync(new byte[] { 0 }, 1, context.pluginEndpoint);
                DebugLogger.Log($"Transport started in {delta.TotalMilliseconds} ms.");
                State = VpnPluginState.Connected;
            }
            catch (Exception ex)
            {
                var msg = "Error connecting to VPN tunnel: " + ex.ToString();
                channel.TerminateConnection(msg);
                DebugLogger.Log(msg);
                State = VpnPluginState.Disconnected;
            }
        }