Search in sources :

Example 6 with I2PClient

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

the class StreamingITBase method createSession.

protected I2PSession createSession() throws Exception {
    I2PClient client = I2PClientFactory.createClient();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
    client.createDestination(baos);
    Properties p = getProperties();
    I2PSession sess = client.createSession(new ByteArrayInputStream(baos.toByteArray()), p);
    sess.connect();
    return sess;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSession(net.i2p.client.I2PSession) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties)

Example 7 with I2PClient

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

the class I2PSocketManagerFactory method createManager.

/**
 * Create a socket manager using a brand new destination connected to the
 * I2CP router on the given machine reachable through the given port.
 *
 * Blocks for a long time while the router builds tunnels.
 * The nonblocking createDisconnectedManager() is preferred.
 *
 * @param i2cpHost I2CP host null to use default, ignored if in router context
 * @param i2cpPort I2CP port <= 0 to use default, ignored if in router context
 * @param opts Streaming and I2CP options, may be null
 * @return the newly created socket manager, or null if there were errors
 */
public static I2PSocketManager createManager(String i2cpHost, int i2cpPort, Properties opts) {
    I2PClient client = I2PClientFactory.createClient();
    ByteArrayOutputStream keyStream = new ByteArrayOutputStream(1024);
    try {
        client.createDestination(keyStream, getSigType(opts));
        ByteArrayInputStream in = new ByteArrayInputStream(keyStream.toByteArray());
        return createManager(in, i2cpHost, i2cpPort, opts);
    } catch (IOException ioe) {
        getLog().error("Error creating the destination for socket manager", ioe);
        return null;
    } catch (I2PException ie) {
        getLog().error("Error creating the destination for socket manager", ie);
        return null;
    }
}
Also used : I2PException(net.i2p.I2PException) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 8 with I2PClient

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

the class I2PSocketManagerFactory method createDisconnectedManager.

/**
 * Create a disconnected socket manager using the destination loaded from the given private key
 * stream, or null for a transient destination.
 *
 * Non-blocking. Does not connect to the router or build tunnels.
 * For servers, caller MUST call getSession().connect() to build tunnels and start listening.
 * For clients, caller may do that to build tunnels in advance;
 * otherwise, the first call to connect() will initiate a connection to the router,
 * with significant delay for tunnel building.
 *
 * @param myPrivateKeyStream private key stream, format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
 *                           or null for a transient destination. Caller must close.
 * @param i2cpHost I2CP host null to use default, ignored if in router context
 * @param i2cpPort I2CP port <= 0 to use default, ignored if in router context
 * @param opts Streaming and I2CP options, may be null
 * @return the newly created socket manager, non-null (throws on error)
 * @since 0.9.8
 */
public static I2PSocketManager createDisconnectedManager(InputStream myPrivateKeyStream, String i2cpHost, int i2cpPort, Properties opts) throws I2PSessionException {
    if (myPrivateKeyStream == null) {
        I2PClient client = I2PClientFactory.createClient();
        ByteArrayOutputStream keyStream = new ByteArrayOutputStream(1024);
        try {
            client.createDestination(keyStream, getSigType(opts));
        } catch (I2PException e) {
            throw new I2PSessionException("Error creating keys", e);
        } catch (IOException e) {
            throw new I2PSessionException("Error creating keys", e);
        }
        myPrivateKeyStream = new ByteArrayInputStream(keyStream.toByteArray());
    }
    return createManager(myPrivateKeyStream, i2cpHost, i2cpPort, opts, false);
}
Also used : I2PException(net.i2p.I2PException) ByteArrayInputStream(java.io.ByteArrayInputStream) I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 9 with I2PClient

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

the class PrivateKeyFile method main.

/**
 *  Create a new PrivateKeyFile, or modify an existing one, with various
 *  types of Certificates.
 *
 *  Changing a Certificate does not change the public or private keys.
 *  But it does change the Destination Hash, which effectively makes it
 *  a new Destination. In other words, don't change the Certificate on
 *  a Destination you've already registered in a hosts.txt key add form.
 *
 *  Copied and expanded from that in Destination.java
 */
