Пример #1
0
        private void DoTest(Action <WebProxy>[] toRunActions)
        {
            new WindowsModule().RegisterPlatform();
            const string Username     = "******";
            const string Password     = "******";
            var          combinations =
                from authMethod in
                Enum.GetValues(typeof(Common.AuthenticationMethods)).Cast <Common.AuthenticationMethods>()
                from serverCompression in
                Enum.GetValues(typeof(Common.CompressionTypes)).Cast <Common.CompressionTypes>()
                from serverEncryption in Enum.GetValues(typeof(Common.EncryptionTypes)).Cast <Common.EncryptionTypes>()
                from clientCompression in
                Enum.GetValues(typeof(Common.CompressionTypes)).Cast <Common.CompressionTypes>()
                from clientEncryption in Enum.GetValues(typeof(Common.EncryptionTypes)).Cast <Common.EncryptionTypes>()
                select
                new
            {
                AuthMethod        = authMethod,
                ServerCompression = serverCompression,
                ServerEncryption  = serverEncryption,
                ClientCompression = clientCompression,
                ClientEncryption  = clientEncryption,
            };

            foreach (var combination in
                     combinations.Where(combination => combination.AuthMethod != Common.AuthenticationMethods.Invalid))
            {
                Trace.WriteLine("Testing: " + combination);
                Settings serverSettings = new Settings
                {
                    AuthMethod           = combination.AuthMethod,
                    CompressionType      = combination.ServerCompression,
                    EncryptionType       = combination.ServerEncryption,
                    ServerPort           = 9980,
                    ConfigFileAddress    = string.Empty,
                    BlackListFileAddress = string.Empty,
                    UsersFileAddress     = string.Empty,
                    ServerIp             = IPAddress.Loopback.ToString()
                };

                PeaRoxy clientServer = new PeaRoxy(
                    serverSettings.ServerIp,
                    (ushort)serverSettings.ServerPort,
                    string.Empty,
                    serverSettings.AuthMethod != Common.AuthenticationMethods.None ? Username : string.Empty,
                    serverSettings.AuthMethod != Common.AuthenticationMethods.None ? Password : string.Empty,
                    combination.ClientEncryption,
                    combination.ClientCompression);

                if (serverSettings.AuthMethod != Common.AuthenticationMethods.None)
                {
                    serverSettings.AthorizedUsers.Add(new ConfigUser(Username, Password));
                }

                PeaRoxyController server = new PeaRoxyController(serverSettings);
                ProxyController   client = new ProxyController(clientServer, IPAddress.Loopback, 0)
                {
                    IsAutoConfigEnable
                        = false
                };
                if (!server.Start())
                {
                    throw new Exception("Failed to start the server");
                }
                client.TestServer();
                if (!client.Start())
                {
                    throw new Exception("Failed to start the client");
                }
                WebProxy proxy = CommonMethods.CreateProxy(client.Ip, client.Port);
                foreach (Action <WebProxy> action in toRunActions)
                {
                    action.Invoke(proxy);
                }
                client.Stop();
                server.Stop();
            }
        }
