【问题标题】:Python Twisted bypass NATPython Twisted 绕过 NAT
【发布时间】:2014-05-24 20:54:36
【问题描述】:

我正在使用 Twisted 在 Python 中制作点对点应用程序。它在我的 VPS 之间运行良好,但是当我尝试连接到我的家用机器时它不起作用。我认为这与路由器上的 NAT 或防火墙有关。我的问题是,有谁知道如何绕过路由器防火墙或使用 Twisted 连接到 NAT 后面的机器?这是我的代码:

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()

我很欣赏代码很长,但我真的很感谢任何帮助。谢谢!

【问题讨论】:

    标签: python twisted firewall p2p nat


    【解决方案1】:

    您的代码和您使用扭曲的事实在这里无关紧要。您必须在路由器中设置 NAT 以将端口 9001 转发到本地计算机的 ip。进行配置的方式取决于路由器。搜索“NAT 端口转发 + 路由器名称”。我认为这是一个很好的起点。 可能在极少数情况下是防火墙问题,但更有可能是 NAT 问题。

    【讨论】:

    • 是否可以让我的 Python 程序做到这一点?感谢您的回答!
    • 理论上是的。您也许可以使用 UPnP(如果您的路由器支持)来解决您的代码问题。但是,这不是微不足道的。如果你不是一个有经验的网络/扭曲的程序员,我猜你会失败:(这里有一个实现。github.com/fuzeman/PyUPnP 但我真的鼓励你只配置你的路由器。创建一个 Nat 规则和一个静态 DHCP 规则你的设备,你很高兴。
    【解决方案2】:

    如果您想使用 Twisted 编写点对点应用程序,Vertex 提供了一些 NAT 遍历逻辑的实现,用于通过 UDP 建立基于流的连接。不幸的是,它的文档非常薄,确切地写出如何使用它对于一个 SO 答案来说是相当冗长的,但是如果你尝试使用它并开始提交错误,你可能会激发项目中的一些活动:-)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 2012-12-20
      • 2013-10-16
      • 2010-12-26
      • 1970-01-01
      • 2020-04-03
      • 2013-01-19
      相关资源
      最近更新 更多