0

すばやく簡単なポートフォワーダーを作成する必要があります。現時点では、接続を受け入れ、クライアントから正常に読み取ることができますが、実行中のローカルマシンからは何も取得しません。(参考までに、これはWebサーバーを転送するために作成されました。これは、VS2010 / 2012をネットワークに転送するために通常使用するアプリが見つからないため、自分で作成したためです)。

飲み込むという例外などは悪いことですが、今は重要ではありません。後で対処できます。

import java.io.*;
import java.net.*;

public class SocketForward {
  int src = 0;
  int dest = 0;
  public static void main(String args[]) throws Exception {
    if (args.length < 2) {
      System.out.println("Usage: java SocketForward <source port> <destination port>");
      System.exit(1);
    } else {
      new SocketForward().startRun(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
    }
  }
  public void startRun(int src, int dest) throws Exception {
    try {
      this.src = src;
      this.dest = dest;
    } catch (NumberFormatException e) {
      System.out.println("Usage: java SocketForward <source port> <destination port>");
      System.exit(1);
    }
    ServerSocket server = new ServerSocket(dest);
    System.out.println("Waiting for connections...");
    while (!server.isClosed()) {
      new ConnHandler(src, server.accept());
    }
  }
}

class ConnHandler implements Runnable {
  Socket clientSocket;
  Socket hostSocket;
  int hostport;
  Thread thread;
  BufferedOutputStream hostOut;
  BufferedOutputStream clientOut;
  BufferedInputStream hostIn;
  BufferedInputStream clientIn;
  public ConnHandler(int hostport, Socket clientSocket) {
    System.out.println("Connected to " + clientSocket.getInetAddress());
    this.clientSocket = clientSocket;
    this.hostport = hostport;
    this.thread = new Thread(this);
    thread.start();
  }
  @Override
  public void run() {
    try {
      hostSocket = new Socket("", hostport);
      System.out.println("Connected to localhost:" + hostport);
      hostOut = new BufferedOutputStream(hostSocket.getOutputStream());
      clientOut = new BufferedOutputStream(clientSocket.getOutputStream());
      hostIn = new BufferedInputStream(hostSocket.getInputStream());
      clientIn = new BufferedInputStream(clientSocket.getInputStream());
      new Thread() {
        public void run() {
          while (this == Thread.currentThread()) {
            byte[] buf = new byte[8192];
            try {
              int len = 0;
              while ((len = ConnHandler.this.clientIn.read(buf)) > 0) {
                System.out.println("Client: " + new String(buf, 0, len));
                ConnHandler.this.hostOut.write(buf, 0, len);
              }
            } catch (Exception e) {
              e.printStackTrace();
              break;
            }
          }
        }
      }.start();
      new Thread() {
        public void run() {
          while (this == Thread.currentThread()) {
            byte[] buf = new byte[8192];
            try {
              int len = 0;
              while ((len = ConnHandler.this.hostIn.read(buf)) > 0) {
                System.out.println("Host: " + new String(buf, 0, len));
                ConnHandler.this.clientOut.write(buf, 0, len);
              }
            } catch (Exception e) {
              e.printStackTrace();
              break;
            }
          }
        }
      }.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
4

1 に答える 1

0

フラッシングを行っていないため、不透明な中間者攻撃が発生します。これにより、クライアントとサーバーが期待するデータフローが妨げられる可能性があります。

try-catchコードスタイルに関する補足:ループの外側にある方が便利なwhileので、自動的に発生します。

try {
  while (...) ...
} catch (Throwable t) {
   t.printStackTrace();
}

Throwable代わりに注意してください— sだけでなく、コードを壊すエラーExceptionについて知りたいと思うでしょう。Exception

于 2013-01-18T11:58:34.310 に答える