java端口转发

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 ** java端口转发,8899到1080(ssr),手机wifi设置代理为电脑ip:8899  
 **/
class txrxThread extends Thread{
    private Socket client;
    private Socket server;
    private InputStream inserver;
    private OutputStream outserver;
    private InputStream inclient;
    private OutputStream outclient;
    public txrxThread(Socket client){
        this.client = client;
        try {
            this.server = new Socket("192.168.1.9",1080);
            this.inserver = server.getInputStream();
            this.outserver = server.getOutputStream();
            this.inclient = client.getInputStream();
            this.outclient = client.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                proxy.rxcnt++;
                byte[] abc = new byte[8192];
                int len;
                try {
                    while((len=inclient.read(abc,0,abc.length))!=-1){
                        if(proxy.printEnable){
                            if(client.getPort()==proxy.port){
                                System.out.println("客户端"+client.getPort()+"正在发送数据到服务端,长度"+len);
                                proxy.displayHex(abc,len);
                            }
                        }else{
                            System.out.println("客户端"+client.getPort()+"正在发送数据到服务端,长度"+len);
                        }
                        outserver.write(abc,0,len);
                        outserver.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    inclient.close();
                } catch (IOException e) {
                }
                proxy.rxcnt--;
                //System.out.println("当前收发线程数量分别为"+ proxy.rxcnt+","+ proxy.txcnt);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                proxy.txcnt++;
                byte[] abc = new byte[8192];
                int len;
                try {
                    while((len= inserver.read(abc,0,abc.length))!=-1){
                        if(proxy.printEnable){
                            if(client.getPort()==proxy.port){
                                System.out.println("客户端"+client.getPort()+"正在接收数据,长度"+len);
                                proxy.displayHex(abc,len);
                            }
                        }else{
                            System.out.println("客户端"+client.getPort()+"正在接收数据,长度"+len);
                        }
                        outclient.write(abc,0,len);
                        outclient.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    outclient.close();
                } catch (IOException e) {
                }
                proxy.txcnt--;
                //System.out.println("当前收发线程数量分别为"+ proxy.rxcnt+","+ proxy.txcnt);
            }
        }).start();
    }
}
public class proxy {
    public volatile static int txcnt = 0,rxcnt = 0;
    public volatile static int port = -1;
    public volatile static boolean printEnable = false;
    private JTextField jtf;
    private JButton pe;
    public proxy(){
        JFrame jf = new JFrame();
        jf.setSize(new Dimension(500,500));
        jf.setLocationRelativeTo(null);
        jf.setLayout(null);
        pe = new JButton("端口打印关闭");
        pe.setBounds(100,100,200,50);
        pe.addActionListener(new ActionListener() {
            private boolean enable = false;
            @Override
            public void actionPerformed(ActionEvent e) {
                if(enable) {
                    port = -1;
                    pe.setText("端口打印关闭");
                    printEnable = false;
                    enable = false;
                }else{
                    port = Integer.parseInt(jtf.getText());
                    pe.setText("端口打印开启");
                    printEnable = true;
                    enable = true;
                }
            }
        });
        jf.add(pe);

        jtf = new JTextField(20);
        jtf.setBounds(100,200,200,50);
        jf.add(jtf);

        jf.setVisible(true);
        try {
            //转发到1080端口
            ServerSocket  server = new ServerSocket(8899);
            while(true){
                Socket client = server.accept();
                new txrxThread(client).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
        new proxy();
    }

    public static void displayHex(byte[] buffer,int size){
        int i,j,k;
        String str = "";
        for (i=0; i 126)) {
                        str+=String.format(".");
                    } else {
                        str+=String.format("%c", buffer[i+j]);
                    }
                }
            }
        }
        str+=String.format("\n" );
        System.out.println(str);
    }
}

​

 

你可能感兴趣的:(java)