Java中端口反弹客户端代码,配合lcx使用,可以实现内网穿透
package com.kingron.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.Socket;
public class TcpTunnel extends Thread {
private String remoteIP, localIP;
private int remotePort, localPort;
private static class PipeStream implements Runnable {
private Socket inx, outx;
public PipeStream(Socket inx, Socket outx) {
this.inx = inx;
this.outx = outx;
}
@Override
public void run() {
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inx.getInputStream());
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outx.getOutputStream());
byte[] bytes = new byte[4096];
int len = 0;
while ((len = bufferedInputStream.read(bytes)) > 0) {
bufferedOutputStream.write(bytes, 0, len);
bufferedOutputStream.flush();
}
bufferedInputStream.close();
bufferedOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (!inx.isClosed())
try {
inx.close();
} catch (Exception e) {
}
if (!outx.isClosed())
try {
outx.close();
} catch (Exception e) {
}
}
}
}
public TcpTunnel(String remoteIP, int remotePort, String localIP, int localPort) {
this.remoteIP = remoteIP;
this.remotePort = remotePort;
this.localIP = localIP;
this.localPort = localPort;
}
public void run() {
Socket remote, local;
try {
remote = new Socket(remoteIP, remotePort);
local = new Socket(localIP, localPort);
if (local == null || local.isClosed() || !local.isConnected()
|| remote == null || remote.isClosed() || !remote.isConnected()) return;
new Thread(new PipeStream(remote, local)).start();
new Thread(new PipeStream(local, remote)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}