Search in sources :

Example 11 with I2PClient

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

the class I2PTunnel method makeKey.

/**
 * Create a new destination, storing the destination and its private keys where
 * instructed.
 * Does NOT support non-default sig types.
 * Deprecated - only used by CLI
 *
 * @param writeTo location to store the destination and private keys
 * @param pubDest location to store the destination
 * @param l logger to send messages to
 */
private static void makeKey(OutputStream writeTo, OutputStream pubDest, Logging l) {
    try {
        l.log("Generating new keys...");
        I2PClient client = I2PClientFactory.createClient();
        Destination d = client.createDestination(writeTo);
        l.log("New destination: " + d.toBase32());
        writeTo.flush();
        writeTo.close();
        writePubKey(d, pubDest, l);
    } catch (I2PException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) I2PClient(net.i2p.client.I2PClient) IOException(java.io.IOException)

Example 12 with I2PClient

use of net.i2p.client.I2PClient 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 13 with I2PClient

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

the class DatagramTest method testDatagram.

public void testDatagram() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    I2PClient client = I2PClientFactory.createClient();
    Destination d = client.createDestination(out);
    I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null);
    I2PDatagramMaker dm = new I2PDatagramMaker(session);
    byte[] dg = dm.makeI2PDatagram(DataHelper.getASCII("What's the deal with 42?"));
    I2PDatagramDissector dd = new I2PDatagramDissector();
    dd.loadI2PDatagram(dg);
    byte[] x = dd.getPayload();
    assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?")));
    x = dd.extractPayload();
    assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?")));
    assertEquals(d, dd.getSender());
    assertEquals(d, dd.extractSender());
}
Also used : Destination(net.i2p.data.Destination) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSession(net.i2p.client.I2PSession) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with I2PClient

use of net.i2p.client.I2PClient 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 15 with I2PClient

use of net.i2p.client.I2PClient 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

I2PClient (net.i2p.client.I2PClient)16 I2PSession (net.i2p.client.I2PSession)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOException (java.io.IOException)7 Destination (net.i2p.data.Destination)7 Properties (java.util.Properties)6 I2PException (net.i2p.I2PException)6 I2PSessionException (net.i2p.client.I2PSessionException)4 I2PSimpleClient (net.i2p.client.I2PSimpleClient)3 File (java.io.File)2 SigType (net.i2p.crypto.SigType)2 DataFormatException (net.i2p.data.DataFormatException)2 OrderedProperties (net.i2p.util.OrderedProperties)2 Getopt (gnu.getopt.Getopt)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyException (java.security.KeyException)1