2020-04-09 10:38:55 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Buffers;
|
|
|
|
|
using System.IO.Pipelines;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
|
using BTCPayServer.Lightning.Eclair.Models;
|
|
|
|
|
using BTCPayServer.Logging;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NBitcoin;
|
|
|
|
|
using NBitcoin.Socks;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.HostedServices
|
|
|
|
|
{
|
2020-04-09 11:56:30 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is a very simple Socks HTTP proxy, that can be used through HttpClient.WebProxy
|
|
|
|
|
/// However, it only supports a single request/response, so the client must specify Connection: close to not
|
|
|
|
|
/// reuse the TCP connection to the proxy for another requests.
|
|
|
|
|
/// Inspired from https://devblogs.microsoft.com/dotnet/system-io-pipelines-high-performance-io-in-net/
|
|
|
|
|
/// </summary>
|
2020-04-09 10:38:55 +02:00
|
|
|
|
public class Socks5HttpProxyServer : IHostedService
|
|
|
|
|
{
|
|
|
|
|
class ProxyConnection
|
|
|
|
|
{
|
2020-04-09 13:25:17 +02:00
|
|
|
|
public ServerContext ServerContext;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
public Socket ClientSocket;
|
|
|
|
|
public Socket SocksSocket;
|
|
|
|
|
public CancellationToken CancellationToken;
|
2020-04-09 11:49:30 +02:00
|
|
|
|
public CancellationTokenSource CancellationTokenSource;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Socks5HttpProxyServer.Dispose(ClientSocket);
|
|
|
|
|
Socks5HttpProxyServer.Dispose(SocksSocket);
|
2020-04-09 11:49:30 +02:00
|
|
|
|
CancellationTokenSource.Dispose();
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
|
|
|
|
|
class ServerContext
|
|
|
|
|
{
|
|
|
|
|
public EndPoint SocksEndpoint;
|
|
|
|
|
public Socket ServerSocket;
|
|
|
|
|
public CancellationToken CancellationToken;
|
|
|
|
|
public int ConnectionCount;
|
|
|
|
|
}
|
2020-04-09 10:38:55 +02:00
|
|
|
|
private readonly BTCPayServerOptions _opts;
|
|
|
|
|
|
|
|
|
|
public Socks5HttpProxyServer(Configuration.BTCPayServerOptions opts)
|
|
|
|
|
{
|
|
|
|
|
_opts = opts;
|
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
private ServerContext _ServerContext;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
private CancellationTokenSource _Cts;
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-04-09 13:25:17 +02:00
|
|
|
|
if (_opts.SocksEndpoint is null || _ServerContext != null)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
_Cts = new CancellationTokenSource();
|
2020-04-09 13:25:17 +02:00
|
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
|
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
|
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
|
|
|
|
Port = ((IPEndPoint)(socket.LocalEndPoint)).Port;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
Uri = new Uri($"http://127.0.0.1:{Port}");
|
2020-04-09 13:25:17 +02:00
|
|
|
|
socket.Listen(5);
|
|
|
|
|
_ServerContext = new ServerContext()
|
|
|
|
|
{
|
|
|
|
|
SocksEndpoint = _opts.SocksEndpoint,
|
|
|
|
|
ServerSocket = socket,
|
|
|
|
|
CancellationToken = _Cts.Token,
|
|
|
|
|
ConnectionCount = 0
|
|
|
|
|
};
|
|
|
|
|
socket.BeginAccept(Accept, _ServerContext);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
Logs.PayServer.LogInformation($"Internal Socks HTTP Proxy listening at {Uri}");
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Port { get; private set; }
|
|
|
|
|
public Uri Uri { get; private set; }
|
|
|
|
|
|
2020-04-09 13:25:17 +02:00
|
|
|
|
static void Accept(IAsyncResult ar)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
2020-04-09 13:25:17 +02:00
|
|
|
|
var ctx = (ServerContext)ar.AsyncState;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
Socket clientSocket = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-04-09 13:25:17 +02:00
|
|
|
|
clientSocket = ctx.ServerSocket.EndAccept(ar);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
catch (ObjectDisposedException)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
if (ctx.CancellationToken.IsCancellationRequested)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
|
|
|
|
Dispose(clientSocket);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var toSocksProxy = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
2020-04-09 13:25:17 +02:00
|
|
|
|
var connectionCts = CancellationTokenSource.CreateLinkedTokenSource(ctx.CancellationToken);
|
|
|
|
|
toSocksProxy.BeginConnect(ctx.SocksEndpoint, ConnectToSocks, new ProxyConnection()
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
2020-04-09 13:25:17 +02:00
|
|
|
|
ServerContext = ctx,
|
2020-04-09 10:38:55 +02:00
|
|
|
|
ClientSocket = clientSocket,
|
|
|
|
|
SocksSocket = toSocksProxy,
|
2020-04-09 11:49:30 +02:00
|
|
|
|
CancellationToken = connectionCts.Token,
|
|
|
|
|
CancellationTokenSource = connectionCts
|
2020-04-09 10:38:55 +02:00
|
|
|
|
});
|
2020-04-09 13:25:17 +02:00
|
|
|
|
ctx.ServerSocket.BeginAccept(Accept, ctx);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 13:25:17 +02:00
|
|
|
|
static void ConnectToSocks(IAsyncResult ar)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
|
|
|
|
var connection = (ProxyConnection)ar.AsyncState;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection.SocksSocket.EndConnect(ar);
|
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
catch (Exception)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
|
|
|
|
connection.Dispose();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
Interlocked.Increment(ref connection.ServerContext.ConnectionCount);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
var pipe = new Pipe(PipeOptions.Default);
|
2020-04-09 11:49:30 +02:00
|
|
|
|
var reading = FillPipeAsync(connection.ClientSocket, pipe.Writer, connection.CancellationToken)
|
|
|
|
|
.ContinueWith(_ => connection.CancellationTokenSource.Cancel(), TaskScheduler.Default);
|
|
|
|
|
var writing = ReadPipeAsync(connection.SocksSocket, connection.ClientSocket, pipe.Reader, connection.CancellationToken)
|
|
|
|
|
.ContinueWith(_ => connection.CancellationTokenSource.Cancel(), TaskScheduler.Default);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
_ = Task.WhenAll(reading, writing)
|
|
|
|
|
.ContinueWith(_ =>
|
|
|
|
|
{
|
|
|
|
|
connection.Dispose();
|
2020-04-09 13:25:17 +02:00
|
|
|
|
Interlocked.Decrement(ref connection.ServerContext.ConnectionCount);
|
2020-04-09 11:49:30 +02:00
|
|
|
|
}, TaskScheduler.Default);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
|
|
|
|
|
public int ConnectionCount => _ServerContext is ServerContext s ? s.ConnectionCount : 0;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
private static async Task ReadPipeAsync(Socket socksSocket, Socket clientSocket, PipeReader reader, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
bool handshaked = false;
|
|
|
|
|
bool isConnect = false;
|
|
|
|
|
string firstHeader = null;
|
|
|
|
|
string httpVersion = null;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
ReadResult result = await reader.ReadAsync(cancellationToken);
|
|
|
|
|
ReadOnlySequence<byte> buffer = result.Buffer;
|
|
|
|
|
SequencePosition? position = null;
|
|
|
|
|
|
|
|
|
|
if (!handshaked)
|
|
|
|
|
{
|
|
|
|
|
nextchunk:
|
|
|
|
|
// Look for a EOL in the buffer
|
|
|
|
|
position = buffer.PositionOf((byte)'\n');
|
|
|
|
|
if (position == null)
|
|
|
|
|
goto readnext;
|
|
|
|
|
// Process the line
|
|
|
|
|
var line = GetHeaderLine(buffer.Slice(0, position.Value));
|
|
|
|
|
// Skip the line + the \n character (basically position)
|
|
|
|
|
buffer = buffer.Slice(buffer.GetPosition(1, position.Value));
|
|
|
|
|
if (firstHeader is null)
|
|
|
|
|
{
|
|
|
|
|
firstHeader = line;
|
|
|
|
|
isConnect = line.StartsWith("CONNECT ", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
if (isConnect)
|
|
|
|
|
goto nextchunk;
|
|
|
|
|
else
|
|
|
|
|
goto handshake;
|
|
|
|
|
}
|
|
|
|
|
else if (line.Length == 1 && line[0] == '\r')
|
|
|
|
|
goto handshake;
|
|
|
|
|
else
|
|
|
|
|
goto nextchunk;
|
|
|
|
|
|
|
|
|
|
handshake:
|
|
|
|
|
var split = firstHeader.Split(' ');
|
|
|
|
|
if (split.Length != 3)
|
|
|
|
|
break;
|
|
|
|
|
var targetConnection = split[1].Trim();
|
|
|
|
|
EndPoint destinationEnpoint = null;
|
|
|
|
|
if (isConnect)
|
|
|
|
|
{
|
|
|
|
|
if (!Utils.TryParseEndpoint(targetConnection,
|
|
|
|
|
80,
|
|
|
|
|
out destinationEnpoint))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!System.Uri.TryCreate(targetConnection, UriKind.Absolute, out var uri) ||
|
|
|
|
|
(uri.Scheme != "http" && uri.Scheme != "https"))
|
|
|
|
|
break;
|
|
|
|
|
if (!Utils.TryParseEndpoint($"{uri.DnsSafeHost}:{uri.Port}",
|
|
|
|
|
uri.Scheme == "http" ? 80 : 443,
|
|
|
|
|
out destinationEnpoint))
|
|
|
|
|
break;
|
|
|
|
|
firstHeader = $"{split[0]} {uri.PathAndQuery} {split[2].TrimEnd()}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpVersion = split[2].Trim();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await NBitcoin.Socks.SocksHelper.Handshake(socksSocket, destinationEnpoint, cancellationToken);
|
|
|
|
|
handshaked = true;
|
|
|
|
|
if (isConnect)
|
|
|
|
|
{
|
|
|
|
|
await SendAsync(clientSocket,
|
|
|
|
|
$"{httpVersion} 200 Connection established\r\nConnection: close\r\n\r\n",
|
|
|
|
|
cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await SendAsync(socksSocket, $"{firstHeader}\r\n", cancellationToken);
|
|
|
|
|
foreach (ReadOnlyMemory<byte> segment in buffer)
|
|
|
|
|
{
|
|
|
|
|
await socksSocket.SendAsync(segment, SocketFlags.None, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
buffer = buffer.Slice(buffer.End);
|
|
|
|
|
}
|
|
|
|
|
_ = Relay(socksSocket, clientSocket, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (SocksException e) when (e.SocksErrorCode == SocksErrorCode.HostUnreachable || e.SocksErrorCode == SocksErrorCode.HostUnreachable)
|
|
|
|
|
{
|
2020-04-09 11:49:30 +02:00
|
|
|
|
await SendAsync(clientSocket , $"{httpVersion} 502 Bad Gateway\r\nContent-Length: 0\r\n\r\n", cancellationToken);
|
|
|
|
|
goto done;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
|
|
|
|
catch (SocksException e)
|
|
|
|
|
{
|
2020-04-09 11:49:30 +02:00
|
|
|
|
await SendAsync(clientSocket , $"{httpVersion} 500 Internal Server Error\r\nContent-Length: 0\r\nX-Proxy-Error-Type: Socks {e.SocksErrorCode}\r\n\r\n", cancellationToken);
|
|
|
|
|
goto done;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
|
|
|
|
catch (SocketException e)
|
|
|
|
|
{
|
2020-04-09 11:49:30 +02:00
|
|
|
|
await SendAsync(clientSocket , $"{httpVersion} 500 Internal Server Error\r\nContent-Length: 0\r\nX-Proxy-Error-Type: Socket {e.SocketErrorCode}\r\n\r\n", cancellationToken);
|
|
|
|
|
goto done;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
await SendAsync(clientSocket , $"{httpVersion} 500 Internal Server Error\r\n\r\n", cancellationToken);
|
2020-04-09 11:49:30 +02:00
|
|
|
|
goto done;
|
2020-04-09 10:38:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (ReadOnlyMemory<byte> segment in buffer)
|
|
|
|
|
{
|
|
|
|
|
await socksSocket.SendAsync(segment, SocketFlags.None, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
buffer = buffer.Slice(buffer.End);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readnext:
|
|
|
|
|
// Tell the PipeReader how much of the buffer we have consumed
|
|
|
|
|
reader.AdvanceTo(buffer.Start, buffer.End);
|
|
|
|
|
// Stop reading if there's no more data coming
|
|
|
|
|
if (result.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 11:49:30 +02:00
|
|
|
|
done:
|
2020-04-09 10:38:55 +02:00
|
|
|
|
// Mark the PipeReader as complete
|
|
|
|
|
reader.Complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const int BufferSize = 1024 * 5;
|
|
|
|
|
private static async Task Relay(Socket from, Socket to, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
using var buffer = MemoryPool<byte>.Shared.Rent(BufferSize);
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
int bytesRead = await from.ReceiveAsync(buffer.Memory, SocketFlags.None, cancellationToken);
|
|
|
|
|
if (bytesRead == 0)
|
|
|
|
|
break;
|
|
|
|
|
await to.SendAsync(buffer.Memory.Slice(0, bytesRead), SocketFlags.None, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task SendAsync(Socket clientSocket, string data, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[Encoding.ASCII.GetByteCount(data)];
|
|
|
|
|
Encoding.ASCII.GetBytes(data, bytes);
|
|
|
|
|
await clientSocket.SendAsync(bytes, SocketFlags.None, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetHeaderLine(ReadOnlySequence<byte> buffer)
|
|
|
|
|
{
|
|
|
|
|
if (buffer.IsSingleSegment)
|
|
|
|
|
{
|
|
|
|
|
return Encoding.ASCII.GetString(buffer.First.Span);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Create((int)buffer.Length, buffer, (span, sequence) =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var segment in sequence)
|
|
|
|
|
{
|
|
|
|
|
Encoding.ASCII.GetChars(segment.Span, span);
|
|
|
|
|
|
|
|
|
|
span = span.Slice(segment.Length);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task FillPipeAsync(Socket socket, PipeWriter writer, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
Memory<byte> memory = writer.GetMemory(BufferSize);
|
2020-04-09 13:25:17 +02:00
|
|
|
|
int bytesRead = await socket.ReceiveAsync(memory, SocketFlags.None, cancellationToken);
|
|
|
|
|
if (bytesRead == 0)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-04-09 13:25:17 +02:00
|
|
|
|
writer.Advance(bytesRead);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
FlushResult result = await writer.FlushAsync(cancellationToken);
|
|
|
|
|
if (result.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writer.Complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-04-09 13:25:17 +02:00
|
|
|
|
if (_ServerContext is ServerContext ctx)
|
2020-04-09 10:38:55 +02:00
|
|
|
|
{
|
|
|
|
|
_Cts.Cancel();
|
2020-04-09 13:25:17 +02:00
|
|
|
|
Dispose(ctx.ServerSocket);
|
2020-04-09 10:38:55 +02:00
|
|
|
|
Logs.PayServer.LogInformation($"Internal Socks HTTP Proxy closed");
|
|
|
|
|
}
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void Dispose(Socket s)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
s.Shutdown(SocketShutdown.Both);
|
|
|
|
|
s.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
s.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|