コード例 #1
0
ファイル: ProxyInfo.cs プロジェクト: spzenk/sfdocsamples
    internal void GetProxyResponseStatus(byte[] response)
    {

      switch (FProxyType)
      {

        case ProxyType.ptHTTP:

          Match m = Regex.Match(Encoding.GetEncoding(1252).GetString(response), @"[HTTP/]\d[.]\d\s(?<code>\d+)\s(?<reason>.+)");

          if (m.Success)
          {

            int code = Convert.ToInt32(m.Groups["code"].Value);

            if (code >= 200 && code <= 299)
            {

              FCompleted = true;

            }
            else
            {
              throw new ProxyAuthenticationException(code, m.Groups["reason"].Value);
            }

          }
          else
          {
            throw new ProxyAuthenticationException(0, "Invalid proxy message response.");
          }

          break;

        case ProxyType.ptSOCKS4:
        case ProxyType.ptSOCKS4a:

          if (response[1] == 0x5A)
          {

            FCompleted = true;

          }
          else
          {

            switch (response[1])
            { 
            
              case 0x5B:

                throw new ProxyAuthenticationException(response[1], "Request rejected or failed.");

              case 0x5C:

                throw new ProxyAuthenticationException(response[1], "Client is not running identd.");
                 
              case 0x5D:

                throw new ProxyAuthenticationException(response[1], "Client's identd could not confirm the user ID string in the request.");

            }

          }

          break;

        case ProxyType.ptSOCKS5:

          switch (FSOCKS5Phase)
          {

            case SOCKS5Phase.spGreeting:

              if (response[1] != 0xFF)
              {

                FSOCKS5Authentication = (SOCKS5AuthMode)Enum.ToObject(typeof(SOCKS5AuthMode), response[1]);

                switch (FSOCKS5Authentication)
                {

                  case SOCKS5AuthMode.saNoAuth:

                    FSOCKS5Phase = SOCKS5Phase.spConnecting;
                    break;

                  case SOCKS5AuthMode.ssUserPass:

                    FSOCKS5Phase = SOCKS5Phase.spAuthenticating;
                    break;

                }

              }
              else
              {
                throw new ProxyAuthenticationException(0xFF, "Authentication method not supported.");
              }

              break;

            case SOCKS5Phase.spConnecting:
            case SOCKS5Phase.spAuthenticating:

              if (response[1] == 0x00)
              {

                switch (FSOCKS5Phase)
                { 

                  case SOCKS5Phase.spConnecting:

                    FCompleted = true;
                    break;

                  case SOCKS5Phase.spAuthenticating:

                    FSOCKS5Phase = SOCKS5Phase.spConnecting;
                    break;
                
                }
                
              }
              else
              {

                switch (response[1])
                {

                  case 0x01:

                    throw new ProxyAuthenticationException(response[1], "General Failure.");

                  case 0x02:

                    throw new ProxyAuthenticationException(response[1], "Connection not allowed by ruleset.");

                  case 0x03:

                    throw new ProxyAuthenticationException(response[1], "Network unreachable.");

                  case 0x04:

                    throw new ProxyAuthenticationException(response[1], "Host unreachable.");

                  case 0x05:

                    throw new ProxyAuthenticationException(response[1], "Connection refused by destination host.");

                  case 0x06:

                    throw new ProxyAuthenticationException(response[1], "TTL expired.");

                  case 0x07:

                    throw new ProxyAuthenticationException(response[1], "Command not supported / protocol error.");

                  case 0x08:

                    throw new ProxyAuthenticationException(response[1], "Address type not supported.");

                }

              }

              break;

          }

          break;

      }

    }
コード例 #2
0
        internal void GetProxyResponseStatus(byte[] response)
        {
            switch (FProxyType)
            {
            case ProxyType.ptHTTP:

                Match m = Regex.Match(Encoding.GetEncoding(1252).GetString(response), @"[HTTP/]\d[.]\d\s(?<code>\d+)\s(?<reason>.+)");

                if (m.Success)
                {
                    int code = Convert.ToInt32(m.Groups["code"].Value);

                    if (code >= 200 && code <= 299)
                    {
                        FCompleted = true;
                    }
                    else
                    {
                        throw new ProxyAuthenticationException(code, m.Groups["reason"].Value);
                    }
                }
                else
                {
                    throw new ProxyAuthenticationException(0, "Invalid proxy message response.");
                }

                break;

            case ProxyType.ptSOCKS4:
            case ProxyType.ptSOCKS4a:

                if (response[1] == 0x5A)
                {
                    FCompleted = true;
                }
                else
                {
                    switch (response[1])
                    {
                    case 0x5B:

                        throw new ProxyAuthenticationException(response[1], "Request rejected or failed.");

                    case 0x5C:

                        throw new ProxyAuthenticationException(response[1], "Client is not running identd.");

                    case 0x5D:

                        throw new ProxyAuthenticationException(response[1], "Client's identd could not confirm the user ID string in the request.");
                    }
                }

                break;

            case ProxyType.ptSOCKS5:

                switch (FSOCKS5Phase)
                {
                case SOCKS5Phase.spGreeting:

                    if (response[1] != 0xFF)
                    {
                        FSOCKS5Authentication = (SOCKS5AuthMode)Enum.ToObject(typeof(SOCKS5AuthMode), response[1]);

                        switch (FSOCKS5Authentication)
                        {
                        case SOCKS5AuthMode.saNoAuth:

                            FSOCKS5Phase = SOCKS5Phase.spConnecting;
                            break;

                        case SOCKS5AuthMode.ssUserPass:

                            FSOCKS5Phase = SOCKS5Phase.spAuthenticating;
                            break;
                        }
                    }
                    else
                    {
                        throw new ProxyAuthenticationException(0xFF, "Authentication method not supported.");
                    }

                    break;

                case SOCKS5Phase.spConnecting:
                case SOCKS5Phase.spAuthenticating:

                    if (response[1] == 0x00)
                    {
                        switch (FSOCKS5Phase)
                        {
                        case SOCKS5Phase.spConnecting:

                            FCompleted = true;
                            break;

                        case SOCKS5Phase.spAuthenticating:

                            FSOCKS5Phase = SOCKS5Phase.spConnecting;
                            break;
                        }
                    }
                    else
                    {
                        switch (response[1])
                        {
                        case 0x01:

                            throw new ProxyAuthenticationException(response[1], "General Failure.");

                        case 0x02:

                            throw new ProxyAuthenticationException(response[1], "Connection not allowed by ruleset.");

                        case 0x03:

                            throw new ProxyAuthenticationException(response[1], "Network unreachable.");

                        case 0x04:

                            throw new ProxyAuthenticationException(response[1], "Host unreachable.");

                        case 0x05:

                            throw new ProxyAuthenticationException(response[1], "Connection refused by destination host.");

                        case 0x06:

                            throw new ProxyAuthenticationException(response[1], "TTL expired.");

                        case 0x07:

                            throw new ProxyAuthenticationException(response[1], "Command not supported / protocol error.");

                        case 0x08:

                            throw new ProxyAuthenticationException(response[1], "Address type not supported.");
                        }
                    }

                    break;
                }

                break;
            }
        }