Skip to content

Commit

Permalink
Merge pull request #231 from Moonlight-Panel/AdditionalDnsErrorHandling
Browse files Browse the repository at this point in the history
Added additional dns error handling
  • Loading branch information
Masu-Baumgartner authored Jul 20, 2023
2 parents daba4cb + daeb4dd commit 11708fb
Showing 1 changed file with 53 additions and 42 deletions.
95 changes: 53 additions & 42 deletions Moonlight/App/Services/DomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,59 +182,70 @@ public async Task AddDnsRecord(Domain d, DnsRecord dnsRecord)
{
if (!ConfigService.Get().Moonlight.Domains.Enable)
throw new DisplayException("This operation is disabled");

var domain = EnsureData(d);

var rname = $"{domain.Name}.{domain.SharedDomain.Name}";
var dname = $".{rname}";

if (dnsRecord.Type == DnsRecordType.Srv)
try
{
var parts = dnsRecord.Name.Split(".");
var domain = EnsureData(d);

Protocol protocol = Protocol.Tcp;
var rname = $"{domain.Name}.{domain.SharedDomain.Name}";
var dname = $".{rname}";

if (parts[1].Contains("udp"))
protocol = Protocol.Udp;
if (dnsRecord.Type == DnsRecordType.Srv)
{
var parts = dnsRecord.Name.Split(".");

var valueParts = dnsRecord.Content.Split(" ");
Protocol protocol = Protocol.Tcp;

var nameWithoutProt = dnsRecord.Name.Replace($"{parts[0]}.{parts[1]}.", "");
nameWithoutProt = nameWithoutProt.Replace($"{parts[0]}.{parts[1]}", "");
var name = nameWithoutProt == "" ? rname : nameWithoutProt + dname;
if (parts[1].Contains("udp"))
protocol = Protocol.Udp;

var valueParts = dnsRecord.Content.Split(" ");

var nameWithoutProt = dnsRecord.Name.Replace($"{parts[0]}.{parts[1]}.", "");
nameWithoutProt = nameWithoutProt.Replace($"{parts[0]}.{parts[1]}", "");
var name = nameWithoutProt == "" ? rname : nameWithoutProt + dname;

var srv = new NewDnsRecord<Srv>()
{
Type = dnsRecord.Type,
Data = new()
{
Service = parts[0],
Protocol = protocol,
Name = name,
Weight = int.Parse(valueParts[0]),
Port = int.Parse(valueParts[1]),
Target = valueParts[2],
Priority = dnsRecord.Priority
},
Proxied = dnsRecord.Proxied,
Ttl = dnsRecord.Ttl,
};

var srv = new NewDnsRecord<Srv>()
GetData(await Client.Zones.DnsRecords.AddAsync(d.SharedDomain.CloudflareId, srv));
}
else
{
Type = dnsRecord.Type,
Data = new()
var name = string.IsNullOrEmpty(dnsRecord.Name) ? rname : dnsRecord.Name + dname;

GetData(await Client.Zones.DnsRecords.AddAsync(d.SharedDomain.CloudflareId, new NewDnsRecord()
{
Service = parts[0],
Protocol = protocol,
Name = name,
Weight = int.Parse(valueParts[0]),
Port = int.Parse(valueParts[1]),
Target = valueParts[2],
Priority = dnsRecord.Priority
},
Proxied = dnsRecord.Proxied,
Ttl = dnsRecord.Ttl,
};

GetData(await Client.Zones.DnsRecords.AddAsync(d.SharedDomain.CloudflareId, srv));
Type = dnsRecord.Type,
Priority = dnsRecord.Priority,
Content = dnsRecord.Content,
Proxied = dnsRecord.Proxied,
Ttl = dnsRecord.Ttl,
Name = name
}));
}
}
else
catch (OverflowException)
{
var name = string.IsNullOrEmpty(dnsRecord.Name) ? rname : dnsRecord.Name + dname;

GetData(await Client.Zones.DnsRecords.AddAsync(d.SharedDomain.CloudflareId, new NewDnsRecord()
{
Type = dnsRecord.Type,
Priority = dnsRecord.Priority,
Content = dnsRecord.Content,
Proxied = dnsRecord.Proxied,
Ttl = dnsRecord.Ttl,
Name = name
}));
throw new DisplayException("Invalid dns record values");
}
catch (FormatException)
{
throw new DisplayException("Invalid dns record values");
}

//TODO: AuditLog
Expand Down

0 comments on commit 11708fb

Please sign in to comment.