/
port-forward.py
executable file
·253 lines (200 loc) · 8.96 KB
/
port-forward.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/python
# Utility to forward ports on your router using UPnP
# most code from http://mattscodecave.com/posts/using-python-and-upnp-to-forward-a-port
# - adapted for command line use
# - support multiple routers in the lan
import socket
import re
from urlparse import urlparse
import urllib2
from xml.dom.minidom import parseString
from xml.dom.minidom import Document
import httplib
import time
import argparse
import sys
def discover():
"""Discover UPNP capable routers in the local network
Returns a lit of urls with service descriptions
"""
SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900
SSDP_MX = 2
SSDP_ST = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
WAIT = 1
ssdpRequest = "M-SEARCH * HTTP/1.1\r\n" + \
"HOST: %s:%d\r\n" % (SSDP_ADDR, SSDP_PORT) + \
"MAN: \"ssdp:discover\"\r\n" + \
"MX: %d\r\n" % (SSDP_MX, ) + \
"ST: %s\r\n" % (SSDP_ST, ) + "\r\n"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(0)
sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT))
time.sleep(WAIT)
paths = []
for _ in range(10):
try:
data, fromaddr = sock.recvfrom(1024)
ip = fromaddr[0]
#print "from ip: %s"%ip
parsed = re.findall(r'(?P<name>.*?): (?P<value>.*?)\r\n', data)
# get the location header
location = filter(lambda x: x[0].lower() == "location", parsed)
# use the urlparse function to create an easy to use object to hold a URL
router_path = location[0][1]
paths.append(router_path)
except socket.error:
'''no data yet'''
break
return paths
def get_wanip_path(upnp_url):
# get the profile xml file and read it into a variable
directory = urllib2.urlopen(upnp_url).read()
# create a DOM object that represents the `directory` document
dom = parseString(directory)
# find all 'serviceType' elements
service_types = dom.getElementsByTagName('serviceType')
# iterate over service_types until we get either WANIPConnection
# (this should also check for WANPPPConnection, which, if I remember correctly
# exposed a similar SOAP interface on ADSL routers.
for service in service_types:
# I'm using the fact that a 'serviceType' element contains a single text node, who's data can
# be accessed by the 'data' attribute.
# When I find the right element, I take a step up into its parent and search for 'controlURL'
if service.childNodes[0].data.find('WANIPConnection') > 0:
path = service.parentNode.getElementsByTagName('controlURL')[0].childNodes[0].data
return path
def open_port(service_url,external_port,internal_client,internal_port=None,protocol='TCP',duration=0,description=None,enabled=1):
parsedurl = urlparse(service_url)
if internal_port==None:
internal_port = external_port
if description == None:
description = 'generated by port-forward.py'
if not enabled:
duration=1
doc = Document()
# create the envelope element and set its attributes
envelope = doc.createElementNS('', 's:Envelope')
envelope.setAttribute('xmlns:s', 'http://schemas.xmlsoap.org/soap/envelope/')
envelope.setAttribute('s:encodingStyle', 'http://schemas.xmlsoap.org/soap/encoding/')
# create the body element
body = doc.createElementNS('', 's:Body')
# create the function element and set its attribute
fn = doc.createElementNS('', 'u:AddPortMapping')
fn.setAttribute('xmlns:u', 'urn:schemas-upnp-org:service:WANIPConnection:1')
# setup the argument element names and values
# using a list of tuples to preserve order
arguments = [
('NewRemoteHost', ""), # unused - but required
('NewExternalPort', external_port), # specify port on router
('NewProtocol', protocol), # specify protocol
('NewInternalPort', internal_port), # specify port on internal host
('NewInternalClient', internal_client), # specify IP of internal host
('NewEnabled', enabled), # turn mapping ON
('NewPortMappingDescription', description), # add a description
('NewLeaseDuration', duration)] # how long should it be opened?
# NewEnabled should be 1 by default, but better supply it.
# NewPortMappingDescription Can be anything you want, even an empty string.
# NewLeaseDuration can be any integer BUT some UPnP devices don't support it,
# so set it to 0 for better compatibility.
# container for created nodes
argument_list = []
# iterate over arguments, create nodes, create text nodes,
# append text nodes to nodes, and finally add the ready product
# to argument_list
for k, v in arguments:
v = str(v)
tmp_node = doc.createElement(k)
tmp_text_node = doc.createTextNode(v)
tmp_node.appendChild(tmp_text_node)
argument_list.append(tmp_node)
# append the prepared argument nodes to the function element
for arg in argument_list:
fn.appendChild(arg)
# append function element to the body element
body.appendChild(fn)
# append body element to envelope element
envelope.appendChild(body)
# append envelope element to document, making it the root element
doc.appendChild(envelope)
# our tree is ready, conver it to a string
pure_xml = doc.toxml()
# use the object returned by urlparse.urlparse to get the hostname and port
conn = httplib.HTTPConnection(parsedurl.hostname, parsedurl.port)
# use the path of WANIPConnection (or WANPPPConnection) to target that service,
# insert the xml payload,
# add two headers to make tell the server what we're sending exactly.
conn.request('POST',
parsedurl.path,
pure_xml,
{'SOAPAction': '"urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"',
'Content-Type': 'text/xml'}
)
# wait for a response
resp = conn.getresponse()
return resp.status,resp.read()
def get_my_ip(routerip=None):
if routerip==None:
routerip="8.8.8.8" #default route
ret = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((routerip,80))
ret = s.getsockname()[0]
s.close()
except:
pass
return ret
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="verbose output",
action="store_true")
parser.add_argument("-e", "--external-port", type=int,
help="external port to open",dest="eport")
parser.add_argument("-l", "--local-port", type=int,
help="internal port (default: same as external)",dest="iport", default=None)
parser.add_argument("-i", "--lan-ip",
help="lan-ip where to forward the port, this computer if not specified",dest="lanip", default=None)
parser.add_argument("-r", "--router",
help="router ip to use. by default uses all discovered routers",action='append')
parser.add_argument("-p","--protocol",default='TCP', help="TCP or UDP")
parser.add_argument("-d", "--description",help="description for the rule")
parser.add_argument("--disable", action='store_true',dest='disable', help="disable a existing forwarding")
parser.add_argument("-t","--time", type=int, help="Duration of the rule",default=0)
args = parser.parse_args()
if args.verbose:
print "Discovering routers..."
res = discover()
if args.verbose or args.eport==None:
print "Found %s UPnP routers: "%len(res)," ".join([urlparse(x).netloc for x in res])
if args.eport==None:
print "No external port specified."
sys.exit(0)
if args.iport == None:
args.iport = args.eport
allok = True
for path in res:
discparsed = urlparse(path)
service_path = get_wanip_path(path)
service_url = "%s://%s%s"%(discparsed.scheme,discparsed.netloc,service_path)
routerip = discparsed.netloc.split(':')[0]
if args.router!=None and routerip not in args.router:
continue
localip = args.lanip
if args.lanip == None:
localip = get_my_ip(routerip)
enabled = int(not args.disable)
dis=''
if not enabled:
dis='disable of '
status,message = open_port(service_url,args.eport,internal_client=localip,internal_port=args.iport,protocol=args.protocol,duration=args.time,description=args.description,enabled = enabled)
if status==200:
if args.verbose:
print "%sport forward on %s successful, %s->%s:%s"%(dis,routerip,args.eport,localip,args.iport)
else:
sys.stderr.write("%sport forward on %s failed, status=%s message=%s\n"%(dis,routerip,status,message))
allok = False
if allok:
sys.exit(0)
else:
sys.exit(1)