Search in sources :

Example 1 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class I2PSOCKSTunnel method clientConnectionRun.

protected void clientConnectionRun(Socket s) {
    I2PSocket destSock = null;
    try {
        try {
            s.setSoTimeout(INITIAL_SO_TIMEOUT);
        } catch (SocketException ioe) {
        }
        SOCKSServer serv = SOCKSServerFactory.createSOCKSServer(_context, s, getTunnel().getClientOptions());
        Socket clientSock = serv.getClientSocket();
        try {
            s.setSoTimeout(0);
        } catch (SocketException ioe) {
        }
        destSock = serv.getDestinationI2PSocket(this);
        Thread t = new I2PTunnelRunner(clientSock, destSock, sockLock, null, null, mySockets, (I2PTunnelRunner.FailCallback) null);
        // we are called from an unlimited thread pool, so run inline
        // t.start();
        t.run();
    } catch (SOCKSException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Error from SOCKS connection", e);
    } finally {
        // only because we are running it inline
        closeSocket(s);
        if (destSock != null)
            try {
                destSock.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : SocketException(java.net.SocketException) I2PSocket(net.i2p.client.streaming.I2PSocket) SOCKSException(net.i2p.socks.SOCKSException) I2PTunnelRunner(net.i2p.i2ptunnel.I2PTunnelRunner) IOException(java.io.IOException) Socket(java.net.Socket) I2PSocket(net.i2p.client.streaming.I2PSocket)

Example 2 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class SOCKS4aServer method setupServer.

protected void setupServer() throws SOCKSException {
    if (setupCompleted) {
        return;
    }
    DataInputStream in;
    DataOutputStream out;
    try {
        in = new DataInputStream(clientSock.getInputStream());
        out = new DataOutputStream(clientSock.getOutputStream());
        manageRequest(in, out);
    } catch (IOException e) {
        throw new SOCKSException("Connection error", e);
    }
    setupCompleted = true;
}
Also used : DataOutputStream(java.io.DataOutputStream) SOCKSException(net.i2p.socks.SOCKSException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 3 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class SOCKS4aServer method getDestinationI2PSocket.

/**
 * Get an I2PSocket that can be used to send/receive 8-bit clean data
 * to/from the destination of the SOCKS connection.
 *
 * @return an I2PSocket connected with the destination
 */
public I2PSocket getDestinationI2PSocket(I2PSOCKSTunnel t) throws SOCKSException {
    setupServer();
    if (connHostName == null) {
        _log.error("BUG: destination host name has not been initialized!");
        throw new SOCKSException("BUG! See the logs!");
    }
    if (connPort == 0) {
        _log.error("BUG: destination port has not been initialized!");
        throw new SOCKSException("BUG! See the logs!");
    }
    // for errors
    DataOutputStream out;
    try {
        out = new DataOutputStream(clientSock.getOutputStream());
    } catch (IOException e) {
        throw new SOCKSException("Connection error", e);
    }
    // FIXME: here we should read our config file, select an
    // outproxy, and instantiate the proper socket class that
    // handles the outproxy itself (SOCKS4a, SOCKS4a, HTTP CONNECT...).
    I2PSocket destSock;
    try {
        if (connHostName.toLowerCase(Locale.US).endsWith(".i2p")) {
            Destination dest = _context.namingService().lookup(connHostName);
            if (dest == null) {
                try {
                    sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                } catch (IOException ioe) {
                }
                throw new SOCKSException("Host not found");
            }
            if (_log.shouldDebug())
                _log.debug("connecting to " + connHostName + "...");
            Properties overrides = new Properties();
            I2PSocketOptions sktOpts = t.buildOptions(overrides);
            sktOpts.setPort(connPort);
            destSock = t.createI2PSocket(dest, sktOpts);
        } else if ("localhost".equals(connHostName) || "127.0.0.1".equals(connHostName)) {
            String err = "No localhost accesses allowed through the Socks Proxy";
            _log.error(err);
            try {
                sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
            } catch (IOException ioe) {
            }
            throw new SOCKSException(err);
        /**
         **
         *            } else if (connPort == 80) {
         *                // rewrite GET line to include hostname??? or add Host: line???
         *                // or forward to local eepProxy (but that's a Socket not an I2PSocket)
         *                // use eepProxy configured outproxies?
         *                String err = "No handler for HTTP outproxy implemented - to: " + connHostName;
         *                _log.error(err);
         *                try {
         *                    sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
         *                } catch (IOException ioe) {}
         *                throw new SOCKSException(err);
         ***
         */
        } else {
            Outproxy outproxy = getOutproxyPlugin();
            if (outproxy != null) {
                try {
                    destSock = new SocketWrapper(outproxy.connect(connHostName, connPort));
                } catch (IOException ioe) {
                    try {
                        sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                    } catch (IOException ioe2) {
                    }
                    throw new SOCKSException("connect failed via outproxy plugin", ioe);
                }
            } else {
                List<String> proxies = t.getProxies(connPort);
                if (proxies == null || proxies.isEmpty()) {
                    String err = "No outproxy configured for port " + connPort + " and no default configured either - host: " + connHostName;
                    _log.error(err);
                    try {
                        sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                    } catch (IOException ioe) {
                    }
                    throw new SOCKSException(err);
                }
                int p = _context.random().nextInt(proxies.size());
                String proxy = proxies.get(p);
                Destination dest = _context.namingService().lookup(proxy);
                if (dest == null) {
                    try {
                        sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
                    } catch (IOException ioe) {
                    }
                    throw new SOCKSException("Outproxy not found");
                }
                if (_log.shouldDebug())
                    _log.debug("connecting to port " + connPort + " proxy " + proxy + " for " + connHostName + "...");
                // this isn't going to work, these need to be socks outproxies so we need
                // to do a socks session to them?
                destSock = t.createI2PSocket(dest);
            }
        }
        confirmConnection();
        _log.debug("connection confirmed - exchanging data...");
    } catch (DataFormatException e) {
        try {
            sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Error in destination format", e);
    } catch (IOException e) {
        try {
            sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Error connecting", e);
    } catch (I2PException e) {
        try {
            sendRequestReply(Reply.CONNECTION_REFUSED, InetAddress.getByName("127.0.0.1"), 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Error connecting", e);
    }
    return destSock;
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) DataOutputStream(java.io.DataOutputStream) SOCKSException(net.i2p.socks.SOCKSException) I2PSocket(net.i2p.client.streaming.I2PSocket) Outproxy(net.i2p.app.Outproxy) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions) IOException(java.io.IOException) Properties(java.util.Properties) DataFormatException(net.i2p.data.DataFormatException) List(java.util.List)

Example 4 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class SOCKS5Server method manageRequest.

/**
 * SOCKS5 request management.  This method assumes that all the
 * stuff preceding or enveloping the actual request (e.g. protocol
 * initialization, integrity/confidentiality encapsulations, etc)
 * has been stripped out of the input/output streams.
 */
private int manageRequest(DataInputStream in, DataOutputStream out) throws IOException {
    int socksVer = in.readUnsignedByte();
    if (socksVer != SOCKS_VERSION_5) {
        _log.debug("error in SOCKS5 request (protocol != 5?)");
        throw new SOCKSException("Invalid protocol version in request: " + socksVer);
    }
    int command = in.readUnsignedByte();
    switch(command) {
        case Command.CONNECT:
            break;
        case Command.BIND:
            _log.debug("BIND command is not supported!");
            sendRequestReply(Reply.COMMAND_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
            throw new SOCKSException("BIND command not supported");
        case Command.UDP_ASSOCIATE:
            /**
             * if(!Boolean.parseBoolean(tunnel.getOptions().getProperty("i2ptunnel.socks.allowUDP"))) {
             *            _log.debug("UDP ASSOCIATE command is not supported!");
             *            sendRequestReply(Reply.COMMAND_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
             *            throw new SOCKSException("UDP ASSOCIATE command not supported");
             **
             */
            break;
        default:
            _log.debug("unknown command in request (" + Integer.toHexString(command) + ")");
            sendRequestReply(Reply.COMMAND_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
            throw new SOCKSException("Invalid command in request");
    }
    // Reserved byte, should be 0x00
    in.readByte();
    addressType = in.readUnsignedByte();
    switch(addressType) {
        case AddressType.IPV4:
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 4; ++i) {
                int octet = in.readUnsignedByte();
                builder.append(Integer.toString(octet));
                if (i != 3) {
                    builder.append(".");
                }
            }
            connHostName = builder.toString();
            // Check if the requested IP should be mapped to a domain name
            String mappedDomainName = getMappedDomainNameForIP(connHostName);
            if (mappedDomainName != null) {
                _log.debug("IPV4 address " + connHostName + " was mapped to domain name " + mappedDomainName);
                addressType = AddressType.DOMAINNAME;
                connHostName = mappedDomainName;
            } else if (command != Command.UDP_ASSOCIATE)
                _log.warn("IPV4 address type in request: " + connHostName + ". Is your client secure?");
            break;
        case AddressType.DOMAINNAME:
            {
                int addrLen = in.readUnsignedByte();
                if (addrLen == 0) {
                    _log.debug("0-sized address length?");
                    throw new SOCKSException("Illegal DOMAINNAME length");
                }
                byte[] addr = new byte[addrLen];
                in.readFully(addr);
                connHostName = DataHelper.getUTF8(addr);
            }
            _log.debug("DOMAINNAME address type in request: " + connHostName);
            break;
        case AddressType.IPV6:
            if (command != Command.UDP_ASSOCIATE) {
                _log.warn("IP V6 address type in request! Is your client secure?" + " (IPv6 is not supported, anyway :-)");
                sendRequestReply(Reply.ADDRESS_TYPE_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
                throw new SOCKSException("IPV6 addresses not supported");
            }
            break;
        default:
            _log.debug("unknown address type in request (" + Integer.toHexString(command) + ")");
            sendRequestReply(Reply.ADDRESS_TYPE_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
            throw new SOCKSException("Invalid addresses type in request");
    }
    connPort = in.readUnsignedShort();
    if (connPort == 0) {
        _log.debug("trying to connect to TCP port 0?  Dropping!");
        sendRequestReply(Reply.CONNECTION_NOT_ALLOWED_BY_RULESET, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
        throw new SOCKSException("Invalid port number in request");
    }
    return command;
}
Also used : SOCKSException(net.i2p.socks.SOCKSException)

Example 5 with SOCKSException

use of net.i2p.socks.SOCKSException in project i2p.i2p by i2p.

the class SOCKS5Server method confirmConnection.

protected void confirmConnection() throws SOCKSException {
    DataOutputStream out;
    try {
        out = new DataOutputStream(clientSock.getOutputStream());
        sendRequestReply(Reply.SUCCEEDED, AddressType.IPV4, InetAddress.getByName("127.0.0.1"), null, 1, out);
    } catch (IOException e) {
        throw new SOCKSException("Connection error", e);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) SOCKSException(net.i2p.socks.SOCKSException) IOException(java.io.IOException)

Aggregations

SOCKSException (net.i2p.socks.SOCKSException)12 IOException (java.io.IOException)11 DataOutputStream (java.io.DataOutputStream)8 I2PSocket (net.i2p.client.streaming.I2PSocket)5 DataInputStream (java.io.DataInputStream)4 Properties (java.util.Properties)3 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)3 Destination (net.i2p.data.Destination)3 Socket (java.net.Socket)2 SocketException (java.net.SocketException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 I2PException (net.i2p.I2PException)2 Outproxy (net.i2p.app.Outproxy)2 DataFormatException (net.i2p.data.DataFormatException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1