Skip to content

Commit e8494a3

Browse files
committed
Various fixes
1 parent 370419e commit e8494a3

File tree

7 files changed

+108
-37
lines changed

7 files changed

+108
-37
lines changed

evergreen/cleanup-proxy.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

evergreen/evergreen.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,30 @@ functions:
833833
- "OS"
834834
args:
835835
- evergreen/cleanup-test-resources.sh
836+
- command: shell.exec
837+
params:
838+
shell: "bash"
839+
script: |
840+
echo "Killing proxies"
841+
PID_FILE="socks5_pids.txt"
842+
843+
if [[ ! -f "$PID_FILE" ]]; then
844+
echo "No PID file found ($PID_FILE)"
845+
exit 0
846+
fi
847+
848+
cat "$PID_FILE" | while read -r pid; do
849+
if [[ -n "$pid" ]]; then
850+
if [[ "$OS" =~ Windows|windows ]]; then
851+
powershell -NoProfile -Command "Stop-Process -Id $pid -Force" 2>$null || \
852+
echo "PID $pid already gone"
853+
else
854+
kill "$pid" 2>/dev/null || echo "PID $pid already gone"
855+
fi
856+
fi
857+
done
858+
859+
rm -f "$PID_FILE"
836860
837861
fix-absolute-paths:
838862
- command: shell.exec
@@ -1040,8 +1064,13 @@ functions:
10401064
HOST_PORT=$(echo "$MONGODB_URI" | sed 's|mongodb://||' | cut -d',' -f1)
10411065
10421066
MAP_ARG="localhost:12345 to $HOST_PORT"
1043-
python3 $DRIVERS_TOOLS/.evergreen/socks5srv.py --map "$MAP_ARG" --port 1080 --auth username:p4ssw0rd &
1044-
python3 $DRIVERS_TOOLS/.evergreen/socks5srv.py --map "$MAP_ARG" --port 1081
1067+
python3 $DRIVERS_TOOLS/.evergreen/socks5srv.py --map "$MAP_ARG" --port 1080 --auth username:p4ssw0rd & PROXY_PID1=$!
1068+
python3 $DRIVERS_TOOLS/.evergreen/socks5srv.py --map "$MAP_ARG" --port 1081 & PROXY_PID2=$!
1069+
1070+
echo "$PROXY_PID1" > socks5_pids.txt
1071+
echo "$PROXY_PID2" >> socks5_pids.txt
1072+
1073+
echo "Started proxies with PIDs: $PROXY_PID1, $PROXY_PID2"
10451074
10461075
run-socks5-proxy-tests:
10471076
- command: shell.exec
@@ -1061,8 +1090,7 @@ functions:
10611090
FRAMEWORK=${FRAMEWORK} \
10621091
TARGET="TestSocks5Proxy" \
10631092
evergreen/run-tests.sh
1064-
OS=${OS} \
1065-
evergreen/cleanup-proxy.sh
1093+
10661094
pre:
10671095
- func: fetch-source
10681096
- func: prepare-resources

src/MongoDB.Driver/ClusterRegistry.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private IClusterInternal CreateCluster(ClusterKey clusterKey)
6262
.ConfigureConnectionPool(settings => ConfigureConnectionPool(settings, clusterKey))
6363
.ConfigureConnection(settings => ConfigureConnection(settings, clusterKey))
6464
.ConfigureTcp(settings => ConfigureTcp(settings, clusterKey))
65+
.ConfigureSocks5Proxy(settings => ConfigureSocks5Proxy(settings, clusterKey))
6566
.ConfigureLoggingSettings(_ => clusterKey.LoggingSettings);
6667
#pragma warning restore CS0618 // Type or member is obsolete
6768

@@ -174,6 +175,12 @@ private TcpStreamSettings ConfigureTcp(TcpStreamSettings settings, ClusterKey cl
174175
writeTimeout: clusterKey.SocketTimeout);
175176
}
176177

