001: package vicazh.hyperpool.stream.net.socks;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.net.Socket;
006: import vicazh.hyperpool.Start;
007: import vicazh.hyperpool.stream.*;
008: import vicazh.hyperpool.stream.net.*;
009:
010: class SwitchStream extends ClientStream {
011:
012: SwitchStream(Connection connection) {
013: super (connection, new NullStream());
014: }
015:
016: public void auth(byte[] data) throws IOException {
017: connection.getServer().write(5);
018: connection.getServer().write(0);
019: connection.getServer().flush();
020: super .auth(data);
021: }
022:
023: private Socket socket;
024:
025: private ServerSocket serversocket;
026:
027: public void head(int command, int type, byte[] address, int port)
028: throws IOException {
029: super .head(command, type, address, port);
030: switch (command) {
031: case 1:
032: socket = new Socket(type == 3 ? InetAddress
033: .getByName(new String(address)) : InetAddress
034: .getByAddress(address), port);
035: socket
036: .setSoTimeout(((SwitchService) connection.element).timeout);
037: outputstream = new BufferedOutputStream(socket
038: .getOutputStream());
039: byte[] a = socket.getLocalAddress().getAddress();
040: Start.logger.finest("address:"
041: + InetAddress.getByAddress(a) + " port:"
042: + socket.getLocalPort());
043: ((Stream) connection.getServer()).head(0, a.length == 4 ? 1
044: : 4, a, socket.getLocalPort());
045: connection.getServer().flush();
046: Transfer.start(new BufferedInputStream(socket
047: .getInputStream()), connection.getServer());
048: break;
049: case 2:
050: serversocket = new ServerSocket(0);
051: serversocket
052: .setSoTimeout(((SwitchService) connection.element).timeout);
053: a = serversocket.getInetAddress().getAddress();
054: // if (Arrays.equals(a, new byte[a.length]))
055: // a = InetAddress.getLocalHost().getAddress();
056: ((Stream) connection.getServer()).head(0, a.length == 4 ? 1
057: : 4, a, serversocket.getLocalPort());
058: connection.getServer().flush();
059: socket = serversocket.accept();
060: socket
061: .setSoTimeout(((SwitchService) connection.element).timeout);
062: outputstream = new BufferedOutputStream(socket
063: .getOutputStream());
064: a = socket.getLocalAddress().getAddress();
065: Start.logger.finest("address:"
066: + InetAddress.getByAddress(a) + " port:"
067: + socket.getLocalPort());
068: ((Stream) connection.getServer()).head(0, a.length == 4 ? 1
069: : 4, a, socket.getLocalPort());
070: connection.getServer().flush();
071: Transfer.start(new BufferedInputStream(socket
072: .getInputStream()), connection.getServer());
073: break;
074: case 3:
075: DatagramSocket socket = ((InClientStream) ((Stream) connection
076: .getServer()).getSocket().stream.connection
077: .getClient()).service.socket;
078: a = socket.getLocalAddress().getAddress();
079: Start.logger.finest("address:"
080: + InetAddress.getByAddress(a) + " port:"
081: + socket.getLocalPort());
082: ((Stream) connection.getServer()).head(0, a.length == 4 ? 1
083: : 4, a, socket.getLocalPort());
084: connection.getServer().flush();
085: break;
086: }
087: }
088:
089: public vicazh.hyperpool.stream.net.Socket getSocket() {
090: return new vicazh.hyperpool.stream.net.Socket(socket);
091: }
092:
093: public void close() throws IOException {
094: try {
095: socket.close();
096: } catch (Exception e) {
097: }
098: try {
099: serversocket.close();
100: } catch (Exception e) {
101: }
102: super.close();
103: }
104:
105: }
|