/// <summary> /// Checks to find whether the Windows XP/2003 Firewall is enabled on adapters and if so it opens ports. /// </summary> /// <param name="portMapName">Name of the Port Map to look for (must be the same as when it was added)</param> /// <param name="port">Port Number</param> /// <param name="protocolIsTcp">true if TCP, false if UDP</param> /// <remarks> /// WARNING: This method does not inform the user that the firewall punchthrough is being added. Applications /// should always inform the user when adding punchthroughs to the firewall, for security reasons. /// </remarks> public static void RemoveOldFirewallPort(string portMapName, ushort port, ProtocolType protocol) { ValidateForOldCompatibleFirewall(); ValidateAdministrator(); byte protocolAsByte = ConvertAndValidateProtocol(protocol); INetSharingManager mgr = new NetSharingManagerClass(); // Iterate through all of the available connections foreach (INetConnection iCon in mgr.EnumEveryConnection) { INetSharingConfiguration iShareConfig = mgr.get_INetSharingConfigurationForINetConnection(iCon); if (iShareConfig.InternetFirewallEnabled) // skip this connection if the firewall is disabled { foreach (INetSharingPortMapping portMap in iShareConfig.get_EnumPortMappings(tagSHARINGCONNECTION_ENUM_FLAGS.ICSSC_ENABLED)) { // Remove this port mapping only if the name & port match if ((ushort)(portMap.Properties.ExternalPort) == port && portMap.Properties.IPProtocol == protocolAsByte) { if (String.Compare(portMap.Properties.Name, portMapName) == 0) { iShareConfig.RemovePortMapping(portMap); } } } } } }
public List <NetConnection> GetConnections() { try { netSharingMgr = new NetSharingManagerClass(); connections = netSharingMgr.EnumEveryConnection; var strs = new List <NetConnection>(); foreach (INetConnection connection in connections) { INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection]; INetConnectionProps connProps = netSharingMgr.NetConnectionProps[connection]; var nc = new NetConnection(connProps.Name, connProps.DeviceName); strs.Add(nc); if (connSharcf.SharingEnabled && connSharcf.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) { SharedConnection = nc; } } return(strs); } catch (Exception ex) { MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手"); return(null); } }
/// <summary> /// Initializes a new instance of 'NetworkConnectionCollection'. /// </summary> public NetworkConnectionCollection() { NetSharingManager icsMgr = new NetSharingManagerClass(); foreach (var icsConn in icsMgr.EnumEveryConnection.Cast <INetConnection>()) { Add(new NetworkConnection(icsConn)); } }
/// <summary> /// 重新连接 /// </summary> public static void ReConnect() { var netSharingMgr = new NetSharingManagerClass(); foreach (var connection in from INetConnection connection in netSharingMgr.EnumEveryConnection let connProps = netSharingMgr.NetConnectionProps[connection] where connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN select connection) { connection.Disconnect(); //禁用网络 connection.Connect(); //启用网络 } }
/// <summary> /// 禁用所有网络 /// </summary> public static void Disconnect() { try { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { try { connection.Disconnect(); } catch { } } } catch { } }
/* * private void DisableSharing() * { * connections = netSharingMgr.EnumEveryConnection; * foreach (INetConnection connection in connections) * { * INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection]; * INetConnectionProps connProps = netSharingMgr.NetConnectionProps[connection]; * try * { * if (connSharcf.SharingEnabled) * connSharcf.DisableSharing(); * } * catch (Exception e) * { * MessageBox.Show(e.Message); * } * } * } */ public bool EnableSharing(NetConnection nc) { if (nc == null) { return(false); } try { netSharingMgr = new NetSharingManagerClass(); connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetSharingConfiguration connSharcf = netSharingMgr.INetSharingConfigurationForINetConnection[connection]; INetConnectionProps connProps = netSharingMgr.NetConnectionProps[connection]; try { //判断要配置的网卡 Microsoft Virtual WiFi Miniport Adapter Realtek PCIe GBE Family Controller if (connProps.DeviceName.Equals(nc.DeviceName)) { //配置WAN连接 if (!connSharcf.SharingEnabled || connSharcf.SharingConnectionType != tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) { connSharcf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC); } } else if (connProps.DeviceName.Contains("Virtual")) { //配置LAN连接 if (!connSharcf.SharingEnabled || connSharcf.SharingConnectionType != tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) { connSharcf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); } } } catch (Exception ex) { MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手"); return(false); } } SharedConnection = nc; } catch (Exception ex) { MessageBox.Show("网络共享出现不可预知的错误,请手动共享网络。\r\n\r\n错误信息:" + ex.Message, @"虚拟WIFI助手"); return(false); } return(true); }
/// <summary> /// 重新连接 /// </summary> private void ReConnect() { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { connection.Disconnect(); //禁用网络 connection.Connect(); //启用网络 } } }
private void AddPrivConn(object sender, EventArgs e) { try { NetSharingManager manager = new NetSharingManagerClass(); INetConnection con = inet_connections[priv_con_cb_index[cb_con_priv.SelectedIndex]]; INetConnectionProps prop = manager.NetConnectionProps[con]; INetSharingConfiguration conf = manager.INetSharingConfigurationForINetConnection[con]; conf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); priv_con.Add(prop.DeviceName); } catch (Exception) { } UpdateICS(); }
/// <summary> /// 启用所有网络 /// </summary> public static void Connect() { try { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { try { connection.Connect(); } catch { } // // // //INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); // // // //if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) // // // // try { connection.Connect(); } catch { } } } catch { } }
public static void Control(bool isConnect) { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { if (isConnect) { connection.Connect(); //启用网络 } else { connection.Disconnect(); //禁用网络 } } } }
public void localnetface(string signer) { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { if (signer == "on") { connection.Connect(); //启用网络 } else if (signer == "off") { connection.Disconnect(); //禁用网络 } } } }
/// <summary> /// Checks to find whether the Windows XP/2003 Firewall is enabled on adapters and if so it opens ports. /// </summary> /// <param name="portMapName">Name of the Port Map to look for</param> /// <param name="port">Port Number</param> /// <param name="protocolIsTcp">true if TCP, false if UDP</param> /// <remarks> /// WARNING: This method does not inform the user that the firewall punchthrough is being added. Applications /// should always inform the user when adding punchthroughs to the firewall, for security reasons. /// </remarks> public static void AddOldFirewallPort(string portMapName, ushort port, ProtocolType protocol) { ValidateForOldCompatibleFirewall(); ValidateAdministrator(); // Get the protocolAsByte ICF constant byte protocolAsByte = ConvertAndValidateProtocol(protocol); INetSharingManager mgr = new NetSharingManagerClass(); // Iterate through all of the available connections foreach (INetConnection iCon in mgr.EnumEveryConnection) { INetSharingConfiguration iShareConfig = mgr.get_INetSharingConfigurationForINetConnection(iCon); if (iShareConfig.InternetFirewallEnabled) // skip this connection if the firewall is disabled { // Make sure that this firewall doesn't already have a port map for the same port bool portMapExists = false; foreach (INetSharingPortMapping portMap in iShareConfig.get_EnumPortMappings(tagSHARINGCONNECTION_ENUM_FLAGS.ICSSC_ENABLED)) { if ((ushort)(portMap.Properties.ExternalPort) == port && portMap.Properties.IPProtocol == protocolAsByte) { portMapExists = true; break; } } if (!portMapExists) { // Finally, add & enable the new port map INetSharingPortMapping newPortMap = iShareConfig.AddPortMapping(portMapName, protocolAsByte, port, port, 0, SystemInformation.ComputerName, tagICS_TARGETTYPE.ICSTT_NAME); newPortMap.Enable(); } } } }
/// <summary> /// Checks to find whether the Windows XP/2003 Firewall is enabled on adapters and if so it opens ports. /// </summary> /// <param name="portMapName">Name of the Port Map to look for (must be the same as when it was added)</param> /// <param name="port">Port Number</param> /// <param name="protocolIsTcp">true if TCP, false if UDP</param> /// <remarks> /// WARNING: This method does not inform the user that the firewall punchthrough is being added. Applications /// should always inform the user when adding punchthroughs to the firewall, for security reasons. /// </remarks> public static void RemoveOldFirewallPort(string portMapName, ushort port, ProtocolType protocol) { ValidateForOldCompatibleFirewall(); ValidateAdministrator(); byte protocolAsByte = ConvertAndValidateProtocol(protocol); INetSharingManager mgr = new NetSharingManagerClass(); // Iterate through all of the available connections foreach(INetConnection iCon in mgr.EnumEveryConnection) { INetSharingConfiguration iShareConfig = mgr.get_INetSharingConfigurationForINetConnection(iCon); if( iShareConfig.InternetFirewallEnabled ) // skip this connection if the firewall is disabled { foreach(INetSharingPortMapping portMap in iShareConfig.get_EnumPortMappings(tagSHARINGCONNECTION_ENUM_FLAGS.ICSSC_ENABLED)) { // Remove this port mapping only if the name & port match if ((ushort)(portMap.Properties.ExternalPort) == port && portMap.Properties.IPProtocol == protocolAsByte) { if (String.Compare(portMap.Properties.Name, portMapName) == 0) { iShareConfig.RemovePortMapping(portMap); } } } } } }
/// <summary> /// Checks to find whether the Windows XP/2003 Firewall is enabled on adapters and if so it opens ports. /// </summary> /// <param name="portMapName">Name of the Port Map to look for</param> /// <param name="port">Port Number</param> /// <param name="protocolIsTcp">true if TCP, false if UDP</param> /// <remarks> /// WARNING: This method does not inform the user that the firewall punchthrough is being added. Applications /// should always inform the user when adding punchthroughs to the firewall, for security reasons. /// </remarks> public static void AddOldFirewallPort(string portMapName, ushort port, ProtocolType protocol) { ValidateForOldCompatibleFirewall(); ValidateAdministrator(); // Get the protocolAsByte ICF constant byte protocolAsByte = ConvertAndValidateProtocol(protocol); INetSharingManager mgr = new NetSharingManagerClass(); // Iterate through all of the available connections foreach(INetConnection iCon in mgr.EnumEveryConnection) { INetSharingConfiguration iShareConfig = mgr.get_INetSharingConfigurationForINetConnection(iCon); if( iShareConfig.InternetFirewallEnabled ) // skip this connection if the firewall is disabled { // Make sure that this firewall doesn't already have a port map for the same port bool portMapExists = false; foreach(INetSharingPortMapping portMap in iShareConfig.get_EnumPortMappings(tagSHARINGCONNECTION_ENUM_FLAGS.ICSSC_ENABLED)) { if ((ushort)(portMap.Properties.ExternalPort) == port && portMap.Properties.IPProtocol == protocolAsByte) { portMapExists = true; break; } } if (!portMapExists) { // Finally, add & enable the new port map INetSharingPortMapping newPortMap = iShareConfig.AddPortMapping(portMapName, protocolAsByte, port, port, 0, SystemInformation.ComputerName, tagICS_TARGETTYPE.ICSTT_NAME); newPortMap.Enable(); } } } }
//TODO SLOWWWW void UpdateICS() { NetSharingManager manager = new NetSharingManagerClass(); bool instaled = manager.SharingInstalled; cb_con_pub.Enabled = instaled; lb_con_priv.Enabled = instaled; cb_con_priv.Enabled = instaled; btn_add_privcon.Enabled = instaled; pub_con = new List <string>(); priv_con = new List <string>(); priv_con_cb = new List <string>(); priv_con_cb_index = new List <int>(); int index = 0; int?selected_index = null; foreach (INetConnection con in manager.EnumEveryConnection) { try { inet_connections.Add(con); INetConnectionProps prop = manager.NetConnectionProps[con]; INetSharingConfiguration conf = manager.INetSharingConfigurationForINetConnection[con]; pub_con.Add(prop.Name); if ((tagNETCON_CHARACTERISTIC_FLAGS)prop.Characteristics != tagNETCON_CHARACTERISTIC_FLAGS.NCCF_INCOMING_ONLY && conf.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC && conf.SharingEnabled) { selected_index = index; } else { if (conf.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE) { priv_con.Add(prop.Name); } else { priv_con_cb.Add(prop.Name); priv_con_cb_index.Add(index); } } //Info Print(prop.DeviceName + " - " + prop.Status + " - " + prop.MediaType + " - " + conf.SharingEnabled + " - " + conf.SharingConnectionType + " - " + (tagNETCON_CHARACTERISTIC_FLAGS)prop.Characteristics); index++; } catch (Exception e) { } } cb_con_pub.DataSource = pub_con; if (selected_index != null) { cb_con_pub.SelectedIndex = (int)selected_index; } lb_con_priv.DataSource = priv_con; cb_con_priv.DataSource = priv_con_cb; }