如何解决带有 .onion 域的服务器到服务器套接字连接
我想使用 python 通过 tor 创建 p2p 连接,并直接与终端或使用 python 脚本连接。
为了实现我配置了隐藏服务,创建了一个套接字服务器并绑定到隐藏服务端口。但是没有用。
这是我的服务器文件
#!/usr/bin/python
#server.py
import socket,sys,_thread,os
from time import sleep
import colorama
from colorama import Fore,Back
colorama.init(autoreset=True)
os.system('cls' if os.name == 'nt' else 'clear')
name = 'Server'
print('Setup Server...')
soc = socket.socket()
host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)
port = 1234
soc.bind((host_name,port))
print(host_name,'({})'.format(ip))
# name = input('Enter name: ')
soc.listen(1) #Try to locate using socket
print('Waiting for incoming connections...')
connection,addr = soc.accept()
print("Received connection from ",addr[0],"(",addr[1],")\n")
print('Connection Established. Connected From: {},({})'.format(addr[0],addr[0]))
client_name = connection.recv(1024)
client_name = client_name.decode()
os.system('cls' if os.name == 'nt' else 'clear')
print(client_name + ' has connected...')
print('Enter -q to exit.')
connection.send(name.encode())
def print_msg( thread_name ):
while True:
msg = connection.recv(1024)
msg = msg.decode()
if msg == "-q":
print(f'{client_name} exit the chat room')
elif msg != "":
print(Fore.GREEN + f'{client_name}: {msg}')
sleep(0.5)
while True:
try:
_thread.start_new_thread( print_msg,("Thread-1",) )
except:
break
try:
message = input()
if message == '-q':
connection.send(message.encode())
print("\n")
break
connection.send(message.encode())
except KeyboardInterrupt:
print('Enter -q to close the connection.')
我的客户档案
#!/usr/bin/python3
#client.py
import socket,os
from time import sleep
import socks
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1",9050,True)
os.system('cls' if os.name == 'nt' else 'clear')
remote_host = 'domain_name.onion'
name = 'Rudransh'
print('Client Server...')
soc = socket.socket()
port = 1234
sleep(1)
soc.connect((remote_host,port))
print("Connected...\n")
sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')
soc.send(name.encode())
server_name = soc.recv(1024)
server_name = server_name.decode()
print(f'{server_name} has joined...')
print('Enter -q to exit.')
def print_msg( thread_name ):
while True:
msg = soc.recv(1024)
msg = msg.decode()
if msg == "-q":
print(f'{server_name} close the connection')
elif msg != "":
print(f'{server_name}: {msg}')
sleep(0.5)
while True:
try:
_thread.start_new_thread( print_msg,) )
except:
break
try:
message = input()
if message == "-q":
soc.send(message.encode())
print("\n")
break
soc.send(message.encode())
except KeyboardInterrupt:
print('Enter -q to close the connection.')
如果我使用本地 IP 通过相同的 WiFi 或 LAN 连接进行连接,则它可以工作。 我想通过 tor 连接它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。