gitso

gitso Commit Details


Date:2012-10-14 13:57:44 (12 years 2 months ago)
Author:gerberad
Branch:master
Commit:a5841e68ea9b0f0da4d6e285cd87e16905e2974f
Parents: dbbe1c7b6ec67d1061f211b22a524e2eebf57141
Message:Updated NATPMP library and added BSD support, patch from: jirib@devio.us

Changes:

File differences

gitso/trunk/ArgsParser.py
4343
4444
4545
46
46
4747
4848
4949
self.paths['mode'] = ''
self.paths['low-colors'] = False
if sys.platform.find('linux') != -1:
if re.match('(?:open|free|net)bsd|linux',sys.platform):
self.paths['main'] = os.path.join(sys.path[0], '..', 'share', 'gitso')
self.paths['copyright'] = os.path.join(sys.path[0], '..', 'share', 'doc', 'gitso', 'COPYING')
elif sys.platform == "darwin":
gitso/trunk/ConnectionWindow.py
5050
5151
5252
53
53
5454
5555
5656
......
8888
8989
9090
91
91
9292
9393
9494
......
135135
136136
137137
138
138
139139
140140
141141
......
174174
175175
176176
177
177
178178
179179
180180
181181
182182
183183
184
184
185185
186186
187187
# Disable until 0.7 release
self.enablePMP = False
if sys.platform.find('linux') != -1:
if re.match('(?:open|free|net)bsd|linux',sys.platform):
width = 165
height = 350
xval1 = 155
self.Bind(wx.EVT_RADIOBUTTON, self.RadioToggle, id=self.rb2.GetId())
# checkbox for natpmp
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
if self.enablePMP:
self.cb1 = wx.CheckBox(self, -1, 'Use NAT-PMP', (130, 48))
self.cb1.Enable(False)
menuBar.Append(fileMenu, "&File")
menuBar.Append(editMenu, "&Edit")
if sys.platform.find('linux') != -1 or sys.platform == 'win32':
if re.match('(?:open|free|net)bsd|linux',sys.platform) or sys.platform == 'win32':
menuBar.Append(helpMenu, "&Help")
self.SetMenuBar(menuBar)
self.ToggleValue = 0
self.hostField.Enable(True)
self.cb2.Enable(False)
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
if self.enablePMP:
self.cb1.Enable(False)
else:
self.ToggleValue = 1
self.hostField.Enable(False)
self.cb2.Enable(True)
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
if self.enablePMP:
self.cb1.Enable(True)
gitso/trunk/GitsoThread.py
2727
2828
2929
30
30
3131
3232
3333
......
5959
6060
6161
62
62
6363
6464
6565
......
9999
100100
101101
102
102
103103
104104
105105
......
122122
123123
124124
125
125
126126
127127
128128
......
149149
150150
151151
152
152
153153
154154
155155
import os, sys, signal, os.path
import Processes
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
import NATPMP
class GitsoThread(threading.Thread):
self.error = True
else:
# Give Support
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
if self.window.enablePMP:
self.window.cb1.Enable(False)
if self.window.cb1.GetValue() == True:
@author: Aaron Gerber
"""
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
if self.window.enablePMP:
if self.window.rb1.GetValue() == False: #give support
if self.window.cb1.GetValue() == True:
connection = []
listen = []
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
if self.host <> "":
connection = os.popen('LANG=C netstat -an | grep 5500 | grep ESTABLISHED').readlines()
else:
@author: Dennis Koot
"""
if sys.platform == 'darwin' or sys.platform.find('linux') != -1:
if sys.platform == 'darwin' or re.match('(?:open|free|net)bsd|linux',sys.platform):
if self.window.enablePMP:
if action == 'request':
lifetime = 3600
gitso/trunk/NATPMP.py
1515
1616
1717
18
18
1919
20
21
22
2023
2124
2225
2326
2427
2528
26
27
29
30
2831
2932
3033
......
4952
5053
5154
52
55
5356
54
57
5558
5659
5760
......
199202
200203
201204
202
203
205
204206
205207
206208
207209
208
209
210
211
210
212211
213
214
212
213
214
215
216
215217
216218
217219
......
243245
244246
245247
246
248
247249
248250
249251
......
255257
256258
257259
260
261
258262
259263
260
264
261265
262266
263267
......
265269
266270
267271
268
272
269273
270274
271275
......
279283
280284
281285
282
286
283287
284288
285
289
286290
287
291
288292
289293
290294
......
298302
299303
300304
301
305
302306
303307
304
308
305309
306
310
307311
308312
309313
......
317321
318322
319323
320
324
321325
322326
323327
324328
329
330
325331
326332
327
328
333
334
329335
330336
331337
......
342348
343349
344350
345
351
346352
347353
348354
......
354360
355361
356362
357
358
363
364
359365
360366
361367
http://files.dns-sd.org/draft-cheshire-nat-pmp.txt
Requires Python 2.3 or later.
Tested on Python 2.3, 2.4, 2.5 against Apple AirPort Express.
Tested on Python 2.5, 2.6 against Apple AirPort Express.
0.2.2 - changed gateway autodetect, per github issue #1. thanks to jirib
0.2 - changed useException to use_exception, responseDataClass to response_data_class parameters in function calls for consistency
0.1 - repackaged via setuptools. Fixed major bug in gateway detection. Experimental gateway detection support for Windows 7. Python 2.6 testing.
0.0.1.2 - NT autodetection code. Thanks to roee shlomo for the gateway detection regex!
0.0.1.1 - Removed broken mutex code
0.0.1 - Initial release
"""
__version__ = "0.0.1.2"
__license__ = """Copyright (c) 2008, Yiming Liu, All rights reserved.
__version__ = "0.2"
__license__ = """Copyright (c) 2008-2010, Yiming Liu, All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE."""
__author__ = "Yiming Liu <http://sarth.thallos.org/>"
__author__ = "Yiming Liu <http://www.yimingliu.com/>"
import struct, socket, select, time
import struct, socket, select, time, platform
import sys, os, re
NATPMP_PORT = 5351
It does not guarantee correct results.
This function requires the presence of
netstat on the path on POSIX and NT. It requires ip on
Linux.
netstat on the path on POSIX and NT.
"""
addr = ""
shell_command = 'netstat -rn'
if os.name == "posix":
pattern = re.compile('default\s+([\w.:]+)\s+\w')
if "linux" in sys.platform:
shell_command = "ip route show"
pattern = re.compile('default via\s+([\w.:]+)\s+\w')
pattern = re.compile('(?:default|0\.0\.0\.0|::/0)\s+([\w\.:]+)\s+.*UG')
elif os.name == "nt":
pattern = re.compile(".*?Default Gateway:[ ]+(.*?)\n")
system_out = os.popen(shell_command, 'r').read() # TODO: this could be a security issue
if platform.version().startswith("6.1"):
pattern = re.compile(".*?0.0.0.0[ ]+0.0.0.0[ ]+(.*?)[ ]+?.*?\n")
else:
pattern = re.compile(".*?Default Gateway:[ ]+(.*?)\n")
system_out = os.popen(shell_command, 'r').read()
if not system_out:
raise NATPMPNetworkError(NATPMP_GATEWAY_CANNOT_FIND, error_str(NATPMP_GATEWAY_CANNOT_FIND))
match = pattern.search(system_out)
response_socket.connect((gateway, NATPMP_PORT))
return response_socket
def get_public_address(gateway_ip=get_gateway_addr(), retry=9):
def get_public_address(gateway_ip=None, retry=9):
"""A high-level function that returns the public interface IP of
the current host by querying the NAT-PMP gateway. IP is
returned as string.
retry - the number of times to retry the request if unsuccessful.
Defaults to 9 as per specification.
"""
if gateway_ip == None:
gateway_ip = get_gateway_addr()
addr = None
addr_request = PublicAddressRequest()
addr_response = send_request_with_retry(gateway_ip, addr_request, responseDataClass=PublicAddressResponse, retry=retry)
addr_response = send_request_with_retry(gateway_ip, addr_request, response_data_class=PublicAddressResponse, retry=retry)
if addr_response.result != 0:
#sys.stderr.write("NAT-PMP error %d: %s\n" % (addr_response.result, error_str(addr_response.result)))
#sys.stderr.flush()
addr = addr_response.ip
return addr
def map_tcp_port(public_port, private_port, lifetime=3600, gateway_ip=get_gateway_addr(), retry=9, useException=True):
def map_tcp_port(public_port, private_port, lifetime=3600, gateway_ip=None, retry=9, use_exception=True):
"""A high-level wrapper to map_port() that requests a mapping
for a public TCP port on the NAT to a private TCP port on this host.
Returns the complete response on success.
get_gateway_addr()
retry - the number of times to retry the request if unsuccessful.
Defaults to 9 as per specification.
useException - throw an exception if an error result is
use_exception - throw an exception if an error result is
received from the gateway. Defaults to True.
"""
return map_port(NATPMP_PROTOCOL_TCP, public_port, private_port, lifetime, gateway_ip=gateway_ip, retry=retry, useException=useException)
return map_port(NATPMP_PROTOCOL_TCP, public_port, private_port, lifetime, gateway_ip=gateway_ip, retry=retry, use_exception=use_exception)
def map_udp_port(public_port, private_port, lifetime=3600, gateway_ip=get_gateway_addr(), retry=9, useException=True):
def map_udp_port(public_port, private_port, lifetime=3600, gateway_ip=None, retry=9, use_exception=True):
"""A high-level wrapper to map_port() that requests a mapping for
a public UDP port on the NAT to a private UDP port on this host.
Returns the complete response on success.
get_gateway_addr()
retry - the number of times to retry the request if unsuccessful.
Defaults to 9 as per specification.
useException - throw an exception if an error result is
use_exception - throw an exception if an error result is
received from the gateway. Defaults to True.
"""
return map_port(NATPMP_PROTOCOL_UDP, public_port, private_port, lifetime, gateway_ip=gateway_ip, retry=retry, useException=useException)
return map_port(NATPMP_PROTOCOL_UDP, public_port, private_port, lifetime, gateway_ip=gateway_ip, retry=retry, use_exception=use_exception)
def map_port(protocol, public_port, private_port, lifetime=3600, gateway_ip=get_gateway_addr(), retry=9, useException=True):
def map_port(protocol, public_port, private_port, lifetime=3600, gateway_ip=None, retry=9, use_exception=True):
"""A function to map public_port to private_port of protocol.
Returns the complete response on success.
get_gateway_addr()
retry - the number of times to retry the request if unsuccessful.
Defaults to 9 as per specification.
useException - throw an exception if an error result
use_exception - throw an exception if an error result
is received from the gateway. Defaults to True.
"""
if protocol not in [NATPMP_PROTOCOL_UDP, NATPMP_PROTOCOL_TCP]:
raise ValueError("Must be either NATPMP_PROTOCOL_UDP or NATPMP_PROTOCOL_TCP")
if gateway_ip == None:
gateway_ip = get_gateway_addr()
response = None
port_mapping_request = PortMapRequest(protocol, private_port, public_port, lifetime)
port_mapping_response = send_request_with_retry(gateway_ip, port_mapping_request, responseDataClass=PortMapResponse, retry=retry)
if port_mapping_response.result != 0 and useException:
port_mapping_response = send_request_with_retry(gateway_ip, port_mapping_request, response_data_class=PortMapResponse, retry=retry)
if port_mapping_response.result != 0 and use_exception:
raise NATPMPResultError(port_mapping_response.result, error_str(port_mapping_response.result), port_mapping_response)
return port_mapping_response
data,source_addr = resp_socket.recvfrom(responseSize)
return data,source_addr
def send_request_with_retry(gateway_ip, request, responseDataClass=None, retry=9):
def send_request_with_retry(gateway_ip, request, response_data_class=None, retry=9):
gateway_socket = get_gateway_socket(gateway_ip)
n = 1
data = ""
n += 1
if n >= retry and not data:
raise NATPMPUnsupportedError(NATPMP_GATEWAY_NO_SUPPORT, error_str(NATPMP_GATEWAY_NO_SUPPORT))
if data and responseDataClass:
data = responseDataClass(data)
if data and response_data_class:
data = response_data_class(data)
return data
gitso/trunk/Processes.py
3535
3636
3737
38
38
3939
4040
4141
......
5959
6060
6161
62
62
6363
6464
6565
......
9393
9494
9595
96
96
9797
9898
99
100
99
100
101101
102102
103103
def getSupport(self, host):
if sys.platform == 'darwin':
self.returnPID = os.spawnl(os.P_NOWAIT, '%sOSXvnc/OSXvnc-server' % self.paths['resources'], '%sOSXvnc/OSXvnc-server' % self.paths['resources'], '-connectHost', '%s' % host)
elif sys.platform.find('linux') != -1:
elif re.match('(?:open|free|net)bsd|linux',sys.platform):
# We should include future versions with options for speed.
#self.returnPID = os.spawnlp(os.P_NOWAIT, 'x11vnc', 'x11vnc','-nopw','-ncache','20','-solid','black','-connect','%s' % host)
if sys.platform == 'darwin':
vncviewer = '%scotvnc.app/Contents/MacOS/cotvnc' % self.paths['resources']
self.returnPID = os.spawnlp(os.P_NOWAIT, vncviewer, vncviewer, '--listen')
elif sys.platform.find('linux') != -1:
elif re.match('(?:open|free|net)bsd|linux',sys.platform):
# These are the options for low-res connections.
# In the future, I'd like to support cross-platform low-res options.
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, self.returnPID.pid)
win32api.TerminateProcess(handle, -1)
win32api.CloseHandle(handle)
elif sys.platform.find('linux') != -1:
elif re.match('(?:open|free|net)bsd|linux',sys.platform):
# New processes are created when you made connections. So if you kill self.returnPID,
# you're just killing the dispatch process, not the one actually doing business...
os.spawnlp(os.P_NOWAIT, 'killall', 'killall', 'vncviewer')
os.spawnlp(os.P_NOWAIT, 'killall', 'killall', 'x11vnc')
os.spawnlp(os.P_NOWAIT, 'pkill', 'pkill', '-f', 'vncviewer')
os.spawnlp(os.P_NOWAIT, 'pkill', 'pkill', '-f', 'x11vnc')
else:
os.kill(self.returnPID, signal.SIGKILL)
self.returnPID = 0

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.07572s using 13 queries.