mirror of
https://github.com/opnsense/plugins.git
synced 2026-06-14 20:13:19 -04:00
Refactoring for b2e663b from @AdSchellevis
This commit is contained in:
parent
d1ad9e69fe
commit
38efa880b1
1 changed files with 23 additions and 28 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
"""
|
"""
|
||||||
Copyright (c) 2023 Thomas Cekal <thomas@cekal.org>
|
Copyright (c) 2023 Thomas Cekal <thomas@cekal.org>
|
||||||
|
Copyright (c) 2023 Ad Schellevis <ad@opnsense.org>
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
|
@ -23,9 +24,9 @@
|
||||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
POSSIBILITY OF SUCH DAMAGE.
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
"""
|
"""
|
||||||
|
import json
|
||||||
import syslog
|
import syslog
|
||||||
import requests
|
import requests
|
||||||
from requests.auth import HTTPBasicAuth
|
|
||||||
from . import BaseAccount
|
from . import BaseAccount
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -44,15 +45,10 @@ class Cloudflare(BaseAccount):
|
||||||
return Cloudflare._services.keys()
|
return Cloudflare._services.keys()
|
||||||
|
|
||||||
def match(account):
|
def match(account):
|
||||||
if account.get('service') in Cloudflare._services:
|
return account.get('service') in Cloudflare._services
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
if super().execute():
|
if super().execute():
|
||||||
proto = 'https' if self.settings.get('force_ssl', False) else 'http'
|
|
||||||
|
|
||||||
# IPv4/IPv6
|
# IPv4/IPv6
|
||||||
recordType = None
|
recordType = None
|
||||||
if str(self.current_address).find(':') > 1:
|
if str(self.current_address).find(':') > 1:
|
||||||
|
|
@ -61,14 +57,9 @@ class Cloudflare(BaseAccount):
|
||||||
else:
|
else:
|
||||||
#IPv4
|
#IPv4
|
||||||
recordType = "A"
|
recordType = "A"
|
||||||
if self.is_verbose:
|
|
||||||
syslog.syslog(
|
|
||||||
syslog.LOG_NOTICE,
|
|
||||||
"Account %s record type (IPv4/v6): %s" % (self.description, recordType)
|
|
||||||
)
|
|
||||||
|
|
||||||
# get ZoneID
|
# get ZoneID
|
||||||
url = "%s://%s/client/v4/zones" % (proto, self._services[self.settings.get('service')])
|
url = "https://%s/client/v4/zones" % self._services[self.settings.get('service')]
|
||||||
req_opts = {
|
req_opts = {
|
||||||
'url': url,
|
'url': url,
|
||||||
'params': {
|
'params': {
|
||||||
|
|
@ -88,13 +79,13 @@ class Cloudflare(BaseAccount):
|
||||||
if 'success' not in payload:
|
if 'success' not in payload:
|
||||||
syslog.syslog(
|
syslog.syslog(
|
||||||
syslog.LOG_ERR,
|
syslog.LOG_ERR,
|
||||||
"Account %s error parsing JSON response [ZoneID] %s" % (self.description, payload)
|
"Account %s error parsing JSON response [ZoneID] %s" % (self.description, response.text)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
if not bool(payload['success']):
|
if str(payload.get('success', 'false')).lower() != 'true':
|
||||||
syslog.syslog(
|
syslog.syslog(
|
||||||
syslog.LOG_ERR,
|
syslog.LOG_ERR,
|
||||||
"Account %s error receiving ZoneID [%s - %s]" % (self.description, payload['errors'][0]['code'], payload['errors'][0]['message'])
|
"Account %s error receiving ZoneID [%s]" % (self.description, json.dumps(payload.get('errors', {})))
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -122,13 +113,15 @@ class Cloudflare(BaseAccount):
|
||||||
if 'success' not in payload:
|
if 'success' not in payload:
|
||||||
syslog.syslog(
|
syslog.syslog(
|
||||||
syslog.LOG_ERR,
|
syslog.LOG_ERR,
|
||||||
"Account %s error parsing JSON response [RecordID] %s" % (self.description, payload)
|
"Account %s error parsing JSON response [RecordID] %s" % (self.description, response.text)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
if not bool(payload['success']):
|
if str(payload.get('success', 'false')).lower() != 'true':
|
||||||
syslog.syslog(
|
syslog.syslog(
|
||||||
syslog.LOG_ERR,
|
syslog.LOG_ERR,
|
||||||
"Account %s error receiving RecordID [%s - %s]" % (self.description, payload['errors'][0]['code'], payload['errors'][0]['message'])
|
"Account %s error receiving RecordID [%s]" % (
|
||||||
|
self.description, json.dumps(payload.get('errors', {}))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -157,13 +150,17 @@ class Cloudflare(BaseAccount):
|
||||||
if 'success' not in payload:
|
if 'success' not in payload:
|
||||||
syslog.syslog(
|
syslog.syslog(
|
||||||
syslog.LOG_ERR,
|
syslog.LOG_ERR,
|
||||||
"Account %s error parsing JSON response [UpdateIP] %s" % (self.description, payload)
|
"Account %s error parsing JSON response [UpdateIP] %s" % (self.description, response.text)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
if bool(payload['success']):
|
if str(payload.get('success', 'false')).lower() == 'true':
|
||||||
syslog.syslog(
|
syslog.syslog(
|
||||||
syslog.LOG_NOTICE,
|
syslog.LOG_NOTICE,
|
||||||
"Account %s set new ip %s [%s]" % (self.description, self.current_address, payload['result']['content'])
|
"Account %s set new ip %s [%s]" % (
|
||||||
|
self.description,
|
||||||
|
self.current_address,
|
||||||
|
payload.get('result', {}).get('content', '')
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.update_state(address=self.current_address)
|
self.update_state(address=self.current_address)
|
||||||
|
|
@ -171,10 +168,8 @@ class Cloudflare(BaseAccount):
|
||||||
else:
|
else:
|
||||||
syslog.syslog(
|
syslog.syslog(
|
||||||
syslog.LOG_ERR,
|
syslog.LOG_ERR,
|
||||||
"Account %s failed to set new ip %s [%s - %s]" % (
|
"Account %s failed to set new ip %s [%s]" % (self.description, self.current_address, response.text)
|
||||||
self.description, self.current_address, payload['errors'][0]['code'], payload['errors'][0]['message']
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
return False
|
||||||
Loading…
Reference in a new issue