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); }; }
private static void FallbackProcedures() { Logger.Info("[config] fallback procedures"); if (!Parameter.IsUnix) { return; } const string localNetwork = "10.11.0.0"; const string localIp = "10.11.254.254"; const string localRange = "16"; const string localHostname = "box01"; const string localDomain = "install.local"; #region [ Host Configuration ] HostConfiguration.SetHostInfoName(localHostname); HostConfiguration.ApplyHostInfo(); Logger.Info("host configured"); #endregion #region [ Name Service ] HostConfiguration.SetNsHosts(new[] { "127.0.0.1 localhost", $"{localIp} {localHostname}.{localDomain} {localHostname}" }); HostConfiguration.ApplyNsHosts(); HostConfiguration.SetNsNetworks(new[] { "loopback 127.0.0.0", "link-local 169.254.0.0", $"{localDomain} {localNetwork}" }); HostConfiguration.ApplyNsNetworks(); HostConfiguration.SetNsResolv(new[] { $"nameserver {localIp}", $"search {localDomain}", $"domain {localDomain}" }); HostConfiguration.ApplyNsResolv(); HostConfiguration.SetNsSwitch(new[] { "passwd: compat db files nis", "shadow: compat db files nis", "group: compat db files nis", "hosts: files dns", "networks: files dns", "services: db files", "protocols: db files", "rpc: db files", "ethers: db files", "netmasks: files", "netgroup: files", "bootparams: files", "automount: files", "aliases: files" }); HostConfiguration.ApplyNsSwitch(); Logger.Info("name service ready"); #endregion #region [ Network ] var npi = NetworkConfiguration.InterfacePhysical; var nifs = NetworkConfiguration.Get().Interfaces; const string nifName = "br0"; var tryget = nifs?.FirstOrDefault(_ => _.Interface == nifName); if (tryget == null) { NetworkConfiguration.AddInterfaceSetting(new NetworkInterfaceConfigurationModel { Interface = nifName, Mode = NetworkInterfaceMode.Static, Status = NetworkInterfaceStatus.Up, StaticAddress = localIp, StaticRange = localRange, Type = NetworkInterfaceType.Bridge, InterfaceList = npi.ToList() }); } NetworkConfiguration.ApplyDefaultInterfaceSetting(); #endregion #region [ Dhcpd ] DhcpdConfiguration.Save(new DhcpdConfigurationModel { ZoneName = localDomain, ZonePrimaryAddress = localIp, DdnsDomainName = $"{localDomain}.", Option = new List <string> { $"domain-name \"{localDomain}\"", "routers eth0", "local-proxy-config code 252 = text" }, KeySecret = "ND991KFHCCA9tUrafsf29uxDM3ZKfnrVR4f1I2J27Ow=", SubnetNtpServers = localIp, SubnetTimeServers = localIp, SubnetOptionRouters = localIp, SubnetDomainNameServers = localIp, SubnetIpMask = "255.255.0.0", SubnetMask = "255.255.0.0", SubnetBroadcastAddress = "10.11.255.255", SubnetIpFamily = localNetwork }); DhcpdConfiguration.Set(); #endregion #region [ Bind ] BindConfiguration.Save(new BindConfigurationModel { ControlIp = localIp, AclInternalInterfaces = new List <string> { localIp }, AclInternalNetworks = new List <string> { $"{localNetwork}/{localRange}" }, Zones = new List <BindConfigurationZoneModel> { new BindConfigurationZoneModel { Name = "11.10.in-addr.arpa", Type = "master", File = "" //todo crea e gestisci file della zona }, new BindConfigurationZoneModel { Name = localDomain, Type = "master", File = "" //todo crea e gestisci file della zona }, } }); BindConfiguration.Set(); #endregion }
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); }; }