Search in sources :

Example 1 with I2PSimpleClient

use of net.i2p.client.I2PSimpleClient in project i2p.i2p by i2p.

the class I2PTunnel method destFromName.

/**
 *  @param i2cpHost may be null
 *  @param i2cpPort may be null
 *  @param user may be null
 *  @param pw may be null
 *  @since 0.9.11
 */
private static Destination destFromName(String name, String i2cpHost, String i2cpPort, boolean isSSL, String user, String pw) throws DataFormatException {
    if ((name == null) || (name.trim().length() <= 0))
        throw new DataFormatException("Empty destination provided");
    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    Log log = ctx.logManager().getLog(I2PTunnel.class);
    if (name.startsWith("file:")) {
        Destination result = new Destination();
        byte[] content = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(name.substring("file:".length()));
            byte[] buf = new byte[1024];
            int read = DataHelper.read(in, buf);
            content = new byte[read];
            System.arraycopy(buf, 0, content, 0, read);
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
            return null;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException io) {
                }
        }
        try {
            result.fromByteArray(content);
            return result;
        } catch (RuntimeException ex) {
            if (log.shouldLog(Log.INFO))
                log.info("File is not a binary destination - trying base64");
            try {
                byte[] decoded = Base64.decode(new String(content));
                result.fromByteArray(decoded);
                return result;
            } catch (DataFormatException dfe) {
                if (log.shouldLog(Log.WARN))
                    log.warn("File is not a base64 destination either - failing!");
                return null;
            }
        }
    } else {
        // ask naming service
        name = name.trim();
        NamingService inst = ctx.namingService();
        boolean b32 = name.length() == 60 && name.toLowerCase(Locale.US).endsWith(".b32.i2p");
        Destination d = null;
        if (ctx.isRouterContext() || !b32) {
            // Local lookup.
            // Even though we could do b32 outside router ctx here,
            // we do it below instead so we can set the host and port,
            // which we can't do with lookup()
            d = inst.lookup(name);
            if (d != null || ctx.isRouterContext() || name.length() >= 516)
                return d;
        }
        // Outside router context only,
        // try simple session to ask the router.
        I2PClient client = new I2PSimpleClient();
        Properties opts = new Properties();
        if (i2cpHost != null)
            opts.put(I2PClient.PROP_TCP_HOST, i2cpHost);
        if (i2cpPort != null)
            opts.put(I2PClient.PROP_TCP_PORT, i2cpPort);
        opts.put("i2cp.SSL", Boolean.toString(isSSL));
        if (user != null)
            opts.put("i2cp.username", user);
        if (pw != null)
            opts.put("i2cp.password", pw);
        I2PSession session = null;
        try {
            session = client.createSession(null, opts);
            session.connect();
            d = session.lookupDest(name);
        } catch (I2PSessionException ise) {
            if (log.shouldLog(Log.WARN))
                log.warn("Lookup via router failed", ise);
        } finally {
            if (session != null) {
                try {
                    session.destroySession();
                } catch (I2PSessionException ise) {
                }
            }
        }
        return d;
    }
}
Also used : Destination(net.i2p.data.Destination) I2PAppContext(net.i2p.I2PAppContext) Log(net.i2p.util.Log) IOException(java.io.IOException) OrderedProperties(net.i2p.util.OrderedProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) DataFormatException(net.i2p.data.DataFormatException) NamingService(net.i2p.client.naming.NamingService) I2PSimpleClient(net.i2p.client.I2PSimpleClient) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient)

Example 2 with I2PSimpleClient

use of net.i2p.client.I2PSimpleClient in project i2p.i2p by i2p.

the class BWLimits method getBWLimits.

public static int[] getBWLimits(String host, int port) {
    int[] rv = null;
    try {
        I2PClient client = new I2PSimpleClient();
        Properties opts = new Properties();
        opts.put(I2PClient.PROP_TCP_HOST, host);
        opts.put(I2PClient.PROP_TCP_PORT, "" + port);
        I2PSession session = client.createSession(null, opts);
        session.connect();
        rv = session.bandwidthLimits();
        session.destroySession();
    } catch (I2PSessionException ise) {
    }
    return rv;
}
Also used : I2PSimpleClient(net.i2p.client.I2PSimpleClient) I2PSession(net.i2p.client.I2PSession) I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient) Properties(java.util.Properties)

Example 3 with I2PSimpleClient

use of net.i2p.client.I2PSimpleClient in project i2p.i2p by i2p.

the class LookupDest method lookupHash.

/* Might be useful but not in the context of urls due to upper/lower case */
/**
 **
 *    static Destination lookupBase64Hash(I2PAppContext ctx, String key) {
 *        byte[] h = Base64.decode(key);
 *        if (h == null)
 *            return null;
 *        return lookupHash(ctx, h);
 *    }
 ***
 */
/**
 * @param h 32 byte hash
 */
static Destination lookupHash(I2PAppContext ctx, byte[] h) throws I2PSessionException {
    Hash key = Hash.create(h);
    Destination rv = null;
    I2PClient client = new I2PSimpleClient();
    Properties opts = new Properties();
    if (!ctx.isRouterContext()) {
        String s = ctx.getProperty(I2PClient.PROP_TCP_HOST);
        if (s != null)
            opts.put(I2PClient.PROP_TCP_HOST, s);
        s = ctx.getProperty(I2PClient.PROP_TCP_PORT);
        if (s != null)
            opts.put(I2PClient.PROP_TCP_PORT, s);
        s = ctx.getProperty(PROP_ENABLE_SSL);
        if (s != null)
            opts.put(PROP_ENABLE_SSL, s);
        s = ctx.getProperty(PROP_USER);
        if (s != null)
            opts.put(PROP_USER, s);
        s = ctx.getProperty(PROP_PW);
        if (s != null)
            opts.put(PROP_PW, s);
    }
    I2PSession session = null;
    try {
        session = client.createSession(null, opts);
        session.connect();
        rv = session.lookupDest(key, DEFAULT_TIMEOUT);
    } finally {
        if (session != null)
            session.destroySession();
    }
    return rv;
}
Also used : Destination(net.i2p.data.Destination) I2PSimpleClient(net.i2p.client.I2PSimpleClient) I2PSession(net.i2p.client.I2PSession) I2PClient(net.i2p.client.I2PClient) Hash(net.i2p.data.Hash) Properties(java.util.Properties)

Aggregations

Properties (java.util.Properties)3 I2PClient (net.i2p.client.I2PClient)3 I2PSession (net.i2p.client.I2PSession)3 I2PSimpleClient (net.i2p.client.I2PSimpleClient)3 I2PSessionException (net.i2p.client.I2PSessionException)2 Destination (net.i2p.data.Destination)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 I2PAppContext (net.i2p.I2PAppContext)1 NamingService (net.i2p.client.naming.NamingService)1 DataFormatException (net.i2p.data.DataFormatException)1 Hash (net.i2p.data.Hash)1 Log (net.i2p.util.Log)1 OrderedProperties (net.i2p.util.OrderedProperties)1