Compare commits

..

7 Commits
1.6.0 ... 1.6.1

Author SHA1 Message Date
ChsBuffer
3efbad5e5e bump version to 1.6.1 2020-10-07 21:18:37 +08:00
ChsBuffer
020c2d7e67 refactor: merge ServerForm's parital declarations 2020-10-07 21:11:39 +08:00
ChsBuffer
adcc6a75c9 fix: MainForm Text reload got exception when increase profile count #389 2020-10-07 20:54:00 +08:00
ChsBuffer
7e907eef6f fix: ServerForm High DPI Scale #388 2020-10-07 20:37:23 +08:00
ChsBuffer
d213872bde refact: rename TUNTAPController Gateway to Outbound 2020-10-07 19:26:30 +08:00
ChsBuffer
71687c1da6 Revert "Use GetLocalEndPoint to replace GetBestRoute"
This reverts commit 801e8e3b8a.
2020-10-07 19:26:25 +08:00
ChsBuffer
2eb6b23ca7 fix: trojan form 2020-10-07 10:07:55 +08:00
9 changed files with 522 additions and 5178 deletions

View File

@@ -195,13 +195,13 @@ namespace Netch.Controllers
RouteAction(Action.Create, IPNetwork.Parse("0.0.0.0", 0), RouteType.TUNTAP);
Logging.Info("移除 → 出口网卡路由");
RouteAction(Action.Delete, IPNetwork.Parse("0.0.0.0", 0), RouteType.Gateway);
RouteAction(Action.Delete, IPNetwork.Parse("0.0.0.0", 0), RouteType.Outbound);
break;
}
Logging.Info("设置路由规则");
RouteAction(Action.Create, _directIPs, RouteType.Gateway);
RouteAction(Action.Create, _directIPs, RouteType.Outbound);
RouteAction(Action.Create, _proxyIPs, RouteType.TUNTAP);
}
@@ -217,11 +217,11 @@ namespace Netch.Controllers
break;
case 2:
RouteAction(Action.Delete, IPNetwork.Parse("0.0.0.0", 0), RouteType.TUNTAP);
RouteAction(Action.Create, IPNetwork.Parse("0.0.0.0", 0), RouteType.Gateway);
RouteAction(Action.Create, IPNetwork.Parse("0.0.0.0", 0), RouteType.Outbound);
break;
}
RouteAction(Action.Delete, _directIPs, RouteType.Gateway);
RouteAction(Action.Delete, _directIPs, RouteType.Outbound);
RouteAction(Action.Delete, _proxyIPs, RouteType.TUNTAP);
_directIPs.Clear();
_proxyIPs.Clear();
@@ -321,7 +321,7 @@ namespace Netch.Controllers
private enum RouteType
{
Gateway,
Outbound,
TUNTAP
}
@@ -346,7 +346,7 @@ namespace Netch.Controllers
int index;
switch (routeType)
{
case RouteType.Gateway:
case RouteType.Outbound:
gateway = Global.Outbound.Gateway.ToString();
index = Global.Outbound.Index;
break;
@@ -369,7 +369,7 @@ namespace Netch.Controllers
if (!result)
{
Logging.Warning($"Failed {action} Route on {routeType} Adapter: {ipNetwork} metric {metric}");
Logging.Warning($"Failed to {action} Route on {routeType} Adapter: {ipNetwork} metric {metric}");
}
return result;

View File

@@ -15,7 +15,7 @@ namespace Netch.Controllers
public const string Name = @"Netch";
public const string Copyright = @"Copyright © 2019 - 2020";
public const string AssemblyVersion = @"1.6.0";
public const string AssemblyVersion = @"1.6.1";
private const string Suffix = @"";
public static readonly string Version = $"{AssemblyVersion}{(string.IsNullOrEmpty(Suffix) ? "" : $"-{Suffix}")}";

View File

@@ -234,11 +234,12 @@ namespace Netch.Forms
case ListControl _:
break;
case Control c:
c.Text = ControlText(c.Name);
if (_mainFormText.ContainsKey(c.Name))
c.Text = ControlText(c.Name);
break;
case ToolStripItem c:
c.Text = ControlText(c.Name);
if (_mainFormText.ContainsKey(c.Name))
c.Text = ControlText(c.Name);
break;
}

View File

