Skip to content

Instantly share code, notes, and snippets.

@thirtythreeforty
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thirtythreeforty/11231280 to your computer and use it in GitHub Desktop.
Save thirtythreeforty/11231280 to your computer and use it in GitHub Desktop.
A TCP hole puncher
#!/usr/bin/python3
# tcphole.py
# By George Hilliard ("thirtythreeforty")
# A simple TCP hole puncher for a firewall accepting only ESTABLISHED,RELATED packets
# This currently binds to the localhost address, although that's easy to fix by changing
# the bind() calls.
# This is based on the theory found at http://www.bford.info/pub/net/p2pnat/index.html#sec-tcp.
# You may use this software under the terms of the Creative Commons CC-0 license.
# That is, it is public domain. However, I'd appreciate it if you'd mention my name.
import sys
import socket
import _thread as thread
def client():
c = socket.socket()
c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
c.bind(('', int(sys.argv[3])))
while(c.connect_ex((sys.argv[1], int(sys.argv[2])))):
pass
print("client connected!")
while True:
pass
def server():
c = socket.socket()
c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
c.bind(('', int(sys.argv[3])))
c.listen(5)
c.accept()
print("server connected!")
while True:
pass
def main():
thread.start_new_thread(client, ())
thread.start_new_thread(server, ())
while True:
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment