def main() : public_ip = utils.get_wan_ip() if public_ip is not None: logger.debug("Public IP is %s" % (public_ip)) else: logger.error("Failed to get public IP.") return logger.debug("Cleanup iperf servers.") try: subprocess.check_call('pgrep -f "iperf" | xargs kill -9', shell=True) except: pass server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind(('0.0.0.0', settings.PUBLIC_TCP_PORT)) server_sock.listen(settings.PUBLIC_BACKLOG) logger.debug("Listening on %s (%d)..." % (public_ip, settings.PUBLIC_TCP_PORT)) for t in [HeartbeatThread]: t().start() try: while True: conn, addr = server_sock.accept() data = utils.recv_all(conn) try: request = json.loads(data) except: logger.exception("Failed to read message.") continue if 'action' in request: schema = settings.REQUEST_SCHEMA handler_thread = HANDLER_MAPPING[request['action']](conn, request) elif 'request' in request: schema = settings.REPLY_SCHEMA handler_thread = ReplyHandler(request) else: logger.error("Not a request or reply.") continue try: validate(request, schema) except: logger.exception("Failed to validate request msg.") continue logger.debug("Got message from %s: %s" % (addr, json.dumps(request))) handler_thread.start() except: logger.exception("Failed to listen.") server_sock.close()
def get_ap_status(): status = {'IP': utils.get_wan_ip(), 'MAC': utils.get_wan_mac(), 'timestamp': dt.now().isoformat(), 'band2g':{}, 'band5g':{}} for iface, band in [('wlan0', 'band2g'), ('wlan1', 'band5g')]: if iface not in subprocess.check_output('iwinfo', shell=True): status[band]['enabled'] = False else: status[band]['enabled'] = True status[band].update(parse_iwinfo(subprocess.check_output('iwinfo %s info' % (iface), shell=True))) return status
def set_ip(self, port: str): url = 'http://{}:{}' self.lan_ip.set(url.format(utils.get_lan_ip(), port)) self.wan_ip.set(url.format(utils.get_wan_ip(), port))
# -*- coding:utf-8 -*- import config import utils import logging import namesilo namesilo_token = 'namesilo_token' # put your namesilo token here domain = 'example.com' # the domain to be updated wan_ip = utils.get_wan_ip() config_dict = config.read_config('ddns.conf') requested_ip = config_dict.get('requested_ip') if requested_ip == wan_ip: logging.info('wan_ip equals to requested_ip, this means:\n' '1. everything is normal\n' '2. resolution change request sent, you will have to wait for it to take effect') exit(0) else: ns = namesilo.Namesilo(namesilo_token) ns.update_domain(domain, wan_ip) config_dict['requested_ip'] = wan_ip config.write_config('ddns.conf', config_dict)
def main(): public_ip = utils.get_wan_ip() if public_ip is not None: logger.debug("Public IP is %s" % (public_ip)) else: logger.error("Failed to get public IP.") return logger.debug("Cleanup iperf servers.") try: subprocess.check_call('pgrep -f "iperf" | xargs kill -9', shell=True) except: pass server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind(('0.0.0.0', settings.PUBLIC_TCP_PORT)) server_sock.listen(settings.PUBLIC_BACKLOG) logger.debug("Listening on %s (%d)..." % (public_ip, settings.PUBLIC_TCP_PORT)) for t in [HeartbeatThread]: t().start() try: while True: conn, addr = server_sock.accept() data = utils.recv_all(conn) try: request = json.loads(data) except: logger.exception("Failed to read message.") continue if 'action' in request: schema = settings.REQUEST_SCHEMA handler_thread = HANDLER_MAPPING[request['action']](conn, request) elif 'request' in request: schema = settings.REPLY_SCHEMA handler_thread = ReplyHandler(request) else: logger.error("Not a request or reply.") continue try: validate(request, schema) except: logger.exception("Failed to validate request msg.") continue logger.debug("Got message from %s: %s" % (addr, json.dumps(request))) handler_thread.start() except: logger.exception("Failed to listen.") server_sock.close()