Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow wildcard DNS entryies and allow not registering IpV6 address. #26

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM python:3.8
WORKDIR /app
ADD . /app
ADD requirements.txt /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . /app
CMD ["python", "app.py"]
4 changes: 2 additions & 2 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def main():

# Get records depeneding on mode
if config['mode'] == "tailscale":
ts_records = getTailscaleDevice(config['ts-key'], config['ts-client-id'], config['ts-client-secret'], config['ts-tailnet'])
ts_records = getTailscaleDevice(config['ts-key'], config['ts-client-id'], config['ts-client-secret'], config['ts-tailnet'], config['ignore-ipv6'], config['wildcardhost'])
if config['mode'] == "headscale":
from headscale import getHeadscaleDevice
ts_records = getHeadscaleDevice(config['hs-apikey'], config['hs-baseurl'])
ts_records = getHeadscaleDevice(config['hs-apikey'], config['hs-baseurl'], config['ignore-ipv6'], config['wildcardhost'])

records_typemap = {
4: 'A',
Expand Down
2 changes: 1 addition & 1 deletion app/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def deleteDNSRecord(token, domain, id, zoneId=False):
print("--> [CLOUDFLARE] [{code}] {msg}".format(code=response.status_code, msg=colored('record deleted', "green")))

def isValidDNSRecord(name):
regex = "^([a-zA-Z]|\d|-|\.)*$"
regex = "^(\*\.)?([a-zA-Z]|\d|-|\.)*$"
return re.match(regex, name)


Expand Down
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from termcolor import cprint

keysToImport = ['cf-key', 'cf-domain', 'ts-tailnet']
keysOptional = ['cf-sub', 'prefix', 'postfix', 'ts-key', 'ts-client-id', 'ts-client-secret', "mode", "hs-baseurl", "hs-apikey"]
keysOptional = ['cf-sub', 'prefix', 'postfix', 'ts-key', 'ts-client-id', 'ts-client-secret', "mode", "hs-baseurl", "hs-apikey", 'ignore-ipv6', 'wildcardhost']

def importkey(name, optional=False):
key = name
Expand Down
13 changes: 9 additions & 4 deletions app/headscale.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import requests, json
import requests, json, ipaddress
from termcolor import colored
from tailscale import alterHostname

def getHeadscaleDevice(apikey, baseurl):
url = "{baseurl}/api/v1/machine".format(baseurl=baseurl)
def getHeadscaleDevice(apikey, baseurl, ignoreipv6=False, wildcardhost=False):
url = "{baseurl}/api/v1/node".format(baseurl=baseurl)
payload={}
headers = {
"Authorization": "Bearer {apikey}".format(apikey=apikey)
Expand All @@ -15,10 +15,15 @@ def getHeadscaleDevice(apikey, baseurl):
data = json.loads(response.text)
if (response.status_code == 200):
output = []
for device in data['machines']:
for device in data['nodes']:
for address in device['ipAddresses']:
if not device['givenName'].lower().startswith('localhost'):
ip = ipaddress.ip_address(address)
if ip.version == 6 and ignoreipv6:
continue
output.append({'hostname': alterHostname(device['givenName'].split('.')[0].lower()), 'address': address})
if wildcardhost:
output.append({'hostname': "*.{host}".format(host=alterHostname(device['givenName'].split('.')[0].lower())), 'address': address})
return output
else:
exit(colored("getTailscaleDevice() - {status}, {error}".format(status=str(response.status_code), error=data['message']), "red"))
7 changes: 6 additions & 1 deletion app/tailscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from termcolor import colored

### Get Data
def getTailscaleDevice(apikey, clientid, clientsecret, tailnet):
def getTailscaleDevice(apikey, clientid, clientsecret, tailnet, ignoreipv6=False, wildcardhost=False):
if clientid and clientsecret:
token = OAuth2Session(client=BackendApplicationClient(client_id=clientid)).fetch_token(token_url='https://api.tailscale.com/api/v2/oauth/token', client_id=clientid, client_secret=clientsecret)
apikey = token["access_token"]
Expand All @@ -28,7 +28,12 @@ def getTailscaleDevice(apikey, clientid, clientsecret, tailnet):
for address in device['addresses']:
output.append({'hostname': alterHostname(device['hostname']), 'address': address})
if device['name'].split('.')[0].lower() != device['hostname'].lower():
ip = ipaddress.ip_address(address)
if ip.version == 6 and ignoreipv6:
continue
output.append({'hostname': alterHostname(device['name'].split('.')[0].lower()), 'address': address})
if wildcardhost:
output.append({'hostname': "*.{host}".format(host=alterHostname(device['name'].split('.')[0].lower())), 'address': address})
return output
else:
exit(colored("getTailscaleDevice() - {status}, {error}".format(status=str(response.status_code), error=data['message']), "red"))
Expand Down