public static void AddInterfaceSetting(NetworkInterfaceConfigurationModel model) { var netif = ServiceModel.Interfaces; var check = netif.Where(_ => _.Interface == model.Interface).ToList(); if (check.Any()) { check.ForEach(_ => RemoveInterfaceSetting(_.Guid)); } netif.Add(model); ServiceModel.Interfaces = netif; Save(ServiceModel); }
public static void ApplyInterfaceSetting(NetworkInterfaceConfigurationModel model) { var netif = model.Interface; switch (model.Type) { case NetworkAdapterType.Physical: break; case NetworkAdapterType.Virtual: break; case NetworkAdapterType.Bond: CommandLauncher.Launch("bond-set", new Dictionary <string, string> { { "$bond", netif } }); foreach (var nif in model.InterfaceList) { CommandLauncher.Launch("bond-add-if", new Dictionary <string, string> { { "$bond", netif }, { "$net_if", nif } }); } break; case NetworkAdapterType.Bridge: CommandLauncher.Launch("brctl-add", new Dictionary <string, string> { { "$bridge", netif } }); foreach (var nif in model.InterfaceList) { CommandLauncher.Launch("brctl-add-if", new Dictionary <string, string> { { "$bridge", netif }, { "$net_if", nif } }); } break; case NetworkAdapterType.Other: break; } switch (model.Mode) { case NetworkInterfaceMode.Null: return; case NetworkInterfaceMode.Static: var networkdIsActive = Systemctl.IsActive("systemd-networkd"); if (networkdIsActive) { Systemctl.Stop("systemd-networkd"); } CommandLauncher.Launch("dhcpcd-killall"); CommandLauncher.Launch("ip4-flush-configuration", new Dictionary <string, string> { { "$net_if", netif } }); CommandLauncher.Launch("ip4-add-addr", new Dictionary <string, string> { { "$net_if", netif }, { "$address", model.StaticAddress }, { "$range", model.StaticRange } }); if (networkdIsActive) { Systemctl.Start("systemd-networkd"); } break; case NetworkInterfaceMode.Dynamic: CommandLauncher.Launch("dhcpcd", new Dictionary <string, string> { { "$net_if", netif } }); break; default: return; } CommandLauncher.Launch("ip4-set-mtu", new Dictionary <string, string> { { "$net_if", netif }, { "$mtu", model.Mtu } }); CommandLauncher.Launch("ip4-set-txqueuelen", new Dictionary <string, string> { { "$net_if", netif }, { "$txqueuelen", model.Txqueuelen } }); if (!string.IsNullOrEmpty(model.Route) && !string.IsNullOrEmpty(model.Gateway)) { CommandLauncher.Launch("ip4-add-route", new Dictionary <string, string> { { "$net_if", netif }, { "$gateway", model.Gateway }, { "$ip_address", model.Route } }); } var status = model.Status; switch (status) { case NetworkInterfaceStatus.Down: CommandLauncher.Launch("ip4-disable-if", new Dictionary <string, string> { { "$net_if", netif } }); break; case NetworkInterfaceStatus.Up: CommandLauncher.Launch("ip4-enable-if", new Dictionary <string, string> { { "$net_if", netif } }); ConsoleLogger.Log($"[network] interface '{model.Interface}' configured"); break; default: CommandLauncher.Launch("ip4-disable-if", new Dictionary <string, string> { { "$net_if", netif } }); break; } }
public AntdNetworkModule() { Get["/network"] = x => { var physicalInterfaces = NetworkConfiguration.InterfacePhysicalModel.ToList(); var bridgeInterfaces = NetworkConfiguration.InterfaceBridgeModel.ToList(); var bondInterfaces = NetworkConfiguration.InterfaceBondModel.ToList(); var virtualInterfaces = NetworkConfiguration.InterfaceVirtualModel.ToList(); foreach (var vif in virtualInterfaces) { if (physicalInterfaces.Any(_ => _.Interface == vif.Interface) || bridgeInterfaces.Any(_ => _.Interface == vif.Interface) || bondInterfaces.Any(_ => _.Interface == vif.Interface)) { virtualInterfaces.Remove(vif); } } var model = new PageNetworkModel { NetworkPhysicalIf = physicalInterfaces, NetworkBridgeIf = bridgeInterfaces, NetworkBondIf = bondInterfaces, NetworkVirtualIf = virtualInterfaces, NetworkIfList = NetworkConfiguration.NetworkInterfaces }; return(JsonConvert.SerializeObject(model)); }; Post["/network/restart"] = x => { NetworkConfiguration.Start(); return(HttpStatusCode.OK); }; Post["/network/interface"] = x => { string Interface = Request.Form.Interface; string mode = Request.Form.Mode; string status = Request.Form.Status; string staticAddres = Request.Form.StaticAddres; string staticRange = Request.Form.StaticRange; string txqueuelen = Request.Form.Txqueuelen; string mtu = Request.Form.Mtu; string route = Request.Form.Route; string gateway = Request.Form.Gateway; var model = new NetworkInterfaceConfigurationModel { Interface = Interface, Mode = (NetworkInterfaceMode)Enum.Parse(typeof(NetworkInterfaceMode), mode), Status = (NetworkInterfaceStatus)Enum.Parse(typeof(NetworkInterfaceStatus), status), StaticAddress = staticAddres, StaticRange = staticRange, Txqueuelen = txqueuelen, Mtu = mtu, Type = NetworkAdapterType.Physical, Route = route, Gateway = gateway }; NetworkConfiguration.AddInterfaceSetting(model); return(HttpStatusCode.OK); }; Post["/network/interface/del"] = x => { string guid = Request.Form.Guid; NetworkConfiguration.RemoveInterfaceSetting(guid); return(HttpStatusCode.OK); }; Post["/network/interface/bridge"] = x => { string Interface = Request.Form.Interface; string mode = Request.Form.Mode; string status = Request.Form.Status; string staticAddres = Request.Form.StaticAddres; string staticRange = Request.Form.StaticRange; string txqueuelen = Request.Form.Txqueuelen; string mtu = Request.Form.Mtu; string ifs = Request.Form.InterfaceList; string route = Request.Form.Route; string gateway = Request.Form.Gateway; var ifList = ifs.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); CommandLauncher.Launch("brctl-add", new Dictionary <string, string> { { "$bridge", Interface } }); foreach (var nif in ifList) { CommandLauncher.Launch("brctl-add-if", new Dictionary <string, string> { { "$bridge", Interface }, { "$net_if", nif } }); } var model = new NetworkInterfaceConfigurationModel { Interface = Interface, Mode = (NetworkInterfaceMode)Enum.Parse(typeof(NetworkInterfaceMode), mode), Status = (NetworkInterfaceStatus)Enum.Parse(typeof(NetworkInterfaceStatus), status), StaticAddress = staticAddres, StaticRange = staticRange, Txqueuelen = txqueuelen, Mtu = mtu, Type = NetworkAdapterType.Bridge, InterfaceList = ifList, Route = route, Gateway = gateway }; NetworkConfiguration.AddInterfaceSetting(model); return(HttpStatusCode.OK); }; Post["/network/interface/bond"] = x => { string Interface = Request.Form.Interface; string mode = Request.Form.Mode; string status = Request.Form.Status; string staticAddres = Request.Form.StaticAddres; string staticRange = Request.Form.StaticRange; string txqueuelen = Request.Form.Txqueuelen; string mtu = Request.Form.Mtu; string ifs = Request.Form.InterfaceList; string route = Request.Form.Route; string gateway = Request.Form.Gateway; var ifList = ifs.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); CommandLauncher.Launch("bond-set", new Dictionary <string, string> { { "$bond", Interface } }); foreach (var nif in ifList) { CommandLauncher.Launch("bond-add-if", new Dictionary <string, string> { { "$bond", Interface }, { "$net_if", nif } }); } var model = new NetworkInterfaceConfigurationModel { Interface = Interface, Mode = (NetworkInterfaceMode)Enum.Parse(typeof(NetworkInterfaceMode), mode), Status = (NetworkInterfaceStatus)Enum.Parse(typeof(NetworkInterfaceStatus), status), StaticAddress = staticAddres, StaticRange = staticRange, Txqueuelen = txqueuelen, Mtu = mtu, Type = NetworkAdapterType.Bond, InterfaceList = ifList, Route = route, Gateway = gateway }; NetworkConfiguration.AddInterfaceSetting(model); return(HttpStatusCode.OK); }; }
public WizardModule() { Get["/wizard/data"] = x => { var bash = new Bash(); var timezones = bash.Execute("timedatectl list-timezones --no-pager").SplitBash(); var networkConfiguration = new NetworkConfiguration(); var launcher = new CommandLauncher(); var hostConfiguration = new HostConfiguration(); var hosts = launcher.Launch("cat-etc-hosts").ToArray(); var networks = launcher.Launch("cat-etc-networks").ToArray(); var resolv = launcher.Launch("cat-etc-resolv").ToArray(); var nsswitch = launcher.Launch("cat-etc-nsswitch").ToArray(); var hostnamectl = launcher.Launch("hostnamectl").ToList(); const StringSplitOptions ssoree = StringSplitOptions.RemoveEmptyEntries; var model = new PageWizardModel { Timezones = timezones, NetworkInterfaceList = networkConfiguration.InterfacePhysical, DomainInt = hostConfiguration.Host.InternalDomain, DomainExt = hostConfiguration.Host.ExternalDomain, Hosts = hosts.JoinToString(Environment.NewLine), Networks = networks.JoinToString(Environment.NewLine), Resolv = resolv.JoinToString(Environment.NewLine), Nsswitch = nsswitch.JoinToString(Environment.NewLine), StaticHostname = hostnamectl.First(_ => _.Contains("Static hostname:")).Split(new[] { ":" }, 2, ssoree)[1], Chassis = hostnamectl.First(_ => _.Contains("Chassis:")).Split(new[] { ":" }, 2, ssoree)[1], Deployment = hostnamectl.First(_ => _.Contains("Deployment:")).Split(new[] { ":" }, 2, ssoree)[1], Location = hostnamectl.First(_ => _.Contains("Location:")).Split(new[] { ":" }, 2, ssoree)[1], }; return(JsonConvert.SerializeObject(model)); }; Post["/wizard"] = x => { ConsoleLogger.Log("[wizard] start basic configuration"); string password = Request.Form.Password; var masterManager = new ManageMaster(); masterManager.ChangePassword(password); ConsoleLogger.Log("[wizard] changed master password"); string hostname = Request.Form.Hostname; string location = Request.Form.Location; string chassis = Request.Form.Chassis; string deployment = Request.Form.Deployment; ConsoleLogger.Log($"[wizard] host info:\t{hostname}"); ConsoleLogger.Log($"[wizard] host info:\t{location}"); ConsoleLogger.Log($"[wizard] host info:\t{chassis}"); ConsoleLogger.Log($"[wizard] host info:\t{deployment}"); var hostConfiguration = new HostConfiguration(); hostConfiguration.SetHostInfoName(hostname); hostConfiguration.SetHostInfoChassis(chassis); hostConfiguration.SetHostInfoDeployment(deployment); hostConfiguration.SetHostInfoLocation(location); hostConfiguration.ApplyHostInfo(); string timezone = Request.Form.Timezone; hostConfiguration.SetTimezone(timezone); hostConfiguration.ApplyTimezone(); string ntpServer = Request.Form.NtpServer; hostConfiguration.SetNtpdate(ntpServer); hostConfiguration.ApplyNtpdate(); string domainInt = Request.Form.DomainInt; string domainExt = Request.Form.DomainExt; string hosts = Request.Form.Hosts; string networks = Request.Form.Networks; string resolv = Request.Form.Resolv; string nsswitch = Request.Form.Nsswitch; hostConfiguration.SetNsHosts(hosts.Contains("\n") ? hosts.SplitToList("\n").ToArray() : hosts.SplitToList(Environment.NewLine).ToArray()); hostConfiguration.ApplyNsHosts(); hostConfiguration.SetNsNetworks(networks.Contains("\n") ? networks.SplitToList("\n").ToArray() : networks.SplitToList(Environment.NewLine).ToArray()); hostConfiguration.ApplyNsNetworks(); hostConfiguration.SetNsResolv(resolv.Contains("\n") ? resolv.SplitToList("\n").ToArray() : resolv.SplitToList(Environment.NewLine).ToArray()); hostConfiguration.ApplyNsResolv(); hostConfiguration.SetNsSwitch(nsswitch.Contains("\n") ? nsswitch.SplitToList("\n").ToArray() : nsswitch.SplitToList(Environment.NewLine).ToArray()); hostConfiguration.ApplyNsSwitch(); hostConfiguration.SetInternalDomain(domainInt); hostConfiguration.SetExtenalDomain(domainExt); ConsoleLogger.Log("[wizard] name services configured"); string Interface = Request.Form.Interface; string txqueuelen = Request.Form.Txqueuelen; string mtu = Request.Form.Mtu; string mode = Request.Form.Mode; string staticAddress = Request.Form.StaticAddress; string staticRange = Request.Form.StaticRange; var networkConfiguration = new NetworkConfiguration(); var model = new NetworkInterfaceConfigurationModel { Interface = Interface, Mode = (NetworkInterfaceMode)Enum.Parse(typeof(NetworkInterfaceMode), mode), Status = NetworkInterfaceStatus.Up, StaticAddress = staticAddress, StaticRange = staticRange, Txqueuelen = txqueuelen, Mtu = mtu, Type = NetworkInterfaceType.Physical }; networkConfiguration.AddInterfaceSetting(model); networkConfiguration.ApplyInterfaceSetting(model); ConsoleLogger.Log($"[wizard] network configured at {Interface}"); hostConfiguration.SetHostAsConfigured(); ConsoleLogger.Log("[wizard] configuration complete"); return(HttpStatusCode.OK); }; }