/**
   * Connects to the remote machine by establishing a tunnel through a HTTP proxy with Basic
   * authentication. It issues a CONNECT request and authenticates with the HTTP proxy with Basic
   * protocol.
   *
   * @param address remote machine to connect to
   * @return a TCP/IP socket connected to the remote machine
   * @throws IOException if an I/O error occurs during handshake (a network problem)
   */
  private Socket authenticateBasic(InetSocketAddress address, ConnectivitySettings cs)
      throws IOException {
    Socket proxy = new Socket(cs.getProxyHost(), cs.getProxyPort());
    BufferedReader r =
        new BufferedReader(
            new InputStreamReader(new InterruptibleInputStream(proxy.getInputStream())));
    DataOutputStream dos = new DataOutputStream(proxy.getOutputStream());

    String username = cs.getProxyUsername() == null ? "" : cs.getProxyUsername();
    String password = cs.getProxyPassword() == null ? "" : String.valueOf(cs.getProxyPassword());
    String credentials = username + ":" + password;
    String basicCookie = Base64Encoder.encode(credentials.getBytes("US-ASCII"));

    dos.writeBytes("CONNECT ");
    dos.writeBytes(address.getHostName() + ":" + address.getPort());
    dos.writeBytes(" HTTP/1.0\r\n");
    dos.writeBytes("Connection: Keep-Alive\r\n");
    dos.writeBytes("Proxy-Authorization: Basic " + basicCookie + "\r\n");
    dos.writeBytes("\r\n");
    dos.flush();

    String line = r.readLine();
    if (sConnectionEstablishedPattern.matcher(line).find()) {
      for (; ; ) {
        line = r.readLine();
        if (line.length() == 0) break;
      }
      return proxy;
    }
    throw new IOException("Basic authentication failed: " + line);
  }
Exemple #2
0
  public void run() {
    URL url;
    Base64Encoder base64 = new Base64Encoder();

    try {
      url = new URL(urlString);
    } catch (MalformedURLException e) {
      System.err.println("Invalid URL");
      return;
    }

    try {
      conn = (HttpURLConnection) url.openConnection();
      conn.setRequestProperty("Authorization", "Basic " + base64.encode(user + ":" + pass));
      httpIn = new BufferedInputStream(conn.getInputStream(), 8192);
    } catch (IOException e) {
      System.err.println("Unable to connect: " + e.getMessage());
      return;
    }

    int prev = 0;
    int cur = 0;

    try {
      while (keepAlive && (cur = httpIn.read()) >= 0) {
        if (prev == 0xFF && cur == 0xD8) {
          jpgOut = new ByteArrayOutputStream(8192);
          jpgOut.write((byte) prev);
        }
        if (jpgOut != null) {
          jpgOut.write((byte) cur);
        }
        if (prev == 0xFF && cur == 0xD9) {
          synchronized (curFrame) {
            curFrame = jpgOut.toByteArray();
          }
          frameAvailable = true;
          jpgOut.close();
        }
        prev = cur;
      }
    } catch (IOException e) {
      System.err.println("I/O Error: " + e.getMessage());
    }
    try {
      jpgOut.close();
      httpIn.close();
    } catch (IOException e) {
      System.err.println("Error closing streams: " + e.getMessage());
    }
    conn.disconnect();
  }
Exemple #3
0
 /**
  * 生成签名值
  *
  * @param base
  * @param consumerSecret
  * @param accessTokenSecret
  * @return
  */
 private String generateSignature(String base, String consumerSecret, String accessTokenSecret) {
   try {
     Mac mac = Mac.getInstance(hashAlgorithmName);
     String oauthSignature =
         QStr.encode(consumerSecret)
             + "&"
             + ((accessTokenSecret == null) ? "" : QStr.encode(accessTokenSecret));
     SecretKeySpec spec = new SecretKeySpec(oauthSignature.getBytes(), hashAlgorithmName);
     mac.init(spec);
     byte[] bytes = mac.doFinal(base.getBytes());
     return new String(Base64Encoder.encode(bytes));
   } catch (Exception e) {
   }
   return null;
 }
  public static String CapchaAnswer(
      InputStream is, String apikey, String words, String regsense, String russian)
      throws IOException, InterruptedException {
    api = apikey;
    String base64 = Base64Encoder.encode(is);

    String data = URLEncoder.encode("method", "UTF-8") + "=" + URLEncoder.encode("base64", "UTF-8");
    data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(apikey, "UTF-8");
    data += "&" + URLEncoder.encode("body", "UTF-8") + "=" + URLEncoder.encode(base64, "UTF-8");
    if (words != null) {
      if (words.equals("ONE")) {
        data += "&" + URLEncoder.encode("phrase", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8");
      }
      if (words.equals("TWO")) {
        data += "&" + URLEncoder.encode("phrase", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
      }
    }
    if (regsense != null) {
      if (regsense.equals("NO")) {
        data +=
            "&" + URLEncoder.encode("regsense", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8");
      }
      if (regsense.equals("YES")) {
        data +=
            "&" + URLEncoder.encode("regsense", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
      }
    }
    if (russian != null) {
      if (russian.equals("NO")) {
        data +=
            "&" + URLEncoder.encode("is_russian", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8");
      }
      if (russian.equals("YES")) {
        data +=
            "&" + URLEncoder.encode("is_russian", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
      }
    }

    answer = CapchaPush.pushToServer(data);
    return answer;
  }
 public void testEncodeBase64() throws Exception {
   assertEquals("c3ZhbnNh", Base64Encoder.encode("svansa".getBytes()));
   assertEquals("c3ZhbnNhcg==", Base64Encoder.encode("svansar".getBytes()));
   assertEquals("c3ZhbnNhcnM=", Base64Encoder.encode("svansars".getBytes()));
   assertEquals("D9O/", Base64Encoder.encode(new byte[] {(byte) 15, (byte) 211, (byte) 191}));
 }