001: package net.sourceforge.jtds.tools;
002:
003: import java.io.*;
004: import java.net.*;
005:
006: public class SQLProxy {
007: static final int LOCAL_PORT = 1433;
008:
009: static final String SERVER = "server";
010: static final int SERVER_PORT = 1433;
011:
012: public static void main(String args[]) throws IOException {
013: ServerSocket sock = new ServerSocket(LOCAL_PORT);
014: while (true) {
015: Socket s = sock.accept();
016: new DumpThread(s).start();
017: }
018: }
019: }
020:
021: class DumpThread extends Thread {
022: private Socket client, server;
023: private byte[] buf = new byte[4096];
024:
025: DumpThread(Socket client) {
026: this .client = client;
027: }
028:
029: String hex(int value, int len) {
030: String res = Integer.toHexString(value);
031: while (res.length() < len)
032: res = '0' + res;
033: return res.toUpperCase();
034: }
035:
036: String packetType(int value) {
037: switch (value) {
038: case 0x01:
039: return "4.2 or 7.0 query";
040: case 0x02:
041: return "4.2 or 7.0 login packet";
042: case 0x04:
043: return "Server response";
044: case 0x06:
045: return "Cancel";
046: case 0x0F:
047: return "5.0 query";
048: case 0x10:
049: return "7.0 login packet";
050: default:
051: return "Unknown (0x" + hex(value, 2) + ')';
052: }
053: }
054:
055: int passByte(InputStream in, OutputStream out) throws IOException {
056: int res = in.read();
057: out.write(res);
058: return res;
059: }
060:
061: void skip(int len, InputStream in, OutputStream out)
062: throws IOException {
063: while (len > 0) {
064: int rd = in.read(buf, 0, len > buf.length ? buf.length
065: : len);
066: // for( int i=0; i<rd; i++ )
067: // System.out.print((char)buf[i]);
068: // System.out.println();
069: // for( int i=0; i<rd; i++ )
070: // System.out.print(hex(((int)buf[i])&0xFF, 2)+" ");
071: // System.out.println();
072: out.write(buf, 0, rd);
073: len -= rd;
074: }
075: }
076:
077: boolean nextPacket(InputStream in, OutputStream out)
078: throws IOException {
079: int type = passByte(in, out), last = passByte(in, out);
080:
081: int size = passByte(in, out) << 8 | passByte(in, out);
082:
083: buf[0] = (byte) passByte(in, out);
084: buf[1] = (byte) passByte(in, out);
085: buf[2] = (byte) passByte(in, out);
086: buf[3] = (byte) passByte(in, out);
087:
088: System.out.println("Packet type: " + packetType(type));
089: System.out.println("Packet size: " + size + " (0x"
090: + hex(size, 4) + ")");
091: System.out.print("Rest of header:");
092: for (int i = 0; i < 4; i++)
093: System.out.print(" 0x" + hex(((int) buf[i]) & 0xFF, 2));
094: System.out.println();
095:
096: skip(size - 8, in, out);
097:
098: return last == 0;
099: }
100:
101: public void run() {
102: try {
103: server = new Socket(SQLProxy.SERVER, SQLProxy.SERVER_PORT);
104:
105: InputStream cIn = client.getInputStream();
106: OutputStream cOut = client.getOutputStream();
107: InputStream sIn = server.getInputStream();
108: OutputStream sOut = server.getOutputStream();
109:
110: while (true) {
111: while (nextPacket(cIn, sOut))
112: ;
113: System.out.println("--- Client done. ---\n");
114: while (nextPacket(sIn, cOut))
115: ;
116: System.out.println("--- Server done. ---\n");
117: }
118: } catch (IOException ex) {
119: try {
120: client.close();
121: } catch (IOException exc) {
122: }
123: try {
124: server.close();
125: } catch (IOException exc) {
126: }
127: System.out.println("Disconnected.");
128: }
129: }
130: }
|