Example #1
0
        private void ControlButton_Click(object sender, EventArgs e)
        {
            if (ControlButton.Text.Contains("启动"))
            {
                if (v2rayServerAddressTextBox.Text != "" && v2rayServerPortTextBox.Text != "" && v2rayUserIDTextBox.Text != "" && v2rayAlterIDTextBox.Text != "" && TUNTAPAddressTextBox.Text != "" && TUNTAPGatewayTextBox.Text != "" && TUNTAPDNSTextBox.Text != "" && TUNTAPPointsTextBox.Text != "")
                {
                    status = "启动中 ...";
                    ControlButton.Enabled = false;
                    ControlButton.Text    = "稍等 ...";

                    string adapterAddress     = AdapterAddressComboBox.Text;
                    string adapterGateway     = AdapterGatewayComboBox.Text;
                    string v2rayDefaultDNS    = v2rayDefaultDNSTextBox.Text;
                    string v2rayChinaDNS      = v2rayChinaDNSTextBox.Text;
                    string v2rayServerAddress = v2rayServerAddressTextBox.Text;
                    string v2rayServerPort    = v2rayServerPortTextBox.Text;
                    string v2rayUserID        = v2rayUserIDTextBox.Text;
                    string v2rayAlterID       = v2rayAlterIDTextBox.Text;
                    string v2rayTransferMethod;
                    switch (v2rayTransferMethodComboBox.Text)
                    {
                    case "TCP":
                        v2rayTransferMethod = "tcp";
                        break;

                    case "mKCP":
                        v2rayTransferMethod = "kcp";
                        break;

                    case "WebSockets":
                        v2rayTransferMethod = "ws";
                        break;

                    case "HTTP/2":
                        v2rayTransferMethod = "http";
                        break;

                    case "QUIC":
                        v2rayTransferMethod = "quic";
                        break;

                    default:
                        v2rayTransferMethod = "ws";
                        break;
                    }
                    string v2rayTLSSecure = (v2rayTLSSecureCheckBox.Checked) ? "tls" : "none";
                    string v2rayPath      = v2rayPathTextBox.Text;

                    string TUNTAPGateway = TUNTAPGatewayTextBox.Text;
                    string TUNTAPAddress = TUNTAPAddressTextBox.Text;
                    string TUNTAPDNS     = TUNTAPDNSTextBox.Text;
                    string TUNTAPPoints  = TUNTAPPointsTextBox.Text;

                    Task.Factory.StartNew(() =>
                    {
                        status        = "正在生成 v2ray 配置文件 ...";
                        string config = Encoding.UTF8.GetString(Convert.FromBase64String(v2tap.config));
                        config        = config.Replace("adapterIPAddress", adapterAddress);
                        config        = config.Replace("v2rayDefaultDNS", v2rayDefaultDNS);
                        config        = config.Replace("v2rayChinaDNS", v2rayChinaDNS);
                        config        = config.Replace("v2rayServerAddress", v2rayServerAddress);
                        config        = config.Replace("v2rayServerPort", v2rayServerPort);
                        config        = config.Replace("v2rayUserID", v2rayUserID);
                        config        = config.Replace("v2rayAlterID", v2rayAlterID);
                        config        = config.Replace("v2rayTransferMethod", v2rayTransferMethod);
                        config        = config.Replace("v2rayTLSSecure", v2rayTLSSecure);
                        config        = config.Replace("v2rayPath", v2rayPath);
                        File.WriteAllText("config.json", config);

                        Thread.Sleep(1000);
                        status = "正在启动 v2ray 服务 ...";
                        Utils.SharedUtils.ExecuteCommand("wv2ray.exe -config config.json");

                        Thread.Sleep(1000);
                        status = "正在启动 tun2socks 服务 ...";
                        Utils.SharedUtils.ExecuteCommand("RunHiddenConsole.exe tun2socks.exe -tun-gw " + TUNTAPGateway + " -tun-address " + TUNTAPAddress + " -tun-dns " + TUNTAPDNS + " -enable-dns-cache -public-only -local-socks-addr 127.0.0.1:1099");

                        Thread.Sleep(1000);
                        status = "正在配置 路由表 ...";
                        Utils.SharedUtils.ExecuteCommand("ROUTE CHANGE 0.0.0.0 MASK 0.0.0.0 " + adapterGateway + " METRIC 1000");
                        Utils.SharedUtils.ExecuteCommand("ROUTE ADD " + v2rayDefaultDNS + " MASK 255.255.255.255 " + adapterGateway + " METRIC 10");
                        Utils.SharedUtils.ExecuteCommand("ROUTE ADD " + v2rayChinaDNS + " MASK 255.255.255.255 " + adapterGateway + " METRIC 10");
                        if (TUNTAPDNS.Contains(","))
                        {
                            foreach (string IP in TUNTAPDNS.Split(",".ToCharArray()))
                            {
                                Utils.SharedUtils.ExecuteCommand("ROUTE ADD " + IP + " MASK 255.255.255.255 " + adapterGateway + " METRIC 10");
                            }
                        }
                        else
                        {
                            Utils.SharedUtils.ExecuteCommand("ROUTE ADD " + TUNTAPDNS + " MASK 255.255.255.255 " + adapterGateway + " METRIC 10");
                        }
                        Utils.SharedUtils.ExecuteCommand("ROUTE ADD 0.0.0.0 MASK 0.0.0.0 " + TUNTAPGateway + " METRIC " + TUNTAPPoints);

                        Thread.Sleep(1000);
                        ControlButton.Invoke((MethodInvoker) delegate
                        {
                            ControlButton.Enabled = true;
                            ControlButton.Text    = "停止";
                        });
                        status = "启动完毕 ...";

                        try
                        {
                            File.Delete("config.json");
                        }
                        catch
                        {
                        }
                    });
                }
                else
                {
                    MessageBox.Show("配置信息不可为空", "配置错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                status = "停止中 ...";
                ControlButton.Enabled = false;
                ControlButton.Text    = "稍等 ...";

                string v2rayDefaultDNS = v2rayDefaultDNSTextBox.Text;
                string v2rayChinaDNS   = v2rayChinaDNSTextBox.Text;

                string TUNTAPDNS = TUNTAPDNSTextBox.Text;

                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(1000);
                    status = "正在关闭 v2ray 服务 ...";
                    Utils.SharedUtils.ExecuteCommand("TASKKILL /F /T /IM wv2ray.exe");

                    Thread.Sleep(1000);
                    status = "正在关闭 tun2socks 服务 ...";
                    Utils.SharedUtils.ExecuteCommand("TASKKILL /F /T /IM tun2socks.exe");

                    Thread.Sleep(1000);
                    status = "正在重置 路由表 ...";
                    Utils.SharedUtils.ExecuteCommand("ROUTE DELETE " + v2rayDefaultDNS);
                    Utils.SharedUtils.ExecuteCommand("ROUTE DELETE " + v2rayChinaDNS);
                    if (TUNTAPDNS.Contains(","))
                    {
                        foreach (string IP in TUNTAPDNS.Split(",".ToCharArray()))
                        {
                            Utils.SharedUtils.ExecuteCommand("ROUTE DELETE " + IP);
                        }
                    }
                    else
                    {
                        Utils.SharedUtils.ExecuteCommand("ROUTE DELETE " + TUNTAPDNS);
                    }

                    Thread.Sleep(1000);
                    ControlButton.Invoke((MethodInvoker) delegate
                    {
                        ControlButton.Enabled = true;
                        ControlButton.Text    = "启动";
                    });
                    status = "停止完毕 ...";
                });
            }
        }
