Skip to content

Commit

Permalink
added fixed ip support and dns-alias.conf
Browse files Browse the repository at this point in the history
  • Loading branch information
schwark committed Jul 30, 2022
1 parent 617acbc commit 26ce532
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,5 @@ dmypy.json

# Pyre type checker
.pyre/
.vscode/
.vscode/
.DS_Store
38 changes: 35 additions & 3 deletions command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import re
import argparse
from os import listdir
from os import listdir, environ
from unifi import UniFiClient
from workflow.workflow import MATCH_ATOM, MATCH_STARTSWITH, MATCH_SUBSTRING, MATCH_ALL, MATCH_INITIALS, MATCH_CAPITALS, MATCH_INITIALS_STARTSWITH, MATCH_INITIALS_CONTAIN
from workflow import Workflow3, ICON_WEB, ICON_WARNING, ICON_BURN, ICON_ERROR, ICON_SWITCH, ICON_HOME, ICON_COLOR, ICON_INFO, ICON_SYNC, web, PasswordNotFound
Expand Down Expand Up @@ -113,14 +113,45 @@ def get_hub(wf):
hub = UniFiClient('https://'+ip+':'+str(port), username, password, site, state, unifios, mfa, secret)
return hub

def enhance_client(client, networks):
if 'use_fixedip' in client and client['use_fixedip'] and 'fixed_ip' in client and 'ip' not in client:
client['ip'] = client['fixed_ip']

if 'network_id' in client:
network = next(x for x in networks if x['_id'] == client['network_id'])
client['network_domain'] = network['domain_name'] if network else 'home.arpa'
else:
log.debug("no network id in "+str(client))

return client

def generate_dns_alias_conf(clients):
dns_alias_text = ""
for client in clients:
ip = client['ip'] if 'ip' in client else None
if ip:
name = client['name'] if 'name' in client else client['hostname']
if 'network_domain' in client:
fqdn = name+'.'+client['network_domain']
dns_alias_text += "host-record={},{},{}\n".format(fqdn,name,ip)

log.debug(dns_alias_text)
filename = environ.get('HOME')+'/Downloads/dns-alias.conf'
with open(filename, 'w') as outfile:
outfile.write(dns_alias_text)

def get_clients(wf, hub):
"""Retrieve all clients
Returns a list of clients.
"""
clients = hub.get_clients()
return clients
reservations = hub.get_reservations()
allclients = list({x['mac']:x for x in (reservations + clients) if 'ip' in x or 'fixed_ip' in x}.values())
networks = hub.get_networks()

return map(lambda x: enhance_client(x, networks), allclients)

def get_devices(wf, hub):
"""Retrieve all devices
Expand Down Expand Up @@ -221,7 +252,7 @@ def get_item_type(item):
return 'client'

def post_process_item(icons, item):
log.debug("post processing "+str(item))
#log.debug("post processing "+str(item))
item['_display_name'] = beautify(get_name(item))
item['_type'] = get_item_type(item)
item['_icon'] = get_item_icon(icons, item)
Expand All @@ -238,6 +269,7 @@ def handle_update(wf, args, hub):
fwrules = map(lambda x: post_process_item(icons, x), get_fwrules(wf, hub))
if clients:
wf.cache_data('client', clients)
generate_dns_alias_conf(clients=clients)
if devices:
wf.cache_data('device', devices)
if radius:
Expand Down
4 changes: 3 additions & 1 deletion filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def get_item_subtitle(item, type, device_map):

if 'ip' in item:
# subtitle += u' 📨 '+item['ip']
if 'use_fixedip' in item and item['use_fixedip'] and 'fixed_ip' in item and item['fixed_ip'] == item['ip']:
subtitle += u' ⧈ '
subtitle += item['ip']
# if 'num_sta' in item:
# subtitle += u' 👱 '+str(item['num_sta'])
Expand Down Expand Up @@ -208,7 +210,7 @@ def extract_commands(args, clients, filter_func):
result = vars(args)
if clients:
clients = filter(lambda x: x, clients)
log.debug("clients are: "+str(clients))
#log.debug("clients are: "+str(clients))
full_clients = get_filtered_items(args.query, clients, filter_func)
minusone_clients = get_filtered_items(' '.join(words[0:-1]), clients, filter_func)
minustwo_clients = get_filtered_items(' '.join(words[0:-2]), clients, filter_func)
Expand Down
12 changes: 12 additions & 0 deletions unifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def __init__(self, base, username=None, password=None, site='default', state=Non
'clients': {
'url': '/stat/sta/'
},
'reservations': {
'url': '/list/user/'
},
'networks': {
'url': '/rest/networkconf/'
},
'client': {
'url': lambda sf, **kwargs: '/stat/sta/'+(kwargs['mac'] if 'mac' in kwargs else '')
},
Expand Down Expand Up @@ -316,6 +322,12 @@ def get_results(self, step, **kwargs):
def get_devices(self):
return self._get_results('devices')

def get_networks(self):
return self._get_results('networks')

def get_reservations(self):
return self._get_results('reservations')

def get_clients(self):
return self._get_results('clients')

Expand Down
7 changes: 7 additions & 0 deletions update-dns
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

CURRENT_IP=`ifconfig -l | xargs -n1 ipconfig getifaddr`
CURRENT_ROUTER=$(awk -F"." '{print $1"."$2"."$3".1"}'<<<$CURRENT_IP)
IP=${1:-$CURRENT_ROUTER}
scp ~/Downloads/dns-alias.conf root@$IP:/run/dnsmasq.conf.d/dns-alias.conf
ssh root@$IP "killall dnsmasq"
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.4
1.1.5

0 comments on commit 26ce532

Please sign in to comment.