123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Threading.Tasks;
- using Vinno.FIS.Sonopost.Features.Config;
- using Vinno.FIS.Sonopost.Features.Network;
- using Vinno.FIS.Sonopost.WebApi.Models;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Utilities;
- namespace Vinno.FIS.Sonopost.WebApi.Controllers
- {
- [WebApiController]
- internal class NetworkController : BaseController
- {
- /// <summary>
- /// 获取有线网络信息
- /// </summary>
- /// <returns></returns>
- [WebApiAction]
- public Result GetWireInfos()
- {
- var infos = NetworkManager.GetAllNetworkInterfaceInfos().Where(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet);
- var result = new NetworkWireListModel();
- if (infos != null && infos.Any())
- {
- int index = 1;
- foreach (var info in infos)
- {
- result.Interfaces.Add(new NetworkInterfaceModel
- {
- Guid = info.Guid,
- Description = info.Description,
- Name = $"EthernetInterface{index}",
- MacAddress = info.MacAddress,
- IPAddress = info.IpAddress.Value,
- SubnetMask = info.SubnetMask.Value,
- Gateway = info.GateWay.Value,
- PreferredDNS = info.PreferredDNSServer.Value,
- StandbyDNS = info.StandbyDNSServer.Value,
- EnableDHCP = info.EnableDhcp.Value,
- });
- index++;
- }
- }
- return Result.Success(result);
- }
- /// <summary>
- /// 保存有线网络信息
- /// </summary>
- /// <returns></returns>
- [WebApiAction]
- public Result SaveWireInfos(NetworkWireListModel model)
- {
- var staticIpList = model.Interfaces.Where(x => !x.EnableDHCP);
- if (staticIpList != null && staticIpList.Count() > 0)
- {
- var interfaceInfoList = NetworkManager.GetAllNetworkInterfaceInfos();
- bool hasIpConflict = false;
- Parallel.ForEach(staticIpList, x =>
- {
- var currentIp = interfaceInfoList?.FirstOrDefault(y => y.MacAddress == x.MacAddress && y.NetworkInterfaceType == NetworkInterfaceType.Ethernet)?.IpAddress.Value;
- if (currentIp != x.IPAddress)
- {
- if (NetworkManager.Ping(x.IPAddress, 1500) == IPStatus.Success)
- {
- hasIpConflict = true;
- }
- }
- });
- if (hasIpConflict)
- {
- return Result.Fail("IPAddressExist");
- }
- }
- if (model.Interfaces != null && model.Interfaces.Any())
- {
- foreach (var item in model.Interfaces)
- {
- if (!NetworkManager.SetNetworkInfo(item.MacAddress, item.EnableDHCP, item.IPAddress
- , item.SubnetMask, item.Gateway, item.PreferredDNS, item.StandbyDNS))
- {
- return Result.Fail("SaveFailed");
- }
- }
- NetworkManager.OnNetworkAddressChanged(this, EventArgs.Empty);
- }
- return Result.Success();
- }
- /// <summary>
- /// 获取Wifi信息
- /// </summary>
- /// <returns></returns>
- [WebApiAction]
- public Result GetWifiInfo()
- {
- var info = NetworkManager.GetAllNetworkInterfaceInfos().FirstOrDefault(x => x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
- if (info == null)
- {
- return Result.Fail("WifiDeviceNotFound");
- }
- var wifiInfo = info as WirelessNetworkInterfaceInfo;
- var points = wifiInfo.GetAccessPoints()
- ?.Where(x => !string.IsNullOrWhiteSpace(x.Name))
- ?.OrderByDescending(x => x.IsConnected)
- ?.ThenByDescending(x => x.SignalStrength);
- NetworkWifiInfoResult result = new NetworkWifiInfoResult
- {
- Guid = info.Guid,
- Description = info.Description,
- Name = info.Description,
- MacAddress = info.MacAddress,
- IPAddress = info.IpAddress.Value,
- SubnetMask = info.SubnetMask.Value,
- Gateway = info.GateWay.Value,
- PreferredDNS = info.PreferredDNSServer.Value,
- StandbyDNS = info.StandbyDNSServer.Value,
- EnableDHCP = info.EnableDhcp.Value,
- Points = points?.Select(x => new WifiPointModel
- {
- Name = x.Name,
- IsSecure = x.IsSecure,
- IsConnected = x.IsConnected,
- SignalStrength = x.SignalStrength,
- HasProfile = x.HasProfile,
- })?.ToList() ?? new List<WifiPointModel>()
- };
- return Result.Success(result);
- }
- /// <summary>
- /// 连接Wifi网络
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [WebApiAction]
- public Result ConnectWifi(NetworkConnectWifiRequest request)
- {
- var info = NetworkManager.GetAllNetworkInterfaceInfos().FirstOrDefault(x => x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
- if (info == null)
- {
- return Result.Fail("WifiDeviceNotFound");
- }
- var wifiInfo = info as WirelessNetworkInterfaceInfo;
- var point = wifiInfo.GetAccessPoints()?.FirstOrDefault(x => x.Name.Equals(request.Name));
- if (point == null)
- {
- return Result.Fail("ConnectFailed");
- }
- bool success = wifiInfo.Connect(point, point.Name, request.Password);
- if (success)
- {
- SonopostUserDefinedSettings.Instance.NetworkSetting.IsAutoConnect = true;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiName = point.Name;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiPassword = DesBuilder.Encrypt(request.Password);
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiMacAddress = wifiInfo.MacAddress; ;
- ConfigManager.Save();
- }
- Logger.WriteLineInfo($"Connect Wifi Invoke,Wifi Name:{request.Name},success:{success}");
- return success ? Result.Success() : Result.Fail("ConnectFailed");
- }
- /// <summary>
- /// 断开Wifi网络连接
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [WebApiAction]
- public Result DisconnectWifi(NetworkConnectWifiRequest request)
- {
- var info = NetworkManager.GetAllNetworkInterfaceInfos().FirstOrDefault(x => x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
- if (info == null)
- {
- return Result.Fail("WifiDeviceNotFound");
- }
- var wifiInfo = info as WirelessNetworkInterfaceInfo;
- bool success = wifiInfo.Disconnect();
- SonopostUserDefinedSettings.Instance.NetworkSetting.IsAutoConnect = false;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiName = null;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiPassword = null;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiMacAddress = null;
- ConfigManager.Save();
- Logger.WriteLineInfo($"Disconnect Wifi Invoke,success:{success}");
- return success ? Result.Success() : Result.Fail();
- }
- /// <summary>
- /// 忘记密码
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [WebApiAction]
- public Result ForgetPassword(NetworkConnectWifiRequest request)
- {
- var info = NetworkManager.GetAllNetworkInterfaceInfos().FirstOrDefault(x => x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
- if (info == null)
- {
- return Result.Fail("WifiDeviceNotFound");
- }
- var wifiInfo = info as WirelessNetworkInterfaceInfo;
- var point = wifiInfo.GetAccessPoints().FirstOrDefault(x => x.Name.Equals(request.Name));
- if (point == null)
- {
- return Result.Fail("OperateFailed");
- }
- wifiInfo.DeleteProfile(point);
- if (wifiInfo.MacAddress == SonopostUserDefinedSettings.Instance.NetworkSetting.WifiMacAddress && SonopostUserDefinedSettings.Instance.NetworkSetting.IsAutoConnect && SonopostUserDefinedSettings.Instance.NetworkSetting.WifiName == request.Name)
- {
- SonopostUserDefinedSettings.Instance.NetworkSetting.IsAutoConnect = false;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiName = null;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiPassword = null;
- SonopostUserDefinedSettings.Instance.NetworkSetting.WifiMacAddress = null;
- ConfigManager.Save();
- }
- return Result.Success();
- }
- /// <summary>
- /// 保存wifi网络信息
- /// </summary>
- /// <returns></returns>
- [WebApiAction]
- public Result SaveWifiInfo(NetworkInterfaceModel model)
- {
- if (!model.EnableDHCP)
- {
- var currentIp = NetworkManager.GetAllNetworkInterfaceInfos().FirstOrDefault(x => x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)?.IpAddress.Value;
- if (currentIp != model.IPAddress)
- {
- if (NetworkManager.Ping(model.IPAddress, 1500) == IPStatus.Success)
- {
- return Result.Fail("IPAddressExist");
- }
- }
- }
- if (NetworkManager.SetNetworkInfo(model.MacAddress, model.EnableDHCP, model.IPAddress
- , model.SubnetMask, model.Gateway, model.PreferredDNS, model.StandbyDNS))
- {
- NetworkManager.OnNetworkAddressChanged(this, EventArgs.Empty);
- return Result.Success();
- }
- else
- {
- return Result.Fail("SaveFailed");
- }
- }
- /// <summary>
- /// 刷新DNS
- /// </summary>
- /// <returns></returns>
- [WebApiAction]
- public Result FlushDNS()
- {
- bool success = NetworkManager.FlushDns();
- return success ? Result.Success() : Result.Fail();
- }
- /// <summary>
- /// Ping
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [WebApiAction]
- public Result Ping(NetworkBaseRequest request)
- {
- IPStatus status = NetworkManager.Ping(request.Ipv4, 3000);
- if (status == IPStatus.Success)
- {
- return Result.Success();
- }
- Result failRst = Result.Fail(Enum.GetName(typeof(IPStatus), status));
- failRst.Data = status.GetHashCode();
- return failRst;
- }
- [WebApiAction]
- public Result GetNetworkInterfaces()
- {
- var allNetworkInterfaceInfos = NetworkManager.GetAllNetworkInterfaceInfos();
- var ethernetInfos = allNetworkInterfaceInfos.Where(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet).ToList();
- var wifilessInfo = allNetworkInterfaceInfos.FirstOrDefault(x => x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
- var connectedEthernetInfos = ethernetInfos.Where(c => c.OperationalStatus == OperationStatus.Connected);
- NetworkAdvaneSettingListModel result = new NetworkAdvaneSettingListModel();
- var outsideAddress = SonopostUserDefinedSettings.Instance.NetworkSetting.OutsideNetworkMacAddress;
- if (connectedEthernetInfos != null)
- {
- foreach (var info in connectedEthernetInfos)
- {
- if (info.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
- {
- var index = ethernetInfos.IndexOf(info);
- result.Interfaces.Add(new NetworkAdvanceSettingItem()
- {
- Description = $"EthernetInterface{index + 1}",
- MacAddress = info.MacAddress,
- IsOutSideNetwork = string.Equals(info.MacAddress, outsideAddress)
- });
- }
- }
- }
- if (wifilessInfo != null)
- {
- result.Interfaces.Add(new NetworkAdvanceSettingItem()
- {
- Description = $"WifiInterface1",
- MacAddress = wifilessInfo.MacAddress,
- IsOutSideNetwork = string.Equals(wifilessInfo.MacAddress, outsideAddress)
- });
- }
- return Result.Success(result);
- }
- [WebApiAction]
- public Result SaveAdvanceSetting(NetworkAdvanceSettingItem settingItem)
- {
- SetDualNetworkResult success = null;
- if (settingItem != null)
- {
- if (settingItem.IsOutSideNetwork && !string.IsNullOrEmpty(settingItem.MacAddress))
- {
- success = NetworkManager.SetDualNetworkEnable(settingItem.MacAddress);
- }
- }
- else
- {
- success = NetworkManager.SetDualNetworkDisable();
- }
- return success != null && success.IsSuccess ? Result.Success(success) : Result.Fail();
- }
- }
- }
|