geisi/laravel-dyndns

Laravel DynDns 帮助您发布本地公网IP,无需使用任何外部 DynDns 服务

0.1.1 2022-01-23 16:38 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

请注意,此包目前处于beta状态。不建议在生产环境中使用。

Laravel DynDNS 是一个轻量级的包,帮助您将本地公网IP发布到DNS记录,无需使用任何外部 DynDNS 服务。通常,当您想将本地网络中的任何服务暴露到互联网时,需要这样做。您的ISP提供商可能会在一段时间后更改您的公网IP地址。

此包会保持您的DNS记录与您的公网IP地址同步。

安装

您可以通过composer安装此包

composer require geisi/laravel-dyndns

您必须使用以下命令发布配置文件

php artisan vendor:publish --tag="dyndns-config"

在配置文件中,您必须设置所有要同步到本地公网IP地址的域名。默认情况下,我们使用Cloudflare DynDNS适配器。如果您还没有Cloudflare账户,您可以在cloudflare.com免费创建一个Cloudflare账户。

完成域名设置过程后,您可以继续配置此包。

return [
/*
     * List of all domains which should be synchronized with the systems public IP address
     */
    'domains' => [
        [
            /*
             * Your full domain name e.g. dyndns.foo-bar.com
             */
            'domain_name' => env('DYNDNS_DOMAIN_NAME', ''),
            'dns_service' => [
                /*
                 * The dns service which you want to use to serve your Dns records. By default, this package runs with the Cloudflare Dns service.
                 * If you don't want to use Cloudflare you can create your own implementation.
                 * Your class has to implement the Geisi\DynDns\Contracts\HandlesDynDnsRecords interface.
                 */
                'adapter' => \Geisi\DynDns\Services\CloudflareDynDNS::class,

                /*
                 * The Cloudflare zone id this can be left empty. For performance reasons it is recommended to configure a zone id.
                 */
                'zone_id' => env('DYNDNS_ZONE_ID', ''),

                /*
                 * The Cloudflare dns record id this can be left empty. For performance reasons it is recommended to configure a dns record id.
                 */
                'record_id' => env('DYNDNS_RECORD_ID', ''),

                /*
                 * The Cloudflare DNS record details look Cloudflare API documentation for reference.
                 */
                'record_details' => [
                    'type' => 'A',
                    'proxied' => false,
                    'ttl' => 1
                ],

                /*
                 * The Cloudflare API token.
                 */
                'api_token' => env('CLOUDFLARE_TOKEN', '')
            ],
            /*
             * The public IP Address Resolver Service. By default, we are using the opendns.com resolver to query the current public IP address.
             * If you don't want to use opendns.com you can create your own implementation.
             * Your class has to implement the Geisi\DynDns\Contracts\DiscoversIpAddress interface.
             */
            'resolver_service' => Geisi\DynDns\Services\OpenDNSPublicIPResolver::class,
            /*
             * The email address which should be notified when the public IP address changes
             * leave empty if you want to disable mail notifications
             */
            'notification_email' => env('DYNDNS_NOTIFICATION_EMAIL', '')
        ]
    ]
];

如果您只想进行快速设置,请将以下两个条目添加到您的.env文件中

DYNDNS_DOMAIN_NAME="dyndns-subdomain.your-domain.com"
CLOUDFLARE_TOKEN="<enter your cloudflare api token here>"

用法

您可以使用以下命令启动 dyndns 同步过程

php artisan dyndns:run

我们建议在 App/Console/Kernel.php 文件中使用 Laravel 调度器来保持您的域名与IP地址同步。只需将以下行添加到 Kernel.php 文件的 schedule 方法中

 $schedule->command('dyndns:run')->everyMinute();

当您想在控制台外触发重新同步时,可以使用 Geisi\DynDns\Facades\DynDns facade。

Geisi\DynDns\Facades\DynDns::handle();

通知

当您想了解您的公网IP地址已更改时,您可以按域名设置 notification_email 配置。请注意,您必须添加一个有效的邮件配置来发送电子邮件。

事件

Laravel DynDns 在您的IP地址每次更改时都会触发一个 Geisi\DynDns\Events\DynDNSUpdated 事件。当同步过程失败时,会发送一个 Geisi\DynDns\Events\DynDNSUpdateError 事件。

您可以监听这些事件并实现自己的应用程序逻辑。

添加新的DNS服务

技术上,任何可以通过1分钟TTL配置的DNS服务都可以用作您的 DynDNS 服务。今天,我们仅支持Cloudflare,但您可以通过扩展 Geisi\DynDns\DynDnsProvider 类轻松添加其他服务。您只需实现两个方法 getRecordIpupdateRecord

创建新的 DynDNSProvider 后,您可以将其添加到域名配置中。

测试

composer test

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全漏洞

有关报告安全漏洞的详细信息,请参阅我们的安全策略

致谢

许可

MIT许可证(MIT)。请参阅许可文件获取更多信息。