@@ -1,144 +0,0 @@
namespace Netch.Forms
{
partial class ServerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServerForm));
this.ConfigurationGroupBox = new System.Windows.Forms.GroupBox();
this.AddressLabel = new System.Windows.Forms.Label();
this.PortTextBox = new System.Windows.Forms.TextBox();
this.AddressTextBox = new System.Windows.Forms.TextBox();
this.RemarkTextBox = new System.Windows.Forms.TextBox();
this.RemarkLabel = new System.Windows.Forms.Label();
this.PortLabel = new System.Windows.Forms.Label();
this.ConfigurationGroupBox.SuspendLayout();
this.SuspendLayout();
//
// ConfigurationGroupBox
//
this.ConfigurationGroupBox.AutoSize = true;
this.ConfigurationGroupBox.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ConfigurationGroupBox.Controls.Add(this.AddressLabel);
this.ConfigurationGroupBox.Controls.Add(this.PortTextBox);
this.ConfigurationGroupBox.Controls.Add(this.AddressTextBox);
this.ConfigurationGroupBox.Controls.Add(this.RemarkTextBox);
this.ConfigurationGroupBox.Controls.Add(this.RemarkLabel);
this.ConfigurationGroupBox.Controls.Add(this.PortLabel);
this.ConfigurationGroupBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.ConfigurationGroupBox.Location = new System.Drawing.Point(5, 5);
this.ConfigurationGroupBox.Name = "ConfigurationGroupBox";
this.ConfigurationGroupBox.Size = new System.Drawing.Size(434, 127);
this.ConfigurationGroupBox.TabIndex = 0;
this.ConfigurationGroupBox.TabStop = false;
this.ConfigurationGroupBox.Text = "Configuration";
//
// AddressLabel
//
this.AddressLabel.AutoSize = true;
this.AddressLabel.Location = new System.Drawing.Point(10, ControlLineHeight*2);
this.AddressLabel.Name = "AddressLabel";
this.AddressLabel.Size = new System.Drawing.Size(56, 17);
this.AddressLabel.TabIndex = 2;
this.AddressLabel.Text = "Address";
//
// PortTextBox
//
this.PortTextBox.Location = new System.Drawing.Point(358, ControlLineHeight*2);
this.PortTextBox.Name = "PortTextBox";
this.PortTextBox.Size = new System.Drawing.Size(56, 23);
this.PortTextBox.TabIndex = 5;
this.PortTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// AddressTextBox
//
this.AddressTextBox.Location = new System.Drawing.Point(120, ControlLineHeight*2);
this.AddressTextBox.Name = "AddressTextBox";
this.AddressTextBox.Size = new System.Drawing.Size(232, 23);
this.AddressTextBox.TabIndex = 3;
this.AddressTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// RemarkTextBox
//
this.RemarkTextBox.Location = new System.Drawing.Point(120, ControlLineHeight);
this.RemarkTextBox.Name = "RemarkTextBox";
this.RemarkTextBox.Size = new System.Drawing.Size(294, 23);
this.RemarkTextBox.TabIndex = 1;
this.RemarkTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// RemarkLabel
//
this.RemarkLabel.AutoSize = true;
this.RemarkLabel.Location = new System.Drawing.Point(10, ControlLineHeight);
this.RemarkLabel.Name = "RemarkLabel";
this.RemarkLabel.Size = new System.Drawing.Size(53, 17);
this.RemarkLabel.TabIndex = 0;
this.RemarkLabel.Text = "Remark";
//
// PortLabel
//
this.PortLabel.AutoSize = true;
this.PortLabel.Location = new System.Drawing.Point(351, ControlLineHeight*2);
this.PortLabel.Name = "PortLabel";
this.PortLabel.Size = new System.Drawing.Size(11, 17);
this.PortLabel.TabIndex = 4;
this.PortLabel.Text = ":";
//
// ServerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(444, 137);
this.Controls.Add(this.ConfigurationGroupBox);
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon) (resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.MaximizeBox = false;
this.Name = "ServerForm";
this.Padding = new System.Windows.Forms.Padding(11, 5, 11, 4);
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Load += new System.EventHandler(this.ServerForm_Load);
this.ConfigurationGroupBox.ResumeLayout(false);
this.ConfigurationGroupBox.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox ConfigurationGroupBox;
private System.Windows.Forms.Label RemarkLabel;
protected System.Windows.Forms.TextBox RemarkTextBox;
private System.Windows.Forms.Label PortLabel;
protected System.Windows.Forms.TextBox AddressTextBox;
private System.Windows.Forms.TextBox PortTextBox;
private System.Windows.Forms.Label AddressLabel;
}
}

