public static NetShare GetCurrentlySharedConnections() { INetConnection sharedConnection = null; INetConnection homeConnection = null; INetSharingEveryConnectionCollection connections = SharingManager.EnumEveryConnection; foreach (INetConnection c in connections) { try { INetSharingConfiguration config = GetConfiguration(c); if (config.SharingEnabled) { if (config.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC) { sharedConnection = c; Console.WriteLine("SharedConnection=" + c.ToString()); } else if (config.SharingConnectionType == tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE) { homeConnection = c; Console.WriteLine("homeConnection=" + c.ToString()); } } } catch (System.Runtime.InteropServices.ExternalException) { } } return(new NetShare(sharedConnection, homeConnection)); }
/// <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); } }
public static void DisableAllShares() { INetSharingEveryConnectionCollection connections = SharingManager.EnumEveryConnection; foreach (INetConnection con in connections) { try { INetSharingConfiguration config = GetConfiguration(con); if (config.SharingEnabled) { Console.WriteLine("Sharing was enabled. Disabeling: " + con.ToString()); config.DisableSharing(); } } catch (System.Runtime.InteropServices.ExternalException ex) { Console.WriteLine("DisableAllShares InteropServices.ExternalException: " + ex.Message); } catch (Exception ex) { Console.WriteLine("DisableAllShares EX: " + ex.Message); } } }
public bool Start() { try { INetConnection dialer = GetDialerConnection(); INetConnection wifi = GetAPConnection(); if (dialer == null || wifi == null) { return(false); } INetSharingConfiguration wconf = nsm.INetSharingConfigurationForINetConnection[wifi]; Disable_ICS_WMI(false); if (wconf.SharingEnabled) { wconf.DisableSharing(); } wconf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); INetSharingConfiguration dconf = nsm.INetSharingConfigurationForINetConnection[dialer]; Disable_ICS_WMI(true); if (dconf.SharingEnabled) { dconf.DisableSharing(); } // System.AccessViolationException??? dconf.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC); } catch (Exception e) { Log4Net.WriteLog("Enable sharing wifi failed", e); return(false); } return(true); }
/* * 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); }
internal ICSConnection(INetSharingManager nsManager, INetConnection netConnection) { if (nsManager == null) { throw new ArgumentNullException("nsManager"); } if (netConnection == null) { throw new ArgumentNullException("netConnection"); } this._netConnectionProperties = nsManager.NetConnectionProps[netConnection]; this._netSharingConfiguration = nsManager.INetSharingConfigurationForINetConnection[netConnection]; }
internal ICSConnection(INetSharingManager nsManager, INetConnection netConnection) { if (nsManager == null) { throw new ArgumentNullException("nsManager"); } if (netConnection == null) { throw new ArgumentNullException("netConnection"); } this._netConnectionProperties = nsManager.NetConnectionProps[netConnection]; this._netSharingConfiguration = nsManager.INetSharingConfigurationForINetConnection[netConnection]; }
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(); }
private void RefreshStatus() { bool SharingEnabled = false; cbSharedConnection.Items.Clear(); cbHomeConnection.Items.Clear(); foreach (NetworkInterface nic in IcsManager.GetAllIPv4Interfaces()) { ConnectionItem connItem = new ConnectionItem(nic); cbSharedConnection.Items.Add(connItem); cbHomeConnection.Items.Add(connItem); INetConnection netShareConnection = connItem.Connection; if (netShareConnection != null) { INetSharingConfiguration config = IcsManager.GetConfiguration(netShareConnection); if (config.SharingEnabled) { SharingEnabled = true; switch (config.SharingConnectionType) { case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC: cbSharedConnection.SelectedIndex = cbSharedConnection.Items.Count - 1; break; case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE: cbHomeConnection.SelectedIndex = cbSharedConnection.Items.Count - 1; break; } } } } this.ButtonApply.Enabled = !SharingEnabled; this.buttonStopSharing.Enabled = SharingEnabled; this.cbSharedConnection.Enabled = !SharingEnabled; this.cbHomeConnection.Enabled = !SharingEnabled; cbHomeConnection_SelectedIndexChanged(null, null); cbSharedConnection_SelectedIndexChanged(null, null); }
public static void StopSharing() { foreach (INetConnection NetConnection in INetSharingMgr.EnumEveryConnection) { INetConnectionProps INetConProps = INetSharingMgr.NetConnectionProps[NetConnection]; INetSharingConfiguration INetShConfig = INetSharingMgr.INetSharingConfigurationForINetConnection[NetConnection]; if (!INetShConfig.SharingEnabled) { continue; } switch (INetShConfig.SharingConnectionType) { case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC: INetShConfig.DisableSharing(); Trace.WriteLine( $"\n" + $"Stop ICS Type: Public" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE: INetShConfig.DisableSharing(); Trace.WriteLine( $"\n" + $"Stop ICS Type: Private" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; } } }
public static void StartSharing(Guid InterfaceGuid, tagSHARINGCONNECTIONTYPE ShConType) { foreach (INetConnection NetConnection in INetSharingMgr.EnumEveryConnection) { INetConnectionProps INetConProps = INetSharingMgr.NetConnectionProps[NetConnection]; INetSharingConfiguration INetShConfig = INetSharingMgr.INetSharingConfigurationForINetConnection[NetConnection]; if (!Guid.Parse(INetConProps.Guid).Equals(InterfaceGuid)) { continue; } switch (ShConType) { case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC: INetShConfig.EnableSharing(ShConType); Trace.WriteLine( $"\n" + $"Start ICS Type: Public" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; case tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE: INetShConfig.EnableSharing(ShConType); Trace.WriteLine( $"\n" + $"Start ICS Type: Private" + $"\nName: {INetConProps.Name}" + $"\nDeviceName: {INetConProps.DeviceName}" + $"\nGuid: {INetConProps.Guid}" + $"\n"); break; } } }
/// <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(); } } } }
public bool JShareWIFI(bool isShare, out string jShareWIFIRet) { jShareWIFIRet = "未找到本地网络连接!"; if (!isShare) { if (sharingCfg != null) { sharingCfg = null; } return(true); } try { string connectionToShare = getBestInterface()?.Name; // 被共享的网络连接 string sharedForConnection = ""; // 共享的家庭网络连接 var manager = new NetSharingManager(); var connections = manager.EnumEveryConnection; NetworkInterface[] Ninterface = NetworkInterface.GetAllNetworkInterfaces();//确定虚拟网络名称 foreach (NetworkInterface IN in Ninterface) { if (IN.Description.Contains("Microsoft Hosted Network Virtual Adapter") && IN.OperationalStatus == OperationalStatus.Up) { sharedForConnection = IN.Name; break; } } if (string.IsNullOrWhiteSpace(connectionToShare) || string.IsNullOrWhiteSpace(sharedForConnection)) { return(false); } foreach (INetConnection c in connections) { var props = manager.NetConnectionProps[c]; INetSharingConfiguration tempSharingCfg = manager.INetSharingConfigurationForINetConnection[c]; if (props.Name == connectionToShare) { tempSharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC); if (tempSharingCfg.SharingEnabled == true) { jShareWIFIRet = "已设置" + props.Name + "用于共享"; sharingCfg = tempSharingCfg; } } else if (props.Name == sharedForConnection) { tempSharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE); priSharingCfg = tempSharingCfg; } } jShareWIFIRet = "Success"; return(true); } catch (Exception e) { jShareWIFIRet = e.Message; return(false); } }
//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; }