public void ConvertToDynamicLease(HttpListenerRequest request) { string scopeName = request.QueryString["name"]; if (string.IsNullOrEmpty(scopeName)) { throw new DnsWebServiceException("Parameter 'name' missing."); } Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName); if (scope == null) { throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName); } string strHardwareAddress = request.QueryString["hardwareAddress"]; if (string.IsNullOrEmpty(strHardwareAddress)) { throw new DnsWebServiceException("Parameter 'hardwareAddress' missing."); } scope.ConvertToDynamicLease(strHardwareAddress); _dnsWebService.DhcpServer.SaveScope(scopeName); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope's lease was unreserved successfully: " + scopeName); }
public void FlushBlockedZone(HttpListenerRequest request) { _dnsWebService.DnsServer.BlockedZoneManager.Flush(); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Blocked zone was flushed successfully."); _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile(); }
public async Task ListStoreApps(JsonTextWriter jsonWriter) { string storeAppsJsonData = await GetStoreAppsJsonData(); dynamic jsonStoreAppsArray = JsonConvert.DeserializeObject(storeAppsJsonData); jsonWriter.WritePropertyName("storeApps"); jsonWriter.WriteStartArray(); foreach (dynamic jsonStoreApp in jsonStoreAppsArray) { string name = jsonStoreApp.name.Value; string version = jsonStoreApp.version.Value; string description = jsonStoreApp.description.Value; string url = jsonStoreApp.url.Value; string size = jsonStoreApp.size.Value; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("name"); jsonWriter.WriteValue(name); jsonWriter.WritePropertyName("version"); jsonWriter.WriteValue(version); jsonWriter.WritePropertyName("description"); jsonWriter.WriteValue(description); jsonWriter.WritePropertyName("url"); jsonWriter.WriteValue(url); jsonWriter.WritePropertyName("size"); jsonWriter.WriteValue(size); bool installed = _dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication installedApp); jsonWriter.WritePropertyName("installed"); jsonWriter.WriteValue(installed); if (installed) { jsonWriter.WritePropertyName("installedVersion"); jsonWriter.WriteValue(DnsWebService.GetCleanVersion(installedApp.Version)); jsonWriter.WritePropertyName("updateAvailable"); jsonWriter.WriteValue(new Version(version) > installedApp.Version); } jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); }
public DnsServiceWorker() { string configFolder = null; string[] args = Environment.GetCommandLineArgs(); if (args.Length == 2) { configFolder = args[1]; } _service = new DnsWebService(configFolder, new Uri("https://go.technitium.com/?id=43"), new Uri("https://go.technitium.com/?id=40")); }
public void DeleteDhcpScope(HttpListenerRequest request) { string scopeName = request.QueryString["name"]; if (string.IsNullOrEmpty(scopeName)) { throw new DnsWebServiceException("Parameter 'name' missing."); } _dnsWebService.DhcpServer.DeleteScope(scopeName); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was deleted successfully: " + scopeName); }
public void UninstallApp(HttpListenerRequest request) { string name = request.QueryString["name"]; if (string.IsNullOrEmpty(name)) { throw new DnsWebServiceException("Parameter 'name' missing."); } name = name.Trim(); _dnsWebService.DnsServer.DnsApplicationManager.UninstallApplication(name); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' was uninstalled successfully."); }
public void DeleteCachedZone(HttpListenerRequest request) { string domain = request.QueryString["domain"]; if (string.IsNullOrEmpty(domain)) { throw new DnsWebServiceException("Parameter 'domain' missing."); } if (_dnsWebService.DnsServer.CacheZoneManager.DeleteZone(domain)) { _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Cached zone was deleted: " + domain); } }
public void DisableDhcpScope(HttpListenerRequest request) { string scopeName = request.QueryString["name"]; if (string.IsNullOrEmpty(scopeName)) { throw new DnsWebServiceException("Parameter 'name' missing."); } if (!_dnsWebService.DhcpServer.DisableScope(scopeName)) { throw new DnsWebServiceException("Failed to disable DHCP scope, please check logs for details: " + scopeName); } _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was disabled successfully: " + scopeName); }
public async Task SetAppConfigAsync(HttpListenerRequest request) { string name = request.QueryString["name"]; if (string.IsNullOrEmpty(name)) { throw new DnsWebServiceException("Parameter 'name' missing."); } name = name.Trim(); if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application)) { throw new DnsWebServiceException("DNS application was not found: " + name); } string formRequest; using (StreamReader sR = new StreamReader(request.InputStream, request.ContentEncoding)) { formRequest = await sR.ReadToEndAsync(); } string[] formParts = formRequest.Split('&'); foreach (string formPart in formParts) { if (formPart.StartsWith("config=")) { string config = formPart.Substring(7); if (config.Length == 0) { config = null; } await application.SetConfigAsync(config); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' app config was saved successfully."); return; } } throw new DnsWebServiceException("Missing POST parameter: config"); }
public void ImportBlockedZones(HttpListenerRequest request) { if (!request.ContentType.StartsWith("application/x-www-form-urlencoded")) { throw new DnsWebServiceException("Invalid content type. Expected application/x-www-form-urlencoded."); } string formRequest; using (StreamReader sR = new StreamReader(request.InputStream, request.ContentEncoding)) { formRequest = sR.ReadToEnd(); } string[] formParts = formRequest.Split('&'); foreach (string formPart in formParts) { if (formPart.StartsWith("blockedZones=")) { string[] blockedZones = formPart.Substring(13).Split(','); bool added = false; foreach (string blockedZone in blockedZones) { if (_dnsWebService.DnsServer.BlockedZoneManager.BlockZone(blockedZone)) { added = true; } } if (added) { _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Total " + blockedZones.Length + " zones were imported into blocked zone successfully."); _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile(); } return; } } throw new DnsWebServiceException("Parameter 'blockedZones' missing."); }
public void RemoveDhcpLease(HttpListenerRequest request) { string scopeName = request.QueryString["name"]; if (string.IsNullOrEmpty(scopeName)) { throw new DnsWebServiceException("Parameter 'name' missing."); } string strHardwareAddress = request.QueryString["hardwareAddress"]; if (string.IsNullOrEmpty(strHardwareAddress)) { throw new DnsWebServiceException("Parameter 'hardwareAddress' missing."); } _dnsWebService.DhcpServer.RemoveLease(scopeName, strHardwareAddress); _dnsWebService.DhcpServer.SaveScope(scopeName); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope's lease was removed successfully: " + scopeName); }
public void ConvertToDynamicLease(HttpListenerRequest request) { string scopeName = request.QueryString["name"]; if (string.IsNullOrEmpty(scopeName)) { throw new DnsWebServiceException("Parameter 'name' missing."); } Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName); if (scope == null) { throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName); } string strClientIdentifier = request.QueryString["clientIdentifier"]; string strHardwareAddress = request.QueryString["hardwareAddress"]; if (!string.IsNullOrEmpty(strClientIdentifier)) { scope.ConvertToDynamicLease(ClientIdentifierOption.Parse(strClientIdentifier)); } else if (!string.IsNullOrEmpty(strHardwareAddress)) { scope.ConvertToDynamicLease(strHardwareAddress); } else { throw new DnsWebServiceException("Parameter 'hardwareAddress' or 'clientIdentifier' missing. At least one of them must be specified."); } _dnsWebService.DhcpServer.SaveScope(scopeName); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope's lease was unreserved successfully: " + scopeName); }
public async Task ListInstalledAppsAsync(JsonTextWriter jsonWriter) { List <string> apps = new List <string>(_dnsWebService.DnsServer.DnsApplicationManager.Applications.Keys); apps.Sort(); dynamic jsonStoreAppsArray = null; if (apps.Count > 0) { try { string storeAppsJsonData = await GetStoreAppsJsonData().WithTimeout(5000); jsonStoreAppsArray = JsonConvert.DeserializeObject(storeAppsJsonData); } catch { } } jsonWriter.WritePropertyName("apps"); jsonWriter.WriteStartArray(); foreach (string app in apps) { if (_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(app, out DnsApplication application)) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("name"); jsonWriter.WriteValue(application.Name); jsonWriter.WritePropertyName("version"); jsonWriter.WriteValue(DnsWebService.GetCleanVersion(application.Version)); if (jsonStoreAppsArray != null) { foreach (dynamic jsonStoreApp in jsonStoreAppsArray) { string name = jsonStoreApp.name.Value; if (name.Equals(application.Name)) { string version = jsonStoreApp.version.Value; string url = jsonStoreApp.url.Value; jsonWriter.WritePropertyName("updateVersion"); jsonWriter.WriteValue(version); jsonWriter.WritePropertyName("updateUrl"); jsonWriter.WriteValue(url); jsonWriter.WritePropertyName("updateAvailable"); jsonWriter.WriteValue(new Version(version) > application.Version); break; } } } jsonWriter.WritePropertyName("dnsApps"); { jsonWriter.WriteStartArray(); foreach (KeyValuePair <string, IDnsApplication> dnsApp in application.DnsApplications) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("classPath"); jsonWriter.WriteValue(dnsApp.Key); jsonWriter.WritePropertyName("description"); jsonWriter.WriteValue(dnsApp.Value.Description); if (dnsApp.Value is IDnsAppRecordRequestHandler appRecordHandler) { jsonWriter.WritePropertyName("isAppRecordRequestHandler"); jsonWriter.WriteValue(true); jsonWriter.WritePropertyName("recordDataTemplate"); jsonWriter.WriteValue(appRecordHandler.ApplicationRecordDataTemplate); } else { jsonWriter.WritePropertyName("isAppRecordRequestHandler"); jsonWriter.WriteValue(false); } jsonWriter.WritePropertyName("isRequestController"); jsonWriter.WriteValue(dnsApp.Value is IDnsRequestController); jsonWriter.WritePropertyName("isAuthoritativeRequestHandler"); jsonWriter.WriteValue(dnsApp.Value is IDnsAuthoritativeRequestHandler); jsonWriter.WritePropertyName("isQueryLogger"); jsonWriter.WriteValue(dnsApp.Value is IDnsQueryLogger); jsonWriter.WriteEndObject(); } jsonWriter.WriteEndArray(); } jsonWriter.WriteEndObject(); } } jsonWriter.WriteEndArray(); }
public WebServiceOtherZonesApi(DnsWebService dnsWebService) { _dnsWebService = dnsWebService; }
public WebServiceAppsApi(DnsWebService dnsWebService, Uri appStoreUri) { _dnsWebService = dnsWebService; _appStoreUri = appStoreUri; }
public async Task SetDhcpScopeAsync(HttpListenerRequest request) { string scopeName = request.QueryString["name"]; if (string.IsNullOrEmpty(scopeName)) { throw new DnsWebServiceException("Parameter 'name' missing."); } string strStartingAddress = request.QueryString["startingAddress"]; string strEndingAddress = request.QueryString["endingAddress"]; string strSubnetMask = request.QueryString["subnetMask"]; bool scopeExists; Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName); if (scope is null) { //scope does not exists; create new scope if (string.IsNullOrEmpty(strStartingAddress)) { throw new DnsWebServiceException("Parameter 'startingAddress' missing."); } if (string.IsNullOrEmpty(strEndingAddress)) { throw new DnsWebServiceException("Parameter 'endingAddress' missing."); } if (string.IsNullOrEmpty(strSubnetMask)) { throw new DnsWebServiceException("Parameter 'subnetMask' missing."); } scopeExists = false; scope = new Scope(scopeName, true, IPAddress.Parse(strStartingAddress), IPAddress.Parse(strEndingAddress), IPAddress.Parse(strSubnetMask)); } else { scopeExists = true; IPAddress startingAddress; if (string.IsNullOrEmpty(strStartingAddress)) { startingAddress = scope.StartingAddress; } else { startingAddress = IPAddress.Parse(strStartingAddress); } IPAddress endingAddress; if (string.IsNullOrEmpty(strEndingAddress)) { endingAddress = scope.EndingAddress; } else { endingAddress = IPAddress.Parse(strEndingAddress); } IPAddress subnetMask; if (string.IsNullOrEmpty(strSubnetMask)) { subnetMask = scope.SubnetMask; } else { subnetMask = IPAddress.Parse(strSubnetMask); } //validate scope address foreach (KeyValuePair <string, Scope> entry in _dnsWebService.DhcpServer.Scopes) { Scope existingScope = entry.Value; if (existingScope.Equals(scope)) { continue; } if (existingScope.IsAddressInRange(startingAddress) || existingScope.IsAddressInRange(endingAddress)) { throw new DhcpServerException("Scope with overlapping range already exists: " + existingScope.StartingAddress.ToString() + "-" + existingScope.EndingAddress.ToString()); } } scope.ChangeNetwork(startingAddress, endingAddress, subnetMask); } string strLeaseTimeDays = request.QueryString["leaseTimeDays"]; if (!string.IsNullOrEmpty(strLeaseTimeDays)) { scope.LeaseTimeDays = ushort.Parse(strLeaseTimeDays); } string strLeaseTimeHours = request.QueryString["leaseTimeHours"]; if (!string.IsNullOrEmpty(strLeaseTimeHours)) { scope.LeaseTimeHours = byte.Parse(strLeaseTimeHours); } string strLeaseTimeMinutes = request.QueryString["leaseTimeMinutes"]; if (!string.IsNullOrEmpty(strLeaseTimeMinutes)) { scope.LeaseTimeMinutes = byte.Parse(strLeaseTimeMinutes); } string strOfferDelayTime = request.QueryString["offerDelayTime"]; if (!string.IsNullOrEmpty(strOfferDelayTime)) { scope.OfferDelayTime = ushort.Parse(strOfferDelayTime); } string strPingCheckEnabled = request.QueryString["pingCheckEnabled"]; if (!string.IsNullOrEmpty(strPingCheckEnabled)) { scope.PingCheckEnabled = bool.Parse(strPingCheckEnabled); } string strPingCheckTimeout = request.QueryString["pingCheckTimeout"]; if (!string.IsNullOrEmpty(strPingCheckTimeout)) { scope.PingCheckTimeout = ushort.Parse(strPingCheckTimeout); } string strPingCheckRetries = request.QueryString["pingCheckRetries"]; if (!string.IsNullOrEmpty(strPingCheckRetries)) { scope.PingCheckRetries = byte.Parse(strPingCheckRetries); } string strDomainName = request.QueryString["domainName"]; if (strDomainName != null) { scope.DomainName = strDomainName.Length == 0 ? null : strDomainName; } string strDnsTtl = request.QueryString["dnsTtl"]; if (!string.IsNullOrEmpty(strDnsTtl)) { scope.DnsTtl = uint.Parse(strDnsTtl); } string strServerAddress = request.QueryString["serverAddress"]; if (strServerAddress != null) { scope.ServerAddress = strServerAddress.Length == 0 ? null : IPAddress.Parse(strServerAddress); } string strServerHostName = request.QueryString["serverHostName"]; if (strServerHostName != null) { scope.ServerHostName = strServerHostName.Length == 0 ? null : strServerHostName; } string strBootFileName = request.QueryString["bootFileName"]; if (strBootFileName != null) { scope.BootFileName = strBootFileName.Length == 0 ? null : strBootFileName; } string strRouterAddress = request.QueryString["routerAddress"]; if (strRouterAddress != null) { scope.RouterAddress = strRouterAddress.Length == 0 ? null : IPAddress.Parse(strRouterAddress); } string strUseThisDnsServer = request.QueryString["useThisDnsServer"]; if (!string.IsNullOrEmpty(strUseThisDnsServer)) { scope.UseThisDnsServer = bool.Parse(strUseThisDnsServer); } if (!scope.UseThisDnsServer) { string strDnsServers = request.QueryString["dnsServers"]; if (strDnsServers != null) { if (strDnsServers.Length == 0) { scope.DnsServers = null; } else { string[] strDnsServerParts = strDnsServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); IPAddress[] dnsServers = new IPAddress[strDnsServerParts.Length]; for (int i = 0; i < strDnsServerParts.Length; i++) { dnsServers[i] = IPAddress.Parse(strDnsServerParts[i]); } scope.DnsServers = dnsServers; } } } string strWinsServers = request.QueryString["winsServers"]; if (strWinsServers != null) { if (strWinsServers.Length == 0) { scope.WinsServers = null; } else { string[] strWinsServerParts = strWinsServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); IPAddress[] winsServers = new IPAddress[strWinsServerParts.Length]; for (int i = 0; i < strWinsServerParts.Length; i++) { winsServers[i] = IPAddress.Parse(strWinsServerParts[i]); } scope.WinsServers = winsServers; } } string strNtpServers = request.QueryString["ntpServers"]; if (strNtpServers != null) { if (strNtpServers.Length == 0) { scope.NtpServers = null; } else { string[] strNtpServerParts = strNtpServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); IPAddress[] ntpServers = new IPAddress[strNtpServerParts.Length]; for (int i = 0; i < strNtpServerParts.Length; i++) { ntpServers[i] = IPAddress.Parse(strNtpServerParts[i]); } scope.NtpServers = ntpServers; } } string strStaticRoutes = request.QueryString["staticRoutes"]; if (strStaticRoutes != null) { if (strStaticRoutes.Length == 0) { scope.StaticRoutes = null; } else { string[] strStaticRoutesParts = strStaticRoutes.Split('|'); List <ClasslessStaticRouteOption.Route> staticRoutes = new List <ClasslessStaticRouteOption.Route>(); for (int i = 0; i < strStaticRoutesParts.Length; i += 3) { staticRoutes.Add(new ClasslessStaticRouteOption.Route(IPAddress.Parse(strStaticRoutesParts[i + 0]), IPAddress.Parse(strStaticRoutesParts[i + 1]), IPAddress.Parse(strStaticRoutesParts[i + 2]))); } scope.StaticRoutes = staticRoutes; } } string strVendorInfo = request.QueryString["vendorInfo"]; if (strVendorInfo != null) { if (strVendorInfo.Length == 0) { scope.VendorInfo = null; } else { string[] strVendorInfoParts = strVendorInfo.Split('|'); Dictionary <string, VendorSpecificInformationOption> vendorInfo = new Dictionary <string, VendorSpecificInformationOption>(); for (int i = 0; i < strVendorInfoParts.Length; i += 2) { vendorInfo.Add(strVendorInfoParts[i + 0], new VendorSpecificInformationOption(strVendorInfoParts[i + 1])); } scope.VendorInfo = vendorInfo; } } string strExclusions = request.QueryString["exclusions"]; if (strExclusions != null) { if (strExclusions.Length == 0) { scope.Exclusions = null; } else { string[] strExclusionsParts = strExclusions.Split('|'); List <Exclusion> exclusions = new List <Exclusion>(); for (int i = 0; i < strExclusionsParts.Length; i += 2) { exclusions.Add(new Exclusion(IPAddress.Parse(strExclusionsParts[i + 0]), IPAddress.Parse(strExclusionsParts[i + 1]))); } scope.Exclusions = exclusions; } } string strReservedLeases = request.QueryString["reservedLeases"]; if (strReservedLeases != null) { if (strReservedLeases.Length == 0) { scope.ReservedLeases = null; } else { string[] strReservedLeaseParts = strReservedLeases.Split('|'); List <Lease> reservedLeases = new List <Lease>(); for (int i = 0; i < strReservedLeaseParts.Length; i += 4) { reservedLeases.Add(new Lease(LeaseType.Reserved, strReservedLeaseParts[i + 0], DhcpMessageHardwareAddressType.Ethernet, strReservedLeaseParts[i + 1], IPAddress.Parse(strReservedLeaseParts[i + 2]), strReservedLeaseParts[i + 3])); } scope.ReservedLeases = reservedLeases; } } string strAllowOnlyReservedLeases = request.QueryString["allowOnlyReservedLeases"]; if (!string.IsNullOrEmpty(strAllowOnlyReservedLeases)) { scope.AllowOnlyReservedLeases = bool.Parse(strAllowOnlyReservedLeases); } if (scopeExists) { _dnsWebService.DhcpServer.SaveScope(scopeName); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was updated successfully: " + scopeName); } else { await _dnsWebService.DhcpServer.AddScopeAsync(scope); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was added successfully: " + scopeName); } string newName = request.QueryString["newName"]; if (!string.IsNullOrEmpty(newName) && !newName.Equals(scopeName)) { _dnsWebService.DhcpServer.RenameScope(scopeName, newName); _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was renamed successfully: '" + scopeName + "' to '" + newName + "'"); } }
public WebServiceDhcpApi(DnsWebService dnsWebService) { _dnsWebService = dnsWebService; }