Skip to main content
 首页 » 编程设计

Python Twisted 绕过 NAT

2024年11月01日10sxdcgaq8080

我正在使用 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() 

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

请您参考如下方法:

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