public static void main(String[] args) {
    int hashEffort = HASH_EFFORT;
    String stype = null;
    String hostname = null;
    int mode = 0;
    boolean error = false;
    Getopt g = new Getopt("pkf", args, "t:nuxhse:c:a:");
    int c;
    while ((c = g.getopt()) != -1) {
        switch(c) {
            case 'c':
                stype = g.getOptarg();
                break;
            case 't':
                stype = g.getOptarg();
            case 'n':
            case 'u':
            case 'x':
            case 'h':
            case 's':
                if (mode == 0)
                    mode = c;
                else
                    error = true;
                break;
            case 'a':
                hostname = g.getOptarg();
                if (mode == 0)
                    mode = c;
                else
                    error = true;
                break;
            case 'e':
                hashEffort = Integer.parseInt(g.getOptarg());
                break;
            case '?':
            case ':':
            default:
                error = true;
                break;
        }
    // switch
    }
    // while
    int remaining = args.length - g.getOptind();
    int reqd = mode == 's' ? 2 : 1;
    if (error || remaining != reqd) {
        usage();
        System.exit(1);
    }
    String filearg = args[g.getOptind()];
    I2PClient client = I2PClientFactory.createClient();
    try {
        File f = new File(filearg);
        boolean exists = f.exists();
        PrivateKeyFile pkf = new PrivateKeyFile(f, client);
        Destination d;
        if (stype != null) {
            SigType type = SigType.parseSigType(stype);
            if (type == null)
                throw new IllegalArgumentException("Signature type " + stype + " is not supported");
            d = pkf.createIfAbsent(type);
        } else {
            d = pkf.createIfAbsent();
        }
        if (exists)
            System.out.println("Original Destination:");
        else
            System.out.println("Created Destination:");
        System.out.println(pkf);
        verifySignature(d);
        switch(mode) {
            case 0:
                // we are done
                break;
            case 'n':
                // Cert constructor generates a null cert
                pkf.setCertType(Certificate.CERTIFICATE_TYPE_NULL);
                System.out.println("New destination with null cert is:");
                break;
            case 'u':
                pkf.setCertType(99);
                System.out.println("New destination with unknown cert is:");
                break;
            case 'x':
                pkf.setCertType(Certificate.CERTIFICATE_TYPE_HIDDEN);
                System.out.println("New destination with hidden cert is:");
                break;
            case 'h':
                System.out.println("Estimating hashcash generation time, stand by...");
                System.out.println(estimateHashCashTime(hashEffort));
                pkf.setHashCashCert(hashEffort);
                System.out.println("New destination with hashcash cert is:");
                break;
            case 's':
                // Sign dest1 with dest2's Signing Private Key
                PrivateKeyFile pkf2 = new PrivateKeyFile(args[g.getOptind() + 1]);
                pkf.setSignedCert(pkf2);
                System.out.println("New destination with signed cert is:");
                break;
            case 't':
                // KeyCert
                SigType type = SigType.parseSigType(stype);
                if (type == null)
                    throw new IllegalArgumentException("Signature type " + stype + " is not supported");
                pkf.setKeyCert(type);
                System.out.println("New destination with key cert is:");
                break;
            case 'a':
                // addressbook auth
                OrderedProperties props = new OrderedProperties();
                HostTxtEntry he = new HostTxtEntry(hostname, d.toBase64(), props);
                he.sign(pkf.getSigningPrivKey());
                System.out.println("Addressbook Authentication String:");
                OutputStreamWriter out = new OutputStreamWriter(System.out);
                he.write(out);
                out.flush();
                System.out.println("");
                return;
            default:
                // shouldn't happen
                usage();
                return;
        }
        if (mode != 0) {
            System.out.println(pkf);
            pkf.write();
            verifySignature(pkf.getDestination());
        }
    } catch (I2PException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : I2PException(net.i2p.I2PException) HostTxtEntry(net.i2p.client.naming.HostTxtEntry) IOException(java.io.IOException) SigType(net.i2p.crypto.SigType) Getopt(gnu.getopt.Getopt) I2PClient(net.i2p.client.I2PClient) OrderedProperties(net.i2p.util.OrderedProperties) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 10 with I2PClient

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

the class I2PTunnelUDPServerBase method init.

private void init(boolean verify, InputStream privData, String privkeyname, Logging l) {
    this.l = l;
    // create i2pclient
    I2PClient client = I2PClientFactory.createClient();
    try {
        // FIXME this may not pick up non-default I2CP host/port settings from tunnel
        _session = client.createSession(privData, getTunnel().getClientOptions());
        connected(_session);
    } catch (I2PSessionException exc) {
        throw new RuntimeException("failed to create session", exc);
    }
    // Setup the source. Always expect repliable datagrams, optionally verify
    _i2pSource = new I2PSource(_session, verify, false);
    // Setup the sink. Always send raw datagrams.
    _i2pSink = new I2PSinkAnywhere(_session, true);
}
Also used : I2PSessionException(net.i2p.client.I2PSessionException) I2PClient(net.i2p.client.I2PClient)

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