Пример #2
0
        /// <summary>
        ///     The start server.
        /// </summary>
        /// <returns>
        ///     The <see cref="bool" />.
        /// </returns>
        /// <exception cref="ArgumentException">
        ///     Username is null or empty
        /// </exception>
        /// <exception cref="Exception">
        ///     Failed to start server
        /// </exception>
        public bool StartServer(bool compatibility = false)
        {
            try
            {
                if (string.IsNullOrEmpty(Settings.Default.UserAndPassword_User))
                {
                    // ReSharper disable once NotResolvedInText
                    throw new ArgumentException(@"Invalid value.", "UserName");
                }

                if (this.listener != null)
                {
                    this.StopServer();
                }
                else
                {
                    this.listener = new ProxyController(null, null, 0);
                    this.listener.AutoDisconnectedDueToFailureNotify += this.FailDisconnected;
                }

                this.Enabled = false;

                this.listener.Ip   = IPAddress.Loopback;
                this.listener.Port = 0;
                this.listener.IsAutoConfigEnable = false;
                this.listener.IsHttpSupported    = false;
                this.listener.IsHttpsSupported   = false;
                this.listener.IsSocksSupported   = true;
                this.listener.SendPacketSize     = Settings.Default.Connection_SendPacketSize;
                this.listener.ReceivePacketSize  = Settings.Default.Connection_RecPacketSize;
                this.listener.AutoDisconnect     = Settings.Default.Connection_StopOnInterrupt;

                this.listener.ErrorRenderer.EnableOnHttp    = false;
                this.listener.ErrorRenderer.EnableOnPort80  = false;
                this.listener.ErrorRenderer.EnableOnPort443 = false;

                this.listener.DnsResolver.DnsResolverSupported        =
                    this.listener.DnsResolver.DnsResolverUdpSupported = false;
                this.listener.DnsResolver.DnsResolverServerIp         = IPAddress.Parse(Settings.Default.DNS_IPAddress);

                this.listener.SmartPear.DetectorTreatPort80AsHttp   = false;
                this.listener.SmartPear.ForwarderHttpEnable         = false;
                this.listener.SmartPear.DetectorHttpCheckEnable     = false;
                this.listener.SmartPear.DetectorHttpPattern         = string.Empty;
                this.listener.SmartPear.DetectorDnsPoisoningPattern = string.Empty;
                this.listener.SmartPear.DetectorDnsPoisoningEnable  = false;
                this.listener.SmartPear.ForwarderHttpsEnable        = false;
                this.listener.SmartPear.DetectorTimeoutEnable       = false;
                this.listener.SmartPear.DetectorTimeout             = 0;
                this.listener.SmartPear.ForwarderTreatPort80AsHttp  = false;
                this.listener.SmartPear.ForwarderSocksEnable        = false;
                this.listener.SmartPear.ForwarderHttpList.Clear();
                this.listener.SmartPear.ForwarderDirectList.Clear();

                string serverAddress = Settings.Default.ServerAddress;
                ushort serverPort    = Settings.Default.ServerPort;
                int    pIndex        = serverAddress.IndexOf(":", StringComparison.Ordinal);
                if (pIndex > 0)
                {
                    if (!ushort.TryParse(serverAddress.Substring(pIndex + 1), out serverPort))
                    {
                        serverPort = Settings.Default.ServerPort;
                    }
                    serverAddress = serverAddress.Substring(0, pIndex);
                }

                ServerType ser = new PeaRoxy(
                    serverAddress,
                    serverPort,
                    string.Empty,
                    Settings.Default.UserAndPassword_User,
                    Settings.Default.UserAndPassword_Pass,
                    Common.EncryptionTypes.SimpleXor)
                {
                    ForgerCompatibility = compatibility
                };

                ser.NoDataTimeout          = Settings.Default.Connection_NoDataTimeout;
                this.listener.ActiveServer = ser;

                this.listener.TestServerAsyc(
                    (suc, mes) => this.Invoke(
                        (SimpleVoidDelegate) delegate
                {
                    try
                    {
                        if (suc)
                        {
                            this.listener.Start();

                            // Grabber Pro-Listen Settings
                            if ((GrabberType)Settings.Default.Grabber == GrabberType.Tap)
                            {
                                List <IPAddress> hostIps = new List <IPAddress>();
                                if (Common.IsIpAddress(serverAddress))
                                {
                                    hostIps.Add(IPAddress.Parse(serverAddress));
                                }
                                else
                                {
                                    hostIps.AddRange(Dns.GetHostEntry(serverAddress).AddressList);
                                }
                                TapTunnelModule.ExceptionIPs = hostIps.ToArray();
                            }

                            this.ReConfig(false);

                            Program.Notify.ShowBalloonTip(
                                2000,
                                "Z A Я A",
                                "Connected to the server\r\n" + ser.ToString(),
                                ToolTipIcon.Info);
                            Program.Notify.Text = @"Z A Я A - Working";
                            this.Status         = CurrentStatus.Connected;
                            this.RefreshStatus();
                        }
                        else
                        {
                            throw new Exception(mes);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (this.listener != null)
                        {
                            this.listener.Stop();
                        }

                        if (this.listener != null && this.listener.ActiveServer is PeaRoxy && !compatibility)
                        {
                            this.StartServer(true);
                            return;
                        }

                        this.ShowDialog(
                            "Error: " + ex.Message + ex.StackTrace,
                            "Start Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);

                        this.RefreshStatus();
                        if (!this.updaterWorker.IsBusy)
                        {
                            this.updaterWorker.RunWorkerAsync();
                        }
                    }
                },
                        new object[] { }));

                return(true);
            }
            catch (Exception ex)
            {
                this.ShowDialog("Error: " + ex.Message, "Start Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (this.listener != null)
                {
                    this.listener.Stop();
                }

                this.RefreshStatus();
            }

            return(false);
        }