了解dotPeek反编译代码中生成的编译器类型

18

嗨。我正在阅读Digi Traffic Accelerator的反编译源代码(我认为这是学习的最佳方式),直到我遇到了一些无法理解的代码!请看:

  internal class ProxyFarm
  {
    private static Random rand = new Random();
    private static Regex UserPassAtHostPort = new Regex("\r\n                ^\r\n                (?<user>[^:]+?) : (?<pass>[^@]+?)\r\n                @\r\n                (?<host>[^:]+? (?: : \\d+)? )\r\n                $", RegexOptions.IgnorePatternWhitespace);
    private static Regex HostPortUserPass = new Regex("\r\n                ^\r\n                (?<host>[^:]+? : \\d+) : (?<user>[^:]+?) : (?<pass>.+?)\r\n                $", RegexOptions.IgnorePatternWhitespace);
    public const string NEW = "new";
    public const string ACTIVE = "active";
    public const string BLOCKED_BY_GOOGLE = "blocked by Google";
    public const string INACTIVE = "inactive";
    public const string CHECKING = "Checking...";

    static ProxyFarm()
    {
    }

    public static StorageDataSet.ProxiesRow GetRandomProxy(bool WillUseRecaptcha = true)
    {
      // some normal code
    }

    public static WebProxy MakeWebProxy(StorageDataSet.ProxiesRow row)
    {
      // some normal code
    }

    public static void CheckProxy(StorageDataSet.ProxiesRow proxy)
    {
      // ISSUE: object of a compiler-generated type is created
      // ISSUE: variable of a compiler-generated type
      ProxyFarm.\u003C\u003Ec__DisplayClass5 cDisplayClass5 = new ProxyFarm.\u003C\u003Ec__DisplayClass5();
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.proxy = proxy;
      // ISSUE: reference to a compiler-generated field
      if (cDisplayClass5.proxy == null)
        return;
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.logger = LogManager.GetLogger("Program");
      // ISSUE: reference to a compiler-generated field
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.state = new ProxyFarm.CallbackState()
      {
        Proxy = cDisplayClass5.proxy,
        Google = ProxyFarm.CheckStatus.NotChecked,
        Post = ProxyFarm.CheckStatus.NotChecked
      };
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.r = (HttpWebRequest) WebRequest.Create("http://www.digitrafficgenerator.com/post_test.php");
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.r.Method = "POST";
      try
      {
        // ISSUE: reference to a compiler-generated field
        // ISSUE: reference to a compiler-generated field
        cDisplayClass5.r.Proxy = (IWebProxy) ProxyFarm.MakeWebProxy(cDisplayClass5.proxy);
      }
      catch (Exception ex)
      {
        // ISSUE: reference to a compiler-generated field
        // ISSUE: reference to a compiler-generated field
        cDisplayClass5.logger.Warn((object) ("Invalid proxy entered: " + cDisplayClass5.proxy.Address), ex);
        // ISSUE: reference to a compiler-generated field
        ProxyFarm.ChangeStatus(cDisplayClass5.proxy, "inactive");
        return;
      }
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.r.ConnectionGroupName = Guid.NewGuid().ToString();
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.byteArray = Encoding.UTF8.GetBytes("q=test");
      // ISSUE: reference to a compiler-generated field
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.r.ContentLength = (long) cDisplayClass5.byteArray.Length;
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.r.ContentType = "application/x-www-form-urlencoded";
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.r.Referer = "http://www.digitrafficgenerator.com/post_test.php";
      // ISSUE: reference to a compiler-generated field
      cDisplayClass5.r.Timeout = 20000;
      // ISSUE: reference to a compiler-generated field
      // ISSUE: reference to a compiler-generated method
      IAsyncResult res = cDisplayClass5.r.BeginGetRequestStream(new AsyncCallback(cDisplayClass5.\u003CCheckProxy\u003Eb__1), (object) null);
      // ISSUE: reference to a compiler-generated field
      WebObject.AbortRequestAfterTimeout(res, cDisplayClass5.r);
      try
      {
        // ISSUE: reference to a compiler-generated field
        // ISSUE: reference to a compiler-generated method
        // ISSUE: reference to a compiler-generated field
        res = cDisplayClass5.r.BeginGetResponse(new AsyncCallback(cDisplayClass5.\u003CCheckProxy\u003Eb__2), (object) cDisplayClass5.r);
      }
      catch (Exception ex)
      {
        // ISSUE: reference to a compiler-generated field
        // ISSUE: reference to a compiler-generated field
        cDisplayClass5.logger.Info((object) ("Got an exception using " + cDisplayClass5.proxy.Address), ex);
      }
      // ISSUE: reference to a compiler-generated field
      WebObject.AbortRequestAfterTimeout(res, cDisplayClass5.r);
      HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://www.google.com/recaptcha/api/js/recaptcha.js");
      request.Method = "GET";
      // ISSUE: reference to a compiler-generated field
      request.Proxy = (IWebProxy) ProxyFarm.MakeWebProxy(cDisplayClass5.proxy);
      request.Timeout = 20000;
      request.ConnectionGroupName = Guid.NewGuid().ToString();
      // ISSUE: reference to a compiler-generated method
      WebObject.AbortRequestAfterTimeout(request.BeginGetResponse(new AsyncCallback(cDisplayClass5.\u003CCheckProxy\u003Eb__3), (object) request), request);
    }

    protected static void ProxyChecked(IAsyncResult result, HttpWebRequest req, ProxyFarm.CallbackState state)
    {
      ILog logger = LogManager.GetLogger("Program");
      ProxyFarm.CheckStatus checkStatus = ProxyFarm.CheckStatus.NotOk;
      try
      {
        HttpWebResponse httpWebResponse = (HttpWebResponse) req.EndGetResponse(result);
        if (req.RequestUri == httpWebResponse.ResponseUri)
        {
          if (httpWebResponse.StatusCode == HttpStatusCode.OK)
            checkStatus = ProxyFarm.CheckStatus.Ok;
        }
      }
      catch (Exception ex)
      {
      }
      lock (state)
      {
        if (req.RequestUri.Authority.Contains("google.com"))
          state.Google = checkStatus;
        else
          state.Post = checkStatus;
        if (state.Post == ProxyFarm.CheckStatus.NotOk)
        {
          ProxyFarm.ChangeStatus(state.Proxy, "inactive");
          logger.Info((object) ("Marking " + state.Proxy.Address + " as inactive as it cannot POST"));
        }
        else if (state.Google == ProxyFarm.CheckStatus.NotOk)
        {
          ProxyFarm.ChangeStatus(state.Proxy, "inactive");
          logger.Info((object) ("Marking " + state.Proxy.Address + " as inactive as it is blocked by google"));
        }
        else
        {
          if (state.Google != ProxyFarm.CheckStatus.Ok || state.Post != ProxyFarm.CheckStatus.Ok)
            return;
          ProxyFarm.ChangeStatus(state.Proxy, "active");
        }
      }
    }

    public static void ReportBrokenProxy(StorageDataSet.ProxiesRow proxy, bool recaptcha_fail = false)
    {
      // some normal code
    }

    private static void ChangeStatus(StorageDataSet.ProxiesRow Proxy, string status)
    {
      // some normal code
    }

    public enum CheckStatus
    {
      NotChecked,
      Ok,
      NotOk,
    }

    protected class CallbackState
    {
      public StorageDataSet.ProxiesRow Proxy { get; set; }

      public ProxyFarm.CheckStatus Post { get; set; }

      public ProxyFarm.CheckStatus Google { get; set; }
    }
  }

