我希望在通过我的.NET应用程序连接到IRC时隐藏我的IP。我目前使用的是IrcDotNet库,但它似乎不支持代理。
我对插槽没有多少经验,所以我认为修改IrcDotNet比制作我自己的IRC库更容易。我四处寻找处理代理连接的套接字库,我可以在IrcDotNet中实现代理连接。我发现了一个名为ProxySocket但它只支持BeginConnect而不是IrcDotNet使用的新ASyncConnect方法。
按照优先顺序分解,这就是我要找的东西;
我使用的IrcDotNet版本在https://launchpad.net/ircdotnet找到0.4.1。
更新1:我还是没有运气,我很害怕。 Fredrik92的回答虽然有帮助,但不适用于我正在使用的IrcDotNet版本(见上文)。
答案 0 :(得分:0)
IRC.NET库使用System.Net.Sockets命名空间中的标准Socket类。
所以你可以修改IRC.NET源代码中的 IrcDotNet / IrcClient.cs 文件(@ http://ircdotnet.codeplex.com/SourceControl/latest)。
您应该为启用代理的IRC客户端添加构造函数并调用默认构造函数。
然后您需要做的就是修改同一文件中的Connect方法(几乎在底部)。每次调用this.client.BeginConnect(..)
时,您都必须添加用于连接代理(而不是远程主机)的代码
现在,您只需创建一个新的Connect-callback方法,该方法将HTTP CONNECT请求发送到代理。从HTTP代理中读取响应,然后其他所有内容都应该有效。
在这种情况下,我会将HTTP请求作为原始ASCII字节写入代理(而不是使用HttpWebRequest类),这样您就可以完全控制您获得的网络流...
你应该添加某事。像这样的IrcClient类:
private bool useProxy = false;
private IWebProxy proxy;
private IEnumerable<Uri> proxyRemoteUris;
public IrcClient(IWebProxy proxy)
: this()
{
this.useProxy = true;
this.proxy = proxy;
}
private void ProxyPerformHttpConnect(Uri remoteIrcUri)
{
string httpConnectRequest = string.Format("CONNECT {0}:{1} HTTP/1.1\r\nHost: {2}\r\n\r\n",
remoteIrcUri.Host, remoteIrcUri.Port, this.proxy.GetProxy(remoteIrcUri));
byte[] httpConnectData = Encoding.ASCII.GetBytes(httpConnectRequest);
this.stream.Write(httpConnectData, 0, httpConnectData.Length);
bool responseReady = false;
string responseText = string.Empty;
// Byte-by-byte reading required, because StringReader will read more than just the HTTP response header
do
{
int readByte = this.stream.ReadByte();
if (readByte < 0)
throw new WebException(message: null, status: WebExceptionStatus.ConnectionClosed);
char readChar = (char)(readByte); // Only works because HTTP Headers are ASCII encoded.
responseText += readChar;
responseReady = responseText.EndsWith("\r\n\r\n");
} while (!responseReady);
int statusStart = responseText.IndexOf(' ') + 1;
int reasonStart = responseText.IndexOf(' ', statusStart) + 1;
int reasonEnd = responseText.IndexOfAny(new char[] { '\r', '\n'});
HttpStatusCode responseStatus = (HttpStatusCode)(int.Parse(responseText.Substring(responseText.IndexOf(' ') + 1, length: 3)));
if (responseStatus != HttpStatusCode.OK)
{
string reasonText = responseText.Substring(reasonStart, reasonEnd - reasonStart);
if (string.IsNullOrWhiteSpace(reasonText))
reasonText = null;
throw new WebException(reasonText, WebExceptionStatus.ConnectFailure);
}
// Finished Response Header read...
}
private void ProxyConnectCallback(IAsyncResult ar)
{
try
{
this.client.EndConnect(ar);
this.stream = this.client.GetStream();
bool proxyTunnelEstablished = false;
WebException lastWebException = null;
foreach (Uri remoteIrcUri in this.proxyRemoteUris)
{
if (this.client.Connected == false)
{
// Re-establish connection with proxy...
Uri proxyUri = this.proxy.GetProxy(remoteIrcUri);
this.client.Connect(proxyUri.Host, proxyUri.Port);
}
try
{
ProxyPerformHttpConnect(remoteIrcUri);
proxyTunnelEstablished = true;
break;
}
catch (WebException webExcept)
{
lastWebException = webExcept;
}
}
if (!proxyTunnelEstablished)
{
OnConnectFailed(new IrcErrorEventArgs(lastWebException));
return;
}
this.writer = new StreamWriter(this.stream, Encoding.Default);
this.reader = new StreamReader(this.stream, Encoding.Default);
HandleClientConnected((IrcRegistrationInfo)ar.AsyncState);
this.readThread.Start();
this.writeThread.Start();
OnConnected(new EventArgs());
}
catch (Exception ex)
{
OnConnectFailed(new IrcErrorEventArgs(ex));
}
}
IrcClient
类的所有Connect方法中代理处理的代码因此看起来很苛刻。像这样:
// Code snippet to insert before the call to this.client.BeginConnect(...)
if (this.useProxy)
{
// Assign host and port variables for EndPoint objects:
// var host = remoteEP.Address;
// var port = remoteEP.Port;
this.proxyRemoteUris = new Uri[] { new Uri(string.Format("irc://{0}:{1}/", host, port)) };
// Replace the line above with the following line in the method where an array of IP addresses is specified as a parameter
// this.proxyRemoteUris = from ip in addresses select new Uri(string.Format("irc://{0}:{1}/", ip, port));
Uri proxyUri = this.proxy.GetProxy(this.proxyRemoteUris.First());
string proxyHost = proxyUri.Host;
int proxyPort = proxyUri.Port;
this.client.BeginConnect(proxyHost, proxyPort, ProxyConnectCallback, registrationInfo);
}
else
// Original this.client.BeginConnect(...) call here...