178+
private ProxyStreamSettings ConfigureSocks5Proxy(ProxyStreamSettings settings, ClusterKey clusterKey)
179+
{
180+
return settings.With(
181+
clusterKey.Socks5ProxySettings);
182+
}
183+
177184
internal IClusterInternal GetOrCreateCluster(ClusterKey clusterKey)
178185
{
179186
lock (_lock)

src/MongoDB.Driver/Core/Configuration/ClusterBuilder.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ClusterBuilder
4040
private SslStreamSettings _sslStreamSettings;
4141
private Func<IStreamFactory, IStreamFactory> _streamFactoryWrapper;
4242
private TcpStreamSettings _tcpStreamSettings;
43-
private Socks5ProxySettings _socks5ProxySettings;
43+
private ProxyStreamSettings _proxyStreamSettings;
4444

4545
// constructors
4646
/// <summary>
@@ -161,11 +161,13 @@ public ClusterBuilder ConfigureTcp(Func<TcpStreamSettings, TcpStreamSettings> co
161161
/// <summary>
162162
/// //TODO
163163
/// </summary>
164-
/// <param name="settings"></param>
164+
/// <param name="configurator"></param>
165165
/// <returns></returns>
166-
public ClusterBuilder SetSocks5ProxySettings(Socks5ProxySettings settings)
166+
public ClusterBuilder ConfigureSocks5Proxy(Func<ProxyStreamSettings, ProxyStreamSettings> configurator)
167167
{
168-
_socks5ProxySettings = settings;
168+
Ensure.IsNotNull(configurator, nameof(configurator));
169+
170+
_proxyStreamSettings = configurator(_proxyStreamSettings ?? new ProxyStreamSettings());
169171
return this;
170172
}
171173

@@ -299,9 +301,9 @@ private IStreamFactory CreateTcpStreamFactory(TcpStreamSettings tcpStreamSetting
299301
{
300302
var streamFactory = (IStreamFactory)new TcpStreamFactory(tcpStreamSettings);
301303

302-
if (_socks5ProxySettings != null)
304+
if (_proxyStreamSettings != null)
303305
{
304-
streamFactory = new ProxyStreamFactory(_socks5ProxySettings, streamFactory);
306+
streamFactory = new ProxyStreamFactory(_proxyStreamSettings, streamFactory);
305307
}
306308

307309
if (_sslStreamSettings != null)

src/MongoDB.Driver/Core/Configuration/ClusterBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ public static ClusterBuilder ConfigureWithConnectionString(
121121

122122
if (connectionString.ProxyHost != null)
123123
{
124-
builder = builder.SetSocks5ProxySettings(Socks5ProxySettings.Create(
124+
builder = builder.ConfigureSocks5Proxy(s => s.With(Socks5ProxySettings.Create(
125125
connectionString.ProxyHost,
126126
connectionString.ProxyPort,
127127
connectionString.ProxyUsername,
128-
connectionString.ProxyPassword));
128+
connectionString.ProxyPassword)));
129129
}
130130

131131
if (connectionString.SocketTimeout != null)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Driver.Core.Connections;
17+
using MongoDB.Driver.Core.Misc;
18+
19+
namespace MongoDB.Driver.Core.Configuration;
20+
21+
//TODO Add docs
22+
/// <summary>
23+
///
24+
/// </summary>
25+
public class ProxyStreamSettings
26+
{
27+
private Socks5ProxySettings _socks5ProxySettings;
28+
29+
/// <summary>
30+
///
31+
/// </summary>
32+
/// <param name="socks5ProxySettings"></param>
33+
public ProxyStreamSettings(Optional<Socks5ProxySettings> socks5ProxySettings = default)
34+
{
35+
_socks5ProxySettings = socks5ProxySettings.WithDefault(null);
36+
}
37+
38+
/// <summary>
39+
///
40+
/// </summary>
41+
public Socks5ProxySettings Socks5ProxySettings => _socks5ProxySettings;
42+
43+
/// <summary>
44+
///
45+
/// </summary>
46+
/// <param name="socks5ProxySettings"></param>
47+
/// <returns></returns>
48+
public ProxyStreamSettings With(Socks5ProxySettings socks5ProxySettings)
49+
{
50+
return new ProxyStreamSettings(socks5ProxySettings);
51+
}
52+
}

src/MongoDB.Driver/Core/Connections/ProxyStreamFactory.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,35 @@
1717
using System.Net;
1818
using System.Threading;
1919
using System.Threading.Tasks;
20+
using MongoDB.Driver.Core.Configuration;
2021
using MongoDB.Driver.Core.Misc;
2122

2223
namespace MongoDB.Driver.Core.Connections;
2324

2425
internal class ProxyStreamFactory: IStreamFactory
2526
{
26-
private readonly Socks5ProxySettings _settings;
27+
private readonly ProxyStreamSettings _settings;
2728
private readonly IStreamFactory _wrapped;
2829

29-
public ProxyStreamFactory(Socks5ProxySettings settings, IStreamFactory wrapped)
30+
public ProxyStreamFactory(ProxyStreamSettings settings, IStreamFactory wrapped)
3031
{
3132
_settings = Ensure.IsNotNull(settings, nameof(settings));
3233
_wrapped = Ensure.IsNotNull(wrapped, nameof(wrapped));
3334
}
3435

3536
public Stream CreateStream(EndPoint endPoint, CancellationToken cancellationToken)
3637
{
37-
var proxyEndpoint = new DnsEndPoint(_settings.Host, _settings.Port);
38+
var proxyEndpoint = new DnsEndPoint(_settings.Socks5ProxySettings.Host, _settings.Socks5ProxySettings.Port);
3839
var stream = _wrapped.CreateStream(proxyEndpoint, cancellationToken);
39-
Socks5Helper.PerformSocks5Handshake(stream, endPoint, _settings.Authentication, cancellationToken);
40+
Socks5Helper.PerformSocks5Handshake(stream, endPoint, _settings.Socks5ProxySettings.Authentication, cancellationToken);
4041
return stream;
4142
}
4243

4344
public async Task<Stream> CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
4445
{
45-
var proxyEndpoint = new DnsEndPoint(_settings.Host, _settings.Port);
46+
var proxyEndpoint = new DnsEndPoint(_settings.Socks5ProxySettings.Host, _settings.Socks5ProxySettings.Port);
4647
var stream = await _wrapped.CreateStreamAsync(proxyEndpoint, cancellationToken).ConfigureAwait(false);
47-
await Socks5Helper.PerformSocks5HandshakeAsync(stream, endPoint, _settings.Authentication, cancellationToken).ConfigureAwait(false);
48+
await Socks5Helper.PerformSocks5HandshakeAsync(stream, endPoint, _settings.Socks5ProxySettings.Authentication, cancellationToken).ConfigureAwait(false);
4849
return stream;
4950
}
5051
}

0 commit comments

Comments
 (0)