- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试编写一个 tcp 流“隧道”(类似于默认情况下 SSH 处理的隧道),但有一个异常(exception),我必须在它流经时重写某些信息。
我确定那里有类似的东西,但我没能找到它。我有三个主要问题:
编辑:被重写的信息只是初始身份验证。
最佳答案
可以从现有(或容易找到的)实用程序中拼凑出带有日志记录的直通隧道。
socat -v -x tcp-l:8080,fork,reuseaddr tcp:localhost:80 2>log
In this example, connecting to http://localhost:8080/
will pass through to http://localhost:80/
, and log data transferred to log
.
The tool TCPreen is specialized for this exact purpose.
If you have root privileges, there are many analyzers such as tcpdump and tcpflow which can capture packets directly from the network, without having to redirect traffic.
socat
can also do some very basic stream modification with the ,cr
and ,crnl
options, which strip/add/replace \r
characters.
In any case, back to the original question… It's been ages since I've written any Java, and this is totally untested, but a tunnel that can modify traffic before retransmitting isn't difficult.
public class ForwardAndChangeCaseThread extends Thread {
private Socket in, out;
public ForwardAndChangeCaseThread(Socket in, Socket out) {
this.in = in; this.out = out;
}
public void run() {
byte[] buf = new byte[4096];
InputStream in = this.in.getInputStream();
OutputStream out = this.out.getOutputStream();
int count;
while ((count = in.read(buf)) > 0) {
for (int i = 0; i < count; i++)
if (buf[i] >= 0x40) buf[i] ^= 0x20;
out.write(buf, 0, count);
}
}
}
public class TcpForwarder {
public static void main(String[] args) {
ServerSocket listen = new ServerSocket(8080, 1);
for (;;) {
Socket local = listen.accept();
Socket remote = new Socket("localhost", 80);
new ForwardAndChangeCaseThread(local, remote).start();
new ForwardAndChangeCaseThread(remote, local).start();
}
}
}
关于java - 即时重写 tcp 流 : how difficult is it? 如何转储所述流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2166399/
我是一名优秀的程序员,十分优秀!