mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Refactor Cloudflare DNS to add and remove A records for custom domains (#1905)
* Refactor Cloudflare DNS to add and remove A records for custom domains * rebase
This commit is contained in:
parent
1d1d785174
commit
8e7b53fd26
21
app/Events/SubdomainWasRemoved.php
Normal file
21
app/Events/SubdomainWasRemoved.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events;
|
||||||
|
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class SubdomainWasRemoved extends Event
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
public $account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*
|
||||||
|
* @param $account
|
||||||
|
*/
|
||||||
|
public function __construct($account)
|
||||||
|
{
|
||||||
|
$this->account = $account;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Events\SubdomainWasRemoved;
|
||||||
use App\Events\SubdomainWasUpdated;
|
use App\Events\SubdomainWasUpdated;
|
||||||
use App\Events\UserSettingsChanged;
|
use App\Events\UserSettingsChanged;
|
||||||
use App\Events\UserSignedUp;
|
use App\Events\UserSignedUp;
|
||||||
@ -824,8 +825,12 @@ class AccountController extends BaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
(bool) $fireUpdateSubdomainEvent = false;
|
||||||
|
|
||||||
if ($account->subdomain !== $request->subdomain) {
|
if ($account->subdomain !== $request->subdomain) {
|
||||||
event(new SubdomainWasUpdated($account));
|
$fireUpdateSubdomainEvent = true;
|
||||||
|
event(new SubdomainWasRemoved($account));
|
||||||
}
|
}
|
||||||
|
|
||||||
$account->fill($request->all());
|
$account->fill($request->all());
|
||||||
@ -834,6 +839,11 @@ class AccountController extends BaseController
|
|||||||
$account->iframe_url = $request->iframe_url;
|
$account->iframe_url = $request->iframe_url;
|
||||||
$account->save();
|
$account->save();
|
||||||
|
|
||||||
|
if ($fireUpdateSubdomainEvent) {
|
||||||
|
event(new SubdomainWasUpdated($account));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return redirect('settings/' . ACCOUNT_CLIENT_PORTAL)
|
return redirect('settings/' . ACCOUNT_CLIENT_PORTAL)
|
||||||
->with('message', trans('texts.updated_settings'));
|
->with('message', trans('texts.updated_settings'));
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Listeners;
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use App\Events\SubdomainWasRemoved;
|
||||||
use App\Events\SubdomainWasUpdated;
|
use App\Events\SubdomainWasUpdated;
|
||||||
use App\Ninja\DNS\Cloudflare;
|
use App\Ninja\DNS\Cloudflare;
|
||||||
|
|
||||||
@ -19,4 +20,11 @@ class DNSListener
|
|||||||
if(env("CLOUDFLARE_DNS_ENABLED"))
|
if(env("CLOUDFLARE_DNS_ENABLED"))
|
||||||
Cloudflare::addDNSRecord($event->account);
|
Cloudflare::addDNSRecord($event->account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function removeDNSRecord(SubdomainWasRemoved $event)
|
||||||
|
{
|
||||||
|
if(env("CLOUDFLARE_DNS_ENABLED"))
|
||||||
|
Cloudflare::removeDNSRecord($event->account);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,90 @@ class Cloudflare
|
|||||||
if($account->subdomain != "")
|
if($account->subdomain != "")
|
||||||
{
|
{
|
||||||
|
|
||||||
$curl = curl_init();
|
|
||||||
$jsonEncodedData = json_encode(['type' => 'A', 'name' => $account->subdomain, 'content' => env('CLOUDFLARE_TARGET_IP_ADDRESS', ''), 'proxied' => true]);
|
$jsonEncodedData = json_encode(['type' => 'A', 'name' => $account->subdomain, 'content' => env('CLOUDFLARE_TARGET_IP_ADDRESS', ''), 'proxied' => true]);
|
||||||
|
|
||||||
|
$requestType = 'POST';
|
||||||
|
|
||||||
|
$url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/dns_records';
|
||||||
|
|
||||||
|
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
|
||||||
|
|
||||||
|
if ($response['status'] != 200)
|
||||||
|
Utils::logError('Unable to update subdomain ' . $account->subdomain . ' @ Cloudflare - ' . $response['result']['result']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function removeDNSRecord(Account $account) {
|
||||||
|
|
||||||
|
$zones = json_decode(env('CLOUDFLARE_ZONE_IDS',''), true);
|
||||||
|
|
||||||
|
foreach($zones as $zone)
|
||||||
|
{
|
||||||
|
|
||||||
|
if($account->subdomain != "")
|
||||||
|
{
|
||||||
|
|
||||||
|
$dnsRecordId = self::getDNSRecord($zone, $account->subdomain);
|
||||||
|
|
||||||
|
$jsonEncodedData = json_encode([]);
|
||||||
|
|
||||||
|
$requestType = 'DELETE';
|
||||||
|
|
||||||
|
$url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/dns_records/'. $dnsRecordId .'';
|
||||||
|
|
||||||
|
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
|
||||||
|
|
||||||
|
if ($response['status'] != 200)
|
||||||
|
Utils::logError('Unable to delete subdomain ' . $account->subdomain . ' @ Cloudflare - ' . $response['result']['result']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getDNSRecord($zone, $aRecord)
|
||||||
|
{
|
||||||
|
//harvest the zone_name
|
||||||
|
$url = 'https://api.cloudflare.com/client/v4/zones/'. $zone .'/dns_records?type=A&per_page=1';
|
||||||
|
|
||||||
|
$requestType = 'GET';
|
||||||
|
|
||||||
|
$jsonEncodedData = json_encode([]);
|
||||||
|
|
||||||
|
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
|
||||||
|
|
||||||
|
if ($response['status'] != 200)
|
||||||
|
Utils::logError('Unable to get the zone name for ' . $aRecord . ' @ Cloudflare - ' . $response['result']['result']);
|
||||||
|
|
||||||
|
$zoneName = $response['result']['result'][0]['zone_name'];
|
||||||
|
|
||||||
|
//get the A record
|
||||||
|
$url = 'https://api.cloudflare.com/client/v4/zones/'. $zone .'/dns_records?type=A&name='. $aRecord .'.'. $zoneName .' ';
|
||||||
|
|
||||||
|
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
|
||||||
|
|
||||||
|
if ($response['status'] != 200)
|
||||||
|
Utils::logError('Unable to get the record ID for ' . $aRecord . ' @ Cloudflare - ' . $response['result']['result']);
|
||||||
|
|
||||||
|
return $response['result']['result'][0]['id'];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function curlCloudFlare($requestType, $url, $jsonEncodedData)
|
||||||
|
{
|
||||||
|
|
||||||
|
$curl = curl_init();
|
||||||
|
|
||||||
$opts = [
|
$opts = [
|
||||||
CURLOPT_URL => 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/dns_records',
|
CURLOPT_URL => $url,
|
||||||
CURLOPT_RETURNTRANSFER => true,
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
CURLOPT_CUSTOMREQUEST => 'POST',
|
CURLOPT_CUSTOMREQUEST => $requestType,
|
||||||
CURLOPT_POST => 1,
|
CURLOPT_POST => 1,
|
||||||
CURLOPT_POSTFIELDS => $jsonEncodedData,
|
CURLOPT_POSTFIELDS => $jsonEncodedData,
|
||||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json',
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json',
|
||||||
@ -37,17 +114,16 @@ class Cloudflare
|
|||||||
curl_setopt_array($curl, $opts);
|
curl_setopt_array($curl, $opts);
|
||||||
|
|
||||||
$result = curl_exec($curl);
|
$result = curl_exec($curl);
|
||||||
|
|
||||||
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
$data['status'] = $status;
|
||||||
|
|
||||||
|
$data['result'] = \json_decode($result, true);
|
||||||
|
|
||||||
curl_close($curl);
|
curl_close($curl);
|
||||||
|
|
||||||
if ($status != 200)
|
return $data;
|
||||||
Utils::logError('unable to update subdomain ' . $account->subdomain . ' @ Cloudflare - ' . $result);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,9 +225,14 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
'App\Listeners\InvoiceListener@jobFailed'
|
'App\Listeners\InvoiceListener@jobFailed'
|
||||||
],
|
],
|
||||||
|
|
||||||
//DNS
|
//DNS Add A record to Cloudflare
|
||||||
'App\Events\SubdomainWasUpdated' => [
|
'App\Events\SubdomainWasUpdated' => [
|
||||||
'App\Listeners\DNSListener@addDNSRecord'
|
'App\Listeners\DNSListener@addDNSRecord'
|
||||||
|
],
|
||||||
|
|
||||||
|
//DNS Remove A record from Cloudflare
|
||||||
|
'App\Events\SubdomainWasRemoved' => [
|
||||||
|
'App\Listeners\DNSListener@removeDNSRecord'
|
||||||
]
|
]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user