Example #2
0
        private void ControlButton_Click(object sender, EventArgs e)
        {
            Status = "正在处理中";
            if (!isStarted)
            {
                v2rayProxyComboBox.Enabled = false;
                v2rayModeComboBox.Enabled  = false;
                ControlButton.Enabled      = false;
                ControlButton.Text         = "处理中";

                Task.Run(() =>
                {
                    var address = Global.Configs.AdapterAddress[Global.Configs.AdapterAddressIndex];
                    var gateway = Global.Configs.AdapterGateway[Global.Configs.AdapterGatewayIndex];

                    Thread.Sleep(2000);
                    Status = "正在生成 v2ray 配置文件中";
                    string v2rayConfig;
                    if (v2rayModeComboBox.SelectedIndex == 1)
                    {
                        v2rayConfig = Encoding.UTF8.GetString(Properties.Resources.v2ray);
                    }
                    else
                    {
                        v2rayConfig = Encoding.UTF8.GetString(Properties.Resources.v2rayNotBypassChina);
                    }
                    v2rayConfig = v2rayConfig
                                  .Replace("v2rayLoggingLevel", Global.Configs.v2rayLoggingLevel.ToLower())
                                  .Replace("AdapterAddress", address)
                                  .Replace("v2rayServerAddress", Global.Proxies[v2rayProxyComboBox.SelectedIndex].Address)
                                  .Replace("v2rayServerPort", Global.Proxies[v2rayProxyComboBox.SelectedIndex].Port.ToString())
                                  .Replace("v2rayUserID", Global.Proxies[v2rayProxyComboBox.SelectedIndex].UserID)
                                  .Replace("v2rayAlterID", Global.Proxies[v2rayProxyComboBox.SelectedIndex].AlterID.ToString())
                                  .Replace("v2rayTLSSecure", Global.Proxies[v2rayProxyComboBox.SelectedIndex].TLSSecure ? "tls" : "none")
                                  .Replace("v2rayPath", Global.Proxies[v2rayProxyComboBox.SelectedIndex].Path);
                    switch (Global.Proxies[v2rayProxyComboBox.SelectedIndex].TransferMethod)
                    {
                    case "TCP":
                        v2rayConfig = v2rayConfig.Replace("v2rayTransferMethod", "tcp");
                        break;

                    case "mKCP":
                        v2rayConfig = v2rayConfig.Replace("v2rayTransferMethod", "kcp");
                        break;

                    case "WebSockets":
                        v2rayConfig = v2rayConfig.Replace("v2rayTransferMethod", "ws");
                        break;

                    case "HTTP/2":
                        v2rayConfig = v2rayConfig.Replace("v2rayTransferMethod", "http");
                        break;

                    case "QUIC":
                        v2rayConfig = v2rayConfig.Replace("v2rayTransferMethod", "quic");
                        break;

                    default:
                        v2rayConfig = v2rayConfig.Replace("v2rayTransferMethod", "tcp");
                        break;
                    }
                    var regex = new Regex(Regex.Escape("v2rayFakeType"));
                    switch (Global.Proxies[v2rayProxyComboBox.SelectedIndex].FakeType)
                    {
                    case "None":
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "none");
                        break;

                    case "HTTP":
                        v2rayConfig = regex.Replace(v2rayConfig, "http", 1);
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "none");
                        break;

                    case "SRTP":
                        v2rayConfig = regex.Replace(v2rayConfig, "none", 1);
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "srtp");
                        break;

                    case "UTP":
                        v2rayConfig = regex.Replace(v2rayConfig, "none", 1);
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "utp");
                        break;

                    case "DTLS":
                        v2rayConfig = regex.Replace(v2rayConfig, "none", 1);
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "dtls");
                        break;

                    case "WireGuard":
                        v2rayConfig = regex.Replace(v2rayConfig, "none", 1);
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "wireguard");
                        break;

                    case "WeChat":
                        v2rayConfig = regex.Replace(v2rayConfig, "none", 1);
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "wechat-video");
                        break;

                    default:
                        v2rayConfig = v2rayConfig.Replace("v2rayFakeType", "none");
                        break;
                    }
                    File.WriteAllText("config.json", v2rayConfig);

                    Thread.Sleep(2000);
                    Status = "正在启动 v2ray 服务中";
                    Utils.Util.ExecuteCommand("wv2ray.exe -config config.json");

                    Thread.Sleep(2000);
                    Status = "正在启动 tun2socks 服务中";
                    Task.Run(() =>
                    {
                        Utils.Util.ExecuteCommand(String.Format("tun2socks.exe -tun-address {0} -tun-gw {1} -tun-dns {2} -local-socks-addr 127.0.0.1:1099 -enable-dns-cache -public-only > logging\\tun2socks.txt 2>&1", Global.Configs.TUNTAPAddress, Global.Configs.TUNTAPGateway, Global.Configs.TUNTAPDNS));
                    });

                    Thread.Sleep(2000);
                    Status = "正在配置 路由表 中";
                    if (Global.Configs.AutoAdapterMetric)
                    {
                        Utils.Util.ExecuteCommand(String.Format("ROUTE CHANGE 0.0.0.0 MASK 0.0.0.0 {0} METRIC 1000", gateway));
                    }

                    if (v2rayModeComboBox.SelectedIndex == 0 && v2rayModeComboBox.SelectedIndex == 1)
                    {
                        Utils.Util.ExecuteCommand(String.Format("ROUTE ADD 0.0.0.0 MASK 0.0.0.0 {0} METRIC {1}", Global.Configs.TUNTAPGateway, Global.Configs.TUNTAPMetric));
                    }
                    else
                    {
                        foreach (var rule in Global.Modes[v2rayModeComboBox.SelectedIndex - 2].Rule)
                        {
                            Utils.Util.ExecuteCommand(String.Format("ROUTE ADD {0} MASK {1} {2} METRIC {3}", rule.Key, rule.Value, Global.Configs.TUNTAPGateway, Global.Configs.TUNTAPMetric));
                        }
                    }

                    Utils.Util.ExecuteCommand(String.Format("ROUTE ADD 8.8.8.8 MASK 255.255.255.255 {0} METRIC 10", gateway));
                    Utils.Util.ExecuteCommand(String.Format("ROUTE ADD 114.114.114.114 MASK 255.255.255.255 {0} METRIC 10", gateway));

                    if (Global.Configs.TUNTAPDNS.Contains(","))
                    {
                        foreach (var ip in Global.Configs.TUNTAPDNS.Split(','))
                        {
                            Utils.Util.ExecuteCommand(String.Format("ROUTE ADD {0} MASK 255.255.255.255 {1} METRIC 10", ip, gateway));
                        }
                    }
                    else
                    {
                        Utils.Util.ExecuteCommand(String.Format("ROUTE ADD {0} MASK 255.255.255.255 {1} METRIC 10", Global.Configs.TUNTAPDNS, gateway));
                    }

                    Thread.Sleep(2000);
                    isStarted = true;
                    Status    = "启动完毕,请自行检查网络";
                    ControlButton.Invoke(new MethodInvoker(() =>
                    {
                        ControlButton.Enabled = true;
                        ControlButton.Text    = "停止";
                    }));

                    try
                    {
                        File.Delete("config.json");
                    }
                    catch (Exception)
                    {
                    }
                });
            }
            else
            {
                ControlButton.Enabled = false;
                ControlButton.Text    = "处理中";

                Task.Run(() =>
                {
                    isStarted = false;
                    Thread.Sleep(2000);
                    Status = "正在关闭 tun2socks 服务中";
                    Utils.Util.ExecuteCommand("TASKKILL /F /T /IM tun2socks.exe");

                    Thread.Sleep(2000);
                    Status = "正在关闭 v2ray 服务中";
                    Utils.Util.ExecuteCommand("TASKKILL /F /T /IM wv2ray.exe");

                    Thread.Sleep(2000);
                    Status = "正在重置 路由表 中";
                    Utils.Util.ExecuteCommand("TASKKILL /F /T /IM tun2socks.exe");
                    Utils.Util.ExecuteCommand("ROUTE DELETE 114.114.114.114");
                    Utils.Util.ExecuteCommand("ROUTE DELETE 8.8.8.8");

                    if (Global.Configs.TUNTAPDNS.Contains(","))
                    {
                        foreach (var ip in Global.Configs.TUNTAPDNS.Split(','))
                        {
                            Utils.Util.ExecuteCommand(String.Format("ROUTE DELETE {0}", ip));
                        }
                    }
                    else
                    {
                        Utils.Util.ExecuteCommand(String.Format("ROUTE DELETE {0}", Global.Configs.TUNTAPDNS));
                    }

                    if (v2rayProxyComboBox.SelectedIndex != 0 & v2rayProxyComboBox.SelectedIndex != 1)
                    {
                        foreach (var rule in Global.Modes[v2rayModeComboBox.SelectedIndex - 2].Rule)
                        {
                            Utils.Util.ExecuteCommand(String.Format("ROUTE DELETE {0}", rule.Key));
                        }
                    }

                    Thread.Sleep(2000);
                    UsedBandwidth = 0;
                    Status        = "停止完毕";
                    v2rayProxyComboBox.Enabled = true;
                    v2rayModeComboBox.Enabled  = true;
                    ControlButton.Invoke(new MethodInvoker(() =>
                    {
                        ControlButton.Enabled = true;
                        ControlButton.Text    = "启动";
                    }));
                });
            }
        }