NetTunnelWindows/NetTunnelApp/MainForm.cs

120 lines
3.7 KiB
C#

using System;
using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
namespace NetTunnelApp;
public partial class MainForm : Form
{
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);
}
private void test_get_network_info()
{
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetCard)) * 32);
int size = 0;
var v = NetTunnelLib.GetAllNICInfo(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("Name:{0}", card.Name);
Console.WriteLine("Description:{0}", card.Description);
Console.WriteLine("Ip:{0}", card.IpAddr);
Console.WriteLine("NetMask:{0}", card.NetMask);
Console.WriteLine("MacAddr:{0}", card.MacAddr);
}
Marshal.FreeHGlobal(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();
}
}