Commit 987dafc9 authored by Honfika's avatar Honfika

socks 5 username/password authentication

parent 0614f4f9
...@@ -140,6 +140,11 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -140,6 +140,11 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
public bool IsTransparent => ProxyEndPoint is TransparentProxyEndPoint; public bool IsTransparent => ProxyEndPoint is TransparentProxyEndPoint;
/// <summary>
/// Is this a SOCKS endpoint?
/// </summary>
public bool IsSocks => ProxyEndPoint is SocksProxyEndPoint;
/// <summary> /// <summary>
/// The last exception that happened. /// The last exception that happened.
/// </summary> /// </summary>
......
...@@ -90,7 +90,7 @@ namespace Titanium.Web.Proxy ...@@ -90,7 +90,7 @@ namespace Titanium.Web.Proxy
request.Method = requestLine.Method; request.Method = requestLine.Method;
request.HttpVersion = requestLine.Version; request.HttpVersion = requestLine.Version;
if (!args.IsTransparent) if (!args.IsTransparent && !args.IsSocks)
{ {
// proxy authorization check // proxy authorization check
if (connectRequest == null && await checkAuthorization(args) == false) if (connectRequest == null && await checkAuthorization(args) == false)
......
...@@ -99,7 +99,7 @@ namespace Titanium.Web.Proxy ...@@ -99,7 +99,7 @@ namespace Titanium.Web.Proxy
response.Locked = true; response.Locked = true;
if (!args.IsTransparent) if (!args.IsTransparent && !args.IsSocks)
{ {
response.Headers.FixProxyHeaders(); response.Headers.FixProxyHeaders();
} }
......
using System; using System;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions; using Titanium.Web.Proxy.Extensions;
...@@ -48,13 +49,76 @@ namespace Titanium.Web.Proxy ...@@ -48,13 +49,76 @@ namespace Titanium.Web.Proxy
} }
else if (buffer[0] == 5) else if (buffer[0] == 5)
{ {
if (buffer[1] == 0 || buffer[2] != 0) int authenticationMethodCount = buffer[1];
if (read < authenticationMethodCount + 2)
{ {
return; return;
} }
buffer[1] = 0; int acceptedMethod = 255;
for (int i = 0; i < authenticationMethodCount; i++)
{
int method = buffer[i + 2];
if (method == 0 && ProxyBasicAuthenticateFunc == null)
{
acceptedMethod = 0;
break;
}
if (method == 2)
{
acceptedMethod = 2;
break;
}
}
buffer[1] = (byte)acceptedMethod;
await stream.WriteAsync(buffer, 0, 2, cancellationToken); await stream.WriteAsync(buffer, 0, 2, cancellationToken);
if (acceptedMethod == 255)
{
// no acceptable method
return;
}
if (acceptedMethod == 2)
{
read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
if (read < 3 || buffer[0] != 1)
{
// authentication version should be 1
return;
}
int userNameLength = buffer[1];
if (read < 3 + userNameLength)
{
return;
}
string userName = Encoding.ASCII.GetString(buffer, 2, userNameLength);
int passwordLength = buffer[2 + userNameLength];
if (read < 3 + userNameLength + passwordLength)
{
return;
}
string password = Encoding.ASCII.GetString(buffer, 3 + userNameLength, passwordLength);
bool success = true;
if (ProxySchemeAuthenticateFunc != null)
{
success = await ProxyBasicAuthenticateFunc.Invoke(null, userName, password);
}
buffer[1] = success ? (byte)0 : (byte)1;
await stream.WriteAsync(buffer, 0, 2, cancellationToken);
if (!success)
{
return;
}
}
read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); read = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
if (read < 10 || buffer[1] != 1) if (read < 10 || buffer[1] != 1)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment