Search in sources :

Example 31 with DataFormatException

use of net.i2p.data.DataFormatException 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 32 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class ConvertToHash method getHash.

/**
 *  Convert any kind of destination String to a hash
 *
 *  @return null on failure
 */
public static Hash getHash(String peer) {
    if (peer == null)
        return null;
    String peerLC = peer.toLowerCase(Locale.US);
    // b64 hash
    if (peer.length() == 44 && !peerLC.endsWith(".i2p")) {
        byte[] b = Base64.decode(peer);
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // b64 hash.i2p
    if (peer.length() == 48 && peerLC.endsWith(".i2p")) {
        byte[] b = Base64.decode(peer.substring(0, 44));
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // b64 dest.i2p
    if (peer.length() >= 520 && peerLC.endsWith(".i2p")) {
        try {
            Destination d = new Destination();
            d.fromBase64(peer.substring(0, peer.length() - 4));
            return d.calculateHash();
        } catch (DataFormatException dfe) {
        }
    }
    // b64 dest
    if (peer.length() >= 516 && !peerLC.endsWith(".i2p")) {
        try {
            Destination d = new Destination();
            d.fromBase64(peer);
            return d.calculateHash();
        } catch (DataFormatException dfe) {
        }
    }
    // even if the leaseset is not found
    if (peer.length() == 60 && peerLC.endsWith(".b32.i2p")) {
        byte[] b = Base32.decode(peer.substring(0, 52));
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // b32 hash
    if (peer.length() == 52 && !peerLC.endsWith(".i2p")) {
        byte[] b = Base32.decode(peer);
        if (b != null && b.length == Hash.HASH_LENGTH)
            return Hash.create(b);
    }
    // example.i2p
    Destination d = I2PAppContext.getGlobalContext().namingService().lookup(peer);
    if (d != null)
        return d.calculateHash();
    return null;
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException)

Example 33 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class ReconfigureSessionMessage method doWriteMessage.

@Override
protected byte[] doWriteMessage() throws I2CPMessageException, IOException {
    if (_sessionId == null || _sessionConfig == null)
        throw new I2CPMessageException("Unable to write out the message as there is not enough data");
    ByteArrayOutputStream os = new ByteArrayOutputStream(64);
    try {
        _sessionId.writeBytes(os);
        _sessionConfig.writeBytes(os);
    } catch (DataFormatException dfe) {
        throw new I2CPMessageException("Error writing out the message data", dfe);
    }
    return os.toByteArray();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 34 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class ReportAbuseMessage method doWriteMessage.

@Override
protected byte[] doWriteMessage() throws I2CPMessageException, IOException {
    if ((_sessionId == null) || (_severity == null) || (_reason == null))
        throw new I2CPMessageException("Not enough information to construct the message");
    ByteArrayOutputStream os = new ByteArrayOutputStream(32);
    try {
        _sessionId.writeBytes(os);
        _severity.writeBytes(os);
        _reason.writeBytes(os);
        if (_messageId == null) {
            _messageId = new MessageId();
            _messageId.setMessageId(0);
        }
        _messageId.writeBytes(os);
    } catch (DataFormatException dfe) {
        throw new I2CPMessageException("Error writing out the message data", dfe);
    }
    return os.toByteArray();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 35 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class RequestLeaseSetMessage method doReadMessage.

@Override
protected void doReadMessage(InputStream in, int size) throws I2CPMessageException, IOException {
    try {
        _sessionId = new SessionId();
        _sessionId.readBytes(in);
        int numTunnels = (int) DataHelper.readLong(in, 1);
        _endpoints.clear();
        for (int i = 0; i < numTunnels; i++) {
            // Hash router = new Hash();
            // router.readBytes(in);
            Hash router = Hash.create(in);
            TunnelId tunnel = new TunnelId();
            tunnel.readBytes(in);
            _endpoints.add(new TunnelEndpoint(router, tunnel));
        }
        _end = DataHelper.readDate(in);
    } catch (DataFormatException dfe) {
        throw new I2CPMessageException("Unable to load the message data", dfe);
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Hash(net.i2p.data.Hash) TunnelId(net.i2p.data.TunnelId)

Aggregations

DataFormatException (net.i2p.data.DataFormatException)112 IOException (java.io.IOException)53 Destination (net.i2p.data.Destination)32 Properties (java.util.Properties)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 FileInputStream (java.io.FileInputStream)16 Hash (net.i2p.data.Hash)14 File (java.io.File)13 SigType (net.i2p.crypto.SigType)13 I2PSessionException (net.i2p.client.I2PSessionException)12 InputStream (java.io.InputStream)11 PrivateKey (net.i2p.data.PrivateKey)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 RouterInfo (net.i2p.data.router.RouterInfo)11 Signature (net.i2p.data.Signature)10 FileOutputStream (java.io.FileOutputStream)8 InterruptedIOException (java.io.InterruptedIOException)8 HashMap (java.util.HashMap)8 PublicKey (net.i2p.data.PublicKey)8