public static bool Enable() { Utils.Utils.SearchOutboundAdapter(); TUNTAPController.SearchTapAdapter(); if (Global.TUNTAP.Adapter == null || Global.Outbound.Adapter == null) { return(false); } try { CleanupWMISharingEntries(); #region Save Outbound IP Config var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = wmi.GetInstances(); var dhcpEnabled = true; string[] ipAddress = null; string[] subnetMask = null; string[] gateway = null; ushort[] gatewayMetric = null; string[] dns = null; var outboundWmi = WMI.GetManagementObjectByDeviceNameOrDefault(Global.Outbound.Adapter.Description); if (outboundWmi == null) { return(false); } if (!(dhcpEnabled = (bool)outboundWmi["DHCPEnabled"])) { ipAddress = (string[])outboundWmi["IPAddress"]; subnetMask = (string[])outboundWmi["IPSubnet"]; gateway = (string[])outboundWmi["DefaultIPGateway"]; gatewayMetric = (ushort[])outboundWmi["GatewayCostMetric"]; dns = (string[])outboundWmi["DNSServerSearchOrder"]; ipAddress = new[] { ipAddress[0] }; subnetMask = new[] { subnetMask[0] }; } #endregion #region Setting ICS foreach (NetworkConnection connection in new NetworkConnectionCollection()) { if (connection.DeviceName == Global.TUNTAP.Adapter.Description) { if (connection.SharingEnabled) { connection.DisableSharing(); } connection.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC); } else if (connection.DeviceName == Global.Outbound.Adapter.Description) { if (connection.SharingEnabled) { connection.DisableSharing(); } connection.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); } } #endregion #region Reset Outbound IP Config if (dhcpEnabled) { outboundWmi.InvokeMethod("EnableDHCP", null, null); } else { //Set static IP and subnet mask var newIP = outboundWmi.GetMethodParameters("EnableStatic"); newIP["IPAddress"] = ipAddress; newIP["SubnetMask"] = subnetMask; outboundWmi.InvokeMethod("EnableStatic", newIP, null); //Set default gateway var newGateway = outboundWmi.GetMethodParameters("SetGateways"); newGateway["DefaultIPGateway"] = gateway; newGateway["GatewayCostMetric"] = gatewayMetric; outboundWmi.InvokeMethod("SetGateways", newGateway, null); //Set dns servers var newDNS = outboundWmi.GetMethodParameters("SetDNSServerSearchOrder"); newDNS["DNSServerSearchOrder"] = dns; outboundWmi.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); } #endregion return(true); } catch (Exception e) { try { Disable(); } catch { // ignored } Logging.Error($"网络连接共享设置失败: {e}"); return(false); } }