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 <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed Aug 7, 2024
1 parent 4a562f0 commit b7e398d
Show file tree
Hide file tree
Showing 10 changed files with 16,587 additions and 29 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 <noreply@github.com>
author: nextcloud-command <nextcloud-command@users.noreply.github.com>
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 @@ -27,6 +27,7 @@
"christophwurst/kitinerary-sys": "^0.1.0",
"ezyang/htmlpurifier": "4.14.0",
"gravatarphp/gravatar": "^2.0",
"jeremykendall/php-domain-parser": "^5.7",
"pear-pear.horde.org/horde_date": "^2.4.1@stable",
"pear-pear.horde.org/horde_exception": "^2.0.8@stable",
"pear-pear.horde.org/horde_imap_client": "^2.29.16@stable",
Expand Down
168 changes: 159 additions & 9 deletions composer.lock

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

32 changes: 32 additions & 0 deletions lib/Dns/Resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 {
/**
* @return array|false
*/
public function resolve(string $hostname, int $type) {
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() === '';
}
}
8 changes: 6 additions & 2 deletions lib/Service/AutoConfig/IspDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class IspDb {

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

/** @returns string[] */
public function getUrls(): array {
Expand All @@ -50,8 +51,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 @@ -150,7 +154,7 @@ public function query(string $domain, string $email, bool $tryMx = true): array
}
}

if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
if ($tryMx && ($dns = $this->dnsResolver->resolve($domain, DNS_MX))) {
$domain = $dns[0]['target'];
if (!($provider = $this->query($domain, $email, false))) {
[, $domain] = explode('.', $domain, 2);
Expand Down
Loading

0 comments on commit b7e398d

Please sign in to comment.