Exemplo n.º 1
0
        //public static void AddPool(DhcpdPool model) {
        //    var pool = ServiceModel.Pools;
        //    if(pool.Any(_ => _.Guid == model.Guid)) {
        //        return;
        //    }
        //    pool.Add(model);
        //    ServiceModel.Pools = pool;
        //    Save(ServiceModel);
        //}

        //public static void RemovePool(string guid) {
        //    var pool = ServiceModel.Pools;
        //    var model = pool.First(_ => _.Guid == guid);
        //    if(model == null) {
        //        return;
        //    }
        //    pool.Remove(model);
        //    ServiceModel.Pools = pool;
        //    Save(ServiceModel);
        //}

        public static void AddReservation(DhcpdReservation model)
        {
            var hostres = ServiceModel.Reservations;

            if (hostres.Any(_ => _.Guid == model.Guid))
            {
                return;
            }
            hostres.Add(model);
            ServiceModel.Reservations = hostres;
            Save(ServiceModel);
        }
Exemplo n.º 2
0
        //([^#]host[\s]+[\w\d]+ { hard[\w\d\s:;\-.]+[\s]*})
        public static List <DhcpdReservation> ParseReservation(string text)
        {
            var list    = new List <DhcpdReservation>();
            var regex   = new Regex("([^#]host[\\s]+[\\w\\d]+ { hard[\\w\\d\\s:;\\-.]+[\\s]*})");
            var matches = regex.Matches(text);

            foreach (var match in matches)
            {
                var hostName    = CaptureGroup(match.ToString(), "host[\\s]+([\\w\\d]+) {");
                var macAddress  = CaptureGroup(match.ToString(), "hardware ethernet[\\s]+([\\w\\d:]+);");
                var ipAddress   = CaptureGroup(match.ToString(), "fixed-address[\\s]+([\\d.]+);");
                var reservation = new DhcpdReservation {
                    HostName   = hostName,
                    MacAddress = macAddress,
                    IpAddress  = ipAddress
                };
                list.Add(reservation);
            }
            return(list);
        }
Exemplo n.º 3
0
        public AntdDhcpdModule()
        {
            Get["/dhcpd"] = x => {
                var dhcpdIsActive = DhcpdConfiguration.IsActive();
                var model         = new PageDhcpdModel {
                    DhcpdIsActive    = dhcpdIsActive,
                    DhcpdOptions     = DhcpdConfiguration.Get() ?? new DhcpdConfigurationModel(),
                    DhcpdClass       = DhcpdConfiguration.Get()?.Classes,
                    DhcpdReservation = DhcpdConfiguration.Get()?.Reservations
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/dhcpd/set"] = x => {
                DhcpdConfiguration.Set();
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/restart"] = x => {
                DhcpdConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/stop"] = x => {
                DhcpdConfiguration.Stop();
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/enable"] = x => {
                DhcpdConfiguration.Enable();
                DhcpdConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/disable"] = x => {
                DhcpdConfiguration.Disable();
                DhcpdConfiguration.Stop();
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/options"] = x => {
                string allow = Request.Form.Allow;
                string updateStaticLeases      = Request.Form.UpdateStaticLeases;
                string updateConflictDetection = Request.Form.UpdateConflictDetection;
                string useHostDeclNames        = Request.Form.UseHostDeclNames;
                string doForwardUpdates        = Request.Form.DoForwardUpdates;
                string doReverseUpdates        = Request.Form.DoReverseUpdates;
                string logFacility             = Request.Form.LogFacility;
                string option             = Request.Form.Option;
                string zoneName           = Request.Form.ZoneName;
                string zonePrimaryAddress = Request.Form.ZonePrimaryAddress;
                string ddnsUpdateStyle    = Request.Form.DdnsUpdateStyle;
                string ddnsUpdates        = Request.Form.DdnsUpdates;
                string ddnsDomainName     = Request.Form.DdnsDomainName;
                string ddnsRevDomainName  = Request.Form.DdnsRevDomainName;
                string defaultLeaseTime   = Request.Form.DefaultLeaseTime;
                string maxLeaseTime       = Request.Form.MaxLeaseTime;
                string keyName            = Request.Form.KeyName;
                string keySecret          = Request.Form.KeySecret;
                string ipFamily           = Request.Form.IpFamily;
                string ipMask             = Request.Form.IpMask;
                string optionRouters      = Request.Form.OptionRouters;
                string ntpServers         = Request.Form.NtpServers;
                string timeServers        = Request.Form.DoForTimeServerswardUpdates;
                string domainNameServers  = Request.Form.DomainNameServers;
                string broadcastAddress   = Request.Form.BroadcastAddress;
                string subnetMask         = Request.Form.SubnetMask;

                var model = new DhcpdConfigurationModel {
                    Allow = allow.SplitToList().Select(_ => _.Trim()).ToList(),
                    UpdateStaticLeases      = updateStaticLeases,
                    UpdateConflictDetection = updateConflictDetection,
                    UseHostDeclNames        = useHostDeclNames,
                    DoForwardUpdates        = doForwardUpdates,
                    DoReverseUpdates        = doReverseUpdates,
                    LogFacility             = logFacility,
                    ZoneName           = zoneName,
                    ZonePrimaryAddress = zonePrimaryAddress,
                    DdnsUpdateStyle    = ddnsUpdateStyle,
                    DdnsUpdates        = ddnsUpdates,
                    DdnsDomainName     = ddnsDomainName,
                    DdnsRevDomainName  = ddnsRevDomainName,
                    DefaultLeaseTime   = defaultLeaseTime,
                    MaxLeaseTime       = maxLeaseTime,
                    KeyName            = keyName,
                    KeySecret          = keySecret
                };
                DhcpdConfiguration.Save(model);
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/class"] = x => {
                string name      = Request.Form.Name;
                string macVendor = Request.Form.MacVendor;
                var    model     = new DhcpdClass {
                    Name             = name,
                    VendorMacAddress = macVendor
                };
                DhcpdConfiguration.AddClass(model);
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/class/del"] = x => {
                string guid = Request.Form.Guid;
                DhcpdConfiguration.RemoveClass(guid);
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/pool"] = x => {
                //string option = Request.Form.Option;
                //var model = new DhcpdPool {
                //    Options = option.SplitToList().Select(_ => _.Trim()).ToList()
                //};
                //DhcpdConfiguration.AddPool(model);
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/pool/del"] = x => {
                string guid = Request.Form.Guid;
                //DhcpdConfiguration.RemovePool(guid);
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/reservation"] = x => {
                string hostName   = Request.Form.HostName;
                string macAddress = Request.Form.MacAddress;
                string ipAddress  = Request.Form.IpAddress;
                var    model      = new DhcpdReservation {
                    HostName   = hostName,
                    MacAddress = macAddress,
                    IpAddress  = ipAddress
                };
                DhcpdConfiguration.AddReservation(model);
                return(HttpStatusCode.OK);
            };

            Post["/dhcpd/reservation/del"] = x => {
                string guid = Request.Form.Guid;
                DhcpdConfiguration.RemoveReservation(guid);
                return(HttpStatusCode.OK);
            };
        }