예제 #1
0
        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);
            };
        }