java 转发代码_Java端口转发工具源代码

package online.server.proxy.forward;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class TCPServer {

private static TCPServer tcp;

private String forwardIP;

private int forwardPort;

private ServerSocket server ;

private TCPServer(int port,String forwardIP,int forwardPort){

this.forwardIP = forwardIP;

this.forwardPort = forwardPort;

try {

this.server = new ServerSocket(port, 5);

} catch (IOException e) {

e.printStackTrace();

}

}

public void listen(){

new Thread(new Runnable(){

public void run() {

while (true) {

try {

Socket client = server.accept();

Socket forward = new Socket(forwardIP,forwardPort);

ClientThread clientThread = new ClientThread(client,forward);

clientThread.start();

} catch (IOException e) {

e.printStackTrace();

} catch(NullPointerException e){

break;

}

}

}}).start();

}

public void close(){

if(this.server!=null){

try {

this.server.close();

} catch (IOException e) {

e.printStackTrace();

}

this.server = null;

}

}

/**

* @param port

* @param forwardIP

* @param forwardPort

* @return

*/

public static TCPServer getInstance(int port,String forwardIP,int forwardPort){

if(tcp==null){

synchronized(TCPServer.class){

if(tcp==null){

tcp = new TCPServer(port,forwardIP,forwardPort);

}

}

}

return tcp;

}

/**

* @param port

* @param forwardIP

* @param forwardPort

* @return

*/

public static TCPServer createInstance(int port,String forwardIP,int forwardPort){

return new TCPServer(port,forwardIP,forwardPort);

}

}

package online.server.proxy.forward;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

public class ClientThread extends Thread {

private Socket client;

private Socket forward;

private InputStream clientInput;

private OutputStream clientOutput;

private InputStream forwardInput;

private OutputStream forwardOutput;

private boolean forwardActive = false;

public ClientThread(Socket client,Socket forward) {

this.client = client;

this.forward = forward;

try {

this.client.setKeepAlive(true);

this.forward.setKeepAlive(true);

this.clientInput = this.client.getInputStream();

this.clientOutput = this.client.getOutputStream();

this.forwardInput = this.forward.getInputStream();

this.forwardOutput = this.forward.getOutputStream();

} catch (IOException e) {

this.close();

}

}

public void run() {

this.forwardActive = true;

ForwardThread clientForward = new ForwardThread(this, this.clientInput,this.forwardOutput);

clientForward.start();

ForwardThread serverForward = new ForwardThread(this, this.forwardInput,this. clientOutput);

serverForward.start();

//============================//

StringBuilder sb = new StringBuilder();

sb.append("TCP Forwarding " + client.getInetAddress().getHostAddress() + ":" + client.getPort());

sb.append(" ");

sb.append(forward.getInetAddress().getHostAddress() + ":" + forward.getPort());

sb.append(" started.");

ForwardUtil.debug(sb.toString());

//============================//

}

public synchronized void close() {

ForwardUtil.close(this.forward);

ForwardUtil.close(this.client);

if (this.forwardActive) {

//============================//

StringBuilder sb = new StringBuilder();

sb.append("TCP Forwarding " + client.getInetAddress().getHostAddress() + ":" + client.getPort());

sb.append(" ");

sb.append(forward.getInetAddress().getHostAddress() + ":" + forward.getPort());

sb.append(" stopped.");

ForwardUtil.debug(sb.toString());

//============================//

this.forwardActive = false;

}

}

}

package online.server.proxy.forward;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

/**

*/

public class ForwardThread extends Thread {

private static final int BUFFER_SIZE = 8192;

private InputStream input;

private OutputStream output;

ClientThread parent;

public ForwardThread(ClientThread parent, InputStream input, OutputStream output) {

this.parent = parent;

this.input = input;

this.output = output;

}

public void run() {

try {

byte[] buffer = new byte[BUFFER_SIZE];

while (true) {

int bytesRead = this.input.read(buffer);

if (bytesRead == -1){

break;

}

this.output.write(buffer, 0, bytesRead);

this.output.flush();

}

} catch (IOException e) {

}

this.parent.close();

}

}

package online.server.proxy.forward;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

public class ForwardUtil {

private static final boolean debug = true;

/**

* @param input

*/

public static void close(InputStream input){

if(input!=null){

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

}

input=null;

}

}

/**

* @param output

*/

public static void close(OutputStream output){

if(output!=null){

try {

output.close();

} catch (IOException e) {

e.printStackTrace();

}

output=null;

}

}

/**

* @param socket

*/

public static void close(Socket socket){

if(socket!=null){

try {

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

socket=null;

}

}

/**

* @param o

*/

public static void debug(Object o){

if(debug){

System.out.println(o);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值