Package vicazh.hyperpool.stream.net.socks

Source Code of vicazh.hyperpool.stream.net.socks.SwitchStream

package vicazh.hyperpool.stream.net.socks;

import java.io.*;
import java.net.*;
import java.net.Socket;
import vicazh.hyperpool.Start;
import vicazh.hyperpool.stream.*;
import vicazh.hyperpool.stream.net.*;

class SwitchStream extends ClientStream {

  SwitchStream(Connection connection) {
    super(connection, new NullStream());
  }

  public void auth(byte[] data) throws IOException {
    connection.getServer().write(5);
    connection.getServer().write(0);
    connection.getServer().flush();
    super.auth(data);
  }

  private Socket socket;

  private ServerSocket serversocket;

  public void head(int command, int type, byte[] address, int port)
      throws IOException {
    super.head(command, type, address, port);
    switch (command) {
    case 1:
      byte[] a;
      try {
        socket = new Socket(type == 3 ? InetAddress
            .getByName(new String(address)) : InetAddress
            .getByAddress(address), port);
        socket
            .setSoTimeout(((SwitchService) connection.element).timeout);
        outputstream = new BufferedOutputStream(socket
            .getOutputStream());
        a = socket.getLocalAddress().getAddress();
        Start.logger.finest("address:" + InetAddress.getByAddress(a)
            + " port:" + socket.getLocalPort());
        ((Stream) connection.getServer()).head(0,
            a.length == 4 ? 1 : 4, a, socket.getLocalPort());
        connection.getServer().flush();
        Transfer.start(
            new BufferedInputStream(socket.getInputStream()),
            connection.getServer());
      } catch (IOException e) {
        try {
          connection.getServer().close();
        } catch (Exception e1) {
        }
        throw e;
      }
      break;
    case 2:
      try {
        serversocket = new ServerSocket(0);
        serversocket
            .setSoTimeout(((SwitchService) connection.element).timeout);
        a = serversocket.getInetAddress().getAddress();
        // if (Arrays.equals(a, new byte[a.length]))
        // a = InetAddress.getLocalHost().getAddress();
        ((Stream) connection.getServer()).head(0,
            a.length == 4 ? 1 : 4, a, serversocket.getLocalPort());
        connection.getServer().flush();
        socket = serversocket.accept();
        socket
            .setSoTimeout(((SwitchService) connection.element).timeout);
        outputstream = new BufferedOutputStream(socket
            .getOutputStream());
        a = socket.getLocalAddress().getAddress();
        Start.logger.finest("address:" + InetAddress.getByAddress(a)
            + " port:" + socket.getLocalPort());
        ((Stream) connection.getServer()).head(0,
            a.length == 4 ? 1 : 4, a, socket.getLocalPort());
        connection.getServer().flush();
        Transfer.start(
            new BufferedInputStream(socket.getInputStream()),
            connection.getServer());
      } catch (IOException e) {
        try {
          connection.getServer().close();
        } catch (Exception e1) {
        }
        throw e;
      }
      break;
    case 3:
      DatagramSocket socket = ((InClientStream) ((vicazh.hyperpool.stream.net.socks.Socket) ((Stream) connection
          .getServer()).getSocket()).stream.connection.getClient()).service.socket;
      a = socket.getLocalAddress().getAddress();
      Start.logger.finest("address:" + InetAddress.getByAddress(a)
          + " port:" + socket.getLocalPort());
      ((Stream) connection.getServer()).head(0, a.length == 4 ? 1 : 4, a,
          socket.getLocalPort());
      connection.getServer().flush();
      break;
    }
  }

  public vicazh.hyperpool.stream.net.Socket getSocket() {
    return new vicazh.hyperpool.stream.net.Socket(socket);
  }

  public void close() throws IOException {
    try {
      socket.close();
    } catch (Exception e) {
    }
    try {
      serversocket.close();
    } catch (Exception e) {
    }
    super.close();
  }

}
TOP

Related Classes of vicazh.hyperpool.stream.net.socks.SwitchStream

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.