如何通过Perl中的SOCKS5代理连接到imap?

时间:2014-12-03 16:02:06

标签: php perl sockets imap

我需要通过我的SOCKS5代理连接到imap服务器。以下是在PHP上连接到pop服务器的示例:

首先我们绑定socks服务器

private function bind_socks()
  {
    // check dependency
    if(function_exists("stream_socket_client"))
    {
      $this->stream = stream_socket_client('tcp://'.$this->socks_server.':'.$this->socks_port, $errno, $errstr, $this->timeout);
      if($this->stream !== false)
      {
        stream_set_timeout($this->stream, $this->timeout, 0); 

        stream_set_blocking($this->stream, 1);
        stream_set_write_buffer($this->stream, 0);

        if(isset($this->socks_auth['username']) && isset($this->socks_auth['password']))
        {
          $method = 0x02;
        }
        else
        {
          $method = 0x00;
        }

        if(fwrite($this->stream, pack("C3", 0x05, 0x01, $method)) !== false)
        {
          $buffer = fread($this->stream, 1024);
          $response = unpack("Cversion/Cmethod", $buffer);

          if(isset($response['version']) && isset($response['method']) && $response['version'] == 0x05 && $response['method'] == $method)
          {           
            switch($method)
            {
              case 0x02:
                return $this->auth_userpass();
                break;

              default:
                return true;
                break;
            }
          }
        }

        $this->close(1);
        return false;
      }
      else
      {
        trigger_error(socket_strerror($errstr), E_USER_WARNING);
        return false;
      }
    }
    else
    {
      trigger_error("module 'sockets' not found", E_USER_ERROR);
      return false;     
    }
  }

然后实现连接

private function connect($host, $port, $ip=false)
      {   
        if($this->stream != false)
        {
          if(fwrite($this->stream, $this->get_connection_request($host, $port, $ip)) !== false)
          {
            if(($buffer = fread($this->stream, 1024)) !== false)
            {
              $response = unpack("Cversion/Cresult/Creg/Ctype/Lip/Sport", $buffer);
              if(isset($response['version']) && isset($response['result']) && $response['version'] == 0x05 && $response['result'] == 0x00)
              {
                stream_set_blocking($this->stream, 0);
                return true;      
              }
            }     
          }     
        }
        return false;
      }
private function get_connection_request($host, $port, $ip)
  {
    if ($ip)
      return pack("C4Nn", 0x05, 0x01, 0x00, 0x01, ip2long($host), $port);
    else
      switch($this->dnstunnel)
      {
        case true:
          return pack("C5", 0x05, 0x01, 0x00, 0x03, strlen($host)).$host.pack("n", $port);
          break;

        case false:
          return pack("C4Nn", 0x05, 0x01, 0x00, 0x01, ip2long(gethostbyname($host)), $port);
          break;
      }
  }

$ host和$ port其ip和imap服务器端口。我使用IO::Socket::INET成功绑定了socks服务器(它也适用于Socket lib)。但是如何使用这个套接字连接到imap。我需要打包和取消像这个PHP代码,或者我可以使用一些perl库?我对php一无所知,这几乎是第一次使用perl的经验。

0 个答案:

没有答案