python-socketio icon indicating copy to clipboard operation
python-socketio copied to clipboard

tor proxy with requests.Session not working correct

Open ddasdasd506 opened this issue 6 months ago • 5 comments

Client `

import socketio
import requests
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# SOCKS proxy setup
proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050'
}
session = requests.Session()
session.proxies.update(proxies)

# Create client
sio = socketio.Client(
    http_session=session,
    ssl_verify=False,
)


@sio.event
def connect():
    print("Connected!")


@sio.event
def disconnect():
    print("Disconnected.")


@sio.on('task')
def on_task(data):
    print("Got task:", data)
sio.connect(
    "http://127.0.0.1:5000"
)
sio.wait()

`

server `

from flask import Flask
from flask_socketio import SocketIO, send

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins='*')

@app.route('/')
def index():
    return "SocketIO Server Running!"

@socketio.on('message')
def handle_message(msg):
    print(f"Received message: {msg}")
    # send(f"Echo: {msg}", broadcast=True)

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000,allow_unsafe_werkzeug=True)

`

if i comment this line session.proxies.update(proxies)

all working okay but if i use tor proxy, its conneting to server all okay but after connect its disconnecting and i getting "socket io client fails when use tor proxy One or more namespaces failed to connect" I put for example localhost, but already tested onion address, and response the same

ddasdasd506 avatar May 29 '25 14:05 ddasdasd506

Please provide logs of the client and server.

miguelgrinberg avatar May 29 '25 15:05 miguelgrinberg

if i use proxy server `

    Server initialized for eventlet.
    INFO:engineio.server:Server initialized for eventlet.
    INFO:server_test:Starting Flask-SocketIO server on port 5000
    zhnTngXrwx6oGbYwAAAA: Sending packet OPEN data {'sid': 'zhnTngXrwx6oGbYwAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    INFO:engineio.server:zhnTngXrwx6oGbYwAAAA: Sending packet OPEN data {'sid': 'zhnTngXrwx6oGbYwAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    zhnTngXrwx6oGbYwAAAA: Received packet MESSAGE data 0{}
    INFO:engineio.server:zhnTngXrwx6oGbYwAAAA: Received packet MESSAGE data 0{}
    DEBUG:server_test:Client connected
    zhnTngXrwx6oGbYwAAAA: Sending packet MESSAGE data 0{"sid":"DgbPKkDQojUJpooCAAAB"}
    INFO:engineio.server:zhnTngXrwx6oGbYwAAAA: Sending packet MESSAGE data 0{"sid":"DgbPKkDQojUJpooCAAAB"}
    zhnTngXrwx6oGbYwAAAA: Received packet CLOSE data 
    INFO:engineio.server:zhnTngXrwx6oGbYwAAAA: Received packet CLOSE data 
    DEBUG:server_test:Client disconnected

client

  Attempting polling connection to http://ip:3000/socket.io/?transport=polling&EIO=4
  INFO:engineio.client:Attempting polling connection to http://ip:3000/socket.io/?transport=polling&EIO=4
  DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): ip:3000
  DEBUG:urllib3.connectionpool:http://ip:3000 "GET /socket.io/?transport=polling&EIO=4&t=1748564050.798076 HTTP/1.1" 200 118
  Polling connection accepted with {'sid': 'zhnTngXrwx6oGbYwAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
  INFO:engineio.client:Polling connection accepted with {'sid': 'zhnTngXrwx6oGbYwAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
  Engine.IO connection established
  INFO:socketio.client:Engine.IO connection established
  Sending packet MESSAGE data 0{}
  INFO:engineio.client:Sending packet MESSAGE data 0{}
  Attempting WebSocket upgrade to ws://ip:3000/socket.io/?transport=websocket&EIO=4
  INFO:engineio.client:Attempting WebSocket upgrade to ws://ip:3000/socket.io/?transport=websocket&EIO=4
  DEBUG:websocket:Connecting proxy...
  WebSocket upgrade failed: connection error
  WARNING:engineio.client:WebSocket upgrade failed: connection error
  Sending polling GET request to http://ip:3000/socket.io/?transport=polling&EIO=4&sid=zhnTngXrwx6oGbYwAAAA
  INFO:engineio.client:Sending polling GET request to http://ip:3000/socket.io/?transport=polling&EIO=4&sid=zhnTngXrwx6oGbYwAAAA
  DEBUG:urllib3.connectionpool:Starting new HTTP connection (2): ip:3000
  DEBUG:urllib3.connectionpool:http://ip:3000 "POST /socket.io/?transport=polling&EIO=4&sid=zhnTngXrwx6oGbYwAAAA HTTP/1.1" 200 2
  Sending packet CLOSE data None
  INFO:engineio.client:Sending packet CLOSE data None
  Engine.IO connection dropped
  INFO:socketio.client:Engine.IO connection dropped
  Connected!
  DEBUG:urllib3.connectionpool:http://ip:3000 "GET /socket.io/?transport=polling&EIO=4&sid=zhnTngXrwx6oGbYwAAAA&t=1748564051.825738 HTTP/1.1" 200 32
  Received packet MESSAGE data 0{"sid":"DgbPKkDQojUJpooCAAAB"}
  INFO:engineio.client:Received packet MESSAGE data 0{"sid":"DgbPKkDQojUJpooCAAAB"}
  Namespace / is connected
  INFO:socketio.client:Namespace / is connected
  Waiting for write loop task to end
  INFO:engineio.client:Waiting for write loop task to end
  DEBUG:urllib3.connectionpool:http://ip:3000 "POST /socket.io/?transport=polling&EIO=4&sid=zhnTngXrwx6oGbYwAAAA HTTP/1.1" 200 2
  Exiting write loop task
  INFO:engineio.client:Exiting write loop task
  Exiting read loop task
  INFO:engineio.client:Exiting read loop task

venv/lib/python3.12/site-packages/socketio/client.py", line 168, in connect raise exceptions.ConnectionError( socketio.exceptions.ConnectionError: One or more namespaces failed to connect

`

