⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csocks.java

📁 JAVA实现的短信网关程序
💻 JAVA
字号:
package sms.PHS;

import java.io.*;
import java.net.*;

public class CSocks {
  static final byte SOCKS_CONNECT = 1;
  static final byte SOCKS_CONNECT_RESPONSE_LEN = 8;
  static final byte SOCKS_ERROR_GRANTED = 90;
  static final byte SOCKS_ERROR_IDENTD = 92;
  static final byte SOCKS_ERROR_REJECTED = 91;
  static final byte SOCKS_ERROR_USERID = 93;
  static final byte SOCKS_VERSION = 4;
  private static InetAddress socksAddress;
  private static int socksPort;
  private static String userid;

  public CSocks() {
  }

  public static Socket Socket(String s, int i) throws CSocksException,
      IOException, UnknownHostException {
    InetAddress inetaddress;// = null; //repaired 2004-11-24
    try {
      inetaddress = InetAddress.getByName(s);
    }
    catch (NullPointerException nullpointerexception) {
      System.out.println("Cannot convert address to ip ".concat(String.valueOf(
          String.valueOf(nullpointerexception))));
      throw new UnknownHostException(
          "Cannot resolve host (suspect application has gone off line) ".concat(
          String.valueOf(String.valueOf(nullpointerexception))));
    }

    return Socket(inetaddress, i);
  }

  public static Socket Socket(InetAddress inetaddress, int i) throws
      IOException, CSocksException {
    int j;
    byte abyte0[];
    byte abyte1[];
    InputStream inputstream;
    OutputStream outputstream;
    Socket socket;

    if (socksAddress == null || i == 0) {
      return new Socket(inetaddress, i);
    }
    if (userid == null) {
      j = 9;
    }
    else {
      j = 9 + userid.length();
    }
    abyte0 = new byte[j];
    abyte1 = inetaddress.getAddress();
    if (abyte1.length < 4) {
      throw new CSocksException("Cant convert IP address for server");
    }

    try {
      socket = new Socket(socksAddress, socksPort);
      inputstream = socket.getInputStream();
      outputstream = socket.getOutputStream();
    }
    catch (Exception exception) {
      System.out.println("Socket error:" + exception.getMessage());
      throw new CSocksException(" ".concat(String.valueOf(String.valueOf(
          exception))));
    }

    abyte0[0] = 4;
    abyte0[1] = 1;
    abyte0[2] = (byte) (i >>> 8 & 0xff);
    abyte0[3] = (byte) (i & 0xff);
    abyte0[4] = abyte1[0]; //添加ip
    abyte0[5] = abyte1[1];
    abyte0[6] = abyte1[2];
    abyte0[7] = abyte1[3];
    j = 8;
    if (userid == null) {
      abyte0[j] = 0;
      j++;
    }
    else {
      abyte0 = userid.getBytes();
      j += userid.length();
      abyte0[j] = 0;
      j++;
    }
    try{
      outputstream.write(abyte0, 0, j);
    }
    catch(Exception e){
      j = 0;
      throw new CSocksException("Cannot send a request to the proxy");
    }


    if (true) {
      int k;
      try {
        k = inputstream.read(abyte0, j, 1);
        if (k == -1) {
          throw new CSocksException("proxy prematurely closed connection");
        }
      }
      catch (IOException ex) {
        System.out.println("annot read a response from proxy" + ex.getMessage());
      }


      if (j == 1) {
        switch (abyte0[1]) {
          case 91: // '['
            throw new CSocksException("proxy rejected the connect request");

          case 92: // '\\'
            throw new CSocksException(
                "proxy cannot connect to client's ident daemon");

          case 93: // ']'
            throw new CSocksException(
                "ident daemon and client user-ids conflict");

          default:
            throw new CSocksException(String.valueOf(String.valueOf( (new
                StringBuffer("proxy returned unknown error (")).append(abyte0[1]).
                append(")"))));

          case 90: // 'Z'
            break;
        }
      }

    }
    if (++j >= 8) {
      return socket;
    }
    else {
      return null;
    }

  }

  public static void setProxy(String s, int i, String s1) throws
      UnknownHostException {
    setProxy(InetAddress.getByName(s), i, s1);
  }

  public static void setProxy(String s, int i) throws UnknownHostException {
    setProxy(InetAddress.getByName(s), i);
  }

  public static void setProxy(InetAddress inetaddress, int i) {
    socksAddress = inetaddress;
    socksPort = i;
    userid = null;
  }

  public static void setProxy(InetAddress inetaddress, int i, String s) {
    socksAddress = inetaddress;
    socksPort = i;
    userid = s;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -