using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace NetTunnelApp; public partial class MainForm : Form { private List _curNetCard = new List(); private List _curFilePaths = new List(); private UserClientCfg _userConfig = new UserClientCfg(); private string _wireguardPath = string.Empty; public void GetCurrentWgCfgs(string username) { IntPtr pt = new IntPtr(); //Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FilePath)) * 32); int size = 0; _curFilePaths.Clear(); var v = NetTunnelLib.GetUserConfigFiles(username, out pt, ref size); for (int i = 0; i < size; i++) { IntPtr ptr = new IntPtr(pt.ToInt64() + Marshal.SizeOf(typeof(FilePath)) * i); FilePath path = (FilePath)Marshal.PtrToStructure(ptr, typeof(FilePath)); //Console.WriteLine("{0}: {1} status {2}", i, path.CfgPath, path.curUsed); _curFilePaths.Add(path); } Marshal.FreeCoTaskMem(pt); } public void GetCurrentNetCard() { IntPtr pt = new IntPtr(); //Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetCard)) * 32); int size = 0; var v = NetTunnelLib.GetAllNICInfo(out pt, ref size); _curNetCard.Clear(); if (v == 0) { for (int i = 0; i < size; i++) { IntPtr ptr = new IntPtr(pt.ToInt64() + Marshal.SizeOf(typeof(NetCard)) * i); NetCard card = (NetCard)Marshal.PtrToStructure(ptr, typeof(NetCard)); _curNetCard.Add(card); } } Marshal.FreeCoTaskMem(pt); } public MainForm() { InitializeComponent(); var path = Environment.CurrentDirectory + "\\tunnelsdk_" + string.Format("{0:yyyyMMdd}", DateTime.Now) + ".log"; NetTunnelLib.InitTunnelSDKLog(path, LogLevel.LOG_DEBUG); NetTunnelLib.TunnelSDKInitEnv(Environment.CurrentDirectory, "http://localhost:9276", menuSetModeServer.Checked); GetCurrentNetCard(); NetCardMenuItems menuItems = new NetCardMenuItems(_curNetCard); NetCardMenuItems ctxMenu = new NetCardMenuItems(_curNetCard); foreach (var item in menuItems.GetMenuItems()) { menuSetModeShareNet.DropDownItems.Add(item); } foreach (var item in ctxMenu.GetMenuItems()) { trapMenuSysInterface.DropDownItems.Add(item); } } private void test_get_network_info() { IntPtr pt = new IntPtr(); //Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetCard)) * 32); int size = 0; var v = NetTunnelLib.GetAllNICInfo(out pt, ref size); Console.WriteLine("Result {0}, total {1}", v, size); for (int i = 0; i < size; i++) { IntPtr ptr = new IntPtr(pt.ToInt64() + Marshal.SizeOf(typeof(NetCard)) * i); NetCard card = (NetCard)Marshal.PtrToStructure(ptr, typeof(NetCard)); Console.WriteLine("UUID:{0}", card.UUID); Console.WriteLine("Name:{0}", card.Name); Console.WriteLine("Description:{0}", card.Description); Console.WriteLine("Index:{0}", card.IfIndex); Console.WriteLine("Ip:{0}", card.IpAddr); Console.WriteLine("NetMask:{0}", card.NetMask); Console.WriteLine("MacAddr:{0}", card.MacAddr); } Marshal.FreeCoTaskMem(pt); } private void test_create_wg_key() { var buffer = new StringBuilder(64 + 1); var buffer2 = new StringBuilder(64 + 1); var v = NetTunnelLib.GenerateWireguardKeyPairs(buffer, 64, buffer2, 64); lstLogs.Items.Add(v.ToString() + ": [" + buffer.ToString() + "] / [" + buffer2.ToString() + "]"); } private void test_create_wg_svr_cfg() { WgSvrConfig wsg = new WgSvrConfig(); wsg.Name = "wg_svr"; wsg.Address = "10.10.10.250/24"; wsg.ListenPort = 10000; wsg.PrivateKey = "EOHYEXSK4xw0LlB4I3ZlF5UqSaoma3Ea2KPhLfIdA14="; wsg.CliPubKey = "6BWnmkCJqJC5iNoCEZWTxwGNG7qwkxFoVgAk4DoIKCk="; wsg.AllowNet = "10.10.10.5/32"; var v = NetTunnelLib.WireGuardCreateServerConfig(wsg); Console.WriteLine("Create WireGuard Server Configure File: {0}", v); } public void test_create_wg_cli_cfg() { WgCliConfig cli = new WgCliConfig(); cli.Name = "wg_cli"; cli.Address = "10.10.10.1/32"; cli.PrivateKey = "WGAlqvys3O83VmWug6Z8NzUrxGr/PNHSeOVFnKLSe2k="; cli.SvrPubKey = "8KEaqtWM5U35SR8S3QJriGHPr0VIqvk8A7BEuOjjp1M="; cli.AllowNet = "10.10.10.0/24, 10.0.4.16/30"; cli.ServerURL = "101.35.234.160:10010"; var v = NetTunnelLib.WireGuardCreateClientConfig(cli); Console.WriteLine("Create WireGuard Client Configure File: {0}", v); } private void test_uninstall_wg_server() { var v = NetTunnelLib.WireGuardInstallServerService(false); Console.WriteLine("Uninstall Result: {0}", v); } private void test_install_wg_server() { var v = NetTunnelLib.WireGuardInstallServerService(true); Console.WriteLine("Install Result: {0}", v); } private void button1_Click(object sender, EventArgs e) { //var buffer = new StringBuilder(256 + 1); //NetTunnelLib.TunnelSDKInitEnv(); //var v = NetTunnelLib.FindWireguardExe(buffer, 256); //MessageBox.Show(buffer.ToString() + "----" + v.ToString()); //test_install_wg_server(); } private void button2_Click(object sender, EventArgs e) { //test_uninstall_wg_server(); //test_get_network_info(); //IntPtr pt = new IntPtr(); //int size = 0; //var v = NetTunnelLib.GetUserConfigure2("admin", out pt, ref size); //for (int i = 0; i < size; i++) //{ // IntPtr ptr = new IntPtr(pt.ToInt64() + Marshal.SizeOf(typeof(FilePath)) * i); // FilePath path = (FilePath)Marshal.PtrToStructure(ptr, typeof(FilePath)); // Console.WriteLine("{0}: {1} status {2}", i, path.CfgPath, path.curUsed); // _curFilePaths.Add(path); //} //MessageBox.Show(v.ToString()); //Marshal.FreeCoTaskMem(pt); } private void menuControlRefInterface_Click(object sender, EventArgs e) { GetCurrentNetCard(); NetCardMenuItems menuItems = new NetCardMenuItems(_curNetCard); NetCardMenuItems ctxMenu = new NetCardMenuItems(_curNetCard); foreach (var item in menuItems.GetMenuItems()) { menuSetModeShareNet.DropDownItems.Add(item); } foreach (var item in ctxMenu.GetMenuItems()) { trapMenuSysInterface.DropDownItems.Add(item); } } private void menuSetVPNPath_Click(object sender, EventArgs e) { DialogResult result = ofDlgWireGuard.ShowDialog(); if (result == DialogResult.OK) { _wireguardPath = ofDlgWireGuard.FileName; NetTunnelLib.SetWireguardPath(_wireguardPath); } } private void menuSetModeClient_Click(object sender, EventArgs e) { var o = (ToolStripMenuItem)sender; if (o.Checked) { o.Checked = false; menuSetModeServer.Checked = true; } else { o.Checked = true; menuSetModeServer.Checked = false; } } private void menuSetModeServer_Click(object sender, EventArgs e) { var o = (ToolStripMenuItem)sender; if (o.Checked) { o.Checked = false; menuSetModeClient.Checked = true; } else { o.Checked = true; menuSetModeClient.Checked = false; } } private void MainForm_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { this.notifyIconMain.Visible = true; this.Hide(); this.ShowInTaskbar = false; } else { this.notifyIconMain.Visible = false; this.ShowInTaskbar = true; } } private void menuFileLogin_Click(object sender, EventArgs e) { LoginForm f = new LoginForm(); f.onLoginServer += (o, args) => { IntPtr pt = new IntPtr(); if (NetTunnelLib.UserLogin(args.HashArgs["UserName"] as string, args.HashArgs["Password"] as string, out pt) == 0) { IntPtr p = new IntPtr(pt.ToInt64()); UserClientConfig cfg = (UserClientConfig)Marshal.PtrToStructure(p, typeof(UserClientConfig)); //Console.WriteLine("scgCtrlAppId:{0}", cfg.scgCtrlAppId); //Console.WriteLine("scgTunnelAppId:{0}", cfg.scgTunnelAppId); //Console.WriteLine("ClientPublicKey:{0}", cfg.cliPrivateKey); //Console.WriteLine("ClientAddress:{0}", cfg.cliAddress); _userConfig.scgCtrlAppId = cfg.scgCtrlAppId; _userConfig.scgTunnelAppId = cfg.scgTunnelAppId; _userConfig.cliPrivateKey = cfg.cliPrivateKey; _userConfig.cliPrivateKey = cfg.cliAddress; _userConfig.vmList.Clear(); for (int i = 0; i < cfg.tolVM; i++) { IntPtr ptr = new IntPtr(cfg.pVMConfig.ToInt64() + Marshal.SizeOf(typeof(VirtualMathineConfig)) * i); VirtualMathineConfig vm = (VirtualMathineConfig)Marshal.PtrToStructure(ptr, typeof(VirtualMathineConfig)); //Console.WriteLine("[{1}]:id: {0}", vm.vmId, i); //Console.WriteLine("[{1}]:Name:{0}", vm.vmName, i); //Console.WriteLine("[{1}]:ServerPublicKey:{0}", vm.svrPublicKey, i); //Console.WriteLine("[{1}]:Network:{0}", vm.vmNetwork, i); //Console.WriteLine("[{1}]:EndPoint:{0}", vm.scgGateWay, i); _userConfig.vmList.Add(vm); } Marshal.FreeCoTaskMem(cfg.pVMConfig); Marshal.FreeCoTaskMem(pt); GetCurrentWgCfgs(args.HashArgs["UserName"] as string); WgCfgMenuItems wgMenu = new WgCfgMenuItems(_curFilePaths); WgCfgMenuItems wgPopMenu = new WgCfgMenuItems(_curFilePaths); ServerMenuItems svrMenu = new ServerMenuItems(_userConfig.vmList); foreach (var item in wgMenu.GetMenuItems()) { menuSetVPNCfgFiles.DropDownItems.Add(item); } foreach (var item in wgPopMenu.GetMenuItems()) { trapMenuCfgFiles.DropDownItems.Add(item); } foreach (var item in svrMenu.GetMenuItems()) { menuSetModeShareServer.DropDownItems.Add(item); } } else { //this.DialogResult = DialogResult.Cancel; MessageBox.Show("Login Error"); } }; f.ShowDialog(); } private void trapMenuShowMainWnd_Click(object sender, EventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); } private void trapMenuHideMain_Click(object sender, EventArgs e) { this.Hide(); } private void trapMenuExit_Click(object sender, EventArgs e) { System.Environment.Exit(0); } }