use of net.i2p.I2PException in project i2p.i2p by i2p.
the class DoCMDS method run.
/**
* The actual parser.
* It probabbly needs a rewrite into functions, but I kind-of like inline code.
*/
public void run() {
dk = ns = ip = op = false;
try {
try {
// Get input from the client
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintStream out = new PrintStream(server.getOutputStream());
quit: {
die: {
prikey = new ByteArrayOutputStream();
out.println("BOB " + BOBversion);
out.println("OK");
while ((line = in.readLine()) != null) {
// use a space as a delimiter
StringTokenizer token = new StringTokenizer(line, " ");
String Command = "";
String Arg = "";
NamedDB info;
if (token.countTokens() != 0) {
Command = token.nextToken();
Command = Command.toLowerCase(Locale.US);
if (token.countTokens() != 0) {
Arg = token.nextToken();
} else {
Arg = "";
}
// and discarded without any warnings.
if (Command.equals(C_help)) {
for (int i = 0; !C_ALL[i][0].equals(" "); i++) {
if (C_ALL[i][0].equalsIgnoreCase(Arg)) {
out.println("OK " + C_ALL[i][1]);
}
}
} else if (Command.equals(C_visit)) {
visitAllThreads();
out.println("OK ");
} else if (Command.equals(C_lookup)) {
Destination dest = null;
String reply = null;
if (Arg.endsWith(".i2p")) {
try {
// try {
// dest = I2PTunnel.destFromName(Arg);
// } catch (DataFormatException ex) {
// }
dest = I2PAppContext.getGlobalContext().namingService().lookup(Arg);
if (dest != null) {
reply = dest.toBase64();
}
} catch (NullPointerException npe) {
// Could not find the destination!?
}
}
if (reply == null) {
out.println("ERROR Address Not found.");
} else {
out.println("OK " + reply);
}
} else if (Command.equals(C_getdest)) {
if (ns) {
if (dk) {
rlock();
try {
out.println("OK " + nickinfo.get(P_DEST));
} catch (Exception e) {
break die;
} finally {
runlock();
}
} else {
out.println("ERROR keys not set.");
}
} else {
nns(out);
}
} else if (Command.equals(C_list)) {
// Produce a formatted list of all nicknames
database.getReadLock();
try {
for (Object ndb : database.values()) {
try {
info = (NamedDB) ndb;
out.print("DATA");
} catch (Exception e) {
break die;
}
nickprint(out, info);
}
} finally {
database.releaseReadLock();
}
out.println("OK Listing done");
} else if (Command.equals(C_quit)) {
// End the command session
break quit;
} else if (Command.equals(C_zap)) {
// Kill BOB!! (let's hope this works!)
LIVE.set(false);
// End the command session
break quit;
} else if (Command.equals(C_newkeys)) {
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
try {
// Make a new PublicKey and PrivateKey
prikey = new ByteArrayOutputStream();
d = I2PClientFactory.createClient().createDestination(prikey);
wlock();
try {
nickinfo.add(P_KEYS, prikey.toByteArray());
nickinfo.add(P_DEST, d.toBase64());
out.println("OK " + nickinfo.get(P_DEST));
} catch (Exception e) {
break die;
} finally {
wunlock();
}
dk = true;
} catch (I2PException ipe) {
_log.error("Error generating keys", ipe);
out.println("ERROR generating keys");
}
}
} catch (Exception e) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_getkeys)) {
// Return public key
if (dk) {
prikey = new ByteArrayOutputStream();
rlock();
try {
prikey.write(((byte[]) nickinfo.get(P_KEYS)));
} catch (Exception ex) {
break die;
} finally {
runlock();
}
out.println("OK " + net.i2p.data.Base64.encode(prikey.toByteArray()));
} else {
out.println("ERROR no public key has been set");
}
} else if (Command.equals(C_quiet)) {
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
wlock();
try {
nickinfo.add(P_QUIET, Boolean.valueOf(Arg));
} catch (Exception ex) {
break die;
} finally {
wunlock();
}
out.println("OK Quiet set");
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_verify)) {
if (is64ok(Arg)) {
out.println("OK");
} else {
out.println("ERROR not in BASE64 format");
}
} else if (Command.equals(C_setkeys)) {
// Set the NamedDB to a privatekey in BASE64 format
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
try {
prikey = new ByteArrayOutputStream();
prikey.write(net.i2p.data.Base64.decode(Arg));
d = new Destination();
d.fromBase64(Arg);
} catch (Exception ex) {
Arg = "";
}
if ((Arg.length() == 884) && is64ok(Arg)) {
wlock();
try {
nickinfo.add(P_KEYS, prikey.toByteArray());
nickinfo.add(P_DEST, d.toBase64());
out.println("OK " + nickinfo.get(P_DEST));
} catch (Exception ex) {
break die;
} finally {
wunlock();
}
dk = true;
} else {
out.println("ERROR not in BASE64 format");
}
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_setnick)) {
ns = dk = ip = op = false;
database.getReadLock();
try {
nickinfo = (NamedDB) database.get(Arg);
if (!tunnelactive(nickinfo)) {
nickinfo = null;
ns = true;
}
} catch (Exception b) {
nickinfo = null;
ns = true;
} finally {
database.releaseReadLock();
}
// Clears and Sets the initial NamedDB structure to work with
if (ns) {
nickinfo = new NamedDB();
wlock();
try {
database.add(Arg, nickinfo);
nickinfo.add(P_NICKNAME, Arg);
nickinfo.add(P_STARTING, Boolean.FALSE);
nickinfo.add(P_RUNNING, Boolean.FALSE);
nickinfo.add(P_STOPPING, Boolean.FALSE);
nickinfo.add(P_QUIET, Boolean.FALSE);
nickinfo.add(P_INHOST, "localhost");
nickinfo.add(P_OUTHOST, "localhost");
Properties Q = new Properties();
Lifted.copyProperties(this.props, Q);
Q.setProperty("inbound.nickname", Arg);
Q.setProperty("outbound.nickname", Arg);
nickinfo.add(P_PROPERTIES, Q);
} catch (Exception e) {
break die;
} finally {
wunlock();
}
out.println("OK Nickname set to " + Arg);
} else {
out.println("ERROR tunnel is active");
}
} else if (Command.equals(C_option)) {
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
// use an equal sign as a delimiter
StringTokenizer otoken = new StringTokenizer(Arg, "=");
if (otoken.countTokens() != 2) {
out.println("ERROR too many or no options.");
} else {
String pname = otoken.nextToken();
String pval = otoken.nextToken();
wlock();
try {
Properties Q = (Properties) nickinfo.get(P_PROPERTIES);
Q.setProperty(pname, pval);
nickinfo.add(P_PROPERTIES, Q);
} catch (Exception ex) {
break die;
} finally {
wunlock();
}
out.println("OK " + pname + " set to " + pval);
}
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_getnick)) {
// Get the NamedDB to work with...
boolean nsfail = false;
database.getReadLock();
try {
nickinfo = (NamedDB) database.get(Arg);
ns = true;
} catch (RuntimeException b) {
nsfail = true;
nns(out);
} finally {
database.releaseReadLock();
}
if (ns && !nsfail) {
rlock();
try {
dk = nickinfo.exists(P_KEYS);
ip = nickinfo.exists(P_INPORT);
op = nickinfo.exists(P_OUTPORT);
} catch (Exception ex) {
break die;
} finally {
runlock();
}
// Finally say OK.
out.println("OK Nickname set to " + Arg);
}
} else if (Command.equals(C_inport)) {
// app --> BOB
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
int prt;
wlock();
try {
nickinfo.kill(P_INPORT);
prt = Integer.parseInt(Arg);
if (prt > 1 && prt < 65536) {
try {
nickinfo.add(P_INPORT, Integer.valueOf(prt));
} catch (Exception ex) {
break die;
}
}
ip = nickinfo.exists(P_INPORT);
} catch (NumberFormatException nfe) {
out.println("ERROR not a number");
} finally {
wunlock();
}
if (ip) {
out.println("OK inbound port set");
} else {
out.println("ERROR port out of range");
}
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_outport)) {
// BOB --> app
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
int prt;
wlock();
try {
nickinfo.kill(P_OUTPORT);
prt = Integer.parseInt(Arg);
if (prt > 1 && prt < 65536) {
nickinfo.add(P_OUTPORT, Integer.valueOf(prt));
}
ip = nickinfo.exists(P_OUTPORT);
} catch (NumberFormatException nfe) {
out.println("ERROR not a number");
} catch (Exception ex) {
break die;
} finally {
wunlock();
}
if (ip) {
out.println("OK outbound port set");
} else {
out.println("ERROR port out of range");
}
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_inhost)) {
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
wlock();
try {
nickinfo.add(P_INHOST, Arg);
} catch (Exception ex) {
break die;
} finally {
wunlock();
}
out.println("OK inhost set");
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_outhost)) {
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
wlock();
try {
nickinfo.add(P_OUTHOST, Arg);
} catch (Exception ex) {
break die;
} finally {
wunlock();
}
out.println("OK outhost set");
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_show)) {
// Get the current NamedDB properties
if (ns) {
out.print("OK");
nickprint(out, nickinfo);
} else {
nns(out);
}
} else if (Command.equals(C_show_props)) {
// Get the current options properties
if (ns) {
out.print("OK");
propprint(out, nickinfo);
} else {
nns(out);
}
} else if (Command.equals(C_start)) {
// Start the tunnel, if we have all the information
if (ns && dk && (ip || op)) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
MUXlisten tunnel;
try {
while (!lock.compareAndSet(false, true)) {
// wait
}
tunnel = new MUXlisten(lock, database, nickinfo, _log);
Thread t = new I2PAppThread(tunnel);
t.start();
// try {
// Thread.sleep(1000 * 10); // Slow down the startup.
// } catch(InterruptedException ie) {
// // ignore it
// }
out.println("OK tunnel starting");
} catch (I2PException e) {
lock.set(false);
out.println("ERROR starting tunnel: " + e);
} catch (IOException e) {
lock.set(false);
out.println("ERROR starting tunnel: " + e);
}
}
} catch (Exception ex) {
break die;
}
} else {
out.println("ERROR tunnel settings incomplete");
}
} else if (Command.equals(C_stop)) {
// Stop the tunnel, if it is running
if (ns) {
rlock();
boolean released = false;
try {
if (nickinfo.get(P_RUNNING).equals(Boolean.TRUE) && nickinfo.get(P_STOPPING).equals(Boolean.FALSE) && nickinfo.get(P_STARTING).equals(Boolean.FALSE)) {
runlock();
released = true;
wlock();
try {
nickinfo.add(P_STOPPING, Boolean.TRUE);
} catch (Exception e) {
break die;
} finally {
wunlock();
}
out.println("OK tunnel stopping");
} else {
out.println("ERROR tunnel is inactive");
}
} catch (Exception e) {
break die;
} finally {
if (!released)
runlock();
}
} else {
nns(out);
}
} else if (Command.equals(C_clear)) {
// Clear use of the NamedDB if stopped
if (ns) {
try {
if (tunnelactive(nickinfo)) {
out.println("ERROR tunnel is active");
} else {
database.getWriteLock();
try {
database.kill((String) nickinfo.get(P_NICKNAME));
} catch (Exception e) {
} finally {
database.releaseWriteLock();
}
dk = ns = ip = op = false;
out.println("OK cleared");
}
} catch (Exception ex) {
break die;
}
} else {
nns(out);
}
} else if (Command.equals(C_status)) {
database.getReadLock();
try {
if (database.exists(Arg)) {
// Show status of a NamedDB
out.print("OK ");
try {
ttlpnt(out, Arg);
} catch (Exception e) {
// this will cause an IOE if IOE
out.println();
break die;
}
} else {
nns(out);
}
} catch (Exception e) {
break die;
} finally {
database.releaseReadLock();
}
} else {
out.println("ERROR UNKNOWN COMMAND! Try help");
}
}
}
}
// die
out.print("ERROR A really bad error just happened, ");
}
// quit
// Say goodbye.
out.println("OK Bye!");
} catch (IOException ioe) {
// not really needed, except to debug.
// BOB.warn("IOException on socket listen: " + ioe);
// ioe.printStackTrace();
}
} finally {
try {
server.close();
} catch (IOException ex) {
// nop
}
}
}
use of net.i2p.I2PException in project i2p.i2p by i2p.
the class ConnectionAcceptor method run2.
private void run2() {
while (!stop) {
I2PServerSocket serverSocket = _util.getServerSocket();
while ((serverSocket == null) && (!stop)) {
if (!(_util.isConnecting() || _util.connected())) {
stop = true;
break;
}
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException ie) {
}
serverSocket = _util.getServerSocket();
}
if (stop)
break;
try {
I2PSocket socket = serverSocket.accept();
if (socket == null) {
continue;
} else {
if (socket.getPeerDestination().equals(_util.getMyDestination())) {
_log.error("Incoming connection from myself");
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
Hash h = socket.getPeerDestination().calculateHash();
if (socket.getLocalPort() == 80) {
_badCounter.increment(h);
if (_log.shouldLog(Log.WARN))
_log.error("Dropping incoming HTTP from " + h);
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
int bad = _badCounter.count(h);
if (bad >= MAX_BAD) {
if (_log.shouldLog(Log.WARN))
_log.warn("Rejecting connection from " + h + " after " + bad + " failures, max is " + MAX_BAD);
try {
socket.close();
} catch (IOException ioe) {
}
continue;
}
Thread t = new I2PAppThread(new Handler(socket), "I2PSnark incoming connection");
t.start();
}
} catch (I2PException ioe) {
int level = stop ? Log.WARN : Log.ERROR;
if (_log.shouldLog(level))
_log.log(level, "Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
} catch (ConnectException ioe) {
// which does not currently call our halt(), although it should
if (_log.shouldWarn())
_log.warn("Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
} catch (IOException ioe) {
int level = stop ? Log.WARN : Log.ERROR;
if (_log.shouldLog(level))
_log.log(level, "Error while accepting", ioe);
synchronized (this) {
if (!stop) {
locked_halt();
thread = null;
stop = true;
}
}
}
// catch oom?
}
if (_log.shouldLog(Log.WARN))
_log.warn("ConnectionAcceptor closed");
}
use of net.i2p.I2PException in project i2p.i2p by i2p.
the class ConnectionAcceptor method locked_halt.
/**
* Caller must synch
* @since 0.9.9
*/
private void locked_halt() {
I2PServerSocket ss = _util.getServerSocket();
if (ss != null) {
try {
ss.close();
} catch (I2PException ioe) {
}
}
_badCounter.clear();
_cleaner.cancel();
}
use of net.i2p.I2PException in project i2p.i2p by i2p.
the class MUXlisten method run.
/**
* MUX sockets, fire off a thread to connect, get destination info, and do I/O
*/
public void run() {
I2PServerSocket SS = null;
Thread t = null;
Thread q = null;
try {
wlock();
try {
try {
info.add("RUNNING", Boolean.TRUE);
} catch (Exception e) {
lock.set(false);
return;
}
} catch (Exception e) {
lock.set(false);
return;
} finally {
wunlock();
}
lives.set(true);
lock.set(false);
quit: {
try {
tg = new ThreadGroup(N);
{
if (go_out) {
// I2P -> TCP
SS = socketManager.getServerSocket();
I2Plistener conn = new I2Plistener(SS, socketManager, info, database, _log, lives);
t = new I2PAppThread(tg, conn, "BOBI2Plistener " + N);
t.start();
}
if (come_in) {
// TCP -> I2P
TCPlistener conn = new TCPlistener(listener, socketManager, info, database, _log, lives);
q = new I2PAppThread(tg, conn, "BOBTCPlistener " + N);
q.start();
}
wlock();
try {
try {
info.add("STARTING", Boolean.FALSE);
} catch (Exception e) {
break quit;
}
} catch (Exception e) {
break quit;
} finally {
wunlock();
}
boolean spin = true;
while (spin && lives.get()) {
try {
// sleep for 1 second
Thread.sleep(1000);
} catch (InterruptedException e) {
break quit;
}
rlock();
try {
try {
spin = info.get("STOPPING").equals(Boolean.FALSE);
} catch (Exception e) {
break quit;
}
} catch (Exception e) {
break quit;
} finally {
runlock();
}
}
}
// die
} catch (Exception e) {
// System.out.println("MUXlisten: Caught an exception" + e);
break quit;
}
}
// quit
} finally {
lives.set(false);
// Some grace time.
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
try {
wlock();
try {
info.add("STARTING", Boolean.FALSE);
info.add("STOPPING", Boolean.TRUE);
info.add("RUNNING", Boolean.FALSE);
} catch (Exception e) {
lock.set(false);
return;
}
} catch (Exception e) {
} finally {
wunlock();
}
// Start cleanup.
while (!lock.compareAndSet(false, true)) {
// wait
}
if (SS != null) {
try {
SS.close();
} catch (I2PException ex) {
}
}
if (listener != null) {
try {
listener.close();
} catch (IOException e) {
}
}
// Some grace time.
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
// Hopefully nuke stuff here...
{
String groupName = tg.getName();
try {
_log.warn("destroySocketManager " + groupName);
socketManager.destroySocketManager();
_log.warn("destroySocketManager Successful" + groupName);
} catch (Exception e) {
// nop
_log.warn("destroySocketManager Failed" + groupName);
_log.warn(e.toString());
}
}
// zero out everything.
try {
wlock();
try {
info.add("STARTING", Boolean.FALSE);
info.add("STOPPING", Boolean.FALSE);
info.add("RUNNING", Boolean.FALSE);
} catch (Exception e) {
lock.set(false);
return;
} finally {
wunlock();
}
} catch (Exception e) {
}
// Should we force waiting for all threads??
lock.set(false);
// Wait around till all threads are collected.
if (tg != null) {
String groupName = tg.getName();
// System.out.println("BOB: MUXlisten: Starting thread collection for: " + groupName);
_log.warn("BOB: MUXlisten: Starting thread collection for: " + groupName);
if (tg.activeCount() + tg.activeGroupCount() != 0) {
// visit(tg, 0, groupName);
int foo = tg.activeCount() + tg.activeGroupCount();
// Happily spin forever :-(
while (foo != 0) {
foo = tg.activeCount() + tg.activeGroupCount();
// bar = lives;
try {
// sleep for 100 ms (One tenth second)
Thread.sleep(100);
} catch (InterruptedException ex) {
// nop
}
}
}
// System.out.println("BOB: MUXlisten: Threads went away. Success: " + groupName);
_log.warn("BOB: MUXlisten: Threads went away. Success: " + groupName);
tg.destroy();
// Zap reference to the ThreadGroup so the JVM can GC it.
tg = null;
}
try {
socketManager.destroySocketManager();
} catch (Exception e) {
// nop
}
}
}
use of net.i2p.I2PException in project i2p.i2p by i2p.
the class PrivateKeyFile method setSignedCert.
/**
* sign this dest by dest found in pkf2 - caller must also call write()
*/
public Certificate setSignedCert(PrivateKeyFile pkf2) {
Certificate c = setCertType(Certificate.CERTIFICATE_TYPE_SIGNED);
Destination d2;
try {
d2 = pkf2.getDestination();
} catch (I2PException e) {
return null;
} catch (IOException e) {
return null;
}
if (d2 == null)
return null;
SigningPrivateKey spk2 = pkf2.getSigningPrivKey();
System.out.println("Signing With Dest:");
System.out.println(pkf2.toString());
// no cert
int len = PublicKey.KEYSIZE_BYTES + SigningPublicKey.KEYSIZE_BYTES;
byte[] data = new byte[len];
System.arraycopy(this.dest.getPublicKey().getData(), 0, data, 0, PublicKey.KEYSIZE_BYTES);
System.arraycopy(this.dest.getSigningPublicKey().getData(), 0, data, PublicKey.KEYSIZE_BYTES, SigningPublicKey.KEYSIZE_BYTES);
byte[] payload = new byte[Hash.HASH_LENGTH + Signature.SIGNATURE_BYTES];
Signature sign = DSAEngine.getInstance().sign(new ByteArrayInputStream(data), spk2);
if (sign == null)
return null;
byte[] sig = sign.getData();
System.arraycopy(sig, 0, payload, 0, Signature.SIGNATURE_BYTES);
// Add dest2's Hash for reference
byte[] h2 = d2.calculateHash().getData();
System.arraycopy(h2, 0, payload, Signature.SIGNATURE_BYTES, Hash.HASH_LENGTH);
c.setCertificateType(Certificate.CERTIFICATE_TYPE_SIGNED);
c.setPayload(payload);
return c;
}
Aggregations