Example #1
0
        private void OpenFD()
        {
            if (vpnFD == null)
            {
                try
                {
                    var configFile = File.ReadAllText(prepareConfigPath);
                    var route      = ParseType(configFile);

                    Log.Debug(TAG, "VPN Route Type: " + route);

                    Builder builder = new Builder(this);
                    builder.AddAddress(VpnAddress, 32)
                    .SetMtu(VpnMtu)
                    .SetConfigureIntent(CreatePendingIntent())
                    .AddDnsServer(VpnDnsServer)
                    .SetSession(GetString(Resource.String.app_name));

                    if (route == HostModel.RouteType.Route_all ||
                        route == HostModel.RouteType.Route_bypass_cn_mainland)
                    {
                        builder.AddRoute("0.0.0.0", 0);
                    }
                    else
                    {
                        var ips = Resources.GetTextArray(Resource.Array.bypass_private_route);
                        foreach (var ip in ips)
                        {
                            string[] addr = ip.Split('/');
                            builder.AddRoute(addr[0], int.Parse(addr[1]));
                        }

                        builder.AddRoute(VpnDnsServer, 32);
                    }

                    vpnFD = builder.Establish();

                    configFile = configFile.Replace("${tun.tun_fd}", vpnFD.Fd.ToString());

                    runConfigPath = prepareConfigPath + TrojanPlusMainActivity.RunningConfigSuffix;
                    File.WriteAllText(runConfigPath, configFile);

                    if (showNotification)
                    {
                        notification = new TrojanPlusNotification(this);
                    }

                    worker = new Thread(new WorkerThread(this).Run);
                    worker.Start();
                }
                catch (Exception ex)
                {
                    Crashes.TrackError(ex);
                    Log.Error(TAG, ex.StackTrace);

                    CloseFD();
                    StopSelf();
                }
            }
        }
        private void OpenFD()
        {
            if (m_vpnFD == null)
            {
                try
                {
                    var configFile = System.IO.File.ReadAllText(m_configPath);
                    var route      = ParseType(configFile);

                    Log.Debug(TAG, "VPN Route Type: " + route);

                    PendingIntent pintent = PendingIntent.GetActivity(Application.Context, 0,
                                                                      new Intent(Application.Context, typeof(MainActivity)).SetFlags(ActivityFlags.ReorderToFront), 0);

                    Builder builder = new Builder(this);
                    builder.AddAddress(VPN_ADDRESS, 32)
                    .SetMtu(VPN_MTU)
                    .SetConfigureIntent(pintent)
                    .AddDnsServer(VPN_DNS_SERVER)
                    .SetSession(GetString(Resource.String.app_name));

                    if (route == HostModel.RouteType.Route_all ||
                        route == HostModel.RouteType.Route_bypass_cn_mainland)
                    {
                        builder.AddRoute("0.0.0.0", 0);
                    }
                    else
                    {
                        var ips = Resources.GetTextArray(Resource.Array.bypass_private_route);
                        foreach (var ip in ips)
                        {
                            string[] addr = ip.Split('/');
                            builder.AddRoute(addr[0], int.Parse(addr[1]));
                        }
                        builder.AddRoute(VPN_DNS_SERVER, 32);
                    }

                    m_vpnFD = builder.Establish();

                    configFile = configFile.Replace("${tun.tun_fd}", m_vpnFD.Fd.ToString());

                    System.IO.File.WriteAllText(m_configPath, configFile);
                }
                catch (Exception ex)
                {
                    Log.Error(TAG, ex.StackTrace);
                    CloseFD();
                    StopSelf();
                    return;
                }

                m_worker = new Thread(new WorkerThread(this).Run);
                m_worker.Start();
            }
        }
            public /*async*/ void OnStart()
            {
                if (isStart)
                {
                    return;
                }
                isStart = true;

                IForegroundService f = this;

                AndroidNotificationServiceImpl.Instance.StartForeground(this, f.NotificationType, f.NotificationText, f.NotificationEntranceAction);

                ProxyService.Current.ProxyStatus = true;

                // wait ???
                //await Task.Delay(3500);

                if (localTunnel != null)
                {
                    return;
                }

                // Configure a new interface from our VpnService instance. This must be done
                // from inside a VpnService.
                var builder = new Builder(this).SetSession(AssemblyTrademark);

                // Create a local TUN interface using predetermined addresses. In your app,
                // you typically use values returned from the VPN gateway during handshaking.
                builder.AddAddress("10.1.10.1", 32);
                //builder.AddAddress("fd00:1:fd00:1:fd00:1:fd00:1", 128);
                builder.AddRoute("0.0.0.0", 0);
                //builder.AddRoute("0:0:0:0:0:0:0:0", 0);

                var dnss = GetDefaultDNS();

                Array.ForEach(dnss, x => builder.AddDnsServer(x));

                int mtu = jni_get_mtu();

                builder.SetMtu(mtu);

                var pkg = PackageName !;

                builder.AddDisallowedApplication(pkg);

                localTunnel = builder.Establish() !;

                var httpProxyService = IHttpProxyService.Instance;
                var address          = httpProxyService.ProxyIp;
                var port             = httpProxyService.ProxyPort;

                jni_start(localTunnel.Fd, false, 3, address !.ToString(), port);
            }
Example #4
0
 private void SetupVPN()
 {
     if (vpnInterface == null)
     {
         Builder builder = new Builder(this);
         builder.AddAddress(VPN_ADDRESS, 32);
         builder.AddRoute(VPN_ROUTE, 0);
         builder.AddDnsServer(DNS_ADDRESS);
         builder.SetSession(GetString(Resource.String.app_name));
         builder.SetConfigureIntent(pendingIntent);
         vpnInterface = builder.Establish();
     }
 }