2021-09-18 19:02:40 +05:30
/ *
Technitium DNS Server
2025-02-15 12:51:16 +05:30
Copyright ( C ) 2025 Shreyas Zare ( shreyas @technitium . com )
2021-09-18 19:02:40 +05:30
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
2023-01-01 18:10:10 +05:30
using DnsServerCore.Auth ;
2021-09-18 19:02:40 +05:30
using DnsServerCore.Dns.Zones ;
2023-01-01 18:10:10 +05:30
using Microsoft.AspNetCore.Http ;
2021-09-18 19:02:40 +05:30
using System.Collections.Generic ;
using System.IO ;
using System.Net ;
2022-12-24 13:12:02 +05:30
using System.Text.Json ;
2022-04-09 17:36:52 +05:30
using System.Threading.Tasks ;
2022-11-26 11:53:04 +05:30
using TechnitiumLibrary.Net ;
2023-04-23 16:30:49 +05:30
using TechnitiumLibrary.Net.Dns ;
2021-11-06 13:30:18 +05:30
using TechnitiumLibrary.Net.Dns.ResourceRecords ;
2021-09-18 19:02:40 +05:30
namespace DnsServerCore
{
2025-02-15 12:51:16 +05:30
public partial class DnsWebService
2021-09-18 19:02:40 +05:30
{
2025-02-15 12:51:16 +05:30
class WebServiceOtherZonesApi
{
#region variables
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
readonly DnsWebService _dnsWebService ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#endregion
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#region constructor
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public WebServiceOtherZonesApi ( DnsWebService dnsWebService )
{
_dnsWebService = dnsWebService ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#endregion
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#region public
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#region cache api
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
public void FlushCache ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Cache , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . CacheZoneManager . Flush ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Cache was flushed." ) ;
}
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
public void ListCachedZones ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Cache , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
string domain = request . GetQueryOrForm ( "domain" , "" ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string direction = request . QueryOrForm ( "direction" ) ;
if ( direction is not null )
direction = direction . ToLowerInvariant ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
List < string > subZones = new List < string > ( ) ;
List < DnsResourceRecord > records = new List < DnsResourceRecord > ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
while ( true )
{
subZones . Clear ( ) ;
records . Clear ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . CacheZoneManager . ListSubDomains ( domain , subZones ) ;
_dnsWebService . _dnsServer . CacheZoneManager . ListAllRecords ( domain , records ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( records . Count > 0 )
break ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( subZones . Count ! = 1 )
2021-09-18 19:02:40 +05:30
break ;
2025-02-15 12:51:16 +05:30
if ( direction = = "up" )
{
if ( domain . Length = = 0 )
break ;
int i = domain . IndexOf ( '.' ) ;
if ( i < 0 )
domain = "" ;
else
domain = domain . Substring ( i + 1 ) ;
}
else if ( domain . Length = = 0 )
{
domain = subZones [ 0 ] ;
}
2021-09-18 19:02:40 +05:30
else
2025-02-15 12:51:16 +05:30
{
domain = subZones [ 0 ] + "." + domain ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
subZones . Sort ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
Utf8JsonWriter jsonWriter = context . GetCurrentJsonWriter ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "domain" , domain ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . TryConvertDomainNameToUnicode ( domain , out string idn ) )
jsonWriter . WriteString ( "domainIdn" , idn ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "zones" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( domain . Length ! = 0 )
domain = "." + domain ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
foreach ( string subZone in subZones )
{
string zone = subZone + domain ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . TryConvertDomainNameToUnicode ( zone , out string zoneIdn ) )
zone = zoneIdn ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteStringValue ( zone ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
WebServiceZonesApi . WriteRecordsAsJson ( records , jsonWriter , false ) ;
}
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
public void DeleteCachedZone ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Cache , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string domain = context . Request . GetQueryOrForm ( "domain" ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( _dnsWebService . _dnsServer . CacheZoneManager . DeleteZone ( domain ) )
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Cached zone was deleted: " + domain ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#endregion
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#region allowed zones api
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
public void ListAllowedZones ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Allowed , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string domain = request . GetQueryOrForm ( "domain" , "" ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string direction = request . QueryOrForm ( "direction" ) ;
if ( direction is not null )
direction = direction . ToLowerInvariant ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
List < string > subZones = new List < string > ( ) ;
List < DnsResourceRecord > records = new List < DnsResourceRecord > ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
while ( true )
{
subZones . Clear ( ) ;
records . Clear ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . AllowedZoneManager . ListSubDomains ( domain , subZones ) ;
_dnsWebService . _dnsServer . AllowedZoneManager . ListAllRecords ( domain , records ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( records . Count > 0 )
break ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( subZones . Count ! = 1 )
2021-09-18 19:02:40 +05:30
break ;
2025-02-15 12:51:16 +05:30
if ( direction = = "up" )
{
if ( domain . Length = = 0 )
break ;
int i = domain . IndexOf ( '.' ) ;
if ( i < 0 )
domain = "" ;
else
domain = domain . Substring ( i + 1 ) ;
}
else if ( domain . Length = = 0 )
{
domain = subZones [ 0 ] ;
}
2021-09-18 19:02:40 +05:30
else
2025-02-15 12:51:16 +05:30
{
domain = subZones [ 0 ] + "." + domain ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
subZones . Sort ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
Utf8JsonWriter jsonWriter = context . GetCurrentJsonWriter ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "domain" , domain ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . TryConvertDomainNameToUnicode ( domain , out string idn ) )
jsonWriter . WriteString ( "domainIdn" , idn ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "zones" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( domain . Length ! = 0 )
domain = "." + domain ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( string subZone in subZones )
{
string zone = subZone + domain ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . TryConvertDomainNameToUnicode ( zone , out string zoneIdn ) )
zone = zoneIdn ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteStringValue ( zone ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
WebServiceZonesApi . WriteRecordsAsJson ( records , jsonWriter , true ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void ImportAllowedZones ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Allowed , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
string allowedZones = request . GetQueryOrForm ( "allowedZones" ) ;
string [ ] allowedZonesList = allowedZones . Split ( ',' ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
for ( int i = 0 ; i < allowedZonesList . Length ; i + + )
{
if ( DnsClient . IsDomainNameUnicode ( allowedZonesList [ i ] ) )
allowedZonesList [ i ] = DnsClient . ConvertDomainNameToAscii ( allowedZonesList [ i ] ) ;
}
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . AllowedZoneManager . ImportZones ( allowedZonesList ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Total " + allowedZonesList . Length + " zones were imported into allowed zone successfully." ) ;
_dnsWebService . _dnsServer . AllowedZoneManager . SaveZoneFile ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public async Task ExportAllowedZonesAsync ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Allowed , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
IReadOnlyList < AuthZoneInfo > zoneInfoList = _dnsWebService . _dnsServer . AllowedZoneManager . GetAllZones ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
HttpResponse response = context . Response ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
response . ContentType = "text/plain" ;
response . Headers . ContentDisposition = "attachment;filename=AllowedZones.txt" ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
await using ( StreamWriter sW = new StreamWriter ( response . Body ) )
{
foreach ( AuthZoneInfo zoneInfo in zoneInfoList )
await sW . WriteLineAsync ( zoneInfo . Name ) ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
public void DeleteAllowedZone ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Allowed , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
string domain = context . Request . GetQueryOrForm ( "domain" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( _dnsWebService . _dnsServer . AllowedZoneManager . DeleteZone ( domain ) )
{
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Allowed zone was deleted: " + domain ) ;
_dnsWebService . _dnsServer . AllowedZoneManager . SaveZoneFile ( ) ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
public void FlushAllowedZone ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Allowed , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . AllowedZoneManager . Flush ( ) ;
2022-03-26 11:56:52 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Allowed zone was flushed successfully." ) ;
_dnsWebService . _dnsServer . AllowedZoneManager . SaveZoneFile ( ) ;
}
2022-03-26 11:56:52 +05:30
2025-02-15 12:51:16 +05:30
public void AllowZone ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Allowed , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
string domain = context . Request . GetQueryOrForm ( "domain" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( IPAddress . TryParse ( domain , out IPAddress ipAddress ) )
domain = ipAddress . GetReverseDomain ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( _dnsWebService . _dnsServer . AllowedZoneManager . AllowZone ( domain ) )
{
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Zone was allowed: " + domain ) ;
_dnsWebService . _dnsServer . AllowedZoneManager . SaveZoneFile ( ) ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
#endregion
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#region blocked zones api
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
public void ListBlockedZones ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Blocked , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string domain = request . GetQueryOrForm ( "domain" , "" ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
string direction = request . QueryOrForm ( "direction" ) ;
if ( direction is not null )
direction = direction . ToLowerInvariant ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
List < string > subZones = new List < string > ( ) ;
List < DnsResourceRecord > records = new List < DnsResourceRecord > ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
while ( true )
{
subZones . Clear ( ) ;
records . Clear ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . BlockedZoneManager . ListSubDomains ( domain , subZones ) ;
_dnsWebService . _dnsServer . BlockedZoneManager . ListAllRecords ( domain , records ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( records . Count > 0 )
break ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( subZones . Count ! = 1 )
2021-09-18 19:02:40 +05:30
break ;
2025-02-15 12:51:16 +05:30
if ( direction = = "up" )
{
if ( domain . Length = = 0 )
break ;
int i = domain . IndexOf ( '.' ) ;
if ( i < 0 )
domain = "" ;
else
domain = domain . Substring ( i + 1 ) ;
}
else if ( domain . Length = = 0 )
{
domain = subZones [ 0 ] ;
}
2021-09-18 19:02:40 +05:30
else
2025-02-15 12:51:16 +05:30
{
domain = subZones [ 0 ] + "." + domain ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
subZones . Sort ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
Utf8JsonWriter jsonWriter = context . GetCurrentJsonWriter ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteString ( "domain" , domain ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . TryConvertDomainNameToUnicode ( domain , out string idn ) )
jsonWriter . WriteString ( "domainIdn" , idn ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WritePropertyName ( "zones" ) ;
jsonWriter . WriteStartArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( domain . Length ! = 0 )
domain = "." + domain ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
foreach ( string subZone in subZones )
{
string zone = subZone + domain ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . TryConvertDomainNameToUnicode ( zone , out string zoneIdn ) )
zone = zoneIdn ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteStringValue ( zone ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
jsonWriter . WriteEndArray ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
WebServiceZonesApi . WriteRecordsAsJson ( records , jsonWriter , true ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public void ImportBlockedZones ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Blocked , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
HttpRequest request = context . Request ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
string blockedZones = request . GetQueryOrForm ( "blockedZones" ) ;
string [ ] blockedZonesList = blockedZones . Split ( ',' ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
for ( int i = 0 ; i < blockedZonesList . Length ; i + + )
{
if ( DnsClient . IsDomainNameUnicode ( blockedZonesList [ i ] ) )
blockedZonesList [ i ] = DnsClient . ConvertDomainNameToAscii ( blockedZonesList [ i ] ) ;
}
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . BlockedZoneManager . ImportZones ( blockedZonesList ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Total " + blockedZonesList . Length + " zones were imported into blocked zone successfully." ) ;
_dnsWebService . _dnsServer . BlockedZoneManager . SaveZoneFile ( ) ;
}
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
public async Task ExportBlockedZonesAsync ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Blocked , session . User , PermissionFlag . View ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
IReadOnlyList < AuthZoneInfo > zoneInfoList = _dnsWebService . _dnsServer . BlockedZoneManager . GetAllZones ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
HttpResponse response = context . Response ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
response . ContentType = "text/plain" ;
response . Headers . ContentDisposition = "attachment;filename=BlockedZones.txt" ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
await using ( StreamWriter sW = new StreamWriter ( response . Body ) )
{
foreach ( AuthZoneInfo zoneInfo in zoneInfoList )
await sW . WriteLineAsync ( zoneInfo . Name ) ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
public void DeleteBlockedZone ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Blocked , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
string domain = context . Request . GetQueryOrForm ( "domain" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( _dnsWebService . _dnsServer . BlockedZoneManager . DeleteZone ( domain ) )
{
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Blocked zone was deleted: " + domain ) ;
_dnsWebService . _dnsServer . BlockedZoneManager . SaveZoneFile ( ) ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
public void FlushBlockedZone ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Blocked , session . User , PermissionFlag . Delete ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _dnsServer . BlockedZoneManager . Flush ( ) ;
2022-03-26 11:56:52 +05:30
2025-02-15 12:51:16 +05:30
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Blocked zone was flushed successfully." ) ;
_dnsWebService . _dnsServer . BlockedZoneManager . SaveZoneFile ( ) ;
}
2022-03-26 11:56:52 +05:30
2025-02-15 12:51:16 +05:30
public void BlockZone ( HttpContext context )
{
UserSession session = context . GetCurrentSession ( ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
if ( ! _dnsWebService . _authManager . IsPermitted ( PermissionSection . Blocked , session . User , PermissionFlag . Modify ) )
throw new DnsWebServiceException ( "Access was denied." ) ;
2023-01-01 18:10:10 +05:30
2025-02-15 12:51:16 +05:30
string domain = context . Request . GetQueryOrForm ( "domain" ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( DnsClient . IsDomainNameUnicode ( domain ) )
domain = DnsClient . ConvertDomainNameToAscii ( domain ) ;
2023-04-23 16:30:49 +05:30
2025-02-15 12:51:16 +05:30
if ( IPAddress . TryParse ( domain , out IPAddress ipAddress ) )
domain = ipAddress . GetReverseDomain ( ) ;
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
if ( _dnsWebService . _dnsServer . BlockedZoneManager . BlockZone ( domain ) )
{
_dnsWebService . _log . Write ( context . GetRemoteEndPoint ( _dnsWebService . _webServiceRealIpHeader ) , "[" + session . User . Username + "] Domain was added to blocked zone: " + domain ) ;
_dnsWebService . _dnsServer . BlockedZoneManager . SaveZoneFile ( ) ;
}
2021-09-18 19:02:40 +05:30
}
2025-02-15 12:51:16 +05:30
#endregion
2021-09-18 19:02:40 +05:30
2025-02-15 12:51:16 +05:30
#endregion
}
2021-09-18 19:02:40 +05:30
}
}