你看,似乎在 CheckProxy 方法中,编译器生成了一个新类型!你有任何想法吗?我该如何理解这段代码?


这是反编译器的缺陷,请报告它。 - usr
这段内容来自五年前,我认为现在的dotPeek处理这些字符的能力更好了。但它仍然会做一些奇怪的事情。如果在标识符中有一个美元符号($)(在C#中无效但在IL中有效),那么在某些情况下,dotPeek会正确地显示$,并且在查找标识符时也可以正常工作。在其他情况下,它会显示$字符,但在幕后它已将其转换为"\u0024"。如果您将其复制并粘贴到文本编辑器中,这一点就会变得明显,并且这也意味着在尝试查找该标识符时,这些行将不会被找到。 - RenniePet
2个回答

20

\u003C\u003E分别是<>的Unicode表示。

这些字符在IL中是有效的...所以编译器会生成类似于那样的变量名称以避免冲突。

您的反编译程序只是不知道如何将其转换为<>并且没有删除临时变量。


谢谢回答。我懂了。那么,如果您想重写CheckProxy方法,您会如何做?我的意思是,为什么编译器会生成<>__DisplayClass5类?哪些条件使编译器执行这些操作? - amiry jd
1
编译器会为闭包、匿名类型或在try..finally子句中使用对象等情况处理这些内容。我不太熟悉确切的语义,但这并不是一个问题。 - Simon Whitehead

10

dnSpy中打开它,并查找与之相匹配的方法(或类似的方法)。它们的顺序可能不同,但dnSpy能够很好地反编译lambda函数。


谢谢,与dotpeek相比,我从这里得到了更好的结果。 - Liam
6
我按照这个答案去下载了dnSpy,但是在尝试构建和调试它时(不要尝试发布它!)费了很多周折,最终的结果并没有比dotPeek更清晰。 - Matt Kemp
谢谢您,dnSpy的结果更加准确。 - Jean-Daniel Gasser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接