2018-10-01 15:51:31 +02:00
|
|
|
import os
|
|
|
|
import json
|
2018-07-30 23:48:20 +02:00
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
from twisted.internet import reactor, defer
|
|
|
|
from txupnp.upnp import UPnP
|
|
|
|
|
|
|
|
log = logging.getLogger("txupnp")
|
|
|
|
|
|
|
|
|
2018-08-01 22:29:00 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_external_ip(u, *_):
|
|
|
|
ip = yield u.get_external_ip()
|
|
|
|
print(ip)
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def list_mappings(u, *_):
|
|
|
|
redirects = yield u.get_redirects()
|
2018-08-01 23:57:27 +02:00
|
|
|
ext_ip = yield u.get_external_ip()
|
|
|
|
for (ext_host, ext_port, proto, int_port, int_host, enabled, desc, lease) in redirects:
|
|
|
|
print("{}:{}/{} --> {}:{} ({}) (expires: {}) - {} ".format(
|
|
|
|
ext_host or ext_ip, ext_port, proto, int_host, int_port, "enabled" if enabled else "disabled",
|
|
|
|
"never" if not lease else lease, desc)
|
|
|
|
)
|
2018-08-01 22:29:00 +02:00
|
|
|
|
|
|
|
|
2018-08-02 15:38:00 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def add_mapping(u, *_):
|
2018-10-01 15:51:31 +02:00
|
|
|
port = 51413
|
2018-08-02 15:38:00 +02:00
|
|
|
protocol = "UDP"
|
|
|
|
description = "txupnp test mapping"
|
2018-08-07 19:53:35 +02:00
|
|
|
ext_port = yield u.get_next_mapping(port, protocol, description)
|
|
|
|
if ext_port:
|
|
|
|
print("external port: %i to local %i/%s" % (ext_port, port, protocol))
|
2018-08-02 15:38:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def delete_mapping(u, *_):
|
|
|
|
port = 4567
|
|
|
|
protocol = "UDP"
|
|
|
|
yield u.delete_port_mapping(port, protocol)
|
|
|
|
mapping = yield u.get_specific_port_mapping(port, protocol)
|
|
|
|
if mapping:
|
|
|
|
print("failed to remove mapping")
|
|
|
|
else:
|
|
|
|
print("removed mapping")
|
|
|
|
|
|
|
|
|
2018-10-01 15:51:31 +02:00
|
|
|
def _encode(x):
|
|
|
|
if isinstance(x, bytes):
|
|
|
|
return x.decode()
|
|
|
|
elif isinstance(x, Exception):
|
|
|
|
return str(x)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def generate_test_data(u, *_):
|
|
|
|
external_ip = yield u.get_external_ip()
|
|
|
|
redirects = yield u.get_redirects()
|
|
|
|
ext_port = yield u.get_next_mapping(4567, "UDP", "txupnp test mapping")
|
|
|
|
delete = yield u.delete_port_mapping(ext_port, "UDP")
|
|
|
|
after_delete = yield u.get_specific_port_mapping(ext_port, "UDP")
|
|
|
|
|
|
|
|
commands_test_case = (
|
|
|
|
("get_external_ip", (), "1.2.3.4"),
|
|
|
|
("get_redirects", (), redirects),
|
|
|
|
("get_next_mapping", (4567, "UDP", "txupnp test mapping"), ext_port),
|
|
|
|
("delete_port_mapping", (ext_port, "UDP"), delete),
|
|
|
|
("get_specific_port_mapping", (ext_port, "UDP"), after_delete),
|
|
|
|
)
|
|
|
|
|
|
|
|
gateway = u.gateway
|
|
|
|
device = list(gateway.devices.values())[0]
|
|
|
|
assert device.manufacturer and device.modelName
|
|
|
|
device_path = os.path.join(os.getcwd(), "%s %s" % (device.manufacturer, device.modelName))
|
|
|
|
commands = gateway.debug_commands()
|
|
|
|
with open(device_path, "w") as f:
|
|
|
|
f.write(json.dumps({
|
|
|
|
"router_address": u.router_ip,
|
|
|
|
"client_address": u.lan_address,
|
|
|
|
"port": gateway.port,
|
|
|
|
"gateway_dict": gateway.as_dict(),
|
|
|
|
'expected_devices': [
|
|
|
|
{
|
|
|
|
'cache_control': 'max-age=1800',
|
|
|
|
'location': gateway.location,
|
|
|
|
'server': gateway.server,
|
|
|
|
'st': gateway.urn,
|
|
|
|
'usn': gateway.usn
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'commands': commands,
|
|
|
|
'ssdp': u.sspd_factory.get_ssdp_packet_replay(),
|
|
|
|
'scpd': gateway.requester.dump_packets(),
|
|
|
|
'soap': commands_test_case
|
|
|
|
}, default=_encode, indent=2).replace(external_ip, "1.2.3.4"))
|
|
|
|
print("Generated test data! -> %s" % device_path)
|
|
|
|
|
|
|
|
|
2018-08-01 22:29:00 +02:00
|
|
|
cli_commands = {
|
|
|
|
"get_external_ip": get_external_ip,
|
2018-08-02 15:38:00 +02:00
|
|
|
"list_mappings": list_mappings,
|
|
|
|
"add_mapping": add_mapping,
|
2018-10-01 15:51:31 +02:00
|
|
|
"delete_mapping": delete_mapping,
|
|
|
|
"generate_test_data": generate_test_data,
|
2018-08-01 22:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 23:48:20 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-08-01 22:29:00 +02:00
|
|
|
def run_command(found, u, command, debug_xml):
|
2018-07-30 23:48:20 +02:00
|
|
|
if not found:
|
|
|
|
print("failed to find gateway")
|
|
|
|
reactor.callLater(0, reactor.stop)
|
|
|
|
return
|
2018-08-01 22:29:00 +02:00
|
|
|
if command not in cli_commands:
|
|
|
|
print("unrecognized command: valid commands: %s" % list(cli_commands.keys()))
|
|
|
|
else:
|
|
|
|
yield cli_commands[command](u, debug_xml)
|
2018-07-30 23:48:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-08-07 19:53:35 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger("txupnp")
|
|
|
|
handler = logging.StreamHandler()
|
|
|
|
handler.setFormatter(logging.Formatter('%(asctime)-15s-%(filename)s:%(lineno)s->%(message)s'))
|
|
|
|
log.addHandler(handler)
|
|
|
|
log.setLevel(logging.INFO)
|
|
|
|
|
2018-07-30 23:48:20 +02:00
|
|
|
parser = argparse.ArgumentParser(description="upnp command line utility")
|
2018-08-02 15:38:00 +02:00
|
|
|
parser.add_argument(dest="command", type=str, help="debug_gateway | list_mappings | get_external_ip | add_mapping | delete_mapping")
|
2018-07-31 17:35:24 +02:00
|
|
|
parser.add_argument("--debug_logging", dest="debug_logging", default=False, action="store_true")
|
2018-08-01 22:29:00 +02:00
|
|
|
parser.add_argument("--include_igd_xml", dest="include_igd_xml", default=False, action="store_true")
|
2018-07-30 23:48:20 +02:00
|
|
|
args = parser.parse_args()
|
2018-07-31 17:35:24 +02:00
|
|
|
if args.debug_logging:
|
2018-10-01 15:51:31 +02:00
|
|
|
# from twisted.python import log as tx_log
|
|
|
|
# observer = tx_log.PythonLoggingObserver(loggerName="txupnp")
|
|
|
|
# observer.start()
|
2018-07-30 23:48:20 +02:00
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
command = args.command
|
2018-08-07 19:53:35 +02:00
|
|
|
command = command.replace("-", "_")
|
|
|
|
if command not in cli_commands:
|
|
|
|
print("unrecognized command: %s is not in %s" % (command, cli_commands.keys()))
|
|
|
|
return
|
2018-07-30 23:48:20 +02:00
|
|
|
|
2018-07-31 22:53:08 +02:00
|
|
|
def show(err):
|
|
|
|
print("error: {}".format(err))
|
|
|
|
|
2018-10-01 15:51:31 +02:00
|
|
|
u = UPnP(reactor, debug_ssdp=(command == "generate_test_data"))
|
2018-07-30 23:48:20 +02:00
|
|
|
d = u.discover()
|
2018-08-01 22:29:00 +02:00
|
|
|
d.addCallback(run_command, u, command, args.include_igd_xml)
|
2018-07-31 22:53:08 +02:00
|
|
|
d.addErrback(show)
|
2018-07-30 23:48:20 +02:00
|
|
|
d.addBoth(lambda _: reactor.callLater(0, reactor.stop))
|
|
|
|
reactor.run()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|