View File

@@ -1,14 +1,16 @@
using System;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Netch.Models;
using Netch.Properties;
using Netch.Utils;
namespace Netch.Forms
{
public abstract partial class ServerForm : Form
public abstract class ServerForm : Form
{
protected abstract string TypeName { get; }
protected Server Server { get; set; }
@@ -32,6 +34,35 @@ namespace Netch.Forms
_saveActions.Add(PortTextBox, s => Server.Port = ushort.Parse((string) s));
}
public new void ShowDialog()
{
AfterFactor();
base.ShowDialog();
}
public new void Show()
{
AfterFactor();
base.Show();
}
private void AfterFactor()
{
Text = TypeName ?? string.Empty;
RemarkTextBox.Text = Server.Remark;
AddressTextBox.Text = Server.Hostname;
PortTextBox.Text = Server.Port.ToString();
AddSaveButton();
i18N.TranslateForm(this);
ConfigurationGroupBox.ResumeLayout(false);
ConfigurationGroupBox.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
protected void CreateTextBox(string name, string remark, Func<string, bool> check, Action<string> save, string value, int width = InputBoxWidth)
{
_controlLines++;
@@ -120,18 +151,6 @@ namespace Netch.Forms
private readonly Dictionary<Control, Action<object>> _saveActions = new Dictionary<Control, Action<object>>();
private void ServerForm_Load(object sender, EventArgs e)
{
Text = TypeName ?? string.Empty;
RemarkTextBox.Text = Server.Remark;
AddressTextBox.Text = Server.Hostname;
PortTextBox.Text = Server.Port.ToString();
AddSaveButton();
i18N.TranslateForm(this);
}
private void AddSaveButton()
{
_controlLines++;
@@ -174,5 +193,124 @@ namespace Netch.Forms
Close();
}
private IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing)
{
components?.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
ConfigurationGroupBox = new GroupBox();
AddressLabel = new Label();
PortTextBox = new TextBox();
AddressTextBox = new TextBox();
RemarkTextBox = new TextBox();
RemarkLabel = new Label();
PortLabel = new Label();
ConfigurationGroupBox.SuspendLayout();
SuspendLayout();
//
// ConfigurationGroupBox
//
ConfigurationGroupBox.AutoSize = true;
ConfigurationGroupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink;
ConfigurationGroupBox.Controls.Add(AddressLabel);
ConfigurationGroupBox.Controls.Add(PortTextBox);
ConfigurationGroupBox.Controls.Add(AddressTextBox);
ConfigurationGroupBox.Controls.Add(RemarkTextBox);
ConfigurationGroupBox.Controls.Add(RemarkLabel);
ConfigurationGroupBox.Controls.Add(PortLabel);
ConfigurationGroupBox.Dock = DockStyle.Fill;
ConfigurationGroupBox.Location = new Point(5, 5);
ConfigurationGroupBox.Name = "ConfigurationGroupBox";
ConfigurationGroupBox.Size = new Size(434, 127);
ConfigurationGroupBox.TabIndex = 0;
ConfigurationGroupBox.TabStop = false;
ConfigurationGroupBox.Text = "Configuration";
//
// AddressLabel
//
AddressLabel.AutoSize = true;
AddressLabel.Location = new Point(10, ControlLineHeight * 2);
AddressLabel.Name = "AddressLabel";
AddressLabel.Size = new Size(56, 17);
AddressLabel.TabIndex = 2;
AddressLabel.Text = "Address";
//
// PortTextBox
//
PortTextBox.Location = new Point(358, ControlLineHeight * 2);
PortTextBox.Name = "PortTextBox";
PortTextBox.Size = new Size(56, 23);
PortTextBox.TabIndex = 5;
PortTextBox.TextAlign = HorizontalAlignment.Center;
//
// AddressTextBox
//
AddressTextBox.Location = new Point(120, ControlLineHeight * 2);
AddressTextBox.Name = "AddressTextBox";
AddressTextBox.Size = new Size(232, 23);
AddressTextBox.TabIndex = 3;
AddressTextBox.TextAlign = HorizontalAlignment.Center;
//
// RemarkTextBox
//
RemarkTextBox.Location = new Point(120, ControlLineHeight);
RemarkTextBox.Name = "RemarkTextBox";
RemarkTextBox.Size = new Size(294, 23);
RemarkTextBox.TabIndex = 1;
RemarkTextBox.TextAlign = HorizontalAlignment.Center;
//
// RemarkLabel
//
RemarkLabel.AutoSize = true;
RemarkLabel.Location = new Point(10, ControlLineHeight);
RemarkLabel.Name = "RemarkLabel";
RemarkLabel.Size = new Size(53, 17);
RemarkLabel.TabIndex = 0;
RemarkLabel.Text = "Remark";
//
// PortLabel
//
PortLabel.AutoSize = true;
PortLabel.Location = new Point(351, ControlLineHeight * 2);
PortLabel.Name = "PortLabel";
PortLabel.Size = new Size(11, 17);
PortLabel.TabIndex = 4;
PortLabel.Text = ":";
//
// ServerForm
//
AutoScaleDimensions = new SizeF(96F, 96F);
AutoScaleMode = AutoScaleMode.Dpi;
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
ClientSize = new Size(444, 137);
Controls.Add(ConfigurationGroupBox);
Font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, (byte) 134);
FormBorderStyle = FormBorderStyle.FixedSingle;
Icon = Icon.FromHandle(Resources.Netch.GetHicon());
Margin = new Padding(3, 4, 3, 4);
MaximizeBox = false;
Name = "ServerForm";
Padding = new Padding(11, 5, 11, 4);
StartPosition = FormStartPosition.CenterScreen;
}
private GroupBox ConfigurationGroupBox;
private Label RemarkLabel;
protected TextBox RemarkTextBox;
private Label PortLabel;
protected TextBox AddressTextBox;
private TextBox PortTextBox;
private Label AddressLabel;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -10,6 +10,14 @@ namespace Netch.Servers.Trojan.Form
{
server ??= new Trojan();
Server = server;
CreateTextBox("Password", "Password",
s => true,
s => server.Password = s,
server.Password);
CreateTextBox("Host", "Host",
s => true,
s => server.Host = s,
server.Host);
}
}
}

