Search in sources :

Example 11 with SOCKSException

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

the class SOCKS5Server method handleUDP.

/**
 * We got a UDP associate command.
 * Loop here looking for more, never return normally,
 * or else I2PSocksTunnel will create a streaming lib connection.
 *
 * Do UDP Socks clients actually send more than one Associate request?
 * RFC 1928 isn't clear... maybe not.
 */
private void handleUDP(DataInputStream in, DataOutputStream out) throws SOCKSException {
    List<Integer> ports = new ArrayList<Integer>(1);
    synchronized (_startLock) {
        if (_tunnel == null) {
            // tunnel options?
            _tunnel = new SOCKSUDPTunnel(new I2PTunnel());
            _tunnel.startRunning();
        }
    }
    while (true) {
        // Set it up. connHostName and connPort are the client's info.
        InetAddress ia = null;
        try {
            ia = InetAddress.getByAddress(connHostName, dummyIP);
        }// won't happen, no resolving done here
         catch (UnknownHostException uhe) {
        }
        int myPort = _tunnel.add(ia, connPort);
        ports.add(Integer.valueOf(myPort));
        try {
            sendRequestReply(Reply.SUCCEEDED, AddressType.IPV4, InetAddress.getByName("127.0.0.1"), null, myPort, out);
        } catch (IOException ioe) {
            break;
        }
        // wait for more ???
        try {
            int command = manageRequest(in, out);
            // don't do this...
            if (command != Command.UDP_ASSOCIATE)
                break;
        } catch (IOException ioe) {
            break;
        }
    }
    for (Integer i : ports) _tunnel.remove(i);
    // 
    throw new SOCKSException("End of UDP Processing");
}
Also used : I2PTunnel(net.i2p.i2ptunnel.I2PTunnel) UnknownHostException(java.net.UnknownHostException) SOCKSException(net.i2p.socks.SOCKSException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 12 with SOCKSException

use of net.i2p.socks.SOCKSException 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)

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