if i dont use proxy

server

`

  Server initialized for eventlet.
  INFO:engineio.server:Server initialized for eventlet.
  INFO:server_test:Starting Flask-SocketIO server on port 5000
  pdkHILFODwgSqFYBAAAA: Sending packet OPEN data {'sid': 'pdkHILFODwgSqFYBAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
  INFO:engineio.server:pdkHILFODwgSqFYBAAAA: Sending packet OPEN data {'sid': 'pdkHILFODwgSqFYBAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
  pdkHILFODwgSqFYBAAAA: Received request to upgrade to websocket
  INFO:engineio.server:pdkHILFODwgSqFYBAAAA: Received request to upgrade to websocket
  pdkHILFODwgSqFYBAAAA: Upgrade to websocket successful
  INFO:engineio.server:pdkHILFODwgSqFYBAAAA: Upgrade to websocket successful
  pdkHILFODwgSqFYBAAAA: Received packet MESSAGE data 0{}
  INFO:engineio.server:pdkHILFODwgSqFYBAAAA: Received packet MESSAGE data 0{}
  DEBUG:server_test:Client connected
  pdkHILFODwgSqFYBAAAA: Sending packet MESSAGE data 0{"sid":"Ii2o-BT-frVDAr7pAAAB"}
  INFO:engineio.server:pdkHILFODwgSqFYBAAAA: Sending packet MESSAGE data 0{"sid":"Ii2o-BT-frVDAr7pAAAB"}

`

client `

    Attempting polling connection to http://ip:3000/socket.io/?transport=polling&EIO=4
    INFO:engineio.client:Attempting polling connection to http://ip:3000/socket.io/?transport=polling&EIO=4
    DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): ip:3000
    DEBUG:urllib3.connectionpool:http://ip:3000 "GET /socket.io/?transport=polling&EIO=4&t=1748564300.70734 HTTP/1.1" 200 118
    Polling connection accepted with {'sid': 'pdkHILFODwgSqFYBAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    INFO:engineio.client:Polling connection accepted with {'sid': 'pdkHILFODwgSqFYBAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    Engine.IO connection established
    INFO:socketio.client:Engine.IO connection established
    Sending packet MESSAGE data 0{}
    INFO:engineio.client:Sending packet MESSAGE data 0{}
    Attempting WebSocket upgrade to ws://ip:3000/socket.io/?transport=websocket&EIO=4
    INFO:engineio.client:Attempting WebSocket upgrade to ws://ip:3000/socket.io/?transport=websocket&EIO=4
    WebSocket upgrade was successful
    INFO:engineio.client:WebSocket upgrade was successful
    Received packet NOOP data 
    INFO:engineio.client:Received packet NOOP data 
    Received packet MESSAGE data 0{"sid":"Ii2o-BT-frVDAr7pAAAB"}
    INFO:engineio.client:Received packet MESSAGE data 0{"sid":"Ii2o-BT-frVDAr7pAAAB"}
    Namespace / is connected
    INFO:socketio.client:Namespace / is connected
    Connected!
    Received packet PING data 
    INFO:engineio.client:Received packet PING data 
    Sending packet PONG data 
    INFO:engineio.client:Sending packet PONG data 
    Received packet PING data 
    INFO:engineio.client:Received packet PING data 
    Sending packet PONG data 
    INFO:engineio.client:Sending packet PONG data 

`

ddasdasd506 avatar May 30 '25 00:05 ddasdasd506

I'm not sure this is the main problem, but your WebSocket connection is failing. Wouldn't you need to configure the ws:// and wss:// protocols in your proxy setup to be able to use WebSocket connections?

miguelgrinberg avatar May 30 '25 09:05 miguelgrinberg

yes, if i use asyncClient and aiohttp session all work okay

ddasdasd506 avatar Jun 01 '25 07:06 ddasdasd506

The async option uses aiohttp both for HTTP and WebSocket. The sync option uses requests and websocket-client. Maybe there is an issue in websocket-client that does not like working with a proxy.

miguelgrinberg avatar Jun 01 '25 14:06 miguelgrinberg

I've been reviewing this issue and your diagnosis seems spot on. The problem appears to be isolated to how the websocket-client library handles SOCKS proxies during the protocol upgrade. To confirm this, I can try to create a minimal, reproducible example using only websocket-client (without python-socketio) to attempt a WebSocket connection through a SOCKS5 proxy. If it fails as expected, that would confirm the issue lies within the dependency itself.

If you think this is a helpful next step, I'm happy to work on it and report back with my findings.

CarlosNatanael avatar Aug 18 '25 19:08 CarlosNatanael

@CarlosNatanael I'm not working on this issue right now, and the OP apparently doesn't care either, so feel free to investigate.

miguelgrinberg avatar Aug 18 '25 23:08 miguelgrinberg