tcp转发

时间:2023-02-10 10:58:55

Proxy.java

package com.dc.tcp.proxy;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Proxy {
 
 public static void main(String[] args) {
  try {
   ServerSocket server = new ServerSocket(8081);
   while (true) {
    Socket socket = server.accept();
    Pool.getInstance().getExecutor().execute(new ProxyWorker(socket));
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
}

ProxyWorker.java

package com.dc.tcp.proxy;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException; public class ProxyWorker implements Runnable {     private Socket server = null;
    private Socket client = null;     public ProxyWorker(Socket client) {
        try {
            server = new Socket("10.126.3.163", 3306);
            this.client = client;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }     @Override
    public void run() {
        Pool.getInstance().getExecutor().execute(new ProxySender(client, server));
        Pool.getInstance().getExecutor().execute(new ProxyReceiver(client, server));
    }
}

ProxySender.java

package com.dc.tcp.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class ProxySender implements Runnable { private Socket client = null;
private Socket server = null;
private Logger log = LoggerFactory.getLogger(ProxySender.class); public ProxySender(Socket client, Socket server) {
super();
this.server = server;
this.client = client;
} @Override
public void run() {
try {
InputStream is = server.getInputStream();
OutputStream os = client.getOutputStream();
// log.info("send:");
while (true) {
int flag = 0;
if ((flag = is.read()) == -1) {
break;
} else {
int count = is.available();
if (count > 0) {
byte[] b = new byte[count];
is.read(b);
byte[] temp = new byte[count + 1];
temp[0] = (byte) flag;
for (int i = 0; i < b.length; i++) {
temp[i + 1] = b[i];
}
// log.info(new String(temp));
String tempStr = "";
for (int i = 0; i < temp.length; i++) {
tempStr += " " + temp[i];
}
// log.info(tempStr);
os.write(temp);
}
}
}
} catch (IOException e) {
e.printStackTrace();
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("sender close socket");
}
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("sender close socket");
} }

ProxyReceiver.java

package com.dc.tcp.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class ProxyReceiver implements Runnable { private Socket server = null;
private Socket client = null;
private Logger log = LoggerFactory.getLogger(ProxyReceiver.class); public ProxyReceiver(Socket client, Socket server) {
super();
this.server = server;
this.client = client;
} @Override
public void run() {
try {
InputStream is = client.getInputStream();
OutputStream os = server.getOutputStream();
log.info("receive:");
while (true) {
int flag = 0;
if ((flag = is.read()) == -1) {
break;
} else {
int count = is.available();
if (count > 0) {
byte[] b = new byte[count];
is.read(b);
byte[] temp = new byte[count + 1];
temp[0] = (byte) flag;
for (int i = 0; i < b.length; i++) {
temp[i + 1] = b[i];
}
log.info(new String(temp));
String tempStr = "";
for (int i = 0; i < temp.length; i++) {
tempStr += " " + temp[i];
}
log.info(tempStr);
os.write(temp);
}
}
}
} catch (IOException e) {
e.printStackTrace();
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("receiver close socket");
}
if (client != null) {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
log.info("receiver close socket");
} }

Pool.java

package com.dc.tcp.proxy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class Pool { private static Pool instance = null;
private ExecutorService executor = null; private Pool() {
super();
executor = Executors.newFixedThreadPool(6);
} public static Pool getInstance() {
if (instance == null) {
synchronized (Pool.class) {
if (instance == null) {
instance = new Pool();
}
}
}
return instance;
} public ExecutorService getExecutor() {
return executor;
} }