[PYTHON-2943] : Add socks5 proxy support
The required tests are specified here.
If you'd like to try running everything locally, I'd suggest something like the following:
git clone https://github.com/mongodb-labs/drivers-evergreen-tools
cd drivers-evergreen-tools
bash .evergreen/setup.sh # create a standalone cli for https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/socks5srv.py
make run-server # start a server on 27017
./evergreen/socks5srv # run the cli
Once you have the tests working locally, I can either help walk through the Evergreen Configuration portion, or just make commits to your branch if you prefer.
The required tests are specified here.
If you'd like to try running everything locally, I'd suggest something like the following:
git clone https://github.com/mongodb-labs/drivers-evergreen-tools cd drivers-evergreen-tools bash .evergreen/setup.sh # create a standalone cli for https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/socks5srv.py make run-server # start a server on 27017 ./evergreen/socks5srv # run the cliOnce you have the tests working locally, I can either help walk through the Evergreen Configuration portion, or just make commits to your branch if you prefer.
I'm not sure how to add the tests to work with proxy on your environment I will try to look into it but if you will be able to assist and push changes into this branch it will be amazing
I'm not sure how to add the tests to work with proxy on your environment
I meant for local testing on your machine. If you can get the tests passing locally I can hook up the CI environment part. If you'd rather not, I understand as well.
I'm not sure how to add the tests to work with proxy on your environment
I meant for local testing on your machine. If you can get the tests passing locally I can hook up the CI environment part. If you'd rather not, I understand as well.
I would appreciate it, if will be able to hook it into the CI
Just to clarify, are you going to try writing the tests and getting them to pass locally?
Just to clarify, are you going to try writing the tests and getting them to pass locally?
I am not getting to and i would love to get your help to implement the tests I have started to write the cases, I hope you will find it helpful
Let me know if it's working
from __future__ import annotations
import pytest
import pymongo
from pymongo.errors import ConnectionFailure
@pytest.mark.parametrize(
"conn_str, should_succeed",
[
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1080&directConnection=true",
False,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1081&directConnection=true",
True,
),
("mongodb://replicaset/?proxyHost=localhost&proxyPort=1080", False),
("mongodb://replicaset/?proxyHost=localhost&proxyPort=1081", True),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1080&proxyUsername=nonexistentuser&proxyPassword=badauth&directConnection=true",
False,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1081&proxyUsername=nonexistentuser&proxyPassword=badauth&directConnection=true",
True,
),
(
"mongodb://replicaset/?proxyHost=localhost&proxyPort=1081&proxyUsername=nonexistentuser&proxyPassword=badauth",
True,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1080&proxyUsername=username&proxyPassword=p4ssw0rd&directConnection=true",
True,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1081&directConnection=true",
True,
),
(
"mongodb://replicaset/?proxyHost=localhost&proxyPort=1080&proxyUsername=username&proxyPassword=p4ssw0rd",
True,
),
("mongodb://replicaset/?proxyHost=localhost&proxyPort=1081", True),
],
)
def test_socks5_proxy_support(conn_str, should_succeed):
client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=1000)
try:
client.admin.command("hello")
assert should_succeed, f"Connection should have failed: {conn_str}"
except ConnectionFailure:
assert not should_succeed, f"Connection should have succeeded: {conn_str}"
finally:
client.close()
@pytest.mark.parametrize(
"conn_str, should_succeed",
[
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1080&directConnection=true",
False,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1081&directConnection=true",
True,
),
("mongodb://replicaset/?proxyHost=localhost&proxyPort=1080", False),
("mongodb://replicaset/?proxyHost=localhost&proxyPort=1081", True),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1080&proxyUsername=nonexistentuser&proxyPassword=badauth&directConnection=true",
False,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1081&proxyUsername=nonexistentuser&proxyPassword=badauth&directConnection=true",
True,
),
(
"mongodb://replicaset/?proxyHost=localhost&proxyPort=1081&proxyUsername=nonexistentuser&proxyPassword=badauth",
True,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1080&proxyUsername=username&proxyPassword=p4ssw0rd&directConnection=true",
True,
),
(
"mongodb://localhost:12345/?proxyHost=localhost&proxyPort=1081&directConnection=true",
True,
),
(
"mongodb://replicaset/?proxyHost=localhost&proxyPort=1080&proxyUsername=username&proxyPassword=p4ssw0rd",
True,
),
("mongodb://replicaset/?proxyHost=localhost&proxyPort=1081", True),
],
)
def test_socks5_proxy_options(conn_str, should_succeed):
client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=1000)
try:
client.admin.command("hello")
assert should_succeed, f"Connection should have failed: {conn_str}"
except ConnectionFailure:
assert not should_succeed, f"Connection should have succeeded: {conn_str}"
finally:
client.close()
Understood, thank you! I won't be able to get to this until at least next week.
Understood, thank you! I won't be able to get to this until at least next week.
If you will be able to do it next week it will be amazing :-)
I had missed the use of the private variable _socket in proxy.connect. That argument is deprecated. It also seems like the from python_socks.sync.v2 import Proxy interface is the preferred way forward, since it is used by httpx_socks. Would you be able to make the necessary changes for the new API?
Another option is to switch to PySocks, which is used by requests and supports direct socket access.
Hi @zaif-yuval, this PR picked up some merge conflicts over time. I'm going to close it. Please feel free to re-open if you'd like to continue working on it.