
8 changed files with 546 additions and 598 deletions
-
6XCoder/Program.cs
-
2XCoder/Tools/FrmMain.cs
-
2XCoder/Tools/FrmSecurity.cs
-
8XCoder/Windows/FrmMain.cs
-
849XCoder/XApi/FrmMain.cs
-
46XCoder/XNet/BenchHelper.cs
-
213XCoder/XNet/FrmIp.cs
-
18XCoder/XNet/FrmMain.cs
@ -1,38 +1,28 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using NewLife.Data; |
|||
using NewLife.Data; |
|||
using NewLife.Net; |
|||
using NewLife.Threading; |
|||
#if !NET4
|
|||
using TaskEx = System.Threading.Tasks.Task; |
|||
#endif
|
|||
|
|||
namespace XCoder.XNet |
|||
namespace XCoder.XNet; |
|||
|
|||
static class BenchHelper |
|||
{ |
|||
static class BenchHelper |
|||
/// <summary>异步多次发送数据</summary>
|
|||
/// <param name="session">会话</param>
|
|||
/// <param name="pk">数据包</param>
|
|||
/// <param name="times">次数</param>
|
|||
/// <param name="msInterval">间隔</param>
|
|||
/// <returns></returns>
|
|||
public static Task SendConcurrency(this ISocketRemote session, Packet pk, Int32 times, Int32 msInterval) |
|||
{ |
|||
/// <summary>异步多次发送数据</summary>
|
|||
/// <param name="session">会话</param>
|
|||
/// <param name="pk">数据包</param>
|
|||
/// <param name="times">次数</param>
|
|||
/// <param name="msInterval">间隔</param>
|
|||
/// <returns></returns>
|
|||
public static Task SendConcurrency(this ISocketRemote session, Packet pk, Int32 times, Int32 msInterval) |
|||
var task = Task.Run(async () => |
|||
{ |
|||
var task = TaskEx.Run(async () => |
|||
for (var i = 0; i < times; i++) |
|||
{ |
|||
for (var i = 0; i < times; i++) |
|||
{ |
|||
session.Send(pk); |
|||
session.Send(pk); |
|||
|
|||
await TaskEx.Delay(msInterval); |
|||
} |
|||
}); |
|||
await Task.Delay(msInterval); |
|||
} |
|||
}); |
|||
|
|||
return task; |
|||
} |
|||
return task; |
|||
} |
|||
} |
@ -1,150 +1,143 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel; |
|||
using System.Linq; |
|||
using System.ComponentModel; |
|||
using System.Net; |
|||
using System.Net.NetworkInformation; |
|||
using System.Windows.Forms; |
|||
using NewLife; |
|||
using NewLife.Log; |
|||
using XCoder; |
|||
#if !NET4
|
|||
#endif
|
|||
|
|||
namespace XNet |
|||
namespace XNet; |
|||
|
|||
[Category("网络通信")] |
|||
[DisplayName("IP设置")] |
|||
public partial class FrmIp : Form, IXForm |
|||
{ |
|||
[Category("网络通信")] |
|||
[DisplayName("IP设置")] |
|||
public partial class FrmIp : Form, IXForm |
|||
#region 窗体
|
|||
public FrmIp() |
|||
{ |
|||
#region 窗体
|
|||
public FrmIp() |
|||
{ |
|||
InitializeComponent(); |
|||
InitializeComponent(); |
|||
|
|||
// 动态调节宽度高度,兼容高DPI
|
|||
this.FixDpi(); |
|||
// 动态调节宽度高度,兼容高DPI
|
|||
this.FixDpi(); |
|||
|
|||
//Icon = IcoHelper.GetIcon("IP");
|
|||
} |
|||
//Icon = IcoHelper.GetIcon("IP");
|
|||
} |
|||
|
|||
private void FrmMain_Load(Object sender, EventArgs e) |
|||
{ |
|||
var ns = NetworkInterface.GetAllNetworkInterfaces(); |
|||
cbAdapter.Tag = ns; |
|||
cbAdapter.DataSource = ns.Select(e => e.Name).ToArray(); |
|||
} |
|||
#endregion
|
|||
private void FrmMain_Load(Object sender, EventArgs e) |
|||
{ |
|||
var ns = NetworkInterface.GetAllNetworkInterfaces(); |
|||
cbAdapter.Tag = ns; |
|||
cbAdapter.DataSource = ns.Select(e => e.Name).ToArray(); |
|||
} |
|||
#endregion
|
|||
|
|||
private void cbMode_SelectedIndexChanged(Object sender, EventArgs e) |
|||
{ |
|||
var name = cbAdapter.SelectedItem + ""; |
|||
if (name.IsNullOrEmpty()) return; |
|||
private void cbMode_SelectedIndexChanged(Object sender, EventArgs e) |
|||
{ |
|||
var name = cbAdapter.SelectedItem + ""; |
|||
if (name.IsNullOrEmpty()) return; |
|||
|
|||
var ns = cbAdapter.Tag as NetworkInterface[]; |
|||
var ni = ns.FirstOrDefault(e => e.Name == name); |
|||
if (ni == null) return; |
|||
var ns = cbAdapter.Tag as NetworkInterface[]; |
|||
var ni = ns.FirstOrDefault(e => e.Name == name); |
|||
if (ni == null) return; |
|||
|
|||
gbInfo.Tag = ni; |
|||
gbInfo.Text = ni.Description; |
|||
gbInfo.Tag = ni; |
|||
gbInfo.Text = ni.Description; |
|||
|
|||
var ps = ni.GetIPProperties(); |
|||
var ips = ps.UnicastAddresses.Where(e => e.Address.IsIPv4()).ToArray(); |
|||
if (ips.Length == 0) return; |
|||
var ps = ni.GetIPProperties(); |
|||
var ips = ps.UnicastAddresses.Where(e => e.Address.IsIPv4()).ToArray(); |
|||
if (ips.Length == 0) return; |
|||
|
|||
txtIp.Text = ips[0].Address + ""; |
|||
txtSubMark.Text = ips[0].IPv4Mask + ""; |
|||
txtGateway.Text = ps.GatewayAddresses.Where(e => e.Address.IsIPv4()).Join(",", e => e.Address); |
|||
txtDns.Text = ps.DnsAddresses.Where(e => e.IsIPv4()).Join(); |
|||
txtIp.Text = ips[0].Address + ""; |
|||
txtSubMark.Text = ips[0].IPv4Mask + ""; |
|||
txtGateway.Text = ps.GatewayAddresses.Where(e => e.Address.IsIPv4()).Join(",", e => e.Address); |
|||
txtDns.Text = ps.DnsAddresses.Where(e => e.IsIPv4()).Join(); |
|||
|
|||
var ips2 = ips.Skip(1).OrderBy(e => e.Address.GetAddressBytes().ToLong()).Select(e => e.Address).ToArray(); |
|||
txtIp2.Text = ips2.Join("\r\n"); |
|||
} |
|||
var ips2 = ips.Skip(1).OrderBy(e => e.Address.GetAddressBytes().ToLong()).Select(e => e.Address).ToArray(); |
|||
txtIp2.Text = ips2.Join("\r\n"); |
|||
} |
|||
|
|||
private void btnSet_Click(Object sender, EventArgs e) |
|||
{ |
|||
var ni = gbInfo.Tag as NetworkInterface; |
|||
if (ni == null) return; |
|||
private void btnSet_Click(Object sender, EventArgs e) |
|||
{ |
|||
var ni = gbInfo.Tag as NetworkInterface; |
|||
if (ni == null) return; |
|||
|
|||
var ip = txtIp.Text?.Trim(); |
|||
var mark = txtSubMark.Text?.Trim(); |
|||
var gateway = txtGateway.Text?.Trim(); |
|||
if (ip.IsNullOrEmpty() || mark.IsNullOrEmpty()) return; |
|||
var ip = txtIp.Text?.Trim(); |
|||
var mark = txtSubMark.Text?.Trim(); |
|||
var gateway = txtGateway.Text?.Trim(); |
|||
if (ip.IsNullOrEmpty() || mark.IsNullOrEmpty()) return; |
|||
|
|||
// 设置主IP
|
|||
var args = $"interface ip add address name=\"{ni.Name}\" {ip} {mark} {gateway}"; |
|||
var rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
// 设置主IP
|
|||
var args = $"interface ip add address name=\"{ni.Name}\" {ip} {mark} {gateway}"; |
|||
var rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
|
|||
// 设置DNS
|
|||
var dns = txtDns.Text.Split(","); |
|||
if (dns.Length > 0) |
|||
{ |
|||
args = $"interface ip set dns name=\"{ni.Name}\" source=static addr={dns[0]} register=primary"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
if (dns.Length > 1) |
|||
{ |
|||
args = $"interface ip add dnsservers name=\"{ni.Name}\" addr={dns[1]} index=2"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
} |
|||
} |
|||
else |
|||
// 设置DNS
|
|||
var dns = txtDns.Text.Split(","); |
|||
if (dns.Length > 0) |
|||
{ |
|||
args = $"interface ip set dns name=\"{ni.Name}\" source=static addr={dns[0]} register=primary"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
if (dns.Length > 1) |
|||
{ |
|||
args = $"interface ip set dns name=\"{ni.Name}\" source=dhcp"; |
|||
args = $"interface ip add dnsservers name=\"{ni.Name}\" addr={dns[1]} index=2"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
args = $"interface ip set dns name=\"{ni.Name}\" source=dhcp"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
} |
|||
|
|||
// 解析私有IP,特殊格式如 10.0.0.30-50
|
|||
var ips = txtIp2.Text.Split("\r", "\n", "\t", ",", " ").ToList(); |
|||
// 倒序,要拆分插入末尾
|
|||
for (var i = ips.Count - 1; i >= 0; i--) |
|||
// 解析私有IP,特殊格式如 10.0.0.30-50
|
|||
var ips = txtIp2.Text.Split("\r", "\n", "\t", ",", " ").ToList(); |
|||
// 倒序,要拆分插入末尾
|
|||
for (var i = ips.Count - 1; i >= 0; i--) |
|||
{ |
|||
ip = ips[i]; |
|||
var p = ip.LastIndexOf('-'); |
|||
if (p > 0) |
|||
{ |
|||
ip = ips[i]; |
|||
var p = ip.LastIndexOf('-'); |
|||
if (p > 0) |
|||
var p2 = ip.LastIndexOf('.'); |
|||
if (p2 > 0) |
|||
{ |
|||
var p2 = ip.LastIndexOf('.'); |
|||
if (p2 > 0) |
|||
// 删掉这一行,因为要拆分为多行
|
|||
ips.RemoveAt(i); |
|||
|
|||
// 解析前缀、开始、结束
|
|||
var prefix = ip.Substring(0, p2 + 1); |
|||
var start = ip.Substring(p2 + 1, p - p2 - 1).ToInt(); |
|||
var end = ip.Substring(p + 1).ToInt(); |
|||
for (var k = start; k <= end; k++) |
|||
{ |
|||
// 删掉这一行,因为要拆分为多行
|
|||
ips.RemoveAt(i); |
|||
|
|||
// 解析前缀、开始、结束
|
|||
var prefix = ip.Substring(0, p2 + 1); |
|||
var start = ip.Substring(p2 + 1, p - p2 - 1).ToInt(); |
|||
var end = ip.Substring(p + 1).ToInt(); |
|||
for (var k = start; k <= end; k++) |
|||
{ |
|||
ips.Add($"{prefix}{k}"); |
|||
} |
|||
ips.Add($"{prefix}{k}"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 设置私有IP,排序
|
|||
var addrs = ips.Select(e => IPAddress.Parse(e)).OrderBy(e => e.GetAddressBytes().ToLong()).ToArray(); |
|||
foreach (var item in addrs) |
|||
{ |
|||
args = $"interface ip add address name=\"{ni.Name}\" {item} {mark} {gateway}"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
} |
|||
|
|||
MessageBox.Show($"执行成功!返回 {rs}"); |
|||
} |
|||
|
|||
private void btnRestore_Click(Object sender, EventArgs e) |
|||
// 设置私有IP,排序
|
|||
var addrs = ips.Select(e => IPAddress.Parse(e)).OrderBy(e => e.GetAddressBytes().ToLong()).ToArray(); |
|||
foreach (var item in addrs) |
|||
{ |
|||
var ni = gbInfo.Tag as NetworkInterface; |
|||
if (ni == null) return; |
|||
args = $"interface ip add address name=\"{ni.Name}\" {item} {mark} {gateway}"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
} |
|||
|
|||
var args = $"interface ip set address name=\"{ni.Name}\" source=dhcp"; |
|||
var rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
MessageBox.Show($"执行成功!返回 {rs}"); |
|||
} |
|||
|
|||
args = $"interface ip set dns name=\"{ni.Name}\" source=dhcp"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
private void btnRestore_Click(Object sender, EventArgs e) |
|||
{ |
|||
var ni = gbInfo.Tag as NetworkInterface; |
|||
if (ni == null) return; |
|||
|
|||
rs = "ipconfig".Run("/renew"); |
|||
var args = $"interface ip set address name=\"{ni.Name}\" source=dhcp"; |
|||
var rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
|
|||
MessageBox.Show($"执行成功!返回 {rs}"); |
|||
} |
|||
args = $"interface ip set dns name=\"{ni.Name}\" source=dhcp"; |
|||
rs = "netsh".Run(args, 5_000, s => XTrace.WriteLine(s)); |
|||
|
|||
rs = "ipconfig".Run("/renew"); |
|||
|
|||
MessageBox.Show($"执行成功!返回 {rs}"); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue