Search in sources :

Example 11 with I2PSocketOptions

use of net.i2p.client.streaming.I2PSocketOptions in project i2p.i2p by i2p.

the class SOCKS5Server 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, SOCKS5, HTTP CONNECT...).
    I2PSocket destSock;
    try {
        if (connHostName.toLowerCase(Locale.US).endsWith(".i2p")) {
            // Let's not do a new Dest for every request, huh?
            // I2PSocketManager sm = I2PSocketManagerFactory.createManager();
            // destSock = sm.connect(I2PTunnel.destFromName(connHostName), null);
            Destination dest = _context.namingService().lookup(connHostName);
            if (dest == null) {
                try {
                    sendRequestReply(Reply.HOST_UNREACHABLE, AddressType.DOMAINNAME, null, "0.0.0.0", 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_NOT_ALLOWED_BY_RULESET, AddressType.DOMAINNAME, null, "0.0.0.0", 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";
         *                _log.error(err);
         *                try {
         *                    sendRequestReply(Reply.CONNECTION_NOT_ALLOWED_BY_RULESET, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
         *                } catch (IOException ioe) {}
         *                throw new SOCKSException(err);
         ***
         */
        } else {
            Outproxy outproxy = getOutproxyPlugin();
            if (outproxy != null) {
                // but here, we wrap a Socket in a I2PSocket and use the regular Runner.
                try {
                    destSock = new SocketWrapper(outproxy.connect(connHostName, connPort));
                } catch (IOException ioe) {
                    try {
                        sendRequestReply(Reply.HOST_UNREACHABLE, AddressType.DOMAINNAME, null, "0.0.0.0", 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";
                    _log.error(err);
                    try {
                        sendRequestReply(Reply.CONNECTION_NOT_ALLOWED_BY_RULESET, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
                    } catch (IOException ioe) {
                    }
                    throw new SOCKSException(err);
                }
                int p = _context.random().nextInt(proxies.size());
                String proxy = proxies.get(p);
                if (_log.shouldLog(Log.DEBUG))
                    _log.debug("connecting to proxy " + proxy + " for " + connHostName + " port " + connPort);
                try {
                    destSock = outproxyConnect(t, proxy);
                } catch (SOCKSException se) {
                    try {
                        sendRequestReply(Reply.HOST_UNREACHABLE, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
                    } catch (IOException ioe) {
                    }
                    throw se;
                }
            }
        }
        confirmConnection();
        _log.debug("connection confirmed - exchanging data...");
    } catch (DataFormatException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("socks error", e);
        try {
            sendRequestReply(Reply.HOST_UNREACHABLE, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Error in destination format");
    } catch (IOException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("socks error", e);
        try {
            sendRequestReply(Reply.HOST_UNREACHABLE, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Connection error", e);
    } catch (I2PException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("socks error", e);
        try {
            sendRequestReply(Reply.HOST_UNREACHABLE, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
        } catch (IOException ioe) {
        }
        throw new SOCKSException("Connection error", 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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with I2PSocketOptions

use of net.i2p.client.streaming.I2PSocketOptions in project i2p.i2p by i2p.

the class StandardSocket method setSoTimeout.

@Override
public void setSoTimeout(int timeout) throws SocketException {
    I2PSocketOptions opts = _socket.getOptions();
    if (opts == null)
        throw new SocketException("No options");
    // Java Socket: 0 is forever
    if (timeout == 0)
        timeout = -1;
    opts.setReadTimeout(timeout);
}
Also used : SocketException(java.net.SocketException) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions)

Example 13 with I2PSocketOptions

use of net.i2p.client.streaming.I2PSocketOptions in project i2p.i2p by i2p.

the class StandardSocket method getSoTimeout.

@Override
public int getSoTimeout() {
    I2PSocketOptions opts = _socket.getOptions();
    if (opts == null)
        return 0;
    long rv = opts.getReadTimeout();
    // Java Socket: 0 is forever, and we don't exactly have nonblocking
    if (rv > Integer.MAX_VALUE)
        rv = Integer.MAX_VALUE;
    else if (rv < 0)
        rv = 0;
    else if (rv == 0)
        rv = 1;
    return (int) rv;
}
Also used : I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions)

Example 14 with I2PSocketOptions

use of net.i2p.client.streaming.I2PSocketOptions in project i2p.i2p by i2p.

the class I2PTunnelClientBase method getDefaultOptions.

/**
 * Create the default options (using the default timeout, etc).
 * Warning, this does not make a copy of I2PTunnel's client options,
 * it modifies them directly.
 * Do not use overrides for per-socket options.
 */
protected I2PSocketOptions getDefaultOptions(Properties overrides) {
    Properties defaultOpts = getTunnel().getClientOptions();
    defaultOpts.putAll(overrides);
    I2PSocketOptions opts = sockMgr.buildOptions(defaultOpts);
    if (!defaultOpts.containsKey(I2PSocketOptions.PROP_CONNECT_TIMEOUT))
        opts.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
    return opts;
}
Also used : I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions) Properties(java.util.Properties)

Example 15 with I2PSocketOptions

use of net.i2p.client.streaming.I2PSocketOptions in project i2p.i2p by i2p.

the class I2PTunnelClientBase method createI2PSocket.

/**
 * Create a new I2PSocket towards to the specified destination,
 * adding it to the list of connections actually managed by this
 * tunnel.
 *
 * @param dest The destination to connect to, non-null
 * @param port The destination port to connect to 0 - 65535
 * @return a new I2PSocket
 * @since 0.9.9
 */
public I2PSocket createI2PSocket(Destination dest, int port) throws I2PException, ConnectException, NoRouteToHostException, InterruptedIOException {
    verifySocketManager();
    I2PSocketOptions opts = getDefaultOptions();
    opts.setPort(port);
    return createI2PSocket(dest, opts);
}
Also used : I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions)

Aggregations

I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)18 Properties (java.util.Properties)10 Destination (net.i2p.data.Destination)9 I2PSocket (net.i2p.client.streaming.I2PSocket)8 IOException (java.io.IOException)6 I2PException (net.i2p.I2PException)6 Outproxy (net.i2p.app.Outproxy)4 DataOutputStream (java.io.DataOutputStream)3 OutputStream (java.io.OutputStream)3 SOCKSException (net.i2p.socks.SOCKSException)3 InputStream (java.io.InputStream)2 Socket (java.net.Socket)2 List (java.util.List)2 ClientApp (net.i2p.app.ClientApp)2 ClientAppManager (net.i2p.app.ClientAppManager)2 DataFormatException (net.i2p.data.DataFormatException)2 I2PAppThread (net.i2p.util.I2PAppThread)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 InterruptedIOException (java.io.InterruptedIOException)1