TLSharp
TLSharp copied to clipboard
http proxy
trying to connect and work via proxy. my code looks like this ` private void Connect(string address, int port, bool useProxy) { if (!useProxy) { _tcpClient = new TcpClient(); var ipAddress = IPAddress.Parse(address); _tcpClient.Connect(ipAddress, port); } else { //TODO port is always 80 if we are behind proxy _tcpClient = connectViaHTTPProxy(address, 80); } }
static TcpClient connectViaHTTPProxy(
string targetHost,
int targetPort)
{
var telegramurl = "http://" + targetHost + ":" + targetPort;
var proxyUri = WebRequest.DefaultWebProxy.GetProxy(new Uri(telegramurl));
WebResponse response = null;
var retry = 10;
while(retry >= 0)
{
var request = (HttpWebRequest)WebRequest.Create(telegramurl);
request.KeepAlive = true;
var webProxy = new WebProxy(proxyUri);
request.Proxy = webProxy;
request.Method = "CONNECT";
request.Timeout = 3000;
retry--;
try
{
response = request.GetResponse();
break;
}
catch (Exception ex)
{
if (retry >=0 && ex.Message.Equals("The operation has timed out", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine($"10/{retry} Timout happened {ex}");
}
else
{
throw new Exception("Something went wrong", ex);
}
}
}
var responseStream = response.GetResponseStream();
Debug.Assert(responseStream != null);
const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
var rsType = responseStream.GetType();
var connectionProperty = rsType.GetProperty("Connection", Flags);
var connection = connectionProperty.GetValue(responseStream, null);
var connectionType = connection.GetType();
var networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);
var networkStream = networkStreamProperty.GetValue(connection, null);
var nsType = networkStream.GetType();
var socketProperty = nsType.GetProperty("Socket", Flags);
var socket = (Socket)socketProperty.GetValue(networkStream, null);
return new TcpClient { Client = socket };
}`
for some reason timeouts are happening while connecting - not always but from time to time.
I think using the StarkSoft is useful. take a look at #824 issue.
@mostafa8026
I have tried StarkSoft with Socks5ProxyClient but not working so i want to try HttpProxyClient but HttpProxyClient not support http proxy with username and password. Is StarkSoft working with you?
@vuminhit I don't use StarkSoft anymore and this repository. you can use the main API with tdsharp. then you can use the core function addProxy.