2020-05-29 03:32:44 +08:00
|
|
|
|
using PortProxyGUI._extern.NStandard;
|
|
|
|
|
using System;
|
2020-09-24 18:06:57 +08:00
|
|
|
|
using System.Linq;
|
2020-05-29 03:32:44 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace PortProxyGUI
|
|
|
|
|
{
|
|
|
|
|
public partial class NewProxy : Form
|
|
|
|
|
{
|
|
|
|
|
public readonly PortProxyGUI PortProxyGUI;
|
2020-11-23 00:45:06 +08:00
|
|
|
|
private string AutoString { get; }
|
2020-05-29 03:32:44 +08:00
|
|
|
|
|
|
|
|
|
public NewProxy(PortProxyGUI portProxyGUI)
|
|
|
|
|
{
|
|
|
|
|
PortProxyGUI = portProxyGUI;
|
|
|
|
|
InitializeComponent();
|
2020-11-23 00:45:06 +08:00
|
|
|
|
AutoString = comboBox_type.Text = comboBox_type.Items.OfType<string>().First();
|
2020-05-29 03:32:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddPortProxy(string type, string listenOn, string listenPort, string connectTo, string connectPort)
|
|
|
|
|
{
|
|
|
|
|
var output = CmdRunner.Execute($"netsh interface portproxy add {type} listenaddress={listenOn} listenport={listenPort} connectaddress={connectTo} connectport={connectPort}");
|
|
|
|
|
Invoke((Action)(() => PortProxyGUI.RefreshProxyList()));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 18:06:57 +08:00
|
|
|
|
private bool IsIPv4(string ip)
|
|
|
|
|
{
|
2020-11-23 00:45:06 +08:00
|
|
|
|
return ip.IsMatch(new Regex(@"^(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])){3}$"));
|
2020-09-24 18:06:57 +08:00
|
|
|
|
}
|
|
|
|
|
private bool IsIPv6(string ip)
|
|
|
|
|
{
|
2020-11-23 00:45:06 +08:00
|
|
|
|
return ip.IsMatch(new Regex(@"^[\dABCDEF]{2}(?::(?:[\dABCDEF]{2})){5}$"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetPassType(string listenOn, string connectTo)
|
|
|
|
|
{
|
|
|
|
|
var from = IsIPv6(listenOn) ? "v6" : "v4";
|
|
|
|
|
var to = IsIPv6(connectTo) ? "v6" : "v4";
|
|
|
|
|
return $"{from}to{to}";
|
2020-09-24 18:06:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 03:32:44 +08:00
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-09-24 18:06:57 +08:00
|
|
|
|
var type = comboBox_type.Text.Trim();
|
|
|
|
|
var listenOn = textBox_listenOn.Text.Trim().ToLower();
|
|
|
|
|
var connectTo = textBox_connectTo.Text.Trim().ToLower();
|
|
|
|
|
var listenPort = textBox_listenPort.Text.Trim();
|
|
|
|
|
var connectPort = textBox_connectPort.Text.Trim();
|
2020-05-29 03:32:44 +08:00
|
|
|
|
|
|
|
|
|
if (!int.TryParse(listenPort, out var _listenPort) || _listenPort < 0 || _listenPort > 65535)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show($"The listen port is invalid.", "Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!int.TryParse(connectPort, out var _connectPort) || _connectPort < 0 || _connectPort > 65535)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show($"The connect port is invalid.", "Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-23 00:45:06 +08:00
|
|
|
|
if (type == AutoString) type = GetPassType(listenOn, connectTo);
|
|
|
|
|
|
|
|
|
|
if (!new[] { "v4tov4", "v4tov6", "v6tov4", "v6tov6" }.Contains(type))
|
2020-05-29 03:32:44 +08:00
|
|
|
|
{
|
2020-11-23 00:45:06 +08:00
|
|
|
|
MessageBox.Show($"Unknow type for ({listenOn} -> {connectTo}).", "Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
2020-09-24 18:06:57 +08:00
|
|
|
|
return;
|
2020-05-29 03:32:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddPortProxy(type, listenOn, listenPort, connectTo, connectPort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NewProxy_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NewProxy_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PortProxyGUI.NewProxyForm = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|