Skip to content

Commit

Permalink
fix(autoconfig): Refactor DNS query for testing
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Aug 6, 2024
1 parent cd789d5 commit 8160d97
Show file tree
Hide file tree
Showing 10 changed files with 16,515 additions and 13 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/update-public-suffix-list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: MIT
name: Update public suffix list

on:
workflow_dispatch:
schedule:
- cron: "5 2 * * *"

jobs:
update-public-suffix-list:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
branches: ['main', 'stable3.7']

name: update-public-suffix-list-${{ matrix.branches }}

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
with:
ref: ${{ matrix.branches }}
submodules: true

- name: Download public suffix list
run: curl --output resources/resources/public_suffix_list.dat https://publicsuffix.org/list/public_suffix_list.dat

- name: Create Pull Request
uses: peter-evans/create-pull-request@c5a7806660adbe173f04e3e038b0ccdcd758773c
with:
token: ${{ secrets.COMMAND_BOT_PAT }}
commit-message: 'fix(dns): Update public suffix list'
committer: GitHub <[email protected]>
author: nextcloud-command <[email protected]>
signoff: true
branch: 'fix/dns/update-public-suffix-list-${{ matrix.branches }}'
title: '[${{ matrix.branches }}] fix(dns): Update public suffix list'
body: |
Auto-generated update of https://publicsuffix.org/
labels: |
dependencies
3. to review
reviewers: ChristophWurst
373 changes: 373 additions & 0 deletions LICENSES/MPL-2.0.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ezyang/htmlpurifier": "4.16.0",
"gravatarphp/gravatar": "^2.0",
"html2text/html2text": "^4.3",
"jeremykendall/php-domain-parser": "^6.0",
"nextcloud/horde-managesieve": "^1.0",
"nextcloud/horde-smtp": "^1.0",
"psr/log": "^1",
Expand Down
95 changes: 94 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions lib/Dns/Resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Dns;

use Pdp\Domain;
use Pdp\Rules;
use function dns_get_record;

class Resolver {

public function resolve(string $hostname, int $type): array|false {
return dns_get_record($hostname, $type);
}

public function isSuffix(string $hostname): bool {
$publicSuffixList = Rules::fromPath(__DIR__ . '/../../resources/public_suffix_list.dat');
$domain = Domain::fromIDNA2008($hostname);

$result = $publicSuffixList->resolve($domain);

return $result->secondLevelDomain()->toString() === '';
}

}
15 changes: 12 additions & 3 deletions lib/Service/AutoConfig/IspDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@

use Exception;
use Horde_Mail_Rfc822_Address;
use OCA\Mail\Dns\Resolver;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;
use SimpleXMLElement;
use function explode;
use function str_replace;
use function strtolower;

Expand All @@ -40,6 +42,7 @@ class IspDb {

/** @var LoggerInterface */
private $logger;
private Resolver $dnsResolver;

/** @returns string[] */
public function getUrls(): array {
Expand All @@ -52,8 +55,11 @@ public function getUrls(): array {
];
}

public function __construct(IClientService $clientService, LoggerInterface $logger) {
public function __construct(IClientService $clientService,
Resolver $dnsResolver,
LoggerInterface $logger) {
$this->client = $clientService->newClient();
$this->dnsResolver = $dnsResolver;
$this->logger = $logger;
}

Expand Down Expand Up @@ -160,11 +166,14 @@ public function query(string $domain, Horde_Mail_Rfc822_Address $email, bool $tr
}
}

if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
if ($tryMx && ($dns = $this->dnsResolver->resolve($domain, DNS_MX))) {
$domain = $dns[0]['target'];
if (!($config = $this->query($domain, $email, false))) {
[, $domain] = explode('.', $domain, 2);
$config = $this->query($domain, $email, false);
// Only try second-level domains and deeper
if (!$this->dnsResolver->isSuffix($domain)) {
$config = $this->query($domain, $email, false);
}
}
}
return $config;
Expand Down
Loading

0 comments on commit 8160d97

Please sign in to comment.