View File

@@ -126,47 +126,38 @@ namespace Netch.Utils
public static string GetFileVersion(string file) => File.Exists(file) ? FileVersionInfo.GetVersionInfo(file).FileVersion : string.Empty;
public static bool SearchOutboundAdapter(bool log = true)
public static bool SearchOutboundAdapter(bool logging = true)
{
IPAddress localEnd;
try
{
localEnd = WebUtil.BestLocalEndPoint(new IPEndPoint(0x72727272, 53)).Address;
}
catch
// 寻找出口适配器
if (Win32Native.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0),
0, out var pRoute) != 0)
{
Logging.Error("GetBestRoute 搜索失败");
return false;
}
Global.Outbound.Index = pRoute.dwForwardIfIndex;
// 根据 IP Index 寻找 出口适配器
try
{
// 根据 IP 寻找 出口适配器
Global.Outbound.Adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ =>
var adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ =>
{
try
{
return _.GetIPProperties().UnicastAddresses.Any(ip =>
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork && ip.Address.ToString().Equals(localEnd.ToString()))
{
Global.Outbound.Index = _.GetIPProperties().GetIPv4Properties().Index;
return true;
}
return false;
});
return _.GetIPProperties().GetIPv4Properties().Index == Global.Outbound.Index;
}
catch
{
return false;
}
});
Global.Outbound.Gateway = Global.Outbound.Adapter.GetIPProperties().GatewayAddresses[0].Address;
if (log)
Global.Outbound.Adapter = adapter;
Global.Outbound.Gateway = new IPAddress(pRoute.dwForwardNextHop);
if (logging)
{
Logging.Info($"出口 IPv4 地址:{Global.Outbound.Address}");
Logging.Info($"出口 网关 地址:{Global.Outbound.Gateway}");
Logging.Info($"出口适配器:{Global.Outbound.Adapter.Name} {Global.Outbound.Adapter.Id} {Global.Outbound.Adapter.Description}, index: {Global.Outbound.Index}");
Logging.Info($"出口适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {Global.Outbound.Index}");
}
return true;

