use of net.i2p.util.I2PAppThread in project i2p.i2p by i2p.
the class BOB method startThread.
/**
* @since 0.9.10
*/
private void startThread() {
I2PAppThread t = new I2PAppThread(this, "BOBListener");
t.start();
_runner = t;
}
use of net.i2p.util.I2PAppThread 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.util.I2PAppThread in project i2p.i2p by i2p.
the class I2PtoTCP method run.
/**
* I2P stream to TCP stream thread starter
*/
public void run() {
String host;
int port;
boolean tell;
InputStream in = null;
OutputStream out = null;
InputStream Iin = null;
OutputStream Iout = null;
Thread t = null;
Thread q = null;
try {
die: {
try {
rlock();
try {
host = info.get("OUTHOST").toString();
port = Integer.parseInt(info.get("OUTPORT").toString());
tell = info.get("QUIET").equals(Boolean.FALSE);
} catch (Exception e) {
break die;
} finally {
runlock();
}
sock = new Socket(host, port);
sock.setKeepAlive(true);
// make readers/writers
in = sock.getInputStream();
out = sock.getOutputStream();
Iin = I2P.getInputStream();
Iout = I2P.getOutputStream();
// temp bugfix, this *SHOULD* be the default
I2P.setReadTimeout(0);
if (tell) {
// tell who is connecting
out.write(DataHelper.getASCII(I2P.getPeerDestination().toBase64()));
// nl
out.write(10);
// not really needed, but...
out.flush();
}
// setup to cross the streams
// app -> I2P
TCPio conn_c = new TCPio(in, Iout, lives);
// I2P -> app
TCPio conn_a = new TCPio(Iin, out, lives);
t = new I2PAppThread(conn_c, Thread.currentThread().getName() + " TCPioA");
q = new I2PAppThread(conn_a, Thread.currentThread().getName() + " TCPioB");
// Fire!
t.start();
q.start();
while (t.isAlive() && q.isAlive() && lives.get()) {
// AND is used here to kill off the other thread
try {
// sleep for 10 ms
Thread.sleep(10);
} catch (InterruptedException e) {
break die;
}
}
// System.out.println("I2PtoTCP: Going away...");
} catch (Exception e) {
// System.out.println("I2PtoTCP: Owch! damn!");
break die;
}
}
// die
} finally {
try {
in.close();
} catch (Exception ex) {
}
try {
out.close();
} catch (Exception ex) {
}
try {
Iin.close();
} catch (Exception ex) {
}
try {
Iout.close();
} catch (Exception ex) {
}
try {
t.interrupt();
} catch (Exception e) {
}
try {
q.interrupt();
} catch (Exception e) {
}
try {
// System.out.println("I2PtoTCP: Close I2P");
I2P.close();
} catch (Exception e) {
tell = false;
}
// System.out.println("I2PtoTCP: Closed I2P");
try {
// System.out.println("I2PtoTCP: Close sock");
sock.close();
} catch (Exception e) {
tell = false;
}
// System.out.println("I2PtoTCP: Done");
}
}
use of net.i2p.util.I2PAppThread 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.util.I2PAppThread in project i2p.i2p by i2p.
the class ConnectionAcceptor method startAccepting.
/**
* May be called even when already running. May be called to start up again after halt().
*/
public synchronized void startAccepting() {
stop = false;
if (_log.shouldLog(Log.WARN))
_log.warn("ConnectionAcceptor startAccepting new thread? " + (thread == null));
if (thread == null) {
thread = new I2PAppThread(this, "I2PSnark acceptor");
thread.setDaemon(true);
thread.start();
_cleaner.reschedule(BAD_CLEAN_INTERVAL, false);
}
}
Aggregations