美文网首页
使用java实现端口转发(代理)

使用java实现端口转发(代理)

作者: 在绝望中飘零 | 来源:发表于2018-09-26 09:30 被阅读0次

            1)我们要实现的就是把客户的请求转发到别一服务器.

    要实现这个功能就是实现一个中间代理,中间代理,我们使用serverSocket 服务器实现端口帧听,

    ServerSocket serverSocket = new ServerSocket(listenPort);

    也就是我说我们请求的实际地址为A服务器,但是事实上这个请求是被转发到B服务器去完成的,也就是A和B进行了socket通信,然后把返回的数据再次传回客户端.

    在这里我们使用了线程池来运行客户端请求,原因在于如果我们不使用线程的话.我们在使用socket = serverSocket.accept();

    后会只有处理完一个连接后才能再次调用accept,因此在此使用线程池来运行连接后的数据处理.

    final ExecutorService tpe=Executors.newCachedThreadPool();

    =====================================================

    public static void main(String[] args) throws Exception {

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    ServerSocket serverSocket = new ServerSocket(listenPort);

    final ExecutorService tpe=Executors.newCachedThreadPool();

    System.out.println("Proxy Server Start At "+sdf.format(new Date()));

    System.out.println("listening port:"+listenPort+"……");

    System.out.println();

    System.out.println();

    while (true) {

    Socket socket = null;

    try {

    socket = serverSocket.accept();

    socket.setKeepAlive(true);

    tpe.execute(new ProxyTask(socket));

    System.out.println("----1");

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    }

    运行了代理服务器后,我们关键的在于转发,怎么把客户的请求转发出去,这里我们去掉对头部的处理,我们只是简单的转发,其实转向很简单,只是在代理服务器开启一个新的连接服务器的socket

    这里我们不去分析原请求的实现IP和端口,我们写成固定的地址,如:

    socketOut = new Socket("172.29.1.99", Integer.parseInt("80"));

    这样我们只要把客户端得到的inputStream然后写到指定服务器端的outputStream就行了.然后再把服务端返回的inputStream写到客户端的outputStream中去就行了.

    这里要注意的有两点.

    1)我们在接收并转发和服务器返回并转发要用线程分开,不能在同一线程中,因为可能两个过程是同步进行的.

    2)我们要实现代理,就必须要取得原有请求的实际IP地址和PORT号,因此我们分析请求的头部,要分析头部,我们必须要读取头部的字节来分析.要读取inputstream后再把inputstream转发时肯定会少了这部分数据,因为在头部分析时已读取出来了,因此为了正确的发送我们必须要把头部的数据重新加入inputsteam中去,然头在转发前选把头部数据写入到服务器的连接中去,这样才正常,除非我们不去处理头部.

    =======================================================================================================================

    public void run() {

    System.out.println("----2");

    StringBuilder builder=new StringBuilder();

    try {

    builder.append("\r\n").append("Request Time  :" + sdf.format(new Date()));

    InputStream isIn = socketIn.getInputStream();

    OutputStream osIn = socketIn.getOutputStream();

    // 查找主机和端口

    socketOut = new Socket("172.29.1.99", Integer.parseInt("80"));

    socketOut.setKeepAlive(true);

    InputStream isOut = socketOut.getInputStream();

    OutputStream osOut = socketOut.getOutputStream();

    //新开一个线程将返回的数据转发给客户端,串行会出问题,尚没搞明白原因

    Thread ot = new DataSendThread(isOut, osIn);

    ot.start();

    //读取客户端请求过来的数据转发给服务器

    readForwardDate(isIn, osOut);

    //等待向客户端转发的线程结束

    ot.join();

    } catch (Exception e) {

    e.printStackTrace();

    if(!socketIn.isOutputShutdown()){

    //如果还可以返回错误状态的话,返回内部错误

    try {

    socketIn.getOutputStream().write(SERVERERROR.getBytes());

    } catch (IOException e1) {}

    }

    } finally {

    try {

    if (socketIn != null) {

    socketIn.close();

    }

    } catch (IOException e) {}

    if (socketOut != null) {

    try {

    socketOut.close();

    } catch (IOException e) {}

    }

    //纪录上下行数据量和最后结束时间并打印

    builder.append("\r\n").append("Up    Bytes  :" + totalUpload);

    builder.append("\r\n").append("Down  Bytes  :" + totalDownload);

    builder.append("\r\n").append("Closed Time  :" + sdf.format(new Date()));

    builder.append("\r\n");

    logRequestMsg(builder.toString());

    }

    }

    相关文章

      网友评论

          本文标题:使用java实现端口转发(代理)

          本文链接:https://www.haomeiwen.com/subject/yufdoftx.html