2

I'm making a peer to peer application in Python using Twisted. It works great between my VPS's but when I try to connect to my home machine it doesn't work. I assume it's either something to do with the NAT or the Firewall on my router. My question is, does anyone know how to bypass a routers firewall or connect to a machine behind a NAT with Twisted? Here's my code:

from twisted.internet import reactor, protocol
from twisted.protocols import basic
from sys import *
import os.path

if (argv[1] == ""):
  HOST = "localhost"
else:
  HOST = argv[1]

PORT = 9001
if (os.path.isfile("zones/root.txt")):
  root = open("zones/root.txt", "rw").read()
else:
  root = ""

root1 = root
root2 = ""

maxPeers = 8
peerCount = 0
routingTable = []
ipaddress = []
depth = 0
ip = ""
peerId = ""

class p2pClient(basic.LineReceiver):

    global root
    global root2
    global checkRoot
    global depth

    clientTable = []

    def checkRoot(root2):

      if (root1 == root2):
        print "Roots are the same"
      else:
        print "Different roots."

    def connectionMade(self):
        print "connected to peer!"

    def connectionLost(self, reason):

        print "Disconnected from server!"

    def lineReceived(self, data):
        data = data.split("{}")
        clientTable = data[0].replace("[","").replace("]","").replace("'","").replace(" ","").split(",")
        root2 = data[1]
        checkRoot(root2)

class p2pClientFactory(protocol.ClientFactory):
    protocol = p2pClient

class p2pServer(basic.LineReceiver):

    global routingTable
    global addToRoutingTable
    global removeFromRoutingTable
    global ipaddress

    def replace(l, X, Y):
        for i,v in enumerate(l):
           if v == X:
              l.pop(i)
              l.insert(i, Y)

    def removeFromRoutingTable(ip):
        routingTable.remove(ip)

    def addToRoutingTable(ip):

        routingTable.append(ip)

    def sendData(self, msg):

        self.transport.write("%s\r\n" % msg)

    def connectionMade(self):

        global peerCount # global allows us to use peerCount in our class
        global depth
        peerCount = peerCount + 1 # Update the peer count

        #ip = self.transport.getHost().host
        ip = self.transport.getPeer()
        ip = str(ip)
        ip = ip.split("'")
        ip = ip[1]
        ipaddress.append(ip)

        addToRoutingTable(ip)

        self.transport.write(str(routingTable) + "{}")

        self.sendData(root) # Send the root zone file to peer 1

        print "Peer Count : [ " + str(peerCount) + " / 8 ]"
        print "Routing Table: " + str(routingTable)

    def connectionLost(self, reason):

        global peerCount

        peerCount = peerCount - 1
        ipaddr = str(self.transport.getPeer()).split("'")

        removeFromRoutingTable(ipaddr[1])

        print "Client Disconnected!"
        print "Peer Count : [ " + str(peerCount) + " / 8 ]"
        print "Routing Table : " + str(routingTable)

class p2pServerFactory(protocol.Factory):

    protocol = p2pServer

sfactory = p2pServerFactory()
reactor.listenTCP(PORT, sfactory)

cfactory = p2pClientFactory()
reactor.connectTCP(HOST, PORT, cfactory)

reactor.run()

I appreciate the code is long but I'd really appreciate any help. Thanks!

2 Answers 2

1

You code and the fact that you are using twisted are irrelevant here. You have to set up the NAT in your router to forward port 9001 to the ip of your local machine. The way to make the configuration is router dependend. Search for "NAT port forwarding + routername". I think that is a good startingpoint. It may in rare cases be a firewall issue, but it is much more likly to be a NAT problem.

2
  • Is it possible to make my Python program do this? Thanks for your answer!
    – Francis
    May 24, 2014 at 22:34
  • 3
    In theory yes. You may be able to use UPnP(if your router supports it) to solve your problem with code. BUT, it is NOT trivial. If you are not an experienced network/twisted programmer, my guess is that you will fail :( An implementation is available here. github.com/fuzeman/PyUPnP But i really encourage you to just configure you router. Create a Nat rule and a static DHCP rule for your device and you are good to go.
    – brunsgaard
    May 24, 2014 at 22:44
1

If you want to write a peer-to-peer application using Twisted, Vertex provides an implementation of some NAT-traversal logic for establishing stream-based connections over UDP. Unfortunately its documentation is pretty thin, and writing up exactly how to use it would be pretty long-winded for an SO answer, but if you try to use it and start filing bugs you will likely motivate some activity on the project :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.