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;
}
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;
}
}
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);
}
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);
}
}
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);
}
Aggregations