通过代理(. net)连接到IRC

本文关键字:连接 IRC net 代理 | 更新日期: 2023-09-27 18:05:25

我希望通过我的。net应用程序连接到IRC时隐藏我的IP。我目前使用IrcDotNet库,但它似乎不支持代理。

我对套接字没有太多的经验,所以我认为修改IrcDotNet会比制作自己的IRC库更容易。我四处寻找处理代理连接的套接字库,我可以在IrcDotNet中实现。我找到了一个叫做ProxySocket,但它只支持BeginConnect,而不是IrcDotNet使用的新的ASyncConnect方法。

为了分解它,按照偏好顺序,这是我要找的;

  1. 支持通过HTTP/SOCKS代理连接的IRC库
  2. 一个套接字库,支持通过HTTP/SOCKS代理进行连接ASyncConnect 关于如何扩展套接字类以支持连接的示例代码通过HTTP/SOCKS代理通过ASyncConnect

我使用的IrcDotNet版本是0.4.1在https://launchpad.net/ircdotnet找到。

更新1:恐怕还是没有运气。Fredrik92的回答,虽然有帮助,但不适用于我正在使用的IrcDotNet版本(见上文)。

通过代理(. net)连接到IRC

IRC。. NET库使用System.Net.Sockets命名空间中的标准Socket类。

所以你可以在IRC中修改IrcDotNet/irclient .cs文件。. NET源代码(@ http://ircdotnet.codeplex.com/SourceControl/latest)。

您应该为启用代理的IRC客户机添加一个构造函数,并调用默认构造函数。

那么您所需要做的就是修改同一文件中的Connect方法(几乎在底部)。每次它们调用this.client.BeginConnect(..)时,您都必须添加连接到代理(而不是远程主机)的代码

现在您只需要创建一个新的连接回调方法,该方法向代理发送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...