View File

@@ -1,11 +1,347 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
namespace Netch
{
public static class Win32Native
{
public enum ForwardType
{
Other = 1,
Invalid = 2,
Direct = 3,
Indirect = 4
}
public enum ForwardProtocol
{
Other = 1,
Local = 2,
NetMGMT = 3,
ICMP = 4,
EGP = 5,
GGP = 6,
Hello = 7,
RIP = 8,
IS_IS = 9,
ES_IS = 10, // 0x0000000A
CISCO = 11, // 0x0000000B
BBN = 12, // 0x0000000C
OSPF = 13, // 0x0000000D
BGP = 14, // 0x0000000E
NT_AUTOSTATIC = 10002, // 0x00002712
NT_STATIC = 10006, // 0x00002716
NT_STATIC_NON_DOD = 10007 // 0x00002717
}
public class RouteEntry
{
internal MIB_IPFORWARDROW _ipFwdNative;
private int _metric1;
private int _metric2;
private int _metric3;
private int _metric4;
private int _metric5;
private IPAddress _destination;
private IPAddress _mask;
private int _policy;
private IPAddress _nextHop;
private NetworkInterface _interface;
private ForwardProtocol _protocol;
private ForwardType _type;
private int _nextHopAS;
private int _age;
private int _index;
public int Index
{
get => _index;
set => _index = value;
}
public IPAddress Destination
{
get => _destination;
set => _destination = value;
}
public IPAddress Mask
{
get => _mask;
set => _mask = value;
}
public int Policy
{
get => _policy;
set => _policy = value;
}
public IPAddress NextHop
{
get => _nextHop;
set => _nextHop = value;
}
public NetworkInterface RelatedInterface => _interface;
public string InterfaceName
{
get
{
if (RelatedInterface == null)
return string.Empty;
return RelatedInterface.Name;
}
}
public ForwardType ForwardType
{
get => _type;
set => _type = value;
}
public ForwardProtocol Protocol
{
get => _protocol;
set => _protocol = value;
}
public int Age
{
get => _age;
set => _age = value;
}
public int NextHopAS
{
get => _nextHopAS;
set => _nextHopAS = value;
}
public int Metric1
{
get => _metric1;
set => _metric1 = value;
}
public int Metric2
{
get => _metric2;
set => _metric2 = value;
}
public int Metric3
{
get => _metric3;
set => _metric3 = value;
}
public int Metric4
{
get => _metric4;
set => _metric4 = value;
}
public int Metric5
{
get => _metric5;
set => _metric5 = value;
}
public RouteEntry(
uint destination,
uint mask,
int policy,
uint nextHop,
NetworkInterface intf,
ForwardType type,
ForwardProtocol proto,
int age,
int nextHopAS,
int metric1,
int metric2,
int metric3,
int metric4,
int metric5,
int idx)
{
_age = age;
_policy = policy;
_protocol = proto;
_type = type;
_destination = new IPAddress(destination);
_mask = new IPAddress(mask);
_nextHop = new IPAddress(nextHop);
_nextHopAS = nextHopAS;
_interface = intf;
_metric1 = metric1;
_metric2 = metric2;
_metric3 = metric3;
_metric4 = metric4;
_metric5 = metric5;
_index = idx;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MIB_IFROW
{
private const int MAX_INTERFACE_NAME_LEN = 256;
private const int MAXLEN_IFDESCR = 256;
private const int MAXLEN_PHYSADDR = 8;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string wszName;
public int dwIndex;
public int dwType;
public int dwMtu;
public int dwSpeed;
public int dwPhysAddrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] bPhysAddr;
public int dwAdminStatus;
public int dwOperStatus;
public int dwLastChange;
public int dwInOctets;
public int dwInUcastPkts;
public int dwInNUcastPkts;
public int dwInDiscards;
public int dwInErrors;
public int dwInUnknownProtos;
public int dwOutOctets;
public int dwOutUcastPkts;
public int dwOutNUcastPkts;
public int dwOutDiscards;
public int dwOutErrors;
public int dwOutQLen;
public int dwDescrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] bDescr;
}
public class AdaptersTable
{
private Dictionary<int, NetworkInterface> _adapters = new Dictionary<int, NetworkInterface>();
public IDictionary<int, NetworkInterface> GetAdapters()
{
return _adapters;
}
public NetworkInterface GetAdapter(int interfaceIndex)
{
NetworkInterface networkInterface = null;
_adapters.TryGetValue(interfaceIndex, out networkInterface);
return networkInterface;
}
public int GetAdapterIndex(NetworkInterface networkInterface)
{
return _adapters.First(a => a.Value == networkInterface).Key;
}
public AdaptersTable()
{
var num1 = IntPtr.Zero;
var pdwSize = 0;
var num2 = 0;
num2 = GetIfTable(IntPtr.Zero, ref pdwSize, true);
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
try
{
num1 = Marshal.AllocHGlobal(pdwSize);
if (GetIfTable(num1, ref pdwSize, true) != 0)
return;
var num3 = Marshal.ReadInt32(num1);
var ptr = new IntPtr(num1.ToInt32() + 4);
for (var index = 0; index < num3; ++index)
{
var structure = (MIB_IFROW)Marshal.PtrToStructure(ptr, typeof(MIB_IFROW));
var pIfRow = new MIB_IFROW();
pIfRow.dwIndex = structure.dwIndex;
if (GetIfEntry(ref pIfRow) == 0)
{
var str = Encoding.ASCII.GetString(structure.bDescr, 0, pIfRow.dwDescrLen - 1);
foreach (var networkInterface in networkInterfaces)
{
if (networkInterface.Description == str)
{
_adapters.Add(structure.dwIndex, networkInterface);
break;
}
}
}
ptr = new IntPtr(ptr.ToInt32() + Marshal.SizeOf(typeof(MIB_IFROW)));
}
}
catch (Exception)
{
// ignored
}
finally
{
Marshal.FreeHGlobal(num1);
}
}
}
public struct MIB_IPFORWARDROW
{
public uint dwForwardDest;
public uint dwForwardMask;
public int dwForwardPolicy;
public uint dwForwardNextHop;
public int dwForwardIfIndex;
public ForwardType dwForwardType;
public ForwardProtocol dwForwardProto;
public int dwForwardAge;
public int dwForwardNextHopAS;
public int dwForwardMetric1;
public int dwForwardMetric2;
public int dwForwardMetric3;
public int dwForwardMetric4;
public int dwForwardMetric5;
public static implicit operator MIB_IPFORWARDROW(RouteEntry value)
{
var mibIpforwardrow = new MIB_IPFORWARDROW();
mibIpforwardrow.dwForwardAge = value.Age;
mibIpforwardrow.dwForwardDest = BitConverter.ToUInt32(value.Destination.GetAddressBytes(), 0);
mibIpforwardrow.dwForwardMask = BitConverter.ToUInt32(value.Mask.GetAddressBytes(), 0);
mibIpforwardrow.dwForwardMetric1 = value.Metric1;
mibIpforwardrow.dwForwardMetric2 = value.Metric2;
mibIpforwardrow.dwForwardMetric3 = value.Metric3;
mibIpforwardrow.dwForwardMetric4 = value.Metric4;
mibIpforwardrow.dwForwardMetric5 = value.Metric5;
mibIpforwardrow.dwForwardNextHop = BitConverter.ToUInt32(value.NextHop.GetAddressBytes(), 0);
mibIpforwardrow.dwForwardNextHopAS = value.NextHopAS;
mibIpforwardrow.dwForwardPolicy = value.Policy;
mibIpforwardrow.dwForwardProto = value.Protocol;
mibIpforwardrow.dwForwardType = value.ForwardType;
var adaptersTable = new AdaptersTable();
mibIpforwardrow.dwForwardIfIndex = adaptersTable.GetAdapterIndex(value.RelatedInterface);
return mibIpforwardrow;
}
}
[DllImport("iphlpapi", SetLastError = true)]
public static extern int GetIfTable(IntPtr pIfTable, ref int pdwSize, bool bOrder);
[DllImport("iphlpapi", SetLastError = true)]
public static extern int GetIfEntry(ref MIB_IFROW pIfRow);
[DllImport("iphlpapi", SetLastError = true)]
public static extern int GetBestRoute(uint dwDestAddr, int dwSourceAddr, out MIB_IPFORWARDROW pRoute);
[DllImport("User32", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("WinINet")]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
}
}
}