001: package vicazh.hyperpool.stream.net.socks;
002:
003: import java.io.*;
004: import vicazh.hyperpool.stream.Connection;
005:
006: /**
007: * This class is the superclass of all socks stream data
008: *
009: * @author Victor Zhigunov
010: * @version 0.3.14
011: */
012: public class Stream extends vicazh.hyperpool.stream.net.Stream {
013: public Stream() {
014: }
015:
016: /**
017: * @param connection
018: * parent connection
019: * @param outputstream
020: * linked output stream
021: */
022: public Stream(Connection connection, OutputStream outputstream) {
023: super (connection, outputstream);
024: }
025:
026: private int type;
027:
028: /**
029: * Set the type
030: */
031: public void setType(int type) {
032: this .type = type;
033: }
034:
035: /**
036: * Get the type
037: */
038: public int getType() {
039: return type;
040: }
041:
042: private byte[] address;
043:
044: /**
045: * Set the address
046: */
047: public void setAddress(byte[] address) {
048: this .address = address;
049: }
050:
051: /**
052: * Get the address
053: */
054: public byte[] getAddress() {
055: return address;
056: }
057:
058: private int command;
059:
060: /**
061: * Set the command
062: */
063: public void setCommand(int command) {
064: this .command = command;
065: }
066:
067: /**
068: * Get the command
069: */
070: public int getCommand() {
071: return command;
072: }
073:
074: private int port;
075:
076: /**
077: * Set the port
078: */
079: public void setPort(int port) {
080: this .port = port;
081: }
082:
083: /**
084: * Get the port
085: */
086: public int getPort() {
087: return port;
088: }
089:
090: protected enum Mode {
091: AUTH, HEAD, CONTENT
092: };
093:
094: protected Mode mode = Mode.AUTH;
095:
096: private int index;
097:
098: public void write(int b) throws IOException {
099: if (mode == Mode.CONTENT)
100: content(b);
101: else {
102: if (index == 1)
103: command = b;
104: else if (index == 3)
105: type = b;
106: else
107: switch (type) {
108: case 1:
109: if (index == 4)
110: address = new byte[4];
111: if (index <= address.length + 3)
112: address[index - 4] = (byte) b;
113: else if (index == address.length + 4)
114: port = b;
115: else
116: head(command, type, address, (port << 8) + b);
117: break;
118: case 3:
119: if (index == 4)
120: address = new byte[b];
121: else if (index <= address.length + 4)
122: address[index - 5] = (byte) b;
123: else if (index == address.length + 5)
124: port = b;
125: else
126: head(command, type, address, (port << 8) + b);
127: break;
128: case 4:
129: if (index == 4)
130: address = new byte[6];
131: if (index <= address.length + 3)
132: address[index - 4] = (byte) b;
133: else if (index == address.length + 4)
134: port = b;
135: else
136: head(command, type, address, (port << 8) + b);
137: break;
138: }
139: index++;
140: }
141: }
142:
143: /**
144: * Writes the socks authentication data to this stream
145: *
146: * @param data
147: * the data
148: */
149: public void auth(byte[] data) throws IOException {
150: super .write(5);
151: for (byte b : data)
152: super .write(b);
153: mode = Mode.HEAD;
154: }
155:
156: /**
157: * Writes the socks parameters to this stream
158: *
159: * @param command
160: * the socks command
161: * @param type
162: * the address type
163: * @param address
164: * the address
165: * @param port
166: * the port
167: */
168: public void head(int command, int type, byte[] address, int port)
169: throws IOException {
170: this .command = command;
171: this .type = type;
172: this .address = address;
173: this .port = port;
174: super .write(5);
175: super .write(command);
176: super .write(0);
177: super .write(type);
178: if (type == 3)
179: super .write(address.length);
180: for (byte b : address)
181: super .write(b);
182: super .write(port >> 8);
183: super .write((byte) port);
184: mode = Mode.CONTENT;
185: }
186:
187: /**
188: * Writes the content to this stream
189: *
190: * @param b
191: * the <code>byte</code>
192: */
193: public void content(int b) throws IOException {
194: super.write(b);
195: }
196: }
|