예제 #1
0
        /// <summary>
        /// Main routine for testing and connecting to a proxy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void buttonConnect_Click(object sender, EventArgs e)
        {
            buttonConnect.Enabled = false;
            if (!string.IsNullOrEmpty(txtBoxProxyUrl.Text))
            {
                if (int.TryParse(txtBoxProxyPort.Text, out int port))
                {
                    if (comboBoxProxyType.Text == "http")
                    {
                        WebRequestHelper.AddHttpProxy(txtBoxProxyUrl.Text, txtBoxProxyPort.Text);
                    }
                    else if (comboBoxProxyType.Text == "socks")
                    {
                        WebRequestHelper.AddSocksProxy(txtBoxProxyUrl.Text, port);
                    }

                    try {
                        var task = WebRequestHelper.GetRequest("http://ipv4.icanhazip.com/");

                        if (await Task.WhenAny(task, Task.Delay(Config.TimeoutMS)) == task)
                        {
                            if (string.IsNullOrEmpty(task.Result))
                            {
                                MessageBox.Show("Unable to connect to proxy try again...", "Connection Failed");
                                WebRequestHelper.ResetHttpClient();
                            }
                            else
                            {
                                MessageBox.Show("Your IP Is : " + task.Result, "Connection Success");
                                buttonConnect.Enabled    = true;
                                buttonResetProxy.Enabled = true;
                                this.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Unable to connect to proxy try again...");
                            WebRequestHelper.ResetHttpClient();
                            buttonResetProxy.Enabled = false;
                        }
                    }
                    catch (Exception) {
                        MessageBox.Show("Unable to connect to proxy try again...");
                        WebRequestHelper.ResetHttpClient();
                        buttonResetProxy.Enabled = false;
                    }
                }
            }
            buttonConnect.Enabled = true;
        }