001: package net.sourceforge.jtds.tools;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006: import java.net.ServerSocket;
007: import java.net.Socket;
008:
009: public class SqlForwarder {
010: String host = "localhost";
011: String logfile = null;
012: int port = 1433;
013: int listenPort = 1444;
014: int lognum = 0;
015:
016: byte[] readPacket(InputStream input) throws IOException {
017: byte[] hdr = new byte[8];
018: int len = input.read(hdr);
019: if (len < 8) {
020: return null;
021: }
022: int packetlen = ((((int) hdr[2]) & 0xff) << 8)
023: + (((int) hdr[3]) & 0xff);
024: byte[] data = new byte[packetlen];
025: while (len < packetlen) {
026: len += input.read(data, len, packetlen - len);
027: }
028: for (int i = 0; i < 8; i++) {
029: data[i] = hdr[i];
030: }
031: return data;
032: }
033:
034: class ConnectionThread extends Thread {
035: Socket client;
036: Socket server;
037:
038: ConnectionThread(Socket client, Socket server) {
039: this .client = client;
040: this .server = server;
041: }
042:
043: public void run() {
044: try {
045: InputStream input[] = new InputStream[2];
046: input[0] = client.getInputStream();
047: input[1] = server.getInputStream();
048:
049: OutputStream output[] = new OutputStream[2];
050: output[0] = server.getOutputStream();
051: output[1] = client.getOutputStream();
052:
053: PacketLogger log;
054: if (logfile == null) {
055: log = new PacketLogger("filter" + lognum++ + ".log");
056: } else {
057: log = new PacketLogger(logfile + lognum++ + ".log");
058: }
059:
060: int direction = 0;
061: while (true) {
062: byte[] data = readPacket(input[direction]);
063: if (data == null) {
064: break;
065: }
066: output[direction].write(data);
067: log.log(data);
068: if (data[1] != 0) {
069: direction = 1 - direction;
070: }
071: }
072: client.close();
073: server.close();
074: } catch (IOException unused) {
075: }
076: }
077: }
078:
079: SqlForwarder() {
080: }
081:
082: void run() throws IOException {
083: System.out.println("Listening on port " + listenPort
084: + "; Connecting to " + host + " at port " + port);
085: ServerSocket srv = new ServerSocket(listenPort);
086: while (true) {
087: Socket client = srv.accept();
088: Socket server = new Socket(host, port);
089: ConnectionThread t = new ConnectionThread(client, server);
090: t.start();
091: }
092: }
093:
094: void parseArgs(String args[]) throws NumberFormatException {
095: for (int i = 0; i < args.length; i++) {
096: String arg = args[i];
097: if (arg.equals("-server")) {
098: i++;
099: host = args[i];
100: } else if (arg.equals("-port")) {
101: i++;
102: port = Integer.parseInt(args[i]);
103: } else if (arg.equals("-listen")) {
104: i++;
105: listenPort = Integer.parseInt(args[i]);
106: } else if (arg.equals("-log")) {
107: i++;
108: logfile = args[i];
109: }
110: }
111: }
112:
113: public static void main(String args[]) throws IOException {
114: SqlForwarder app = new SqlForwarder();
115: app.parseArgs(args);
116: app.run();
117: }
118: }
|