beginning of format, updated imports. (shendaras)
This commit is contained in:
@ -32,36 +32,42 @@ class ClientEngine {
|
|||||||
* @param config the Configuration to load configuration from =p
|
* @param config the Configuration to load configuration from =p
|
||||||
*/
|
*/
|
||||||
public ClientEngine(Heartbeat heartbeat, ClientConfig config) {
|
public ClientEngine(Heartbeat heartbeat, ClientConfig config) {
|
||||||
_heartbeat = heartbeat;
|
_heartbeat = heartbeat;
|
||||||
_data = new PeerData(config);
|
_data = new PeerData(config);
|
||||||
_active = false;
|
_active = false;
|
||||||
_id = ++__id;
|
_id = ++__id;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** stop sending any more pings or writing any more state */
|
/** stop sending any more pings or writing any more state */
|
||||||
public void stopEngine() {
|
public void stopEngine() {
|
||||||
_active = false;
|
_active = false;
|
||||||
if (_log.shouldLog(Log.INFO))
|
if (_log.shouldLog(Log.INFO))
|
||||||
_log.info("Stopping engine talking to peer " + _data.getConfig().getPeer().calculateHash().toBase64());
|
_log.info("Stopping engine talking to peer " + _data.getConfig().getPeer().calculateHash().toBase64());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** start up the test (this does not block, as it fires up the test thread) */
|
/** start up the test (this does not block, as it fires up the test thread) */
|
||||||
public void startEngine() {
|
public void startEngine() {
|
||||||
_active = true;
|
_active = true;
|
||||||
I2PThread t = new I2PThread(new ClientRunner());
|
I2PThread t = new I2PThread(new ClientRunner());
|
||||||
t.setName("HeartbeatClient " + _id);
|
t.setName("HeartbeatClient " + _id);
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Who are we testing?
|
* Who are we testing?
|
||||||
* @return the Destination (peer) we're testing
|
* @return the Destination (peer) we're testing
|
||||||
*/
|
*/
|
||||||
public Destination getPeer() { return _data.getConfig().getPeer(); }
|
public Destination getPeer() {
|
||||||
|
return _data.getConfig().getPeer();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* What is our series identifier (used to locally identify a test)
|
* What is our series identifier (used to locally identify a test)
|
||||||
* @return the series identifier
|
* @return the series identifier
|
||||||
*/
|
*/
|
||||||
public int getSeriesNum() { return _id; }
|
public int getSeriesNum() {
|
||||||
|
return _id;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* receive notification from the heartbeat system that a pong was received in
|
* receive notification from the heartbeat system that a pong was received in
|
||||||
@ -71,53 +77,54 @@ class ClientEngine {
|
|||||||
* @param replyOn when did the peer send the pong?
|
* @param replyOn when did the peer send the pong?
|
||||||
*/
|
*/
|
||||||
public void receivePong(long sentOn, long replyOn) {
|
public void receivePong(long sentOn, long replyOn) {
|
||||||
_data.pongReceived(sentOn, replyOn);
|
_data.pongReceived(sentOn, replyOn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** fire off a new ping */
|
/** fire off a new ping */
|
||||||
private void doSend() {
|
private void doSend() {
|
||||||
long now = Clock.getInstance().now();
|
long now = Clock.getInstance().now();
|
||||||
_heartbeat.sendPing(_data.getConfig().getPeer(), _id, now, _data.getConfig().getSendSize());
|
_heartbeat.sendPing(_data.getConfig().getPeer(), _id, now, _data.getConfig().getSendSize());
|
||||||
_data.addPing(now);
|
_data.addPing(now);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** our actual heartbeat pumper - this drives the test */
|
/** our actual heartbeat pumper - this drives the test */
|
||||||
private class ClientRunner implements Runnable {
|
private class ClientRunner implements Runnable {
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see java.lang.Runnable#run()
|
* @see java.lang.Runnable#run()
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
if (_log.shouldLog(Log.INFO))
|
if (_log.shouldLog(Log.INFO))
|
||||||
_log.info("Starting engine talking to peer " + _data.getConfig().getPeer().calculateHash().toBase64());
|
_log.info("Starting engine talking to peer " + _data.getConfig().getPeer().calculateHash().toBase64());
|
||||||
|
|
||||||
// when do we need to send the next PING?
|
// when do we need to send the next PING?
|
||||||
long nextSend = Clock.getInstance().now();
|
long nextSend = Clock.getInstance().now();
|
||||||
// when do we need to write out the next state data?
|
// when do we need to write out the next state data?
|
||||||
long nextWrite = Clock.getInstance().now();
|
long nextWrite = Clock.getInstance().now();
|
||||||
|
|
||||||
while (_active) {
|
while (_active) {
|
||||||
|
|
||||||
if (Clock.getInstance().now() >= nextSend) {
|
if (Clock.getInstance().now() >= nextSend) {
|
||||||
doSend();
|
doSend();
|
||||||
nextSend = Clock.getInstance().now() + _data.getConfig().getSendFrequency()*1000;
|
nextSend = Clock.getInstance().now() + _data.getConfig().getSendFrequency() * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Clock.getInstance().now() >= nextWrite) {
|
if (Clock.getInstance().now() >= nextWrite) {
|
||||||
boolean written = writer.persist(_data);
|
boolean written = writer.persist(_data);
|
||||||
if (!written) {
|
if (!written) {
|
||||||
if (_log.shouldLog(Log.ERROR))
|
if (_log.shouldLog(Log.ERROR)) _log.error("Unable to write the client state data");
|
||||||
_log.error("Unable to write the client state data");
|
} else {
|
||||||
} else {
|
if (_log.shouldLog(Log.DEBUG)) _log.debug("Client state data written");
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
}
|
||||||
_log.debug("Client state data written");
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_data.cleanup();
|
_data.cleanup();
|
||||||
|
|
||||||
try { Thread.sleep(1000); } catch (InterruptedException ie) {}
|
try {
|
||||||
}
|
Thread.sleep(1000);
|
||||||
}
|
} catch (InterruptedException ie) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -55,7 +55,7 @@ import net.i2p.util.Log;
|
|||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Heartbeat {
|
public class Heartbeat {
|
||||||
private static final Log _log = new Log(Heartbeat.class);
|
private static final Log _log = new Log(Heartbeat.class);
|
||||||
/** location containing this heartbeat's config */
|
/** location containing this heartbeat's config */
|
||||||
private String _configFile;
|
private String _configFile;
|
||||||
@ -76,38 +76,42 @@ public class Heartbeat {
|
|||||||
* @param configFile the name of the configuration file
|
* @param configFile the name of the configuration file
|
||||||
*/
|
*/
|
||||||
public Heartbeat(String configFile) {
|
public Heartbeat(String configFile) {
|
||||||
_configFile = configFile;
|
_configFile = configFile;
|
||||||
_clientConfigs = new HashMap();
|
_clientConfigs = new HashMap();
|
||||||
_clientEngines = new HashMap();
|
_clientEngines = new HashMap();
|
||||||
_eventAdapter = new PingPongAdapter();
|
_eventAdapter = new PingPongAdapter();
|
||||||
_adapter = new I2PAdapter();
|
_adapter = new I2PAdapter();
|
||||||
_adapter.setListener(_eventAdapter);
|
_adapter.setListener(_eventAdapter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Heartbeat() {
|
||||||
}
|
}
|
||||||
private Heartbeat() {}
|
|
||||||
|
|
||||||
/** load up the config data (but don't build any engines or start them up) */
|
/** load up the config data (but don't build any engines or start them up) */
|
||||||
public void loadConfig() {
|
public void loadConfig() {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
FileInputStream fin = null;
|
FileInputStream fin = null;
|
||||||
File configFile = new File (_configFile);
|
File configFile = new File(_configFile);
|
||||||
if (configFile.exists()) {
|
if (configFile.exists()) {
|
||||||
try {
|
try {
|
||||||
fin = new FileInputStream(_configFile);
|
fin = new FileInputStream(_configFile);
|
||||||
props.load(fin);
|
props.load(fin);
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
_log.error("Error reading the config data", ioe);
|
_log.error("Error reading the config data", ioe);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (fin != null) try {
|
||||||
|
fin.close();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
if (fin != null) try { fin.close(); } catch (IOException ioe) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loadBaseConfig(props);
|
loadBaseConfig(props);
|
||||||
loadClientConfigs(props);
|
loadClientConfigs(props);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send a ping message to the peer
|
* send a ping message to the peer
|
||||||
*
|
*
|
||||||
@ -117,8 +121,7 @@ public class Heartbeat {
|
|||||||
* @param size total message size to send
|
* @param size total message size to send
|
||||||
*/
|
*/
|
||||||
void sendPing(Destination peer, int seriesNum, long now, int size) {
|
void sendPing(Destination peer, int seriesNum, long now, int size) {
|
||||||
if (_adapter.getIsConnected())
|
if (_adapter.getIsConnected()) _adapter.sendPing(peer, seriesNum, now, size);
|
||||||
_adapter.sendPing(peer, seriesNum, now, size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,7 +129,7 @@ public class Heartbeat {
|
|||||||
* @param props the properties to load from
|
* @param props the properties to load from
|
||||||
*/
|
*/
|
||||||
private void loadBaseConfig(Properties props) {
|
private void loadBaseConfig(Properties props) {
|
||||||
_adapter.loadConfig(props);
|
_adapter.loadConfig(props);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,46 +137,47 @@ public class Heartbeat {
|
|||||||
* @param props the properties to load from
|
* @param props the properties to load from
|
||||||
* */
|
* */
|
||||||
private void loadClientConfigs(Properties props) {
|
private void loadClientConfigs(Properties props) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
ClientConfig config = new ClientConfig();
|
ClientConfig config = new ClientConfig();
|
||||||
if (!config.load(props, i)) {
|
if (!config.load(props, i)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
_clientConfigs.put(new Integer(i), config);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
_clientConfigs.put(new Integer(i), config);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** connect to the network */
|
/** connect to the network */
|
||||||
private void connect() {
|
private void connect() {
|
||||||
boolean connected = _adapter.connect();
|
boolean connected = _adapter.connect();
|
||||||
if (!connected)
|
if (!connected) _log.error("Unable to connect to the router");
|
||||||
_log.error("Unable to connect to the router");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** disconnect from the network */
|
/** disconnect from the network */
|
||||||
private void disconnect() {
|
private void disconnect() {
|
||||||
_adapter.disconnect();
|
_adapter.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** start up all of the tests */
|
/** start up all of the tests */
|
||||||
public void startEngines() {
|
public void startEngines() {
|
||||||
for (Iterator iter = _clientConfigs.values().iterator(); iter.hasNext(); ) {
|
for (Iterator iter = _clientConfigs.values().iterator(); iter.hasNext();) {
|
||||||
ClientConfig config = (ClientConfig)iter.next();
|
ClientConfig config = (ClientConfig) iter.next();
|
||||||
ClientEngine engine = new ClientEngine(this, config);
|
ClientEngine engine = new ClientEngine(this, config);
|
||||||
config.setUs(_adapter.getLocalDestination());
|
config.setUs(_adapter.getLocalDestination());
|
||||||
config.setNumHops(_adapter.getNumHops());
|
config.setNumHops(_adapter.getNumHops());
|
||||||
_clientEngines.put(new Integer(engine.getSeriesNum()), engine);
|
_clientEngines.put(new Integer(engine.getSeriesNum()), engine);
|
||||||
engine.startEngine();
|
engine.startEngine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** stop all of the tests */
|
/** stop all of the tests */
|
||||||
public void stopEngines() {
|
public void stopEngines() {
|
||||||
for (Iterator iter = _clientEngines.values().iterator(); iter.hasNext(); ) {
|
for (Iterator iter = _clientEngines.values().iterator(); iter.hasNext();) {
|
||||||
ClientEngine engine = (ClientEngine)iter.next();
|
ClientEngine engine = (ClientEngine) iter.next();
|
||||||
engine.stopEngine();
|
engine.stopEngine();
|
||||||
}
|
}
|
||||||
_clientEngines.clear();
|
_clientEngines.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -186,26 +190,27 @@ public class Heartbeat {
|
|||||||
* @param args the list of args passed to the program from the command-line
|
* @param args the list of args passed to the program from the command-line
|
||||||
*/
|
*/
|
||||||
public static void main(String args[]) {
|
public static void main(String args[]) {
|
||||||
String configFile = CONFIG_FILE_DEFAULT;
|
String configFile = CONFIG_FILE_DEFAULT;
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
configFile = args[0];
|
configFile = args[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
_log.info("Starting up with config file " + configFile);
|
_log.info("Starting up with config file " + configFile);
|
||||||
}
|
}
|
||||||
Heartbeat heartbeat = new Heartbeat(configFile);
|
Heartbeat heartbeat = new Heartbeat(configFile);
|
||||||
heartbeat.loadConfig();
|
heartbeat.loadConfig();
|
||||||
heartbeat.connect();
|
heartbeat.connect();
|
||||||
heartbeat.startEngines();
|
heartbeat.startEngines();
|
||||||
Object o = new Object();
|
Object o = new Object();
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
synchronized (o) {
|
synchronized (o) {
|
||||||
o.wait();
|
o.wait();
|
||||||
}
|
}
|
||||||
} catch (InterruptedException ie) {}
|
} catch (InterruptedException ie) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -213,35 +218,35 @@ public class Heartbeat {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private class PingPongAdapter implements I2PAdapter.PingPongEventListener {
|
private class PingPongAdapter implements I2PAdapter.PingPongEventListener {
|
||||||
/**
|
/**
|
||||||
* We were pinged, so always just send a pong back.
|
* We were pinged, so always just send a pong back.
|
||||||
*
|
*
|
||||||
* @param from who sent us the ping?
|
* @param from who sent us the ping?
|
||||||
* @param seriesNum what series did the sender specify?
|
* @param seriesNum what series did the sender specify?
|
||||||
* @param sentOn when did the sender say they sent their ping?
|
* @param sentOn when did the sender say they sent their ping?
|
||||||
* @param data arbitrary payload data
|
* @param data arbitrary payload data
|
||||||
*/
|
*/
|
||||||
public void receivePing(Destination from, int seriesNum, Date sentOn, byte[] data) {
|
public void receivePing(Destination from, int seriesNum, Date sentOn, byte[] data) {
|
||||||
if (_adapter.getIsConnected()) {
|
if (_adapter.getIsConnected()) {
|
||||||
_adapter.sendPong(from, seriesNum, sentOn, data);
|
_adapter.sendPong(from, seriesNum, sentOn, data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We received a pong, so find the right client engine and tell it about the pong.
|
* We received a pong, so find the right client engine and tell it about the pong.
|
||||||
*
|
*
|
||||||
* @param from who sent us the pong
|
* @param from who sent us the pong
|
||||||
* @param seriesNum our client ID
|
* @param seriesNum our client ID
|
||||||
* @param sentOn when did we send the ping?
|
* @param sentOn when did we send the ping?
|
||||||
* @param replyOn when did they send their pong?
|
* @param replyOn when did they send their pong?
|
||||||
* @param data the arbitrary data we sent in the ping (that they sent back in the pong)
|
* @param data the arbitrary data we sent in the ping (that they sent back in the pong)
|
||||||
*/
|
*/
|
||||||
public void receivePong(Destination from, int seriesNum, Date sentOn, Date replyOn, byte[] data) {
|
public void receivePong(Destination from, int seriesNum, Date sentOn, Date replyOn, byte[] data) {
|
||||||
ClientEngine engine = (ClientEngine)_clientEngines.get(new Integer(seriesNum));
|
ClientEngine engine = (ClientEngine) _clientEngines.get(new Integer(seriesNum));
|
||||||
if (engine.getPeer().equals(from)) {
|
if (engine.getPeer().equals(from)) {
|
||||||
engine.receivePong(sentOn.getTime(), replyOn.getTime());
|
engine.receivePong(sentOn.getTime(), replyOn.getTime());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -71,79 +71,88 @@ class I2PAdapter {
|
|||||||
* Constructs an I2PAdapter . . .
|
* Constructs an I2PAdapter . . .
|
||||||
*/
|
*/
|
||||||
public I2PAdapter() {
|
public I2PAdapter() {
|
||||||
_privateDestFile = null;
|
_privateDestFile = null;
|
||||||
_i2cpHost = null;
|
_i2cpHost = null;
|
||||||
_i2cpPort = -1;
|
_i2cpPort = -1;
|
||||||
_localDest = null;
|
_localDest = null;
|
||||||
_listener = null;
|
_listener = null;
|
||||||
_session = null;
|
_session = null;
|
||||||
_numHops = 0;
|
_numHops = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* who are we?
|
* who are we?
|
||||||
* @return the destination (us)
|
* @return the destination (us)
|
||||||
*/
|
*/
|
||||||
public Destination getLocalDestination() { return _localDest; }
|
public Destination getLocalDestination() {
|
||||||
|
return _localDest;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* who gets notified when we receive a ping or a pong?
|
* who gets notified when we receive a ping or a pong?
|
||||||
* @return the event listener who gets notified
|
* @return the event listener who gets notified
|
||||||
*/
|
*/
|
||||||
public PingPongEventListener getListener() { return _listener; }
|
public PingPongEventListener getListener() {
|
||||||
|
return _listener;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets who gets notified when we receive a ping or a pong
|
* Sets who gets notified when we receive a ping or a pong
|
||||||
* @param listener the event listener to get notified
|
* @param listener the event listener to get notified
|
||||||
*/
|
*/
|
||||||
public void setListener(PingPongEventListener listener) { _listener = listener; }
|
public void setListener(PingPongEventListener listener) {
|
||||||
|
_listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how many hops do we want in our tunnels?
|
* how many hops do we want in our tunnels?
|
||||||
* @return the number of hops
|
* @return the number of hops
|
||||||
*/
|
*/
|
||||||
public int getNumHops() { return _numHops; }
|
public int getNumHops() {
|
||||||
|
return _numHops;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* are we connected?
|
* are we connected?
|
||||||
* @return true or false . . .
|
* @return true or false . . .
|
||||||
*/
|
*/
|
||||||
public boolean getIsConnected() { return _session != null; }
|
public boolean getIsConnected() {
|
||||||
|
return _session != null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read in all of the config data
|
* Read in all of the config data
|
||||||
* @param props the properties to load from
|
* @param props the properties to load from
|
||||||
*/
|
*/
|
||||||
void loadConfig(Properties props) {
|
void loadConfig(Properties props) {
|
||||||
String privDestFile = props.getProperty(DEST_FILE_PROP, DEST_FILE_DEFAULT);
|
String privDestFile = props.getProperty(DEST_FILE_PROP, DEST_FILE_DEFAULT);
|
||||||
String host = props.getProperty(I2CP_HOST_PROP, I2CP_HOST_DEFAULT);
|
String host = props.getProperty(I2CP_HOST_PROP, I2CP_HOST_DEFAULT);
|
||||||
String port = props.getProperty(I2CP_PORT_PROP, ""+I2CP_PORT_DEFAULT);
|
String port = props.getProperty(I2CP_PORT_PROP, "" + I2CP_PORT_DEFAULT);
|
||||||
String numHops = props.getProperty(NUMHOPS_PROP, ""+NUMHOPS_DEFAULT);
|
String numHops = props.getProperty(NUMHOPS_PROP, "" + NUMHOPS_DEFAULT);
|
||||||
|
|
||||||
int portNum = -1;
|
int portNum = -1;
|
||||||
try {
|
try {
|
||||||
portNum = Integer.parseInt(port);
|
portNum = Integer.parseInt(port);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
if (_log.shouldLog(Log.WARN)) {
|
if (_log.shouldLog(Log.WARN)) {
|
||||||
_log.warn("Invalid I2CP port specified [" + port + "]");
|
_log.warn("Invalid I2CP port specified [" + port + "]");
|
||||||
|
}
|
||||||
|
portNum = I2CP_PORT_DEFAULT;
|
||||||
}
|
}
|
||||||
portNum = I2CP_PORT_DEFAULT;
|
int hops = -1;
|
||||||
}
|
try {
|
||||||
int hops = -1;
|
hops = Integer.parseInt(numHops);
|
||||||
try {
|
} catch (NumberFormatException nfe) {
|
||||||
hops = Integer.parseInt(numHops);
|
if (_log.shouldLog(Log.WARN)) {
|
||||||
} catch (NumberFormatException nfe) {
|
_log.warn("Invalid # hops specified [" + numHops + "]");
|
||||||
if (_log.shouldLog(Log.WARN)) {
|
}
|
||||||
_log.warn("Invalid # hops specified [" + numHops + "]");
|
hops = NUMHOPS_DEFAULT;
|
||||||
}
|
}
|
||||||
hops = NUMHOPS_DEFAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
_numHops = hops;
|
_numHops = hops;
|
||||||
_privateDestFile = privDestFile;
|
_privateDestFile = privDestFile;
|
||||||
_i2cpHost = host;
|
_i2cpHost = host;
|
||||||
_i2cpPort = portNum;
|
_i2cpPort = portNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,25 +160,25 @@ class I2PAdapter {
|
|||||||
* @param props the properties to write to
|
* @param props the properties to write to
|
||||||
*/
|
*/
|
||||||
void storeConfig(Properties props) {
|
void storeConfig(Properties props) {
|
||||||
if (_privateDestFile != null) {
|
if (_privateDestFile != null) {
|
||||||
props.setProperty(DEST_FILE_PROP, _privateDestFile);
|
props.setProperty(DEST_FILE_PROP, _privateDestFile);
|
||||||
} else {
|
} else {
|
||||||
props.setProperty(DEST_FILE_PROP, DEST_FILE_DEFAULT);
|
props.setProperty(DEST_FILE_PROP, DEST_FILE_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_i2cpHost != null) {
|
if (_i2cpHost != null) {
|
||||||
props.setProperty(I2CP_HOST_PROP, _i2cpHost);
|
props.setProperty(I2CP_HOST_PROP, _i2cpHost);
|
||||||
} else {
|
} else {
|
||||||
props.setProperty(I2CP_HOST_PROP, I2CP_HOST_DEFAULT);
|
props.setProperty(I2CP_HOST_PROP, I2CP_HOST_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_i2cpPort > 0) {
|
if (_i2cpPort > 0) {
|
||||||
props.setProperty(I2CP_PORT_PROP, ""+_i2cpPort);
|
props.setProperty(I2CP_PORT_PROP, "" + _i2cpPort);
|
||||||
} else {
|
} else {
|
||||||
props.setProperty(I2CP_PORT_PROP, ""+I2CP_PORT_DEFAULT);
|
props.setProperty(I2CP_PORT_PROP, "" + I2CP_PORT_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
props.setProperty(NUMHOPS_PROP, ""+_numHops);
|
props.setProperty(NUMHOPS_PROP, "" + _numHops);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final int TYPE_PING = 0;
|
private static final int TYPE_PING = 0;
|
||||||
@ -186,41 +195,42 @@ class I2PAdapter {
|
|||||||
* @throws IllegalStateException if we are not connected to the router
|
* @throws IllegalStateException if we are not connected to the router
|
||||||
*/
|
*/
|
||||||
public void sendPing(Destination peer, int seriesNum, long now, int size) {
|
public void sendPing(Destination peer, int seriesNum, long now, int size) {
|
||||||
if (_session == null) throw new IllegalStateException("Not connected to the router");
|
if (_session == null) throw new IllegalStateException("Not connected to the router");
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
|
||||||
try {
|
try {
|
||||||
_localDest.writeBytes(baos);
|
_localDest.writeBytes(baos);
|
||||||
DataHelper.writeLong(baos, 2, seriesNum);
|
DataHelper.writeLong(baos, 2, seriesNum);
|
||||||
DataHelper.writeLong(baos, 1, TYPE_PING);
|
DataHelper.writeLong(baos, 1, TYPE_PING);
|
||||||
DataHelper.writeDate(baos, new Date(now));
|
DataHelper.writeDate(baos, new Date(now));
|
||||||
int padding = size - baos.size();
|
int padding = size - baos.size();
|
||||||
byte paddingData[] = new byte[padding];
|
byte paddingData[] = new byte[padding];
|
||||||
Arrays.fill(paddingData, (byte)0x2A);
|
Arrays.fill(paddingData, (byte) 0x2A);
|
||||||
DataHelper.writeLong(baos, 2, padding);
|
DataHelper.writeLong(baos, 2, padding);
|
||||||
baos.write(paddingData);
|
baos.write(paddingData);
|
||||||
boolean sent = _session.sendMessage(peer, baos.toByteArray());
|
boolean sent = _session.sendMessage(peer, baos.toByteArray());
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
_log.error("Error sending the ping to " + peer.calculateHash().toBase64() + " for series " + seriesNum);
|
_log.error("Error sending the ping to " + peer.calculateHash().toBase64() + " for series "
|
||||||
|
+ seriesNum);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
|
_log.info("Ping sent to " + peer.calculateHash().toBase64() + " for series " + seriesNum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error sending the ping", ioe);
|
||||||
|
}
|
||||||
|
} catch (DataFormatException dfe) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error writing out the ping message", dfe);
|
||||||
|
}
|
||||||
|
} catch (I2PSessionException ise) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error writing out the ping message", ise);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
|
||||||
_log.info("Ping sent to " + peer.calculateHash().toBase64() + " for series " + seriesNum);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error sending the ping", ioe);
|
|
||||||
}
|
|
||||||
} catch (DataFormatException dfe) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error writing out the ping message", dfe);
|
|
||||||
}
|
|
||||||
} catch (I2PSessionException ise) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error writing out the ping message", ise);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -234,39 +244,41 @@ class I2PAdapter {
|
|||||||
* @throws IllegalStateException if we are not connected to the router
|
* @throws IllegalStateException if we are not connected to the router
|
||||||
*/
|
*/
|
||||||
public void sendPong(Destination peer, int seriesNum, Date sentOn, byte data[]) {
|
public void sendPong(Destination peer, int seriesNum, Date sentOn, byte data[]) {
|
||||||
if (_session == null) throw new IllegalStateException("Not connected to the router");
|
if (_session == null) throw new IllegalStateException("Not connected to the router");
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length + 768);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length + 768);
|
||||||
try {
|
try {
|
||||||
_localDest.writeBytes(baos);
|
_localDest.writeBytes(baos);
|
||||||
DataHelper.writeLong(baos, 2, seriesNum);
|
DataHelper.writeLong(baos, 2, seriesNum);
|
||||||
DataHelper.writeLong(baos, 1, TYPE_PONG);
|
DataHelper.writeLong(baos, 1, TYPE_PONG);
|
||||||
DataHelper.writeDate(baos, sentOn);
|
DataHelper.writeDate(baos, sentOn);
|
||||||
DataHelper.writeDate(baos, new Date(Clock.getInstance().now()));
|
DataHelper.writeDate(baos, new Date(Clock.getInstance().now()));
|
||||||
DataHelper.writeLong(baos, 2, data.length);
|
DataHelper.writeLong(baos, 2, data.length);
|
||||||
baos.write(data);
|
baos.write(data);
|
||||||
boolean sent = _session.sendMessage(peer, baos.toByteArray());
|
boolean sent = _session.sendMessage(peer, baos.toByteArray());
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
_log.error("Error sending the pong to " + peer.calculateHash().toBase64() + " for series " + seriesNum + " which was sent on " + sentOn);
|
_log.error("Error sending the pong to " + peer.calculateHash().toBase64() + " for series "
|
||||||
|
+ seriesNum + " which was sent on " + sentOn);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
|
_log.info("Pong sent to " + peer.calculateHash().toBase64() + " for series " + seriesNum
|
||||||
|
+ " which was sent on " + sentOn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error sending the ping", ioe);
|
||||||
|
}
|
||||||
|
} catch (DataFormatException dfe) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error writing out the pong message", dfe);
|
||||||
|
}
|
||||||
|
} catch (I2PSessionException ise) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error writing out the pong message", ise);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
|
||||||
_log.info("Pong sent to " + peer.calculateHash().toBase64() + " for series " + seriesNum + " which was sent on " + sentOn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error sending the ping", ioe);
|
|
||||||
}
|
|
||||||
} catch (DataFormatException dfe) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error writing out the pong message", dfe);
|
|
||||||
}
|
|
||||||
} catch (I2PSessionException ise) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error writing out the pong message", ise);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -275,57 +287,58 @@ class I2PAdapter {
|
|||||||
* @param data the data to handle
|
* @param data the data to handle
|
||||||
*/
|
*/
|
||||||
private void handleMessage(byte data[]) {
|
private void handleMessage(byte data[]) {
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||||
try {
|
try {
|
||||||
Destination from = new Destination();
|
Destination from = new Destination();
|
||||||
from.readBytes(bais);
|
from.readBytes(bais);
|
||||||
int series = (int)DataHelper.readLong(bais, 2);
|
int series = (int) DataHelper.readLong(bais, 2);
|
||||||
long type = DataHelper.readLong(bais, 1);
|
long type = DataHelper.readLong(bais, 1);
|
||||||
Date sentOn = DataHelper.readDate(bais);
|
Date sentOn = DataHelper.readDate(bais);
|
||||||
Date receivedOn = null;
|
Date receivedOn = null;
|
||||||
if (type == TYPE_PONG) {
|
if (type == TYPE_PONG) {
|
||||||
receivedOn = DataHelper.readDate(bais);
|
receivedOn = DataHelper.readDate(bais);
|
||||||
}
|
}
|
||||||
int size = (int)DataHelper.readLong(bais, 2);
|
int size = (int) DataHelper.readLong(bais, 2);
|
||||||
byte payload[] = new byte[size];
|
byte payload[] = new byte[size];
|
||||||
int read = DataHelper.read(bais, payload);
|
int read = DataHelper.read(bais, payload);
|
||||||
if (read != size) {
|
if (read != size) { throw new IOException("Malformed payload - read " + read + " instead of " + size); }
|
||||||
throw new IOException("Malformed payload - read " + read + " instead of " + size);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_listener == null) {
|
if (_listener == null) {
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
_log.error("Listener isn't set, but we received a valid message of type " + type + " sent from " + from.calculateHash().toBase64());
|
_log.error("Listener isn't set, but we received a valid message of type " + type + " sent from "
|
||||||
}
|
+ from.calculateHash().toBase64());
|
||||||
return;
|
}
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (type == TYPE_PING) {
|
if (type == TYPE_PING) {
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
_log.info("Ping received from " + from.calculateHash().toBase64() + " on series " + series + " sent on " + sentOn + " containing " + size + " bytes");
|
_log.info("Ping received from " + from.calculateHash().toBase64() + " on series " + series
|
||||||
}
|
+ " sent on " + sentOn + " containing " + size + " bytes");
|
||||||
_listener.receivePing(from, series, sentOn, payload);
|
}
|
||||||
} else if (type == TYPE_PONG) {
|
_listener.receivePing(from, series, sentOn, payload);
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
} else if (type == TYPE_PONG) {
|
||||||
_log.info("Pong received from " + from.calculateHash().toBase64() + " on series " + series + " sent on " + sentOn + " with pong sent on " + receivedOn + " containing " + size + " bytes");
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
}
|
_log.info("Pong received from " + from.calculateHash().toBase64() + " on series " + series
|
||||||
_listener.receivePong(from, series, sentOn, receivedOn, payload);
|
+ " sent on " + sentOn + " with pong sent on " + receivedOn + " containing " + size
|
||||||
} else {
|
+ " bytes");
|
||||||
throw new IOException("Invalid message type " + type);
|
}
|
||||||
}
|
_listener.receivePong(from, series, sentOn, receivedOn, payload);
|
||||||
|
} else {
|
||||||
|
throw new IOException("Invalid message type " + type);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
_log.error("Error handling the message", ioe);
|
_log.error("Error handling the message", ioe);
|
||||||
|
}
|
||||||
|
} catch (DataFormatException dfe) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error parsing the message", dfe);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (DataFormatException dfe) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error parsing the message", dfe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* connect to the I2P router and either authenticate ourselves with the
|
* connect to the I2P router and either authenticate ourselves with the
|
||||||
* destination we're given, or create a new one and write that to the
|
* destination we're given, or create a new one and write that to the
|
||||||
@ -334,42 +347,45 @@ class I2PAdapter {
|
|||||||
* @return true if we connect successfully, false otherwise
|
* @return true if we connect successfully, false otherwise
|
||||||
*/
|
*/
|
||||||
boolean connect() {
|
boolean connect() {
|
||||||
I2PClient client = I2PClientFactory.createClient();
|
I2PClient client = I2PClientFactory.createClient();
|
||||||
Destination us = null;
|
Destination us = null;
|
||||||
File destFile = new File(_privateDestFile);
|
File destFile = new File(_privateDestFile);
|
||||||
us = verifyDestination(client, destFile);
|
us = verifyDestination(client, destFile);
|
||||||
if (us == null) return false;
|
if (us == null) return false;
|
||||||
|
|
||||||
// if we're here, we got a destination. lets connect
|
// if we're here, we got a destination. lets connect
|
||||||
FileInputStream fin = null;
|
FileInputStream fin = null;
|
||||||
try {
|
try {
|
||||||
fin = new FileInputStream(destFile);
|
fin = new FileInputStream(destFile);
|
||||||
Properties options = getOptions();
|
Properties options = getOptions();
|
||||||
I2PSession session = client.createSession(fin, options);
|
I2PSession session = client.createSession(fin, options);
|
||||||
I2PListener lsnr = new I2PListener();
|
I2PListener lsnr = new I2PListener();
|
||||||
session.setSessionListener(lsnr);
|
session.setSessionListener(lsnr);
|
||||||
session.connect();
|
session.connect();
|
||||||
_localDest = session.getMyDestination();
|
_localDest = session.getMyDestination();
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
_log.info("I2CP Session created and connected as " + _localDest.calculateHash().toBase64());
|
_log.info("I2CP Session created and connected as " + _localDest.calculateHash().toBase64());
|
||||||
|
}
|
||||||
|
_session = session;
|
||||||
|
_i2pListener = lsnr;
|
||||||
|
} catch (I2PSessionException ise) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error connecting", ise);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error loading the destionation", ioe);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (fin != null) try {
|
||||||
|
fin.close();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_session = session;
|
|
||||||
_i2pListener = lsnr;
|
|
||||||
} catch (I2PSessionException ise) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error connecting", ise);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (IOException ioe) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error loading the destionation", ioe);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} finally {
|
|
||||||
if (fin != null) try { fin.close(); } catch (IOException ioe) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -380,56 +396,68 @@ class I2PAdapter {
|
|||||||
* @return the destination loaded, or null if there was an error
|
* @return the destination loaded, or null if there was an error
|
||||||
*/
|
*/
|
||||||
private Destination verifyDestination(I2PClient client, File destFile) {
|
private Destination verifyDestination(I2PClient client, File destFile) {
|
||||||
Destination us = null;
|
Destination us = null;
|
||||||
FileInputStream fin = null;
|
FileInputStream fin = null;
|
||||||
if (destFile.exists()) {
|
if (destFile.exists()) {
|
||||||
try {
|
try {
|
||||||
fin = new FileInputStream(destFile);
|
fin = new FileInputStream(destFile);
|
||||||
us = new Destination();
|
us = new Destination();
|
||||||
us.readBytes(fin);
|
us.readBytes(fin);
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
_log.info("Existing destination loaded: [" + us.toBase64() + "]");
|
_log.info("Existing destination loaded: [" + us.toBase64() + "]");
|
||||||
|
}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
if (fin != null) try {
|
||||||
|
fin.close();
|
||||||
|
} catch (IOException ioe2) {
|
||||||
|
}
|
||||||
|
fin = null;
|
||||||
|
destFile.delete();
|
||||||
|
us = null;
|
||||||
|
} catch (DataFormatException dfe) {
|
||||||
|
if (fin != null) try {
|
||||||
|
fin.close();
|
||||||
|
} catch (IOException ioe2) {
|
||||||
|
}
|
||||||
|
fin = null;
|
||||||
|
destFile.delete();
|
||||||
|
us = null;
|
||||||
|
} finally {
|
||||||
|
if (fin != null) try {
|
||||||
|
fin.close();
|
||||||
|
} catch (IOException ioe2) {
|
||||||
|
}
|
||||||
|
fin = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException ioe) {
|
|
||||||
if (fin != null) try { fin.close(); } catch (IOException ioe2) {}
|
|
||||||
fin = null;
|
|
||||||
destFile.delete();
|
|
||||||
us = null;
|
|
||||||
} catch (DataFormatException dfe) {
|
|
||||||
if (fin != null) try { fin.close(); } catch (IOException ioe2) {}
|
|
||||||
fin = null;
|
|
||||||
destFile.delete();
|
|
||||||
us = null;
|
|
||||||
} finally {
|
|
||||||
if (fin != null) try { fin.close(); } catch (IOException ioe2) {}
|
|
||||||
fin = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (us == null) {
|
if (us == null) {
|
||||||
// need to create a new one
|
// need to create a new one
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
try {
|
try {
|
||||||
fos = new FileOutputStream(destFile);
|
fos = new FileOutputStream(destFile);
|
||||||
us = client.createDestination(fos);
|
us = client.createDestination(fos);
|
||||||
if (_log.shouldLog(Log.INFO)) {
|
if (_log.shouldLog(Log.INFO)) {
|
||||||
_log.info("New destination created: [" + us.toBase64() + "]");
|
_log.info("New destination created: [" + us.toBase64() + "]");
|
||||||
|
}
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error writing out the destination keys being created", ioe);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (I2PException ie) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
|
_log.error("Error creating the destination", ie);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
if (fos != null) try {
|
||||||
|
fos.close();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException ioe) {
|
return us;
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error writing out the destination keys being created", ioe);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} catch (I2PException ie) {
|
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
|
||||||
_log.error("Error creating the destination", ie);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return us;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -437,27 +465,27 @@ class I2PAdapter {
|
|||||||
* @return the options as Properties
|
* @return the options as Properties
|
||||||
*/
|
*/
|
||||||
private Properties getOptions() {
|
private Properties getOptions() {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
props.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_BEST_EFFORT);
|
props.setProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_BEST_EFFORT);
|
||||||
props.setProperty(I2PClient.PROP_TCP_HOST, _i2cpHost);
|
props.setProperty(I2PClient.PROP_TCP_HOST, _i2cpHost);
|
||||||
props.setProperty(I2PClient.PROP_TCP_PORT, _i2cpPort + "");
|
props.setProperty(I2PClient.PROP_TCP_PORT, _i2cpPort + "");
|
||||||
props.setProperty("tunnels.depthInbound", ""+_numHops);
|
props.setProperty("tunnels.depthInbound", "" + _numHops);
|
||||||
props.setProperty("tunnels.depthOutbound", ""+_numHops);
|
props.setProperty("tunnels.depthOutbound", "" + _numHops);
|
||||||
return props;
|
return props;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** disconnect from the I2P router */
|
/** disconnect from the I2P router */
|
||||||
void disconnect() {
|
void disconnect() {
|
||||||
if (_session != null) {
|
if (_session != null) {
|
||||||
try {
|
try {
|
||||||
_session.destroySession();
|
_session.destroySession();
|
||||||
} catch (I2PSessionException ise) {
|
} catch (I2PSessionException ise) {
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
_log.error("Error destroying the session", ise);
|
_log.error("Error destroying the session", ise);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_session = null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
_session = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -465,26 +493,26 @@ class I2PAdapter {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface PingPongEventListener {
|
public interface PingPongEventListener {
|
||||||
/**
|
/**
|
||||||
* receive a ping message from the peer
|
* receive a ping message from the peer
|
||||||
*
|
*
|
||||||
* @param from peer that sent us the ping
|
* @param from peer that sent us the ping
|
||||||
* @param seriesNum id the peer sent us in the ping
|
* @param seriesNum id the peer sent us in the ping
|
||||||
* @param sentOn date the peer said they sent us the message
|
* @param sentOn date the peer said they sent us the message
|
||||||
* @param data payload from the ping
|
* @param data payload from the ping
|
||||||
*/
|
*/
|
||||||
void receivePing(Destination from, int seriesNum, Date sentOn, byte data[]);
|
void receivePing(Destination from, int seriesNum, Date sentOn, byte data[]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* receive a pong message from the peer
|
* receive a pong message from the peer
|
||||||
*
|
*
|
||||||
* @param from peer that sent us the pong
|
* @param from peer that sent us the pong
|
||||||
* @param seriesNum id the peer sent us in the pong (that we sent them in the ping)
|
* @param seriesNum id the peer sent us in the pong (that we sent them in the ping)
|
||||||
* @param sentOn when we sent out the ping
|
* @param sentOn when we sent out the ping
|
||||||
* @param replyOn when they sent out the pong
|
* @param replyOn when they sent out the pong
|
||||||
* @param data payload from the ping/pong
|
* @param data payload from the ping/pong
|
||||||
*/
|
*/
|
||||||
void receivePong(Destination from, int seriesNum, Date sentOn, Date replyOn, byte data[]);
|
void receivePong(Destination from, int seriesNum, Date sentOn, Date replyOn, byte data[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -493,42 +521,41 @@ class I2PAdapter {
|
|||||||
*/
|
*/
|
||||||
private class I2PListener implements I2PSessionListener {
|
private class I2PListener implements I2PSessionListener {
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see net.i2p.client.I2PSessionListener#disconnected(net.i2p.client.I2PSession)
|
* @see net.i2p.client.I2PSessionListener#disconnected(net.i2p.client.I2PSession)
|
||||||
*/
|
*/
|
||||||
public void disconnected(I2PSession session) {
|
public void disconnected(I2PSession session) {
|
||||||
if (_log.shouldLog(Log.ERROR)) {
|
if (_log.shouldLog(Log.ERROR)) {
|
||||||
_log.error("Session disconnected");
|
_log.error("Session disconnected");
|
||||||
|
}
|
||||||
|
disconnect();
|
||||||
}
|
}
|
||||||
disconnect();
|
|
||||||
}
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see net.i2p.client.I2PSessionListener#errorOccurred(net.i2p.client.I2PSession, java.lang.String, java.lang.Throwable)
|
|
||||||
*/
|
|
||||||
public void errorOccurred(I2PSession session, String message, Throwable error) {
|
|
||||||
if (_log.shouldLog(Log.ERROR))
|
|
||||||
_log.error("Error occurred", error);
|
|
||||||
}
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see net.i2p.client.I2PSessionListener#reportAbuse(net.i2p.client.I2PSession, int)
|
|
||||||
*/
|
|
||||||
public void reportAbuse(I2PSession session, int severity) {
|
|
||||||
if (_log.shouldLog(Log.ERROR))
|
|
||||||
_log.error("Abuse reported");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see net.i2p.client.I2PSessionListener#messageAvailable(net.i2p.client.I2PSession, int, long)
|
* @see net.i2p.client.I2PSessionListener#errorOccurred(net.i2p.client.I2PSession, java.lang.String, java.lang.Throwable)
|
||||||
*/
|
*/
|
||||||
public void messageAvailable(I2PSession session, int msgId, long size) {
|
public void errorOccurred(I2PSession session, String message, Throwable error) {
|
||||||
try {
|
if (_log.shouldLog(Log.ERROR)) _log.error("Error occurred", error);
|
||||||
byte data[] = session.receiveMessage(msgId);
|
}
|
||||||
handleMessage(data);
|
|
||||||
} catch (I2PSessionException ise) {
|
/* (non-Javadoc)
|
||||||
if (_log.shouldLog(Log.ERROR))
|
* @see net.i2p.client.I2PSessionListener#reportAbuse(net.i2p.client.I2PSession, int)
|
||||||
_log.error("Error receiving the message", ise);
|
*/
|
||||||
disconnect();
|
public void reportAbuse(I2PSession session, int severity) {
|
||||||
}
|
if (_log.shouldLog(Log.ERROR)) _log.error("Abuse reported");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see net.i2p.client.I2PSessionListener#messageAvailable(net.i2p.client.I2PSession, int, long)
|
||||||
|
*/
|
||||||
|
public void messageAvailable(I2PSession session, int msgId, long size) {
|
||||||
|
try {
|
||||||
|
byte data[] = session.receiveMessage(msgId);
|
||||||
|
handleMessage(data);
|
||||||
|
} catch (I2PSessionException ise) {
|
||||||
|
if (_log.shouldLog(Log.ERROR)) _log.error("Error receiving the message", ise);
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -37,7 +37,7 @@ public class PeerData {
|
|||||||
private RateStat _lostRate;
|
private RateStat _lostRate;
|
||||||
|
|
||||||
/** how long we wait before timing out pending pings (30 seconds) */
|
/** how long we wait before timing out pending pings (30 seconds) */
|
||||||
private static final long TIMEOUT_PERIOD = 30*1000;
|
private static final long TIMEOUT_PERIOD = 30 * 1000;
|
||||||
|
|
||||||
/** synchronize on this when updating _dataPoints or _pendingPings */
|
/** synchronize on this when updating _dataPoints or _pendingPings */
|
||||||
private Object _updateLock = new Object();
|
private Object _updateLock = new Object();
|
||||||
@ -47,15 +47,18 @@ public class PeerData {
|
|||||||
* @param config configuration to load from
|
* @param config configuration to load from
|
||||||
*/
|
*/
|
||||||
public PeerData(ClientConfig config) {
|
public PeerData(ClientConfig config) {
|
||||||
_peer = config;
|
_peer = config;
|
||||||
_dataPoints = new TreeMap();
|
_dataPoints = new TreeMap();
|
||||||
_pendingPings = new TreeMap();
|
_pendingPings = new TreeMap();
|
||||||
_sessionStart = Clock.getInstance().now();
|
_sessionStart = Clock.getInstance().now();
|
||||||
_lifetimeSent = 0;
|
_lifetimeSent = 0;
|
||||||
_lifetimeReceived = 0;
|
_lifetimeReceived = 0;
|
||||||
_sendRate = new RateStat("sendRate", "How long it takes to send", "peer", getPeriods(config.getAveragePeriods()));
|
_sendRate = new RateStat("sendRate", "How long it takes to send", "peer",
|
||||||
_receiveRate = new RateStat("receiveRate", "How long it takes to receive", "peer", getPeriods(config.getAveragePeriods()));
|
getPeriods(config.getAveragePeriods()));
|
||||||
_lostRate = new RateStat("lostRate", "How frequently we lose messages", "peer", getPeriods(config.getAveragePeriods()));
|
_receiveRate = new RateStat("receiveRate", "How long it takes to receive", "peer",
|
||||||
|
getPeriods(config.getAveragePeriods()));
|
||||||
|
_lostRate = new RateStat("lostRate", "How frequently we lose messages", "peer",
|
||||||
|
getPeriods(config.getAveragePeriods()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,56 +67,73 @@ public class PeerData {
|
|||||||
* @return an array of periods (in milliseconds)
|
* @return an array of periods (in milliseconds)
|
||||||
*/
|
*/
|
||||||
private static long[] getPeriods(int periods[]) {
|
private static long[] getPeriods(int periods[]) {
|
||||||
long rv[] = null;
|
long rv[] = null;
|
||||||
if (periods == null) periods = new int[0];
|
if (periods == null) periods = new int[0];
|
||||||
rv = new long[periods.length];
|
rv = new long[periods.length];
|
||||||
for (int i = 0; i < periods.length; i++)
|
for (int i = 0; i < periods.length; i++)
|
||||||
rv[i] = (long)periods[i] * 60*1000; // they're in minutes
|
rv[i] = (long) periods[i] * 60 * 1000; // they're in minutes
|
||||||
Arrays.sort(rv);
|
Arrays.sort(rv);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how many pings are still outstanding?
|
* how many pings are still outstanding?
|
||||||
* @return the number of pings outstanding
|
* @return the number of pings outstanding
|
||||||
*/
|
*/
|
||||||
public int getPendingCount() { synchronized (_updateLock) { return _pendingPings.size(); } }
|
public int getPendingCount() {
|
||||||
|
synchronized (_updateLock) {
|
||||||
|
return _pendingPings.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how many data points are available in the current window?
|
* how many data points are available in the current window?
|
||||||
* @return the number of datapoints available
|
* @return the number of datapoints available
|
||||||
*/
|
*/
|
||||||
public int getDataPointCount() { synchronized (_updateLock) { return _dataPoints.size(); } }
|
public int getDataPointCount() {
|
||||||
|
synchronized (_updateLock) {
|
||||||
|
return _dataPoints.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* when did this test begin?
|
* when did this test begin?
|
||||||
* @return when the test began
|
* @return when the test began
|
||||||
*/
|
*/
|
||||||
public long getSessionStart() { return _sessionStart; }
|
public long getSessionStart() {
|
||||||
|
return _sessionStart;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how many pings have we sent for this test?
|
* how many pings have we sent for this test?
|
||||||
* @return the number of pings sent
|
* @return the number of pings sent
|
||||||
*/
|
*/
|
||||||
public long getLifetimeSent() { return _lifetimeSent; }
|
public long getLifetimeSent() {
|
||||||
|
return _lifetimeSent;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* how many pongs have we received for this test?
|
* how many pongs have we received for this test?
|
||||||
* @return the number of pings received
|
* @return the number of pings received
|
||||||
*/
|
*/
|
||||||
public long getLifetimeReceived() { return _lifetimeReceived; }
|
public long getLifetimeReceived() {
|
||||||
|
return _lifetimeReceived;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the client configuration
|
* @return the client configuration
|
||||||
*/
|
*/
|
||||||
public ClientConfig getConfig() { return _peer; }
|
public ClientConfig getConfig() {
|
||||||
|
return _peer;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* What periods are we averaging the data over (in minutes)?
|
* What periods are we averaging the data over (in minutes)?
|
||||||
* @return the periods as an array of ints (in minutes)
|
* @return the periods as an array of ints (in minutes)
|
||||||
*/
|
*/
|
||||||
public int[] getAveragePeriods() { return (_peer.getAveragePeriods() != null ? _peer.getAveragePeriods() : new int[0]); }
|
public int[] getAveragePeriods() {
|
||||||
|
return (_peer.getAveragePeriods() != null ? _peer.getAveragePeriods() : new int[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* average time to send over the given period.
|
* average time to send over the given period.
|
||||||
@ -121,7 +141,9 @@ public class PeerData {
|
|||||||
* @param period number of minutes to retrieve the average for
|
* @param period number of minutes to retrieve the average for
|
||||||
* @return milliseconds average, or -1 if we dont track that period
|
* @return milliseconds average, or -1 if we dont track that period
|
||||||
*/
|
*/
|
||||||
public double getAverageSendTime(int period) { return getAverage(_sendRate, period); }
|
public double getAverageSendTime(int period) {
|
||||||
|
return getAverage(_sendRate, period);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* average time to receive over the given period.
|
* average time to receive over the given period.
|
||||||
@ -129,7 +151,9 @@ public class PeerData {
|
|||||||
* @param period number of minutes to retrieve the average for
|
* @param period number of minutes to retrieve the average for
|
||||||
* @return milliseconds average, or -1 if we dont track that period
|
* @return milliseconds average, or -1 if we dont track that period
|
||||||
*/
|
*/
|
||||||
public double getAverageReceiveTime(int period) { return getAverage(_receiveRate, period); }
|
public double getAverageReceiveTime(int period) {
|
||||||
|
return getAverage(_receiveRate, period);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* number of lost messages over the given period.
|
* number of lost messages over the given period.
|
||||||
@ -138,17 +162,15 @@ public class PeerData {
|
|||||||
* @return number of lost messages in the period, or -1 if we dont track that period
|
* @return number of lost messages in the period, or -1 if we dont track that period
|
||||||
*/
|
*/
|
||||||
public double getLostMessages(int period) {
|
public double getLostMessages(int period) {
|
||||||
Rate rate = _lostRate.getRate(period * 60*1000);
|
Rate rate = _lostRate.getRate(period * 60 * 1000);
|
||||||
if (rate == null)
|
if (rate == null) return -1;
|
||||||
return -1;
|
return rate.getCurrentTotalValue();
|
||||||
return rate.getCurrentTotalValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private double getAverage(RateStat stat, int period) {
|
private double getAverage(RateStat stat, int period) {
|
||||||
Rate rate = stat.getRate(period * 60*1000);
|
Rate rate = stat.getRate(period * 60 * 1000);
|
||||||
if (rate == null)
|
if (rate == null) return -1;
|
||||||
return -1;
|
return rate.getAverageValue();
|
||||||
return rate.getAverageValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -157,10 +179,10 @@ public class PeerData {
|
|||||||
* @return list of EventDataPoint objects
|
* @return list of EventDataPoint objects
|
||||||
*/
|
*/
|
||||||
public List getDataPoints() {
|
public List getDataPoints() {
|
||||||
cleanup();
|
cleanup();
|
||||||
synchronized (_updateLock) {
|
synchronized (_updateLock) {
|
||||||
return new ArrayList(_dataPoints.values());
|
return new ArrayList(_dataPoints.values());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -168,11 +190,11 @@ public class PeerData {
|
|||||||
* @param dateSent when the ping was sent
|
* @param dateSent when the ping was sent
|
||||||
*/
|
*/
|
||||||
public void addPing(long dateSent) {
|
public void addPing(long dateSent) {
|
||||||
EventDataPoint sent = new EventDataPoint(dateSent);
|
EventDataPoint sent = new EventDataPoint(dateSent);
|
||||||
synchronized (_updateLock) {
|
synchronized (_updateLock) {
|
||||||
_pendingPings.put(new Long(dateSent), sent);
|
_pendingPings.put(new Long(dateSent), sent);
|
||||||
}
|
}
|
||||||
_lifetimeSent++;
|
_lifetimeSent++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -182,19 +204,19 @@ public class PeerData {
|
|||||||
* @param pongSent when the peer received the ping and sent the pong
|
* @param pongSent when the peer received the ping and sent the pong
|
||||||
*/
|
*/
|
||||||
public void pongReceived(long dateSent, long pongSent) {
|
public void pongReceived(long dateSent, long pongSent) {
|
||||||
long now = Clock.getInstance().now();
|
long now = Clock.getInstance().now();
|
||||||
synchronized (_updateLock) {
|
synchronized (_updateLock) {
|
||||||
EventDataPoint data = (EventDataPoint)_pendingPings.remove(new Long(dateSent));
|
EventDataPoint data = (EventDataPoint) _pendingPings.remove(new Long(dateSent));
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
data.setPongReceived(now);
|
data.setPongReceived(now);
|
||||||
data.setPongSent(pongSent);
|
data.setPongSent(pongSent);
|
||||||
data.setWasPonged(true);
|
data.setWasPonged(true);
|
||||||
_dataPoints.put(new Long(dateSent), data);
|
_dataPoints.put(new Long(dateSent), data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_sendRate.addData(pongSent-dateSent, 0);
|
_sendRate.addData(pongSent - dateSent, 0);
|
||||||
_receiveRate.addData(now-pongSent, 0);
|
_receiveRate.addData(now - pongSent, 0);
|
||||||
_lifetimeReceived++;
|
_lifetimeReceived++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -204,140 +226,157 @@ public class PeerData {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void cleanup() {
|
public void cleanup() {
|
||||||
long dropBefore = Clock.getInstance().now() - _peer.getStatDuration() * 60*1000;
|
long dropBefore = Clock.getInstance().now() - _peer.getStatDuration() * 60 * 1000;
|
||||||
long timeoutBefore = Clock.getInstance().now() - TIMEOUT_PERIOD;
|
long timeoutBefore = Clock.getInstance().now() - TIMEOUT_PERIOD;
|
||||||
long numDropped = 0;
|
long numDropped = 0;
|
||||||
long numTimedOut = 0;
|
long numTimedOut = 0;
|
||||||
|
|
||||||
synchronized (_updateLock) {
|
synchronized (_updateLock) {
|
||||||
List toTimeout = new ArrayList(4);
|
List toTimeout = new ArrayList(4);
|
||||||
List toDrop = new ArrayList(4);
|
List toDrop = new ArrayList(4);
|
||||||
for (Iterator iter = _pendingPings.keySet().iterator(); iter.hasNext(); ) {
|
for (Iterator iter = _pendingPings.keySet().iterator(); iter.hasNext();) {
|
||||||
Long when = (Long)iter.next();
|
Long when = (Long) iter.next();
|
||||||
if (when.longValue() < dropBefore)
|
if (when.longValue() < dropBefore)
|
||||||
toDrop.add(when);
|
toDrop.add(when);
|
||||||
else if (when.longValue() < timeoutBefore)
|
else if (when.longValue() < timeoutBefore)
|
||||||
toTimeout.add(when);
|
toTimeout.add(when);
|
||||||
else
|
else
|
||||||
break; // its ordered, so once we are past timeoutBefore, no need
|
break; // its ordered, so once we are past timeoutBefore, no need
|
||||||
}
|
}
|
||||||
for (Iterator iter = toDrop.iterator(); iter.hasNext(); ) {
|
for (Iterator iter = toDrop.iterator(); iter.hasNext();) {
|
||||||
_pendingPings.remove(iter.next());
|
_pendingPings.remove(iter.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
List toAdd = new ArrayList(toTimeout.size());
|
List toAdd = new ArrayList(toTimeout.size());
|
||||||
for (Iterator iter = toTimeout.iterator(); iter.hasNext(); ) {
|
for (Iterator iter = toTimeout.iterator(); iter.hasNext();) {
|
||||||
Long when = (Long)iter.next();
|
Long when = (Long) iter.next();
|
||||||
EventDataPoint data = (EventDataPoint)_pendingPings.remove(when);
|
EventDataPoint data = (EventDataPoint) _pendingPings.remove(when);
|
||||||
data.setWasPonged(false);
|
data.setWasPonged(false);
|
||||||
toAdd.add(data);
|
toAdd.add(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
numDropped = toDrop.size();
|
numDropped = toDrop.size();
|
||||||
numTimedOut = toDrop.size();
|
numTimedOut = toDrop.size();
|
||||||
toDrop.clear();
|
toDrop.clear();
|
||||||
|
|
||||||
for (Iterator iter = _dataPoints.keySet().iterator(); iter.hasNext(); ) {
|
for (Iterator iter = _dataPoints.keySet().iterator(); iter.hasNext();) {
|
||||||
Long when = (Long)iter.next();
|
Long when = (Long) iter.next();
|
||||||
if (when.longValue() < dropBefore)
|
if (when.longValue() < dropBefore)
|
||||||
toDrop.add(when);
|
toDrop.add(when);
|
||||||
else
|
else
|
||||||
break; // ordered
|
break; // ordered
|
||||||
}
|
}
|
||||||
for (Iterator iter = toDrop.iterator(); iter.hasNext(); ) {
|
for (Iterator iter = toDrop.iterator(); iter.hasNext();) {
|
||||||
_dataPoints.remove(iter.next());
|
_dataPoints.remove(iter.next());
|
||||||
}
|
}
|
||||||
|
|
||||||
numDropped += toDrop.size();
|
numDropped += toDrop.size();
|
||||||
|
|
||||||
for (Iterator iter = toAdd.iterator(); iter.hasNext(); ) {
|
for (Iterator iter = toAdd.iterator(); iter.hasNext();) {
|
||||||
EventDataPoint data = (EventDataPoint)iter.next();
|
EventDataPoint data = (EventDataPoint) iter.next();
|
||||||
_dataPoints.put(new Long(data.getPingSent()), data);
|
_dataPoints.put(new Long(data.getPingSent()), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
numTimedOut += toAdd.size();
|
numTimedOut += toAdd.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
_lostRate.addData(numTimedOut, 0);
|
_lostRate.addData(numTimedOut, 0);
|
||||||
|
|
||||||
_receiveRate.coallesceStats();
|
_receiveRate.coallesceStats();
|
||||||
_sendRate.coallesceStats();
|
_sendRate.coallesceStats();
|
||||||
_lostRate.coallesceStats();
|
_lostRate.coallesceStats();
|
||||||
|
|
||||||
if (_log.shouldLog(Log.DEBUG))
|
if (_log.shouldLog(Log.DEBUG))
|
||||||
_log.debug("Peer data cleaned up " + numTimedOut + " timed out pings and removed " + numDropped + " old entries");
|
_log.debug("Peer data cleaned up " + numTimedOut + " timed out pings and removed " + numDropped
|
||||||
|
+ " old entries");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** actual data point for the peer */
|
/** actual data point for the peer */
|
||||||
public class EventDataPoint {
|
public class EventDataPoint {
|
||||||
private boolean _wasPonged;
|
private boolean _wasPonged;
|
||||||
private long _pingSent;
|
private long _pingSent;
|
||||||
private long _pongSent;
|
private long _pongSent;
|
||||||
private long _pongReceived;
|
private long _pongReceived;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an EventDataPoint
|
* Creates an EventDataPoint
|
||||||
*/
|
*/
|
||||||
public EventDataPoint() {
|
public EventDataPoint() {
|
||||||
this(-1);
|
this(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an EventDataPoint with pingtime associated with it =)
|
* Creates an EventDataPoint with pingtime associated with it =)
|
||||||
* @param pingSentOn the time a ping was sent
|
* @param pingSentOn the time a ping was sent
|
||||||
*/
|
*/
|
||||||
public EventDataPoint(long pingSentOn) {
|
public EventDataPoint(long pingSentOn) {
|
||||||
_wasPonged = false;
|
_wasPonged = false;
|
||||||
_pingSent = pingSentOn;
|
_pingSent = pingSentOn;
|
||||||
_pongSent = -1;
|
_pongSent = -1;
|
||||||
_pongReceived = -1;
|
_pongReceived = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* when did we send this ping?
|
* when did we send this ping?
|
||||||
* @return the time the ping was sent
|
* @return the time the ping was sent
|
||||||
*/
|
*/
|
||||||
public long getPingSent() { return _pingSent; }
|
public long getPingSent() {
|
||||||
|
return _pingSent;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the time the ping was sent
|
* Set the time the ping was sent
|
||||||
* @param when time to set
|
* @param when time to set
|
||||||
*/
|
*/
|
||||||
public void setPingSent(long when) { _pingSent = when; }
|
public void setPingSent(long when) {
|
||||||
|
_pingSent = when;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* when did the peer receive the ping?
|
* when did the peer receive the ping?
|
||||||
* @return the time the ping was receieved
|
* @return the time the ping was receieved
|
||||||
*/
|
*/
|
||||||
public long getPongSent() { return _pongSent; }
|
public long getPongSent() {
|
||||||
|
return _pongSent;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the time the peer received the ping
|
* Set the time the peer received the ping
|
||||||
* @param when the time to set
|
* @param when the time to set
|
||||||
*/
|
*/
|
||||||
public void setPongSent(long when) { _pongSent = when; }
|
public void setPongSent(long when) {
|
||||||
|
_pongSent = when;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* when did we receive the peer's pong?
|
* when did we receive the peer's pong?
|
||||||
* @return the time we receieved the pong
|
* @return the time we receieved the pong
|
||||||
*/
|
*/
|
||||||
public long getPongReceived() { return _pongReceived; }
|
public long getPongReceived() {
|
||||||
|
return _pongReceived;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the time the peer's pong was receieved
|
* Set the time the peer's pong was receieved
|
||||||
* @param when the time to set
|
* @param when the time to set
|
||||||
*/
|
*/
|
||||||
public void setPongReceived(long when) { _pongReceived = when; }
|
public void setPongReceived(long when) {
|
||||||
|
_pongReceived = when;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* did the peer reply in time?
|
* did the peer reply in time?
|
||||||
* @return true or false, whether we got a reply in time */
|
* @return true or false, whether we got a reply in time */
|
||||||
public boolean getWasPonged() { return _wasPonged; }
|
public boolean getWasPonged() {
|
||||||
|
return _wasPonged;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set whether we receieved the peer's reply in time
|
* Set whether we receieved the peer's reply in time
|
||||||
* @param pong true or false
|
* @param pong true or false
|
||||||
*/
|
*/
|
||||||
public void setWasPonged(boolean pong) { _wasPonged = pong; }
|
public void setWasPonged(boolean pong) {
|
||||||
|
_wasPonged = pong;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -27,69 +27,73 @@ class PeerDataWriter {
|
|||||||
* @return true if it was persisted correctly, false on error
|
* @return true if it was persisted correctly, false on error
|
||||||
*/
|
*/
|
||||||
public boolean persist(PeerData data) {
|
public boolean persist(PeerData data) {
|
||||||
String filename = data.getConfig().getStatFile();
|
String filename = data.getConfig().getStatFile();
|
||||||
String header = getHeader(data);
|
String header = getHeader(data);
|
||||||
File statFile = new File(filename);
|
File statFile = new File(filename);
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
try {
|
try {
|
||||||
fos = new FileOutputStream(statFile);
|
fos = new FileOutputStream(statFile);
|
||||||
fos.write(header.getBytes());
|
fos.write(header.getBytes());
|
||||||
fos.write("#action\tstatus\tdate and time sent \tsendMs\treplyMs\n".getBytes());
|
fos.write("#action\tstatus\tdate and time sent \tsendMs\treplyMs\n".getBytes());
|
||||||
for (Iterator iter = data.getDataPoints().iterator(); iter.hasNext(); ) {
|
for (Iterator iter = data.getDataPoints().iterator(); iter.hasNext();) {
|
||||||
PeerData.EventDataPoint point = (PeerData.EventDataPoint)iter.next();
|
PeerData.EventDataPoint point = (PeerData.EventDataPoint) iter.next();
|
||||||
String line = getEvent(point);
|
String line = getEvent(point);
|
||||||
fos.write(line.getBytes());
|
fos.write(line.getBytes());
|
||||||
}
|
}
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
if (_log.shouldLog(Log.ERROR))
|
if (_log.shouldLog(Log.ERROR))
|
||||||
_log.error("Error persisting the peer data for " + data.getConfig().getPeer().calculateHash().toBase64(), ioe);
|
_log.error("Error persisting the peer data for "
|
||||||
return false;
|
+ data.getConfig().getPeer().calculateHash().toBase64(), ioe);
|
||||||
} finally {
|
return false;
|
||||||
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
|
} finally {
|
||||||
}
|
if (fos != null) try {
|
||||||
return true;
|
fos.close();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getHeader(PeerData data) {
|
private String getHeader(PeerData data) {
|
||||||
StringBuffer buf = new StringBuffer(1024);
|
StringBuffer buf = new StringBuffer(1024);
|
||||||
buf.append("peer \t").append(data.getConfig().getPeer().calculateHash().toBase64()).append('\n');
|
buf.append("peer \t").append(data.getConfig().getPeer().calculateHash().toBase64()).append('\n');
|
||||||
buf.append("local \t").append(data.getConfig().getUs().calculateHash().toBase64()).append('\n');
|
buf.append("local \t").append(data.getConfig().getUs().calculateHash().toBase64()).append('\n');
|
||||||
buf.append("peerDest \t").append(data.getConfig().getPeer().toBase64()).append('\n');
|
buf.append("peerDest \t").append(data.getConfig().getPeer().toBase64()).append('\n');
|
||||||
buf.append("localDest \t").append(data.getConfig().getUs().toBase64()).append('\n');
|
buf.append("localDest \t").append(data.getConfig().getUs().toBase64()).append('\n');
|
||||||
buf.append("numTunnelHops\t").append(data.getConfig().getNumHops()).append('\n');
|
buf.append("numTunnelHops\t").append(data.getConfig().getNumHops()).append('\n');
|
||||||
buf.append("comment \t").append(data.getConfig().getComment()).append('\n');
|
buf.append("comment \t").append(data.getConfig().getComment()).append('\n');
|
||||||
buf.append("sendFrequency\t").append(data.getConfig().getSendFrequency()).append('\n');
|
buf.append("sendFrequency\t").append(data.getConfig().getSendFrequency()).append('\n');
|
||||||
buf.append("sendSize \t").append(data.getConfig().getSendSize()).append('\n');
|
buf.append("sendSize \t").append(data.getConfig().getSendSize()).append('\n');
|
||||||
buf.append("sessionStart \t").append(getTime(data.getSessionStart())).append('\n');
|
buf.append("sessionStart \t").append(getTime(data.getSessionStart())).append('\n');
|
||||||
buf.append("currentTime \t").append(getTime(Clock.getInstance().now())).append('\n');
|
buf.append("currentTime \t").append(getTime(Clock.getInstance().now())).append('\n');
|
||||||
buf.append("numPending \t").append(data.getPendingCount()).append('\n');
|
buf.append("numPending \t").append(data.getPendingCount()).append('\n');
|
||||||
buf.append("lifetimeSent \t").append(data.getLifetimeSent()).append('\n');
|
buf.append("lifetimeSent \t").append(data.getLifetimeSent()).append('\n');
|
||||||
buf.append("lifetimeRecv \t").append(data.getLifetimeReceived()).append('\n');
|
buf.append("lifetimeRecv \t").append(data.getLifetimeReceived()).append('\n');
|
||||||
int periods[] = data.getAveragePeriods();
|
int periods[] = data.getAveragePeriods();
|
||||||
buf.append("#averages\tminutes\tsendMs\trecvMs\tnumLost\n");
|
buf.append("#averages\tminutes\tsendMs\trecvMs\tnumLost\n");
|
||||||
for (int i = 0; i < periods.length; i++) {
|
for (int i = 0; i < periods.length; i++) {
|
||||||
buf.append("periodAverage\t").append(periods[i]).append('\t');
|
buf.append("periodAverage\t").append(periods[i]).append('\t');
|
||||||
buf.append(getNum(data.getAverageSendTime(periods[i]))).append('\t');
|
buf.append(getNum(data.getAverageSendTime(periods[i]))).append('\t');
|
||||||
buf.append(getNum(data.getAverageReceiveTime(periods[i]))).append('\t');
|
buf.append(getNum(data.getAverageReceiveTime(periods[i]))).append('\t');
|
||||||
buf.append(getNum(data.getLostMessages(periods[i]))).append('\n');
|
buf.append(getNum(data.getLostMessages(periods[i]))).append('\n');
|
||||||
}
|
}
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getEvent(PeerData.EventDataPoint point) {
|
private String getEvent(PeerData.EventDataPoint point) {
|
||||||
StringBuffer buf = new StringBuffer(128);
|
StringBuffer buf = new StringBuffer(128);
|
||||||
buf.append("EVENT\t");
|
buf.append("EVENT\t");
|
||||||
if (point.getWasPonged())
|
if (point.getWasPonged())
|
||||||
buf.append("OK\t");
|
buf.append("OK\t");
|
||||||
else
|
else
|
||||||
buf.append("LOST\t");
|
buf.append("LOST\t");
|
||||||
buf.append(getTime(point.getPingSent())).append('\t');
|
buf.append(getTime(point.getPingSent())).append('\t');
|
||||||
if (point.getWasPonged()) {
|
if (point.getWasPonged()) {
|
||||||
buf.append(point.getPongSent() - point.getPingSent()).append('\t');
|
buf.append(point.getPongSent() - point.getPingSent()).append('\t');
|
||||||
buf.append(point.getPongReceived() - point.getPongSent()).append('\t');
|
buf.append(point.getPongReceived() - point.getPongSent()).append('\t');
|
||||||
}
|
}
|
||||||
buf.append('\n');
|
buf.append('\n');
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final SimpleDateFormat _fmt = new SimpleDateFormat("yyyyMMdd.HH:mm:ss.SSS", Locale.UK);
|
private static final SimpleDateFormat _fmt = new SimpleDateFormat("yyyyMMdd.HH:mm:ss.SSS", Locale.UK);
|
||||||
@ -100,9 +104,9 @@ class PeerDataWriter {
|
|||||||
* @return the textual representation
|
* @return the textual representation
|
||||||
*/
|
*/
|
||||||
public String getTime(long when) {
|
public String getTime(long when) {
|
||||||
synchronized (_fmt) {
|
synchronized (_fmt) {
|
||||||
return _fmt.format(new Date(when));
|
return _fmt.format(new Date(when));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final DecimalFormat _numFmt = new DecimalFormat("#0", new DecimalFormatSymbols(Locale.UK));
|
private static final DecimalFormat _numFmt = new DecimalFormat("#0", new DecimalFormatSymbols(Locale.UK));
|
||||||
@ -113,8 +117,8 @@ class PeerDataWriter {
|
|||||||
* @return the textual representation
|
* @return the textual representation
|
||||||
*/
|
*/
|
||||||
public String getNum(double val) {
|
public String getNum(double val) {
|
||||||
synchronized (_numFmt) {
|
synchronized (_numFmt) {
|
||||||
return _numFmt.format(val);
|
return _numFmt.format(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,12 +10,13 @@ class PeerPlotConfig {
|
|||||||
private ClientConfig _config;
|
private ClientConfig _config;
|
||||||
|
|
||||||
private final static void foo() {
|
private final static void foo() {
|
||||||
// bar
|
// bar
|
||||||
if (true) {
|
if (true) {
|
||||||
// baz
|
// baz
|
||||||
}
|
}
|
||||||
// baf
|
// baf
|
||||||
}
|
}
|
||||||
|
|
||||||
// moo
|
// moo
|
||||||
|
|
||||||
private final static void biff() {
|
private final static void biff() {
|
||||||
|
@ -28,53 +28,50 @@ public class HTTPListener extends Thread {
|
|||||||
* @param listenHost A host, to connect to.
|
* @param listenHost A host, to connect to.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public HTTPListener(SocketManagerProducer smp, int port,
|
public HTTPListener(SocketManagerProducer smp, int port, String listenHost) {
|
||||||
String listenHost) {
|
this.smp = smp;
|
||||||
this.smp = smp;
|
this.port = port;
|
||||||
this.port = port;
|
start();
|
||||||
start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see java.lang.Thread#run()
|
* @see java.lang.Thread#run()
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
InetAddress lh = listenHost == null
|
InetAddress lh = listenHost == null ? null : InetAddress.getByName(listenHost);
|
||||||
? null
|
ServerSocket ss = new ServerSocket(port, 0, lh);
|
||||||
: InetAddress.getByName(listenHost);
|
while (true) {
|
||||||
ServerSocket ss = new ServerSocket(port, 0, lh);
|
Socket s = ss.accept();
|
||||||
while(true) {
|
new HTTPSocketHandler(this, s);
|
||||||
Socket s = ss.accept();
|
}
|
||||||
new HTTPSocketHandler(this, s);
|
} catch (IOException ex) {
|
||||||
}
|
_log.error("Error while accepting connections", ex);
|
||||||
} catch (IOException ex) {
|
}
|
||||||
_log.error("Error while accepting connections", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean proxyUsed=false;
|
private boolean proxyUsed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query whether this is the first use of the proxy or not . . .
|
* Query whether this is the first use of the proxy or not . . .
|
||||||
* @return Whether this is the first proxy use, no doubt.
|
* @return Whether this is the first proxy use, no doubt.
|
||||||
*/
|
*/
|
||||||
public boolean firstProxyUse() {
|
public boolean firstProxyUse() {
|
||||||
// FIXME: check a config option here
|
// FIXME: check a config option here
|
||||||
if (true) return false;
|
if (true) return false;
|
||||||
if (proxyUsed) {
|
if (proxyUsed) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
proxyUsed=true;
|
proxyUsed = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The SocketManagerProducer being used.
|
* @return The SocketManagerProducer being used.
|
||||||
*/
|
*/
|
||||||
public SocketManagerProducer getSMP() {
|
public SocketManagerProducer getSMP() {
|
||||||
return smp;
|
return smp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,8 +81,7 @@ public class HTTPListener extends Thread {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handleNotImplemented(OutputStream out) throws IOException {
|
public void handleNotImplemented(OutputStream out) throws IOException {
|
||||||
out.write(("HTTP/1.1 200 Document following\n\n"+
|
out.write(("HTTP/1.1 200 Document following\n\n" + "<h1>Feature not implemented</h1>").getBytes("ISO-8859-1"));
|
||||||
"<h1>Feature not implemented</h1>").getBytes("ISO-8859-1"));
|
out.flush();
|
||||||
out.flush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -27,37 +27,36 @@ public class HTTPSocketHandler extends Thread {
|
|||||||
* @param s A socket.
|
* @param s A socket.
|
||||||
*/
|
*/
|
||||||
public HTTPSocketHandler(HTTPListener httpl, Socket s) {
|
public HTTPSocketHandler(HTTPListener httpl, Socket s) {
|
||||||
this.httpl = httpl;
|
this.httpl = httpl;
|
||||||
this.s=s;
|
this.s = s;
|
||||||
h = RootHandler.getInstance();
|
h = RootHandler.getInstance();
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see java.lang.Thread#run()
|
* @see java.lang.Thread#run()
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
OutputStream out = null;
|
OutputStream out = null;
|
||||||
try {
|
try {
|
||||||
in = new BufferedInputStream(s.getInputStream());
|
in = new BufferedInputStream(s.getInputStream());
|
||||||
out = new BufferedOutputStream(s.getOutputStream());
|
out = new BufferedOutputStream(s.getOutputStream());
|
||||||
Request req = new Request(in);
|
Request req = new Request(in);
|
||||||
h.handle(req, httpl, out);
|
h.handle(req, httpl, out);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Error while handling data", ex);
|
_log.error("Error while handling data", ex);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (in != null) in.close();
|
if (in != null) in.close();
|
||||||
if (out != null) {
|
if (out != null) {
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
s.close();
|
s.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("IOException in finalizer", ex);
|
_log.error("IOException in finalizer", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -44,10 +44,9 @@ public class HTTPTunnel {
|
|||||||
* @param shouldThrowAwayManagers whether to throw away a manager after use
|
* @param shouldThrowAwayManagers whether to throw away a manager after use
|
||||||
* @param listenPort which port to listen on
|
* @param listenPort which port to listen on
|
||||||
*/
|
*/
|
||||||
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers,
|
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers, boolean shouldThrowAwayManagers,
|
||||||
boolean shouldThrowAwayManagers, int listenPort) {
|
int listenPort) {
|
||||||
this(initialManagers, maxManagers, shouldThrowAwayManagers, listenPort,
|
this(initialManagers, maxManagers, shouldThrowAwayManagers, listenPort, "127.0.0.1", 7654);
|
||||||
"127.0.0.1", 7654);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,13 +59,11 @@ public class HTTPTunnel {
|
|||||||
* @param i2cpAddress the I2CP address
|
* @param i2cpAddress the I2CP address
|
||||||
* @param i2cpPort the I2CP port
|
* @param i2cpPort the I2CP port
|
||||||
*/
|
*/
|
||||||
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers,
|
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers, boolean shouldThrowAwayManagers,
|
||||||
boolean shouldThrowAwayManagers, int listenPort,
|
int listenPort, String i2cpAddress, int i2cpPort) {
|
||||||
String i2cpAddress, int i2cpPort) {
|
SocketManagerProducer smp = new SocketManagerProducer(initialManagers, maxManagers, shouldThrowAwayManagers,
|
||||||
SocketManagerProducer smp =
|
i2cpAddress, i2cpPort);
|
||||||
new SocketManagerProducer(initialManagers, maxManagers,
|
new HTTPListener(smp, listenPort, "127.0.0.1");
|
||||||
shouldThrowAwayManagers, i2cpAddress, i2cpPort);
|
|
||||||
new HTTPListener(smp, listenPort, "127.0.0.1");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,41 +72,37 @@ public class HTTPTunnel {
|
|||||||
* @param args A list of String passed to the program
|
* @param args A list of String passed to the program
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String host = "127.0.0.1";
|
String host = "127.0.0.1";
|
||||||
int port = 7654, max = 1;
|
int port = 7654, max = 1;
|
||||||
boolean throwAwayManagers = false;
|
boolean throwAwayManagers = false;
|
||||||
if (args.length >1) {
|
if (args.length > 1) {
|
||||||
if (args.length == 4) {
|
if (args.length == 4) {
|
||||||
host = args[2];
|
host = args[2];
|
||||||
port = Integer.parseInt(args[3]);
|
port = Integer.parseInt(args[3]);
|
||||||
} else if (args.length != 2) {
|
} else if (args.length != 2) {
|
||||||
showInfo(); return;
|
showInfo();
|
||||||
}
|
return;
|
||||||
max = Integer.parseInt(args[1]);
|
}
|
||||||
} else if (args.length != 1) {
|
max = Integer.parseInt(args[1]);
|
||||||
showInfo(); return;
|
} else if (args.length != 1) {
|
||||||
}
|
showInfo();
|
||||||
if (max == 0) {
|
return;
|
||||||
max = 1;
|
}
|
||||||
} else if (max <0) {
|
if (max == 0) {
|
||||||
max = -max;
|
max = 1;
|
||||||
throwAwayManagers = true;
|
} else if (max < 0) {
|
||||||
}
|
max = -max;
|
||||||
new HTTPTunnel(null, max, throwAwayManagers, Integer.parseInt(args[0]), host, port);
|
throwAwayManagers = true;
|
||||||
|
}
|
||||||
|
new HTTPTunnel(null, max, throwAwayManagers, Integer.parseInt(args[0]), host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void showInfo() {
|
private static void showInfo() {
|
||||||
System.out.println
|
System.out.println("Usage: java HTTPTunnel <listenPort> [<max> " + "[<i2cphost> <i2cpport>]]\n"
|
||||||
("Usage: java HTTPTunnel <listenPort> [<max> "+
|
+ " <listenPort> port to listen for browsers\n"
|
||||||
"[<i2cphost> <i2cpport>]]\n"+
|
+ " <max> max number of SocketMangers in pool, " + "use neg. number\n"
|
||||||
" <listenPort> port to listen for browsers\n"+
|
+ " to use each SocketManager only once " + "(default: 1)\n"
|
||||||
" <max> max number of SocketMangers in pool, "+
|
+ " <i2cphost> host to connect to the router " + "(default: 127.0.0.1)\n"
|
||||||
"use neg. number\n"+
|
+ " <i2cpport> port to connect to the router " + "(default: 7654)");
|
||||||
" to use each SocketManager only once "+
|
|
||||||
"(default: 1)\n"+
|
|
||||||
" <i2cphost> host to connect to the router "+
|
|
||||||
"(default: 127.0.0.1)\n"+
|
|
||||||
" <i2cpport> port to connect to the router "+
|
|
||||||
"(default: 7654)");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -29,50 +29,49 @@ public class Request {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public Request(InputStream in) throws IOException {
|
public Request(InputStream in) throws IOException {
|
||||||
BufferedReader br = new BufferedReader
|
BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1"));
|
||||||
(new InputStreamReader(in, "ISO-8859-1"));
|
String line = br.readLine();
|
||||||
String line = br.readLine();
|
if (line == null) { // no data at all
|
||||||
if (line == null) { // no data at all
|
method = null;
|
||||||
method = null;
|
_log.error("Connection but no data");
|
||||||
_log.error("Connection but no data");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
int pos = line.indexOf(" ");
|
||||||
int pos = line.indexOf(" ");
|
if (pos == -1) {
|
||||||
if (pos == -1) {
|
method = line;
|
||||||
method = line;
|
url = "";
|
||||||
url="";
|
_log.error("Malformed HTTP request: " + line);
|
||||||
_log.error("Malformed HTTP request: "+line);
|
} else {
|
||||||
} else {
|
method = line.substring(0, pos);
|
||||||
method = line.substring(0,pos);
|
url = line.substring(pos + 1);
|
||||||
url=line.substring(pos+1);
|
}
|
||||||
}
|
proto = "";
|
||||||
proto="";
|
pos = url.indexOf(" ");
|
||||||
pos = url.indexOf(" ");
|
if (pos != -1) {
|
||||||
if (pos != -1) {
|
proto = url.substring(pos); // leading space intended
|
||||||
proto=url.substring(pos); // leading space intended
|
url = url.substring(0, pos);
|
||||||
url = url.substring(0,pos);
|
}
|
||||||
}
|
StringBuffer sb = new StringBuffer(512);
|
||||||
StringBuffer sb = new StringBuffer(512);
|
while ((line = br.readLine()) != null) {
|
||||||
while((line=br.readLine()) != null) {
|
if (line.length() == 0) break;
|
||||||
if (line.length() == 0) break;
|
sb.append(line).append("\r\n");
|
||||||
sb.append(line).append("\r\n");
|
}
|
||||||
}
|
params = sb.toString(); // no leading empty line!
|
||||||
params = sb.toString(); // no leading empty line!
|
sb = new StringBuffer();
|
||||||
sb = new StringBuffer();
|
// hack for POST requests, ripped from HttpClient
|
||||||
// hack for POST requests, ripped from HttpClient
|
// this won't work for large POSTDATA
|
||||||
// this won't work for large POSTDATA
|
// FIXME: do this better, please.
|
||||||
// FIXME: do this better, please.
|
if (!method.equals("GET")) {
|
||||||
if (!method.equals("GET")) {
|
while (br.ready()) { // empty the buffer (POST requests)
|
||||||
while (br.ready()) { // empty the buffer (POST requests)
|
int i = br.read();
|
||||||
int i=br.read();
|
if (i != -1) {
|
||||||
if (i != -1) {
|
sb.append((char) i);
|
||||||
sb.append((char)i);
|
}
|
||||||
}
|
}
|
||||||
}
|
postData = sb.toString();
|
||||||
postData = sb.toString();
|
} else {
|
||||||
} else {
|
postData = "";
|
||||||
postData="";
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -80,21 +79,21 @@ public class Request {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public byte[] toByteArray() throws IOException {
|
public byte[] toByteArray() throws IOException {
|
||||||
if (method == null) return null;
|
if (method == null) return null;
|
||||||
return toISO8859_1String().getBytes("ISO-8859-1");
|
return toISO8859_1String().getBytes("ISO-8859-1");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String toISO8859_1String() throws IOException {
|
private String toISO8859_1String() throws IOException {
|
||||||
if (method == null) return null;
|
if (method == null) return null;
|
||||||
return method+" "+url+proto+"\r\n"+params+"\r\n"+postData;
|
return method + " " + url + proto + "\r\n" + params + "\r\n" + postData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the URL of the request
|
* @return the URL of the request
|
||||||
*/
|
*/
|
||||||
public String getURL() {
|
public String getURL() {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,7 +101,7 @@ public class Request {
|
|||||||
* @param newURL the new URL
|
* @param newURL the new URL
|
||||||
*/
|
*/
|
||||||
public void setURL(String newURL) {
|
public void setURL(String newURL) {
|
||||||
url=newURL;
|
url = newURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,19 +110,17 @@ public class Request {
|
|||||||
* @return The value of the param, or null
|
* @return The value of the param, or null
|
||||||
*/
|
*/
|
||||||
public String getParam(String name) {
|
public String getParam(String name) {
|
||||||
try {
|
try {
|
||||||
BufferedReader br= new BufferedReader(new StringReader(params));
|
BufferedReader br = new BufferedReader(new StringReader(params));
|
||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
if (line.startsWith(name)) {
|
if (line.startsWith(name)) { return line.substring(name.length()); }
|
||||||
return line.substring(name.length());
|
}
|
||||||
}
|
return null;
|
||||||
}
|
} catch (IOException ex) {
|
||||||
return null;
|
_log.error("Error getting parameter", ex);
|
||||||
} catch (IOException ex) {
|
return null;
|
||||||
_log.error("Error getting parameter", ex);
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -132,25 +129,25 @@ public class Request {
|
|||||||
* @param value the value to be set
|
* @param value the value to be set
|
||||||
*/
|
*/
|
||||||
public void setParam(String name, String value) {
|
public void setParam(String name, String value) {
|
||||||
try {
|
try {
|
||||||
StringBuffer sb = new StringBuffer(params.length()+value.length());
|
StringBuffer sb = new StringBuffer(params.length() + value.length());
|
||||||
BufferedReader br= new BufferedReader(new StringReader(params));
|
BufferedReader br = new BufferedReader(new StringReader(params));
|
||||||
String line;
|
String line;
|
||||||
boolean replaced = false;
|
boolean replaced = false;
|
||||||
while((line=br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
if (line.startsWith(name)) {
|
if (line.startsWith(name)) {
|
||||||
replaced=true;
|
replaced = true;
|
||||||
if (value == null) continue; // kill param
|
if (value == null) continue; // kill param
|
||||||
line = name+value;
|
line = name + value;
|
||||||
}
|
}
|
||||||
sb.append(line).append("\r\n");
|
sb.append(line).append("\r\n");
|
||||||
}
|
}
|
||||||
if (!replaced && value != null) {
|
if (!replaced && value != null) {
|
||||||
sb.append(name).append(value).append("\r\n");
|
sb.append(name).append(value).append("\r\n");
|
||||||
}
|
}
|
||||||
params=sb.toString();
|
params = sb.toString();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Error getting parameter", ex);
|
_log.error("Error getting parameter", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -29,53 +29,46 @@ public class SocketManagerProducer extends Thread {
|
|||||||
* @param host which host to listen on
|
* @param host which host to listen on
|
||||||
* @param port which port to listen on
|
* @param port which port to listen on
|
||||||
*/
|
*/
|
||||||
public SocketManagerProducer(I2PSocketManager[] initialManagers,
|
public SocketManagerProducer(I2PSocketManager[] initialManagers, int maxManagers, boolean shouldThrowAwayManagers,
|
||||||
int maxManagers,
|
String host, int port) {
|
||||||
boolean shouldThrowAwayManagers,
|
if (maxManagers < 1) { throw new IllegalArgumentException("maxManagers < 1"); }
|
||||||
String host, int port) {
|
this.host = host;
|
||||||
if (maxManagers < 1) {
|
this.port = port;
|
||||||
throw new IllegalArgumentException("maxManagers < 1");
|
this.shouldThrowAwayManagers = shouldThrowAwayManagers;
|
||||||
}
|
if (initialManagers != null) {
|
||||||
this.host=host;
|
myManagers.addAll(Arrays.asList(initialManagers));
|
||||||
this.port=port;
|
}
|
||||||
this.shouldThrowAwayManagers=shouldThrowAwayManagers;
|
this.maxManagers = maxManagers;
|
||||||
if (initialManagers != null) {
|
this.shouldThrowAwayManagers = shouldThrowAwayManagers;
|
||||||
myManagers.addAll(Arrays.asList(initialManagers));
|
setDaemon(true);
|
||||||
}
|
start();
|
||||||
this.maxManagers=maxManagers;
|
|
||||||
this.shouldThrowAwayManagers=shouldThrowAwayManagers;
|
|
||||||
setDaemon(true);
|
|
||||||
start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thread producing new SocketManagers.
|
* Thread producing new SocketManagers.
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
synchronized(this) {
|
synchronized (this) {
|
||||||
// without mcDonalds mode, we most probably need no
|
// without mcDonalds mode, we most probably need no
|
||||||
// new managers.
|
// new managers.
|
||||||
while (!shouldThrowAwayManagers && myManagers.size() == maxManagers) {
|
while (!shouldThrowAwayManagers && myManagers.size() == maxManagers) {
|
||||||
myWait();
|
myWait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// produce a new manager, regardless whether it is needed
|
// produce a new manager, regardless whether it is needed
|
||||||
// or not. Do not synchronized this part, since it can be
|
// or not. Do not synchronized this part, since it can be
|
||||||
// quite time-consuming.
|
// quite time-consuming.
|
||||||
I2PSocketManager newManager =
|
I2PSocketManager newManager = I2PSocketManagerFactory.createManager(host, port, new Properties());
|
||||||
I2PSocketManagerFactory.createManager(host, port,
|
// when done, check if it is needed.
|
||||||
new Properties());
|
synchronized (this) {
|
||||||
// when done, check if it is needed.
|
while (myManagers.size() == maxManagers) {
|
||||||
synchronized(this) {
|
myWait();
|
||||||
while(myManagers.size() == maxManagers) {
|
}
|
||||||
myWait();
|
myManagers.add(newManager);
|
||||||
}
|
notifyAll();
|
||||||
myManagers.add(newManager);
|
}
|
||||||
notifyAll();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,12 +79,12 @@ public class SocketManagerProducer extends Thread {
|
|||||||
* @return the SocketManager to use
|
* @return the SocketManager to use
|
||||||
*/
|
*/
|
||||||
public synchronized I2PSocketManager getManager(String dest) {
|
public synchronized I2PSocketManager getManager(String dest) {
|
||||||
I2PSocketManager result = (I2PSocketManager) usedManagers.get(dest);
|
I2PSocketManager result = (I2PSocketManager) usedManagers.get(dest);
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
result = getManager();
|
result = getManager();
|
||||||
usedManagers.put(dest,result);
|
usedManagers.put(dest, result);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,26 +95,26 @@ public class SocketManagerProducer extends Thread {
|
|||||||
* @return the SocketManager to use
|
* @return the SocketManager to use
|
||||||
*/
|
*/
|
||||||
public synchronized I2PSocketManager getManager() {
|
public synchronized I2PSocketManager getManager() {
|
||||||
while (myManagers.size() == 0) {
|
while (myManagers.size() == 0) {
|
||||||
myWait(); // no manager here, so wait until one is produced
|
myWait(); // no manager here, so wait until one is produced
|
||||||
}
|
}
|
||||||
int which = (int)(Math.random()*myManagers.size());
|
int which = (int) (Math.random() * myManagers.size());
|
||||||
I2PSocketManager result = (I2PSocketManager) myManagers.get(which);
|
I2PSocketManager result = (I2PSocketManager) myManagers.get(which);
|
||||||
if (shouldThrowAwayManagers) {
|
if (shouldThrowAwayManagers) {
|
||||||
myManagers.remove(which);
|
myManagers.remove(which);
|
||||||
notifyAll();
|
notifyAll();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wait until InterruptedException
|
* Wait until InterruptedException
|
||||||
*/
|
*/
|
||||||
public void myWait() {
|
public void myWait() {
|
||||||
try {
|
try {
|
||||||
wait();
|
wait();
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,49 +14,48 @@ public class ChainFilter implements Filter {
|
|||||||
|
|
||||||
private static final Log _log = new Log(ChainFilter.class);
|
private static final Log _log = new Log(ChainFilter.class);
|
||||||
|
|
||||||
private Collection filters; // perhaps protected?
|
private Collection filters; // perhaps protected?
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param filters A collection (list) of filters to chain to
|
* @param filters A collection (list) of filters to chain to
|
||||||
*/
|
*/
|
||||||
public ChainFilter(Collection filters) {
|
public ChainFilter(Collection filters) {
|
||||||
this.filters=filters;
|
this.filters = filters;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see net.i2p.httptunnel.filter.Filter#filter(byte[])
|
* @see net.i2p.httptunnel.filter.Filter#filter(byte[])
|
||||||
*/
|
*/
|
||||||
public byte[] filter(byte[] toFilter) {
|
public byte[] filter(byte[] toFilter) {
|
||||||
byte[] buf = toFilter;
|
byte[] buf = toFilter;
|
||||||
for (Iterator it = filters.iterator(); it.hasNext();) {
|
for (Iterator it = filters.iterator(); it.hasNext();) {
|
||||||
Filter f = (Filter) it.next();
|
Filter f = (Filter) it.next();
|
||||||
buf = f.filter(buf);
|
buf = f.filter(buf);
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see net.i2p.httptunnel.filter.Filter#finish()
|
* @see net.i2p.httptunnel.filter.Filter#finish()
|
||||||
*/
|
*/
|
||||||
public byte[] finish() {
|
public byte[] finish() {
|
||||||
// this is a bit complicated. Think about it...
|
// this is a bit complicated. Think about it...
|
||||||
try {
|
try {
|
||||||
byte[] buf = EMPTY;
|
byte[] buf = EMPTY;
|
||||||
for (Iterator it = filters.iterator(); it.hasNext();) {
|
for (Iterator it = filters.iterator(); it.hasNext();) {
|
||||||
Filter f = (Filter) it.next();
|
Filter f = (Filter) it.next();
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
if (buf.length != 0) {
|
if (buf.length != 0) {
|
||||||
baos.write(f.filter(buf));
|
baos.write(f.filter(buf));
|
||||||
}
|
}
|
||||||
baos.write(f.finish());
|
baos.write(f.finish());
|
||||||
buf = baos.toByteArray();
|
buf = baos.toByteArray();
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Error chaining filters", ex);
|
_log.error("Error chaining filters", ex);
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -9,13 +9,13 @@ public class NullFilter implements Filter {
|
|||||||
* @see net.i2p.httptunnel.filter.Filter#filter(byte[])
|
* @see net.i2p.httptunnel.filter.Filter#filter(byte[])
|
||||||
*/
|
*/
|
||||||
public byte[] filter(byte[] toFilter) {
|
public byte[] filter(byte[] toFilter) {
|
||||||
return toFilter;
|
return toFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see net.i2p.httptunnel.filter.Filter#finish()
|
* @see net.i2p.httptunnel.filter.Filter#finish()
|
||||||
*/
|
*/
|
||||||
public byte[] finish() {
|
public byte[] finish() {
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
package net.i2p.httptunnel.handler;
|
package net.i2p.httptunnel.handler;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -27,8 +28,8 @@ public class EepHandler {
|
|||||||
|
|
||||||
protected ErrorHandler errorHandler;
|
protected ErrorHandler errorHandler;
|
||||||
|
|
||||||
/* package private */ EepHandler(ErrorHandler eh) {
|
/* package private */EepHandler(ErrorHandler eh) {
|
||||||
errorHandler=eh;
|
errorHandler = eh;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,21 +41,19 @@ public class EepHandler {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handle(Request req, HTTPListener httpl, OutputStream out,
|
public void handle(Request req, HTTPListener httpl, OutputStream out,
|
||||||
/* boolean fromProxy, */ String destination)
|
/* boolean fromProxy, */String destination) throws IOException {
|
||||||
throws IOException {
|
SocketManagerProducer smp = httpl.getSMP();
|
||||||
SocketManagerProducer smp = httpl.getSMP();
|
Destination dest = NamingService.getInstance().lookup(destination);
|
||||||
Destination dest = NamingService.getInstance().lookup(destination);
|
if (dest == null) {
|
||||||
if (dest == null) {
|
errorHandler.handle(req, httpl, out, "Could not lookup host: " + destination);
|
||||||
errorHandler.handle(req, httpl, out,
|
return;
|
||||||
"Could not lookup host: "+destination);
|
}
|
||||||
return;
|
I2PSocketManager sm = smp.getManager(destination);
|
||||||
}
|
Filter f = new NullFilter(); //FIXME: use other filter
|
||||||
I2PSocketManager sm = smp.getManager(destination);
|
req.setParam("Host: ", dest.toBase64());
|
||||||
Filter f = new NullFilter(); //FIXME: use other filter
|
if (!handle(req, f, out, dest, sm)) {
|
||||||
req.setParam("Host: ", dest.toBase64());
|
errorHandler.handle(req, httpl, out, "Unable to reach peer");
|
||||||
if (!handle(req, f, out, dest, sm)) {
|
}
|
||||||
errorHandler.handle(req, httpl, out, "Unable to reach peer");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,45 +65,44 @@ public class EepHandler {
|
|||||||
* @return boolean, true if something was written, false otherwise.
|
* @return boolean, true if something was written, false otherwise.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public boolean handle(Request req, Filter f, OutputStream out,
|
public boolean handle(Request req, Filter f, OutputStream out, Destination dest, I2PSocketManager sm)
|
||||||
Destination dest, I2PSocketManager sm)
|
throws IOException {
|
||||||
throws IOException {
|
I2PSocket s = null;
|
||||||
I2PSocket s = null;
|
boolean written = false;
|
||||||
boolean written = false;
|
try {
|
||||||
try {
|
synchronized (sm) {
|
||||||
synchronized(sm) {
|
s = sm.connect(dest, new I2PSocketOptions());
|
||||||
s = sm.connect(dest, new I2PSocketOptions());
|
}
|
||||||
}
|
InputStream in = new BufferedInputStream(s.getInputStream());
|
||||||
InputStream in = new BufferedInputStream(s.getInputStream());
|
OutputStream sout = new BufferedOutputStream(s.getOutputStream());
|
||||||
OutputStream sout = new BufferedOutputStream(s.getOutputStream());
|
sout.write(req.toByteArray());
|
||||||
sout.write(req.toByteArray());
|
sout.flush();
|
||||||
sout.flush();
|
byte[] buffer = new byte[16384], filtered;
|
||||||
byte[] buffer = new byte[16384], filtered;
|
int len;
|
||||||
int len;
|
while ((len = in.read(buffer)) != -1) {
|
||||||
while ((len=in.read(buffer)) != -1) {
|
if (len != buffer.length) {
|
||||||
if (len != buffer.length) {
|
byte[] b2 = new byte[len];
|
||||||
byte[] b2 = new byte[len];
|
System.arraycopy(buffer, 0, b2, 0, len);
|
||||||
System.arraycopy(buffer, 0, b2, 0, len);
|
filtered = f.filter(b2);
|
||||||
filtered=f.filter(b2);
|
} else {
|
||||||
} else {
|
filtered = f.filter(buffer);
|
||||||
filtered=f.filter(buffer);
|
}
|
||||||
}
|
written = true;
|
||||||
written=true;
|
out.write(filtered);
|
||||||
out.write(filtered);
|
}
|
||||||
}
|
filtered = f.finish();
|
||||||
filtered=f.finish();
|
written = true;
|
||||||
written=true;
|
out.write(filtered);
|
||||||
out.write(filtered);
|
out.flush();
|
||||||
out.flush();
|
} catch (IOException ex) {
|
||||||
} catch (IOException ex) {
|
_log.error("Error while handling eepsite request");
|
||||||
_log.error("Error while handling eepsite request");
|
return written;
|
||||||
return written;
|
} catch (I2PException ex) {
|
||||||
} catch (I2PException ex) {
|
_log.error("Error while handling eepsite request");
|
||||||
_log.error("Error while handling eepsite request");
|
return written;
|
||||||
return written;
|
} finally {
|
||||||
} finally {
|
if (s != null) s.close();
|
||||||
if (s != null) s.close();
|
}
|
||||||
}
|
return true;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
package net.i2p.httptunnel.handler;
|
package net.i2p.httptunnel.handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ public class ErrorHandler {
|
|||||||
|
|
||||||
private static final Log _log = new Log(ErrorHandler.class);
|
private static final Log _log = new Log(ErrorHandler.class);
|
||||||
|
|
||||||
/* package private */ ErrorHandler() {
|
/* package private */ErrorHandler() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,20 +25,17 @@ public class ErrorHandler {
|
|||||||
* @param error the error that happened
|
* @param error the error that happened
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handle(Request req, HTTPListener httpl,
|
public void handle(Request req, HTTPListener httpl, OutputStream out, String error) throws IOException {
|
||||||
OutputStream out, String error) throws IOException {
|
// FIXME: Make nicer messages for more likely errors.
|
||||||
// FIXME: Make nicer messages for more likely errors.
|
out
|
||||||
out.write(("HTTP/1.1 500 Internal Server Error\r\n"+
|
.write(("HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: text/html; charset=iso-8859-1\r\n\r\n")
|
||||||
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n")
|
.getBytes("ISO-8859-1"));
|
||||||
.getBytes("ISO-8859-1"));
|
out
|
||||||
out.write(("<html><head><title>"+error+"</title></head><body><h1>"+
|
.write(("<html><head><title>" + error + "</title></head><body><h1>" + error
|
||||||
error+"</h1>An internal error occurred while "+
|
+ "</h1>An internal error occurred while " + "handling a request by HTTPTunnel:<br><b>" + error + "</b><h2>Complete request:</h2><b>---</b><br><i><pre>\r\n")
|
||||||
"handling a request by HTTPTunnel:<br><b>"+error+
|
.getBytes("ISO-8859-1"));
|
||||||
"</b><h2>Complete request:</h2><b>---</b><br><i><pre>\r\n")
|
out.write(req.toByteArray());
|
||||||
.getBytes("ISO-8859-1"));
|
out.write(("</pre></i><br><b>---</b></body></html>").getBytes("ISO-8859-1"));
|
||||||
out.write(req.toByteArray());
|
out.flush();
|
||||||
out.write(("</pre></i><br><b>---</b></body></html>")
|
|
||||||
.getBytes("ISO-8859-1"));
|
|
||||||
out.flush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
package net.i2p.httptunnel.handler;
|
package net.i2p.httptunnel.handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ public class LocalHandler {
|
|||||||
|
|
||||||
private static final Log _log = new Log(LocalHandler.class);
|
private static final Log _log = new Log(LocalHandler.class);
|
||||||
|
|
||||||
/* package private */ LocalHandler() {
|
/* package private */LocalHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,16 +25,16 @@ public class LocalHandler {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handle(Request req, HTTPListener httpl, OutputStream out
|
public void handle(Request req, HTTPListener httpl, OutputStream out
|
||||||
/*, boolean fromProxy */) throws IOException {
|
/*, boolean fromProxy */) throws IOException {
|
||||||
//FIXME: separate multiple pages, not only a start page
|
//FIXME: separate multiple pages, not only a start page
|
||||||
//FIXME: provide some info on this page
|
//FIXME: provide some info on this page
|
||||||
out.write(("HTTP/1.1 200 Document following\r\n"+
|
out
|
||||||
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n"+
|
.write(("HTTP/1.1 200 Document following\r\n" + "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
|
||||||
"<html><head><title>Welcome to I2P HTTPTunnel</title>"+
|
+ "<html><head><title>Welcome to I2P HTTPTunnel</title>"
|
||||||
"</head><body><h1>Welcome to I2P HTTPTunnel</h1>You can "+
|
+ "</head><body><h1>Welcome to I2P HTTPTunnel</h1>You can "
|
||||||
"browse Eepsites by adding an eepsite name to the request."+
|
+ "browse Eepsites by adding an eepsite name to the request." + "</body></html>")
|
||||||
"</body></html>").getBytes("ISO-8859-1"));
|
.getBytes("ISO-8859-1"));
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -43,11 +44,10 @@ public class LocalHandler {
|
|||||||
* @param out where to write the results
|
* @param out where to write the results
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handleProxyConfWarning(Request req, HTTPListener httpl,
|
public void handleProxyConfWarning(Request req, HTTPListener httpl, OutputStream out) throws IOException {
|
||||||
OutputStream out) throws IOException {
|
//FIXME
|
||||||
//FIXME
|
|
||||||
throw new IOException("jrandom ate the deprecated method. mooo");
|
throw new IOException("jrandom ate the deprecated method. mooo");
|
||||||
//httpl.handleNotImplemented(out);
|
//httpl.handleNotImplemented(out);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,11 +58,10 @@ public class LocalHandler {
|
|||||||
* @param out where to write the results
|
* @param out where to write the results
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handleHTTPWarning(Request req, HTTPListener httpl,
|
public void handleHTTPWarning(Request req, HTTPListener httpl, OutputStream out /*, boolean fromProxy */)
|
||||||
OutputStream out /*, boolean fromProxy */)
|
throws IOException {
|
||||||
throws IOException {
|
// FIXME
|
||||||
// FIXME
|
|
||||||
throw new IOException("jrandom ate the deprecated method. mooo");
|
throw new IOException("jrandom ate the deprecated method. mooo");
|
||||||
//httpl.handleNotImplemented(out);
|
//httpl.handleNotImplemented(out);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
package net.i2p.httptunnel.handler;
|
package net.i2p.httptunnel.handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
@ -19,8 +20,8 @@ public class ProxyHandler extends EepHandler {
|
|||||||
|
|
||||||
private static final Log _log = new Log(ErrorHandler.class);
|
private static final Log _log = new Log(ErrorHandler.class);
|
||||||
|
|
||||||
/* package private */ ProxyHandler(ErrorHandler eh) {
|
/* package private */ProxyHandler(ErrorHandler eh) {
|
||||||
super(eh);
|
super(eh);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,24 +31,23 @@ public class ProxyHandler extends EepHandler {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handle(Request req, HTTPListener httpl, OutputStream out
|
public void handle(Request req, HTTPListener httpl, OutputStream out
|
||||||
/*, boolean fromProxy */) throws IOException {
|
/*, boolean fromProxy */) throws IOException {
|
||||||
SocketManagerProducer smp = httpl.getSMP();
|
SocketManagerProducer smp = httpl.getSMP();
|
||||||
Destination dest = findProxy();
|
Destination dest = findProxy();
|
||||||
if (dest == null) {
|
if (dest == null) {
|
||||||
errorHandler.handle(req, httpl, out,
|
errorHandler.handle(req, httpl, out, "Could not find proxy");
|
||||||
"Could not find proxy");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
// one manager for all proxy requests
|
||||||
// one manager for all proxy requests
|
I2PSocketManager sm = smp.getManager("--proxy--");
|
||||||
I2PSocketManager sm = smp.getManager("--proxy--");
|
Filter f = new NullFilter(); //FIXME: use other filter
|
||||||
Filter f = new NullFilter(); //FIXME: use other filter
|
if (!handle(req, f, out, dest, sm)) {
|
||||||
if (!handle(req, f, out, dest, sm)) {
|
errorHandler.handle(req, httpl, out, "Unable to reach peer");
|
||||||
errorHandler.handle(req, httpl, out, "Unable to reach peer");
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Destination findProxy() {
|
private Destination findProxy() {
|
||||||
//FIXME!
|
//FIXME!
|
||||||
return NamingService.getInstance().lookup("squid.i2p");
|
return NamingService.getInstance().lookup("squid.i2p");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
package net.i2p.httptunnel.handler;
|
package net.i2p.httptunnel.handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
@ -14,10 +15,10 @@ public class RootHandler {
|
|||||||
private static final Log _log = new Log(RootHandler.class);
|
private static final Log _log = new Log(RootHandler.class);
|
||||||
|
|
||||||
private RootHandler() {
|
private RootHandler() {
|
||||||
errorHandler=new ErrorHandler();
|
errorHandler = new ErrorHandler();
|
||||||
localHandler=new LocalHandler();
|
localHandler = new LocalHandler();
|
||||||
proxyHandler=new ProxyHandler(errorHandler);
|
proxyHandler = new ProxyHandler(errorHandler);
|
||||||
eepHandler=new EepHandler(errorHandler);
|
eepHandler = new EepHandler(errorHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ErrorHandler errorHandler;
|
private ErrorHandler errorHandler;
|
||||||
@ -32,10 +33,10 @@ public class RootHandler {
|
|||||||
* @return the one and only instance, yay!
|
* @return the one and only instance, yay!
|
||||||
*/
|
*/
|
||||||
public static synchronized RootHandler getInstance() {
|
public static synchronized RootHandler getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new RootHandler();
|
instance = new RootHandler();
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,76 +46,73 @@ public class RootHandler {
|
|||||||
* @param out where to write the results
|
* @param out where to write the results
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void handle(Request req, HTTPListener httpl,
|
public void handle(Request req, HTTPListener httpl, OutputStream out) throws IOException {
|
||||||
OutputStream out) throws IOException {
|
String url = req.getURL();
|
||||||
String url=req.getURL();
|
System.out.println(url);
|
||||||
System.out.println(url);
|
/* boolean byProxy = false; */
|
||||||
/* boolean byProxy = false; */
|
int pos;
|
||||||
int pos;
|
if (url.startsWith("http://")) { // access via proxy
|
||||||
if (url.startsWith("http://")) { // access via proxy
|
/* byProxy=true; */
|
||||||
/* byProxy=true; */
|
if (httpl.firstProxyUse()) {
|
||||||
if (httpl.firstProxyUse()) {
|
localHandler.handleProxyConfWarning(req, httpl, out);
|
||||||
localHandler.handleProxyConfWarning(req,httpl,out);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
url = url.substring(7);
|
||||||
url = url.substring(7);
|
pos = url.indexOf("/");
|
||||||
pos = url.indexOf("/");
|
String host;
|
||||||
String host;
|
String rest;
|
||||||
String rest;
|
if (pos == -1) {
|
||||||
if (pos == -1) {
|
errorHandler.handle(req, httpl, out, "No host end in URL");
|
||||||
errorHandler.handle(req, httpl, out, "No host end in URL");
|
return;
|
||||||
return;
|
} else {
|
||||||
} else {
|
host = url.substring(0, pos);
|
||||||
host = url.substring(0,pos);
|
url = url.substring(pos);
|
||||||
url = url.substring(pos);
|
if ("i2p".equals(host) || "i2p.i2p".equals(host)) {
|
||||||
if ("i2p".equals(host) || "i2p.i2p".equals(host)) {
|
// normal request; go on below...
|
||||||
// normal request; go on below...
|
} else if (host.endsWith(".i2p")) {
|
||||||
} else if (host.endsWith(".i2p")) {
|
// "old" service request, send a redirect...
|
||||||
// "old" service request, send a redirect...
|
out
|
||||||
out.write(("HTTP/1.1 302 Moved\r\nLocation: "+
|
.write(("HTTP/1.1 302 Moved\r\nLocation: " + "http://i2p.i2p/" + host + url + "\r\n\r\n")
|
||||||
"http://i2p.i2p/"+host+url+
|
.getBytes("ISO-8859-1"));
|
||||||
"\r\n\r\n").getBytes("ISO-8859-1"));
|
return;
|
||||||
return;
|
} else {
|
||||||
} else {
|
// this is for proxying to the real web
|
||||||
// this is for proxying to the real web
|
proxyHandler.handle(req, httpl, out /*, true */);
|
||||||
proxyHandler.handle(req, httpl, out /*, true */);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (url.equals("/")) { // main page
|
||||||
if (url.equals("/")) { // main page
|
url = "/_/local/index";
|
||||||
url="/_/local/index";
|
} else if (!url.startsWith("/")) {
|
||||||
} else if (!url.startsWith("/")) {
|
errorHandler.handle(req, httpl, out, "No leading slash in URL: " + url);
|
||||||
errorHandler.handle(req, httpl, out,
|
return;
|
||||||
"No leading slash in URL: "+url);
|
}
|
||||||
return;
|
String dest;
|
||||||
}
|
url = url.substring(1);
|
||||||
String dest;
|
pos = url.indexOf("/");
|
||||||
url=url.substring(1);
|
if (pos == -1) {
|
||||||
pos = url.indexOf("/");
|
dest = url;
|
||||||
if (pos == -1) {
|
url = "/";
|
||||||
dest=url;
|
} else {
|
||||||
url="/";
|
dest = url.substring(0, pos);
|
||||||
} else {
|
url = url.substring(pos);
|
||||||
dest = url.substring(0,pos);
|
}
|
||||||
url=url.substring(pos);
|
req.setURL(url);
|
||||||
}
|
if (dest.equals("_")) { // no eepsite
|
||||||
req.setURL(url);
|
if (url.startsWith("/local/")) { // local request
|
||||||
if (dest.equals("_")) { // no eepsite
|
req.setURL(url.substring(6));
|
||||||
if (url.startsWith("/local/")) { // local request
|
localHandler.handle(req, httpl, out /*, byProxy */);
|
||||||
req.setURL(url.substring(6));
|
} else if (url.startsWith("/http/")) { // http warning
|
||||||
localHandler.handle(req, httpl, out /*, byProxy */);
|
localHandler.handleHTTPWarning(req, httpl, out /*, byProxy */);
|
||||||
} else if (url.startsWith("/http/")) { // http warning
|
} else if (url.startsWith("/proxy/")) { // http proxying
|
||||||
localHandler.handleHTTPWarning(req, httpl, out /*, byProxy */);
|
req.setURL("http://" + url.substring(7));
|
||||||
} else if (url.startsWith("/proxy/")) { // http proxying
|
proxyHandler.handle(req, httpl, out /*, byProxy */);
|
||||||
req.setURL("http://"+url.substring(7));
|
} else {
|
||||||
proxyHandler.handle(req, httpl, out /*, byProxy */);
|
errorHandler.handle(req, httpl, out, "No local handler for this URL: " + url);
|
||||||
} else {
|
}
|
||||||
errorHandler.handle(req, httpl, out,
|
} else {
|
||||||
"No local handler for this URL: "+url);
|
eepHandler.handle(req, httpl, out, /* byProxy, */dest);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
eepHandler.handle(req, httpl, out, /* byProxy, */ dest);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,14 +18,17 @@ class BufferLogger implements Logging {
|
|||||||
private boolean _ignore;
|
private boolean _ignore;
|
||||||
|
|
||||||
public BufferLogger() {
|
public BufferLogger() {
|
||||||
_baos = new ByteArrayOutputStream(512);
|
_baos = new ByteArrayOutputStream(512);
|
||||||
_ignore = false;
|
_ignore = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static String EMPTY = "";
|
private final static String EMPTY = "";
|
||||||
|
|
||||||
public String getBuffer() {
|
public String getBuffer() {
|
||||||
if (_ignore) return EMPTY;
|
if (_ignore)
|
||||||
else return new String(_baos.toByteArray());
|
return EMPTY;
|
||||||
|
else
|
||||||
|
return new String(_baos.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,11 +39,11 @@ class BufferLogger implements Logging {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void ignoreFurtherActions() {
|
public void ignoreFurtherActions() {
|
||||||
_ignore = true;
|
_ignore = true;
|
||||||
synchronized (_baos) {
|
synchronized (_baos) {
|
||||||
_baos.reset();
|
_baos.reset();
|
||||||
}
|
}
|
||||||
_baos = null;
|
_baos = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,15 +51,15 @@ class BufferLogger implements Logging {
|
|||||||
* @param s String containing what we're logging.
|
* @param s String containing what we're logging.
|
||||||
*/
|
*/
|
||||||
public void log(String s) {
|
public void log(String s) {
|
||||||
if (_ignore) return;
|
if (_ignore) return;
|
||||||
if (s != null) {
|
if (s != null) {
|
||||||
_log.debug("logging [" + s + "]");
|
_log.debug("logging [" + s + "]");
|
||||||
try {
|
try {
|
||||||
_baos.write(s.getBytes());
|
_baos.write(s.getBytes());
|
||||||
_baos.write('\n');
|
_baos.write('\n');
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
_log.error("Error logging [" + s + "]");
|
_log.error("Error logging [" + s + "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
File diff suppressed because it is too large
Load Diff
@ -18,44 +18,42 @@ public class I2PTunnelClient extends I2PTunnelClientBase {
|
|||||||
|
|
||||||
protected Destination dest;
|
protected Destination dest;
|
||||||
|
|
||||||
public I2PTunnelClient(int localPort, String destination,
|
public I2PTunnelClient(int localPort, String destination, Logging l, boolean ownDest, EventDispatcher notifyThis) {
|
||||||
Logging l, boolean ownDest,
|
super(localPort, ownDest, l, notifyThis, "SynSender");
|
||||||
EventDispatcher notifyThis) {
|
|
||||||
super(localPort, ownDest, l, notifyThis, "SynSender");
|
|
||||||
|
|
||||||
if (waitEventValue("openBaseClientResult").equals("error")) {
|
if (waitEventValue("openBaseClientResult").equals("error")) {
|
||||||
notifyEvent("openClientResult", "error");
|
notifyEvent("openClientResult", "error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
dest=I2PTunnel.destFromName(destination);
|
dest = I2PTunnel.destFromName(destination);
|
||||||
if (dest == null) {
|
if (dest == null) {
|
||||||
l.log("Could not resolve " + destination + ".");
|
l.log("Could not resolve " + destination + ".");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (DataFormatException e) {
|
} catch (DataFormatException e) {
|
||||||
l.log("Bad format in destination \"" + destination + "\".");
|
l.log("Bad format in destination \"" + destination + "\".");
|
||||||
notifyEvent("openClientResult", "error");
|
notifyEvent("openClientResult", "error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setName(getLocalPort() + " -> " + destination);
|
setName(getLocalPort() + " -> " + destination);
|
||||||
|
|
||||||
startRunning();
|
startRunning();
|
||||||
|
|
||||||
notifyEvent("openClientResult", "ok");
|
notifyEvent("openClientResult", "ok");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void clientConnectionRun(Socket s) {
|
protected void clientConnectionRun(Socket s) {
|
||||||
try {
|
try {
|
||||||
I2PSocket i2ps = createI2PSocket(dest);
|
I2PSocket i2ps = createI2PSocket(dest);
|
||||||
new I2PTunnelRunner(s, i2ps, sockLock, null);
|
new I2PTunnelRunner(s, i2ps, sockLock, null);
|
||||||
} catch (I2PException ex) {
|
} catch (I2PException ex) {
|
||||||
_log.info("Error connecting", ex);
|
_log.info("Error connecting", ex);
|
||||||
l.log("Unable to reach peer");
|
l.log("Unable to reach peer");
|
||||||
// s has been initialized before the try block...
|
// s has been initialized before the try block...
|
||||||
closeSocket(s);
|
closeSocket(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,13 +23,12 @@ import net.i2p.util.EventDispatcher;
|
|||||||
import net.i2p.util.I2PThread;
|
import net.i2p.util.I2PThread;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
|
||||||
public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runnable {
|
||||||
implements Runnable {
|
|
||||||
|
|
||||||
private static final Log _log = new Log(I2PTunnelClientBase.class);
|
private static final Log _log = new Log(I2PTunnelClientBase.class);
|
||||||
protected Logging l;
|
protected Logging l;
|
||||||
|
|
||||||
private static final long DEFAULT_CONNECT_TIMEOUT = 60*1000;
|
private static final long DEFAULT_CONNECT_TIMEOUT = 60 * 1000;
|
||||||
|
|
||||||
protected Object sockLock = new Object(); // Guards sockMgr and mySockets
|
protected Object sockLock = new Object(); // Guards sockMgr and mySockets
|
||||||
private I2PSocketManager sockMgr;
|
private I2PSocketManager sockMgr;
|
||||||
@ -56,63 +55,59 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
|||||||
// I2PTunnelClientBase(localPort, ownDest, l, (EventDispatcher)null);
|
// I2PTunnelClientBase(localPort, ownDest, l, (EventDispatcher)null);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public I2PTunnelClientBase(int localPort, boolean ownDest,
|
public I2PTunnelClientBase(int localPort, boolean ownDest, Logging l, EventDispatcher notifyThis, String handlerName) {
|
||||||
Logging l, EventDispatcher notifyThis,
|
super(localPort + " (uninitialized)", notifyThis);
|
||||||
String handlerName) {
|
this.localPort = localPort;
|
||||||
super(localPort+" (uninitialized)", notifyThis);
|
this.l = l;
|
||||||
this.localPort=localPort;
|
this.handlerName = handlerName;
|
||||||
this.l = l;
|
|
||||||
this.handlerName=handlerName;
|
|
||||||
|
|
||||||
synchronized(sockLock) {
|
synchronized (sockLock) {
|
||||||
if (ownDest) {
|
if (ownDest) {
|
||||||
sockMgr=buildSocketManager();
|
sockMgr = buildSocketManager();
|
||||||
} else {
|
} else {
|
||||||
sockMgr=getSocketManager();
|
sockMgr = getSocketManager();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sockMgr == null) throw new NullPointerException();
|
if (sockMgr == null) throw new NullPointerException();
|
||||||
l.log("I2P session created");
|
l.log("I2P session created");
|
||||||
|
|
||||||
Thread t = new I2PThread(this);
|
Thread t = new I2PThread(this);
|
||||||
t.setName("Client");
|
t.setName("Client");
|
||||||
listenerReady=false;
|
listenerReady = false;
|
||||||
t.start();
|
t.start();
|
||||||
open=true;
|
open = true;
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
while (!listenerReady) {
|
while (!listenerReady) {
|
||||||
try {
|
try {
|
||||||
wait();
|
wait();
|
||||||
}
|
} catch (InterruptedException e) {
|
||||||
catch (InterruptedException e) {
|
// ignore
|
||||||
// ignore
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (open && listenerReady) {
|
if (open && listenerReady) {
|
||||||
l.log("Ready! Port " + getLocalPort());
|
l.log("Ready! Port " + getLocalPort());
|
||||||
notifyEvent("openBaseClientResult", "ok");
|
notifyEvent("openBaseClientResult", "ok");
|
||||||
} else {
|
} else {
|
||||||
l.log("Error!");
|
l.log("Error!");
|
||||||
notifyEvent("openBaseClientResult", "error");
|
notifyEvent("openBaseClientResult", "error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static I2PSocketManager socketManager;
|
private static I2PSocketManager socketManager;
|
||||||
|
|
||||||
protected static synchronized I2PSocketManager getSocketManager() {
|
protected static synchronized I2PSocketManager getSocketManager() {
|
||||||
if (socketManager == null) {
|
if (socketManager == null) {
|
||||||
socketManager = buildSocketManager();
|
socketManager = buildSocketManager();
|
||||||
}
|
}
|
||||||
return socketManager;
|
return socketManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static I2PSocketManager buildSocketManager() {
|
protected static I2PSocketManager buildSocketManager() {
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
props.putAll(System.getProperties());
|
props.putAll(System.getProperties());
|
||||||
return I2PSocketManagerFactory.createManager
|
return I2PSocketManagerFactory.createManager(I2PTunnel.host, Integer.parseInt(I2PTunnel.port), props);
|
||||||
(I2PTunnel.host, Integer.parseInt(I2PTunnel.port), props);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final int getLocalPort() {
|
public final int getLocalPort() {
|
||||||
@ -120,15 +115,14 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected final InetAddress getListenHost(Logging l) {
|
protected final InetAddress getListenHost(Logging l) {
|
||||||
try {
|
try {
|
||||||
return InetAddress.getByName(I2PTunnel.listenHost);
|
return InetAddress.getByName(I2PTunnel.listenHost);
|
||||||
} catch (UnknownHostException uhe) {
|
} catch (UnknownHostException uhe) {
|
||||||
l.log("Could not find listen host to bind to [" +
|
l.log("Could not find listen host to bind to [" + I2PTunnel.host + "]");
|
||||||
I2PTunnel.host + "]");
|
_log.error("Error finding host to bind", uhe);
|
||||||
_log.error("Error finding host to bind", uhe);
|
notifyEvent("openBaseClientResult", "error");
|
||||||
notifyEvent("openBaseClientResult", "error");
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,10 +131,10 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public final void startRunning() {
|
public final void startRunning() {
|
||||||
synchronized (startLock) {
|
synchronized (startLock) {
|
||||||
startRunning = true;
|
startRunning = true;
|
||||||
startLock.notify();
|
startLock.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,9 +142,9 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private I2PSocketOptions getDefaultOptions() {
|
private I2PSocketOptions getDefaultOptions() {
|
||||||
I2PSocketOptions opts = new I2PSocketOptions();
|
I2PSocketOptions opts = new I2PSocketOptions();
|
||||||
opts.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
|
opts.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -162,7 +156,7 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
|||||||
* @return a new I2PSocket
|
* @return a new I2PSocket
|
||||||
*/
|
*/
|
||||||
public I2PSocket createI2PSocket(Destination dest) throws I2PException {
|
public I2PSocket createI2PSocket(Destination dest) throws I2PException {
|
||||||
return createI2PSocket(dest, getDefaultOptions());
|
return createI2PSocket(dest, getDefaultOptions());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -175,53 +169,53 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
|||||||
* @return a new I2PSocket
|
* @return a new I2PSocket
|
||||||
*/
|
*/
|
||||||
public I2PSocket createI2PSocket(Destination dest, I2PSocketOptions opt) throws I2PException {
|
public I2PSocket createI2PSocket(Destination dest, I2PSocketOptions opt) throws I2PException {
|
||||||
I2PSocket i2ps;
|
I2PSocket i2ps;
|
||||||
|
|
||||||
synchronized (sockLock) {
|
synchronized (sockLock) {
|
||||||
i2ps = sockMgr.connect(dest, opt);
|
i2ps = sockMgr.connect(dest, opt);
|
||||||
mySockets.add(i2ps);
|
mySockets.add(i2ps);
|
||||||
}
|
}
|
||||||
|
|
||||||
return i2ps;
|
return i2ps;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void run() {
|
public final void run() {
|
||||||
try {
|
try {
|
||||||
InetAddress addr = getListenHost(l);
|
InetAddress addr = getListenHost(l);
|
||||||
if (addr == null) return;
|
if (addr == null) return;
|
||||||
ss = new ServerSocket(localPort, 0, addr);
|
ss = new ServerSocket(localPort, 0, addr);
|
||||||
|
|
||||||
// If a free port was requested, find out what we got
|
// If a free port was requested, find out what we got
|
||||||
if (localPort == 0) {
|
if (localPort == 0) {
|
||||||
localPort = ss.getLocalPort();
|
localPort = ss.getLocalPort();
|
||||||
}
|
}
|
||||||
notifyEvent("clientLocalPort", new Integer(ss.getLocalPort()));
|
notifyEvent("clientLocalPort", new Integer(ss.getLocalPort()));
|
||||||
l.log("Listening for clients on port " + localPort +
|
l.log("Listening for clients on port " + localPort + " of " + I2PTunnel.listenHost);
|
||||||
" of " + I2PTunnel.listenHost);
|
|
||||||
|
|
||||||
// Notify constructor that port is ready
|
// Notify constructor that port is ready
|
||||||
synchronized(this) {
|
synchronized (this) {
|
||||||
listenerReady = true;
|
listenerReady = true;
|
||||||
notify();
|
notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait until we are authorized to process data
|
// Wait until we are authorized to process data
|
||||||
synchronized (startLock) {
|
synchronized (startLock) {
|
||||||
while (!startRunning) {
|
while (!startRunning) {
|
||||||
try {
|
try {
|
||||||
startLock.wait();
|
startLock.wait();
|
||||||
} catch (InterruptedException ie) {}
|
} catch (InterruptedException ie) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
Socket s = ss.accept();
|
Socket s = ss.accept();
|
||||||
manageConnection(s);
|
manageConnection(s);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Error listening for connections", ex);
|
_log.error("Error listening for connections", ex);
|
||||||
notifyEvent("openBaseClientResult", "error");
|
notifyEvent("openBaseClientResult", "error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -230,59 +224,58 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask
|
|||||||
* @param s Socket to take care of
|
* @param s Socket to take care of
|
||||||
*/
|
*/
|
||||||
protected void manageConnection(Socket s) {
|
protected void manageConnection(Socket s) {
|
||||||
new ClientConnectionRunner(s, handlerName);
|
new ClientConnectionRunner(s, handlerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean close(boolean forced) {
|
public boolean close(boolean forced) {
|
||||||
if (!open) return true;
|
if (!open) return true;
|
||||||
// FIXME: here we might have to wait quite a long time if
|
// FIXME: here we might have to wait quite a long time if
|
||||||
// there is a connection attempt atm. But without waiting we
|
// there is a connection attempt atm. But without waiting we
|
||||||
// might risk to create an orphan socket. Would be better
|
// might risk to create an orphan socket. Would be better
|
||||||
// to return with an error in that situation quickly.
|
// to return with an error in that situation quickly.
|
||||||
synchronized(sockLock) {
|
synchronized (sockLock) {
|
||||||
mySockets.retainAll(sockMgr.listSockets());
|
mySockets.retainAll(sockMgr.listSockets());
|
||||||
if (!forced && mySockets.size() != 0) {
|
if (!forced && mySockets.size() != 0) {
|
||||||
l.log("There are still active connections!");
|
l.log("There are still active connections!");
|
||||||
_log.debug("can't close: there are still active connections!");
|
_log.debug("can't close: there are still active connections!");
|
||||||
for (Iterator it = mySockets.iterator(); it.hasNext();) {
|
for (Iterator it = mySockets.iterator(); it.hasNext();) {
|
||||||
l.log("->"+it.next());
|
l.log("->" + it.next());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
l.log("Closing client "+toString());
|
l.log("Closing client " + toString());
|
||||||
try {
|
try {
|
||||||
if (ss != null) ss.close();
|
if (ss != null) ss.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
l.log("Client closed.");
|
l.log("Client closed.");
|
||||||
open=false;
|
open = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void closeSocket(Socket s) {
|
public static void closeSocket(Socket s) {
|
||||||
try {
|
try {
|
||||||
s.close();
|
s.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Could not close socket", ex);
|
_log.error("Could not close socket", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ClientConnectionRunner extends I2PThread {
|
public class ClientConnectionRunner extends I2PThread {
|
||||||
private Socket s;
|
private Socket s;
|
||||||
|
|
||||||
public ClientConnectionRunner(Socket s, String name) {
|
public ClientConnectionRunner(Socket s, String name) {
|
||||||
this.s=s;
|
this.s = s;
|
||||||
setName(name);
|
setName(name);
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
clientConnectionRun(s);
|
clientConnectionRun(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,28 +21,28 @@ public class I2PTunnelGUI extends Frame implements ActionListener, Logging {
|
|||||||
I2PTunnel t;
|
I2PTunnel t;
|
||||||
|
|
||||||
public I2PTunnelGUI(I2PTunnel t) {
|
public I2PTunnelGUI(I2PTunnel t) {
|
||||||
super("I2PTunnel control panel");
|
super("I2PTunnel control panel");
|
||||||
this.t=t;
|
this.t = t;
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
add("South", input=new TextField());
|
add("South", input = new TextField());
|
||||||
input.addActionListener(this);
|
input.addActionListener(this);
|
||||||
Font font = new Font("Monospaced",Font.PLAIN,12);
|
Font font = new Font("Monospaced", Font.PLAIN, 12);
|
||||||
add("Center",log=new TextArea("",20,80,TextArea.SCROLLBARS_VERTICAL_ONLY));
|
add("Center", log = new TextArea("", 20, 80, TextArea.SCROLLBARS_VERTICAL_ONLY));
|
||||||
log.setFont(font);
|
log.setFont(font);
|
||||||
log.setEditable(false);
|
log.setEditable(false);
|
||||||
log("enter 'help' for help.");
|
log("enter 'help' for help.");
|
||||||
pack();
|
pack();
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void log(String s) {
|
public void log(String s) {
|
||||||
log.append(s+"\n");
|
log.append(s + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void actionPerformed(ActionEvent evt) {
|
public void actionPerformed(ActionEvent evt) {
|
||||||
log("I2PTunnel>"+input.getText());
|
log("I2PTunnel>" + input.getText());
|
||||||
t.runCommand(input.getText(), this);
|
t.runCommand(input.getText(), this);
|
||||||
log("---");
|
log("---");
|
||||||
input.setText("");
|
input.setText("");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,16 +19,17 @@ import net.i2p.util.EventDispatcher;
|
|||||||
import net.i2p.util.I2PThread;
|
import net.i2p.util.I2PThread;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
|
||||||
public class I2PTunnelHTTPClient extends I2PTunnelClientBase
|
public class I2PTunnelHTTPClient extends I2PTunnelClientBase implements Runnable {
|
||||||
implements Runnable {
|
private static final Log _log = new Log(I2PTunnelHTTPClient.class);
|
||||||
private static final Log _log =
|
|
||||||
new Log(I2PTunnelHTTPClient.class);
|
|
||||||
|
|
||||||
private String wwwProxy;
|
private String wwwProxy;
|
||||||
|
|
||||||
private final static byte[] ERR_REQUEST_DENIED = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=iso-8859-1\r\nCache-control: no-cache\r\n\r\n<html><body><H1>I2P ERROR: REQUEST DENIED</H1>You attempted to connect to a non-I2P website or location.<BR>".getBytes();
|
private final static byte[] ERR_REQUEST_DENIED = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=iso-8859-1\r\nCache-control: no-cache\r\n\r\n<html><body><H1>I2P ERROR: REQUEST DENIED</H1>You attempted to connect to a non-I2P website or location.<BR>"
|
||||||
private final static byte[] ERR_DESTINATION_UNKNOWN = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=iso-8859-1\r\nCache-control: no-cache\r\n\r\n<html><body><H1>I2P ERROR: NOT FOUND</H1>That Desitination was not found. Perhaps you pasted in the wrong BASE64 I2P Destination or the link you are following is bad. The host (or the WWW proxy, if you're using one) could also be temporarily offline. Could not find the following Destination:<BR><BR>".getBytes();
|
.getBytes();
|
||||||
private final static byte[] ERR_TIMEOUT = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=iso-8859-1\r\nCache-control: no-cache\r\n\r\n<html><body><H1>I2P ERROR: TIMEOUT</H1>That Desitination was reachable, but timed out getting a response. This may be a temporary error, so you should simply try to refresh, though if the problem persists, the remote destination may have issues. Could not get a response from the following Destination:<BR><BR>".getBytes();
|
private final static byte[] ERR_DESTINATION_UNKNOWN = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=iso-8859-1\r\nCache-control: no-cache\r\n\r\n<html><body><H1>I2P ERROR: NOT FOUND</H1>That Desitination was not found. Perhaps you pasted in the wrong BASE64 I2P Destination or the link you are following is bad. The host (or the WWW proxy, if you're using one) could also be temporarily offline. Could not find the following Destination:<BR><BR>"
|
||||||
|
.getBytes();
|
||||||
|
private final static byte[] ERR_TIMEOUT = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=iso-8859-1\r\nCache-control: no-cache\r\n\r\n<html><body><H1>I2P ERROR: TIMEOUT</H1>That Desitination was reachable, but timed out getting a response. This may be a temporary error, so you should simply try to refresh, though if the problem persists, the remote destination may have issues. Could not get a response from the following Destination:<BR><BR>"
|
||||||
|
.getBytes();
|
||||||
|
|
||||||
//public I2PTunnelHTTPClient(int localPort, Logging l,
|
//public I2PTunnelHTTPClient(int localPort, Logging l,
|
||||||
// boolean ownDest,
|
// boolean ownDest,
|
||||||
@ -37,299 +38,283 @@ public class I2PTunnelHTTPClient extends I2PTunnelClientBase
|
|||||||
// (EventDispatcher)null);
|
// (EventDispatcher)null);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public I2PTunnelHTTPClient(int localPort, Logging l,
|
public I2PTunnelHTTPClient(int localPort, Logging l, boolean ownDest, String wwwProxy, EventDispatcher notifyThis) {
|
||||||
boolean ownDest,
|
super(localPort, ownDest, l, notifyThis, "HTTPHandler");
|
||||||
String wwwProxy, EventDispatcher notifyThis) {
|
|
||||||
super(localPort, ownDest, l, notifyThis, "HTTPHandler");
|
|
||||||
|
|
||||||
if (waitEventValue("openBaseClientResult").equals("error")) {
|
if (waitEventValue("openBaseClientResult").equals("error")) {
|
||||||
notifyEvent("openHTTPClientResult", "error");
|
notifyEvent("openHTTPClientResult", "error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.wwwProxy = wwwProxy;
|
this.wwwProxy = wwwProxy;
|
||||||
|
|
||||||
setName(getLocalPort()
|
setName(getLocalPort() + " -> HTTPClient [WWW outproxy: " + this.wwwProxy + "]");
|
||||||
+ " -> HTTPClient [WWW outproxy: " + this.wwwProxy + "]");
|
|
||||||
|
|
||||||
startRunning();
|
startRunning();
|
||||||
|
|
||||||
notifyEvent("openHTTPClientResult", "ok");
|
notifyEvent("openHTTPClientResult", "ok");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void clientConnectionRun(Socket s) {
|
protected void clientConnectionRun(Socket s) {
|
||||||
OutputStream out = null;
|
OutputStream out = null;
|
||||||
String targetRequest = null;
|
String targetRequest = null;
|
||||||
boolean usingWWWProxy = false;
|
boolean usingWWWProxy = false;
|
||||||
InactivityTimeoutThread timeoutThread = null;
|
InactivityTimeoutThread timeoutThread = null;
|
||||||
try {
|
try {
|
||||||
out = s.getOutputStream();
|
out = s.getOutputStream();
|
||||||
BufferedReader br = new BufferedReader
|
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "ISO-8859-1"));
|
||||||
(new InputStreamReader(s.getInputStream(),
|
String line, method = null, protocol = null, host = null, destination = null;
|
||||||
"ISO-8859-1"));
|
StringBuffer newRequest = new StringBuffer();
|
||||||
String line, method=null, protocol=null, host=null, destination=null;
|
while ((line = br.readLine()) != null) {
|
||||||
StringBuffer newRequest=new StringBuffer();
|
if (method == null) { // first line (GET /base64/realaddr)
|
||||||
while ((line=br.readLine()) != null) {
|
int pos = line.indexOf(" ");
|
||||||
if (method==null) { // first line (GET /base64/realaddr)
|
if (pos == -1) break;
|
||||||
int pos=line.indexOf(" ");
|
method = line.substring(0, pos);
|
||||||
if (pos == -1) break;
|
String request = line.substring(pos + 1);
|
||||||
method=line.substring(0, pos);
|
if (request.startsWith("/") && System.getProperty("i2ptunnel.noproxy") != null) {
|
||||||
String request = line.substring(pos+1);
|
request = "http://i2p" + request;
|
||||||
if (request.startsWith("/") &&
|
}
|
||||||
System.getProperty("i2ptunnel.noproxy") != null) {
|
pos = request.indexOf("//");
|
||||||
request="http://i2p"+request;
|
if (pos == -1) {
|
||||||
}
|
method = null;
|
||||||
pos = request.indexOf("//");
|
break;
|
||||||
if (pos == -1) {
|
}
|
||||||
method=null;
|
protocol = request.substring(0, pos + 2);
|
||||||
break;
|
request = request.substring(pos + 2);
|
||||||
}
|
|
||||||
protocol=request.substring(0,pos+2);
|
|
||||||
request=request.substring(pos+2);
|
|
||||||
|
|
||||||
targetRequest = request;
|
targetRequest = request;
|
||||||
|
|
||||||
pos = request.indexOf("/");
|
pos = request.indexOf("/");
|
||||||
if (pos == -1) {
|
if (pos == -1) {
|
||||||
method=null;
|
method = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
host=request.substring(0,pos);
|
host = request.substring(0, pos);
|
||||||
|
|
||||||
// Quick hack for foo.bar.i2p
|
// Quick hack for foo.bar.i2p
|
||||||
if (host.toLowerCase().endsWith( ".i2p")) {
|
if (host.toLowerCase().endsWith(".i2p")) {
|
||||||
destination=host;
|
destination = host;
|
||||||
host=getHostName(destination);
|
host = getHostName(destination);
|
||||||
line=method+" "+request.substring(pos);
|
line = method + " " + request.substring(pos);
|
||||||
} else if (host.indexOf(".") != -1) {
|
} else if (host.indexOf(".") != -1) {
|
||||||
// The request must be forwarded to a WWW proxy
|
// The request must be forwarded to a WWW proxy
|
||||||
destination = wwwProxy;
|
destination = wwwProxy;
|
||||||
usingWWWProxy = true;
|
usingWWWProxy = true;
|
||||||
} else {
|
} else {
|
||||||
request=request.substring(pos+1);
|
request = request.substring(pos + 1);
|
||||||
pos = request.indexOf("/");
|
pos = request.indexOf("/");
|
||||||
destination=request.substring(0,pos);
|
destination = request.substring(0, pos);
|
||||||
line=method+" "+request.substring(pos);
|
line = method + " " + request.substring(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isValid = usingWWWProxy ||
|
boolean isValid = usingWWWProxy || isSupportedAddress(host, protocol);
|
||||||
isSupportedAddress(host, protocol);
|
if (!isValid) {
|
||||||
if (!isValid) {
|
if (_log.shouldLog(Log.INFO)) _log.info("notValid(" + host + ")");
|
||||||
if (_log.shouldLog(Log.INFO))
|
method = null;
|
||||||
_log.info("notValid(" + host + ")");
|
destination = null;
|
||||||
method=null;
|
break;
|
||||||
destination=null;
|
} else if (!usingWWWProxy) {
|
||||||
break;
|
if (_log.shouldLog(Log.INFO)) _log.info("host=getHostName(" + destination + ")");
|
||||||
} else if (!usingWWWProxy) {
|
host = getHostName(destination); // hide original host
|
||||||
if (_log.shouldLog(Log.INFO))
|
}
|
||||||
_log.info("host=getHostName(" + destination + ")");
|
|
||||||
host=getHostName(destination); // hide original host
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_log.shouldLog(Log.DEBUG)) {
|
if (_log.shouldLog(Log.DEBUG)) {
|
||||||
_log.debug("METHOD:"+method+":");
|
_log.debug("METHOD:" + method + ":");
|
||||||
_log.debug("PROTOC:"+protocol+":");
|
_log.debug("PROTOC:" + protocol + ":");
|
||||||
_log.debug("HOST :"+host+":");
|
_log.debug("HOST :" + host + ":");
|
||||||
_log.debug("DEST :"+destination+":");
|
_log.debug("DEST :" + destination + ":");
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (line.startsWith("Host: ") && !usingWWWProxy) {
|
} else if (line.startsWith("Host: ") && !usingWWWProxy) {
|
||||||
line="Host: "+host;
|
line = "Host: " + host;
|
||||||
if (_log.shouldLog(Log.INFO))
|
if (_log.shouldLog(Log.INFO)) _log.info("Setting host = " + host);
|
||||||
_log.info("Setting host = " + host);
|
}
|
||||||
}
|
newRequest.append(line).append("\r\n"); // HTTP spec
|
||||||
newRequest.append(line).append("\r\n"); // HTTP spec
|
if (line.length() == 0) break;
|
||||||
if (line.length()==0) break;
|
}
|
||||||
}
|
while (br.ready()) { // empty the buffer (POST requests)
|
||||||
while (br.ready()) { // empty the buffer (POST requests)
|
int i = br.read();
|
||||||
int i=br.read();
|
if (i != -1) {
|
||||||
if (i != -1) {
|
newRequest.append((char) i);
|
||||||
newRequest.append((char)i);
|
}
|
||||||
}
|
}
|
||||||
}
|
if (method == null || destination == null) {
|
||||||
if (method==null || destination==null) {
|
l.log("No HTTP method found in the request.");
|
||||||
l.log("No HTTP method found in the request.");
|
if (out != null) {
|
||||||
if (out != null) {
|
out.write(ERR_REQUEST_DENIED);
|
||||||
out.write(ERR_REQUEST_DENIED);
|
out.write("<p /><i>Generated on: ".getBytes());
|
||||||
out.write("<p /><i>Generated on: ".getBytes());
|
out.write(new Date().toString().getBytes());
|
||||||
out.write(new Date().toString().getBytes());
|
out.write("</i></body></html>\n".getBytes());
|
||||||
out.write("</i></body></html>\n".getBytes());
|
out.flush();
|
||||||
out.flush();
|
}
|
||||||
}
|
s.close();
|
||||||
s.close();
|
return;
|
||||||
return;
|
}
|
||||||
}
|
Destination dest = I2PTunnel.destFromName(destination);
|
||||||
Destination dest=I2PTunnel.destFromName(destination);
|
if (dest == null) {
|
||||||
if (dest == null) {
|
l.log("Could not resolve " + destination + ".");
|
||||||
l.log("Could not resolve "+destination+".");
|
writeErrorMessage(ERR_DESTINATION_UNKNOWN, out, targetRequest, usingWWWProxy, destination);
|
||||||
writeErrorMessage(ERR_DESTINATION_UNKNOWN, out, targetRequest,
|
s.close();
|
||||||
usingWWWProxy, destination);
|
return;
|
||||||
s.close();
|
}
|
||||||
return;
|
String remoteID;
|
||||||
}
|
I2PSocket i2ps = createI2PSocket(dest);
|
||||||
String remoteID;
|
byte[] data = newRequest.toString().getBytes("ISO-8859-1");
|
||||||
I2PSocket i2ps = createI2PSocket(dest);
|
I2PTunnelRunner runner = new I2PTunnelRunner(s, i2ps, sockLock, data);
|
||||||
byte[] data=newRequest.toString().getBytes("ISO-8859-1");
|
timeoutThread = new InactivityTimeoutThread(runner, out, targetRequest, usingWWWProxy, s);
|
||||||
I2PTunnelRunner runner = new I2PTunnelRunner(s, i2ps, sockLock, data);
|
timeoutThread.start();
|
||||||
timeoutThread = new InactivityTimeoutThread(runner, out, targetRequest, usingWWWProxy, s);
|
} catch (IOException ex) {
|
||||||
timeoutThread.start();
|
if (timeoutThread != null) timeoutThread.disable();
|
||||||
} catch (IOException ex) {
|
_log.error("Error sending syn", ex);
|
||||||
if (timeoutThread != null) timeoutThread.disable();
|
handleHTTPClientException(ex, out, targetRequest, usingWWWProxy, wwwProxy);
|
||||||
_log.error("Error sending syn", ex);
|
closeSocket(s);
|
||||||
handleHTTPClientException(ex, out, targetRequest,
|
} catch (I2PException ex) {
|
||||||
usingWWWProxy, wwwProxy);
|
if (timeoutThread != null) timeoutThread.disable();
|
||||||
closeSocket(s);
|
_log.info("Error sending syn", ex);
|
||||||
} catch (I2PException ex) {
|
l.log("Unable to reach peer");
|
||||||
if (timeoutThread != null) timeoutThread.disable();
|
handleHTTPClientException(ex, out, targetRequest, usingWWWProxy, wwwProxy);
|
||||||
_log.info("Error sending syn", ex);
|
closeSocket(s);
|
||||||
l.log("Unable to reach peer");
|
}
|
||||||
handleHTTPClientException(ex, out, targetRequest,
|
|
||||||
usingWWWProxy, wwwProxy);
|
|
||||||
closeSocket(s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final long INACTIVITY_TIMEOUT = 120*1000;
|
private static final long INACTIVITY_TIMEOUT = 120 * 1000;
|
||||||
|
|
||||||
private class InactivityTimeoutThread extends I2PThread {
|
private class InactivityTimeoutThread extends I2PThread {
|
||||||
|
|
||||||
private Socket s;
|
private Socket s;
|
||||||
private I2PTunnelRunner _runner;
|
private I2PTunnelRunner _runner;
|
||||||
private OutputStream _out;
|
private OutputStream _out;
|
||||||
private String _targetRequest;
|
private String _targetRequest;
|
||||||
private boolean _useWWWProxy;
|
private boolean _useWWWProxy;
|
||||||
private boolean _disabled;
|
private boolean _disabled;
|
||||||
private Object _disableLock = new Object();
|
private Object _disableLock = new Object();
|
||||||
|
|
||||||
public InactivityTimeoutThread(I2PTunnelRunner runner, OutputStream out, String targetRequest, boolean useWWWProxy, Socket s) {
|
public InactivityTimeoutThread(I2PTunnelRunner runner, OutputStream out, String targetRequest,
|
||||||
this.s=s;
|
boolean useWWWProxy, Socket s) {
|
||||||
_runner = runner;
|
this.s = s;
|
||||||
_out = out;
|
_runner = runner;
|
||||||
_targetRequest = targetRequest;
|
_out = out;
|
||||||
_useWWWProxy = useWWWProxy;
|
_targetRequest = targetRequest;
|
||||||
_disabled = false;
|
_useWWWProxy = useWWWProxy;
|
||||||
setName("InactivityThread");
|
_disabled = false;
|
||||||
}
|
setName("InactivityThread");
|
||||||
public void disable() {
|
}
|
||||||
_disabled = true;
|
|
||||||
synchronized (_disableLock) { _disableLock.notifyAll(); }
|
public void disable() {
|
||||||
}
|
_disabled = true;
|
||||||
public void run() {
|
synchronized (_disableLock) {
|
||||||
while (!_disabled) {
|
_disableLock.notifyAll();
|
||||||
if (_runner.isFinished()) {
|
}
|
||||||
if (_log.shouldLog(Log.INFO))
|
}
|
||||||
_log.info("HTTP client request completed prior to timeout");
|
|
||||||
return;
|
public void run() {
|
||||||
}
|
while (!_disabled) {
|
||||||
if (_runner.getLastActivityOn() < Clock.getInstance().now() - INACTIVITY_TIMEOUT) {
|
if (_runner.isFinished()) {
|
||||||
if (_runner.getStartedOn() < Clock.getInstance().now() - INACTIVITY_TIMEOUT) {
|
if (_log.shouldLog(Log.INFO)) _log.info("HTTP client request completed prior to timeout");
|
||||||
if (_log.shouldLog(Log.WARN))
|
return;
|
||||||
_log.warn("HTTP client request timed out (lastActivity: " + new Date(_runner.getLastActivityOn()) + ", startedOn: " + new Date(_runner.getLastActivityOn()) + ")");
|
}
|
||||||
timeout();
|
if (_runner.getLastActivityOn() < Clock.getInstance().now() - INACTIVITY_TIMEOUT) {
|
||||||
return;
|
if (_runner.getStartedOn() < Clock.getInstance().now() - INACTIVITY_TIMEOUT) {
|
||||||
} else {
|
if (_log.shouldLog(Log.WARN))
|
||||||
// runner hasn't been going to long enough
|
_log.warn("HTTP client request timed out (lastActivity: "
|
||||||
}
|
+ new Date(_runner.getLastActivityOn()) + ", startedOn: "
|
||||||
} else {
|
+ new Date(_runner.getLastActivityOn()) + ")");
|
||||||
// there has been activity in the period
|
timeout();
|
||||||
}
|
return;
|
||||||
synchronized (_disableLock) {
|
} else {
|
||||||
try {
|
// runner hasn't been going to long enough
|
||||||
_disableLock.wait(INACTIVITY_TIMEOUT);
|
}
|
||||||
} catch (InterruptedException ie) {}
|
} else {
|
||||||
}
|
// there has been activity in the period
|
||||||
}
|
}
|
||||||
}
|
synchronized (_disableLock) {
|
||||||
private void timeout() {
|
try {
|
||||||
_log.info("Inactivity timeout reached");
|
_disableLock.wait(INACTIVITY_TIMEOUT);
|
||||||
l.log("Inactivity timeout reached");
|
} catch (InterruptedException ie) {
|
||||||
if (_out != null) {
|
}
|
||||||
try {
|
}
|
||||||
if (_runner.getLastActivityOn() > 0) {
|
}
|
||||||
// some data has been sent, so don't 404 it
|
}
|
||||||
} else {
|
|
||||||
writeErrorMessage(ERR_TIMEOUT, _out, _targetRequest,
|
private void timeout() {
|
||||||
_useWWWProxy, wwwProxy);
|
_log.info("Inactivity timeout reached");
|
||||||
}
|
l.log("Inactivity timeout reached");
|
||||||
} catch (IOException ioe) {
|
if (_out != null) {
|
||||||
_log.warn("Error writing out the 'timeout' message", ioe);
|
try {
|
||||||
}
|
if (_runner.getLastActivityOn() > 0) {
|
||||||
} else {
|
// some data has been sent, so don't 404 it
|
||||||
_log.warn("Client disconnected before we could say we timed out");
|
} else {
|
||||||
}
|
writeErrorMessage(ERR_TIMEOUT, _out, _targetRequest, _useWWWProxy, wwwProxy);
|
||||||
closeSocket(s);
|
}
|
||||||
}
|
} catch (IOException ioe) {
|
||||||
|
_log.warn("Error writing out the 'timeout' message", ioe);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_log.warn("Client disconnected before we could say we timed out");
|
||||||
|
}
|
||||||
|
closeSocket(s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static String getHostName(String host) {
|
private final static String getHostName(String host) {
|
||||||
try {
|
try {
|
||||||
Destination dest=I2PTunnel.destFromName(host);
|
Destination dest = I2PTunnel.destFromName(host);
|
||||||
if (dest == null) return "i2p";
|
if (dest == null) return "i2p";
|
||||||
return dest.toBase64();
|
return dest.toBase64();
|
||||||
} catch (DataFormatException dfe) {
|
} catch (DataFormatException dfe) {
|
||||||
return "i2p";
|
return "i2p";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeErrorMessage(byte[] errMessage, OutputStream out,
|
private static void writeErrorMessage(byte[] errMessage, OutputStream out, String targetRequest,
|
||||||
String targetRequest,
|
boolean usingWWWProxy, String wwwProxy) throws IOException {
|
||||||
boolean usingWWWProxy,
|
if (out != null) {
|
||||||
String wwwProxy)
|
out.write(errMessage);
|
||||||
throws IOException {
|
if (targetRequest != null) {
|
||||||
if (out != null) {
|
out.write(targetRequest.getBytes());
|
||||||
out.write(errMessage);
|
if (usingWWWProxy) out.write(("<br>WWW proxy: " + wwwProxy).getBytes());
|
||||||
if (targetRequest != null) {
|
}
|
||||||
out.write(targetRequest.getBytes());
|
out.write("<p /><i>Generated on: ".getBytes());
|
||||||
if (usingWWWProxy)
|
out.write(new Date().toString().getBytes());
|
||||||
out.write(("<br>WWW proxy: " +
|
out.write("</i></body></html>\n".getBytes());
|
||||||
wwwProxy).getBytes());
|
out.flush();
|
||||||
}
|
}
|
||||||
out.write("<p /><i>Generated on: ".getBytes());
|
|
||||||
out.write(new Date().toString().getBytes());
|
|
||||||
out.write("</i></body></html>\n".getBytes());
|
|
||||||
out.flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleHTTPClientException (Exception ex, OutputStream out,
|
private static void handleHTTPClientException(Exception ex, OutputStream out, String targetRequest,
|
||||||
String targetRequest,
|
boolean usingWWWProxy, String wwwProxy) {
|
||||||
boolean usingWWWProxy,
|
if (out != null) {
|
||||||
String wwwProxy) {
|
try {
|
||||||
if (out != null) {
|
writeErrorMessage(ERR_DESTINATION_UNKNOWN, out, targetRequest, usingWWWProxy, wwwProxy);
|
||||||
try {
|
} catch (IOException ioe) {
|
||||||
writeErrorMessage(ERR_DESTINATION_UNKNOWN, out, targetRequest,
|
_log.warn("Error writing out the 'destination was unknown' " + "message", ioe);
|
||||||
usingWWWProxy, wwwProxy);
|
}
|
||||||
} catch (IOException ioe) {
|
} else {
|
||||||
_log.warn("Error writing out the 'destination was unknown' "+
|
_log.warn("Client disconnected before we could say that destination " + "was unknown", ex);
|
||||||
"message", ioe);
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_log.warn("Client disconnected before we could say that destination "+
|
|
||||||
"was unknown", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static String SUPPORTED_HOSTS[] = { "i2p", "www.i2p.com",
|
private final static String SUPPORTED_HOSTS[] = { "i2p", "www.i2p.com", "i2p."};
|
||||||
"i2p." };
|
|
||||||
|
|
||||||
private boolean isSupportedAddress(String host, String protocol) {
|
private boolean isSupportedAddress(String host, String protocol) {
|
||||||
if ( (host == null) || (protocol == null) ) return false;
|
if ((host == null) || (protocol == null)) return false;
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
String lcHost = host.toLowerCase();
|
String lcHost = host.toLowerCase();
|
||||||
for (int i = 0; i < SUPPORTED_HOSTS.length; i++) {
|
for (int i = 0; i < SUPPORTED_HOSTS.length; i++) {
|
||||||
if (SUPPORTED_HOSTS[i].equals(lcHost)) {
|
if (SUPPORTED_HOSTS[i].equals(lcHost)) {
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
try {
|
try {
|
||||||
Destination d = I2PTunnel.destFromName(host);
|
Destination d = I2PTunnel.destFromName(host);
|
||||||
if (d == null) return false;
|
if (d == null) return false;
|
||||||
} catch (DataFormatException dfe) {}
|
} catch (DataFormatException dfe) {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return protocol.equalsIgnoreCase("http://");
|
return protocol.equalsIgnoreCase("http://");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -26,14 +26,14 @@ public class I2PTunnelRunner extends I2PThread {
|
|||||||
* Sun's impl of BufferedOutputStream), but that is the streaming
|
* Sun's impl of BufferedOutputStream), but that is the streaming
|
||||||
* api's job...
|
* api's job...
|
||||||
*/
|
*/
|
||||||
static int MAX_PACKET_SIZE = 1024*32;
|
static int MAX_PACKET_SIZE = 1024 * 32;
|
||||||
|
|
||||||
static final int NETWORK_BUFFER_SIZE = MAX_PACKET_SIZE;
|
static final int NETWORK_BUFFER_SIZE = MAX_PACKET_SIZE;
|
||||||
|
|
||||||
private Socket s;
|
private Socket s;
|
||||||
private I2PSocket i2ps;
|
private I2PSocket i2ps;
|
||||||
Object slock, finishLock = new Object();
|
Object slock, finishLock = new Object();
|
||||||
boolean finished=false;
|
boolean finished = false;
|
||||||
HashMap ostreams, sockets;
|
HashMap ostreams, sockets;
|
||||||
I2PSession session;
|
I2PSession session;
|
||||||
byte[] initialData;
|
byte[] initialData;
|
||||||
@ -42,17 +42,16 @@ public class I2PTunnelRunner extends I2PThread {
|
|||||||
/** when the runner started up */
|
/** when the runner started up */
|
||||||
private long startedOn;
|
private long startedOn;
|
||||||
|
|
||||||
public I2PTunnelRunner(Socket s, I2PSocket i2ps, Object slock,
|
public I2PTunnelRunner(Socket s, I2PSocket i2ps, Object slock, byte[] initialData) {
|
||||||
byte[] initialData) {
|
this.s = s;
|
||||||
this.s=s;
|
this.i2ps = i2ps;
|
||||||
this.i2ps=i2ps;
|
this.slock = slock;
|
||||||
this.slock=slock;
|
this.initialData = initialData;
|
||||||
this.initialData = initialData;
|
lastActivityOn = -1;
|
||||||
lastActivityOn = -1;
|
startedOn = -1;
|
||||||
startedOn = -1;
|
_log.info("I2PTunnelRunner started");
|
||||||
_log.info("I2PTunnelRunner started");
|
setName("I2PTunnelRunner");
|
||||||
setName("I2PTunnelRunner");
|
start();
|
||||||
start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,7 +59,9 @@ public class I2PTunnelRunner extends I2PThread {
|
|||||||
* [aka we're done running the streams]?
|
* [aka we're done running the streams]?
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public boolean isFinished() { return finished; }
|
public boolean isFinished() {
|
||||||
|
return finished;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When was the last data for this runner sent or received?
|
* When was the last data for this runner sent or received?
|
||||||
@ -68,119 +69,123 @@ public class I2PTunnelRunner extends I2PThread {
|
|||||||
* @return date (ms since the epoch), or -1 if no data has been transferred yet
|
* @return date (ms since the epoch), or -1 if no data has been transferred yet
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public long getLastActivityOn() { return lastActivityOn; }
|
public long getLastActivityOn() {
|
||||||
private void updateActivity() { lastActivityOn = Clock.getInstance().now(); }
|
return lastActivityOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateActivity() {
|
||||||
|
lastActivityOn = Clock.getInstance().now();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When this runner started up transferring data
|
* When this runner started up transferring data
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public long getStartedOn() { return startedOn; }
|
public long getStartedOn() {
|
||||||
|
return startedOn;
|
||||||
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
startedOn = Clock.getInstance().now();
|
startedOn = Clock.getInstance().now();
|
||||||
try {
|
try {
|
||||||
InputStream in = s.getInputStream();
|
InputStream in = s.getInputStream();
|
||||||
OutputStream out = new BufferedOutputStream(s.getOutputStream(),
|
OutputStream out = new BufferedOutputStream(s.getOutputStream(), NETWORK_BUFFER_SIZE);
|
||||||
NETWORK_BUFFER_SIZE);
|
InputStream i2pin = i2ps.getInputStream();
|
||||||
InputStream i2pin = i2ps.getInputStream();
|
OutputStream i2pout = new BufferedOutputStream(i2ps.getOutputStream(), MAX_PACKET_SIZE);
|
||||||
OutputStream i2pout = new BufferedOutputStream
|
if (initialData != null) {
|
||||||
(i2ps.getOutputStream(), MAX_PACKET_SIZE);
|
synchronized (slock) {
|
||||||
if (initialData != null) {
|
i2pout.write(initialData);
|
||||||
synchronized(slock) {
|
i2pout.flush();
|
||||||
i2pout.write(initialData);
|
}
|
||||||
i2pout.flush();
|
}
|
||||||
}
|
Thread t1 = new StreamForwarder(in, i2pout);
|
||||||
}
|
Thread t2 = new StreamForwarder(i2pin, out);
|
||||||
Thread t1 = new StreamForwarder(in, i2pout);
|
synchronized (finishLock) {
|
||||||
Thread t2 = new StreamForwarder(i2pin, out);
|
while (!finished) {
|
||||||
synchronized(finishLock) {
|
finishLock.wait();
|
||||||
while (!finished) {
|
}
|
||||||
finishLock.wait();
|
}
|
||||||
}
|
// now one connection is dead - kill the other as well.
|
||||||
}
|
s.close();
|
||||||
// now one connection is dead - kill the other as well.
|
s = null;
|
||||||
s.close();
|
i2ps.close();
|
||||||
s = null;
|
i2ps = null;
|
||||||
i2ps.close();
|
t1.join();
|
||||||
i2ps = null;
|
t2.join();
|
||||||
t1.join();
|
} catch (InterruptedException ex) {
|
||||||
t2.join();
|
_log.error("Interrupted", ex);
|
||||||
} catch (InterruptedException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Interrupted", ex);
|
ex.printStackTrace();
|
||||||
} catch (IOException ex) {
|
_log.error("Error forwarding", ex);
|
||||||
ex.printStackTrace();
|
} finally {
|
||||||
_log.error("Error forwarding", ex);
|
try {
|
||||||
} finally {
|
if (s != null) s.close();
|
||||||
try {
|
if (i2ps != null) i2ps.close();
|
||||||
if (s != null) s.close();
|
} catch (IOException ex) {
|
||||||
if (i2ps != null) i2ps.close();
|
ex.printStackTrace();
|
||||||
} catch (IOException ex) {
|
_log.error("Could not close socket", ex);
|
||||||
ex.printStackTrace();
|
}
|
||||||
_log.error("Could not close socket", ex);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class StreamForwarder extends I2PThread {
|
private class StreamForwarder extends I2PThread {
|
||||||
|
|
||||||
InputStream in;
|
InputStream in;
|
||||||
OutputStream out;
|
OutputStream out;
|
||||||
|
|
||||||
private StreamForwarder(InputStream in, OutputStream out) {
|
private StreamForwarder(InputStream in, OutputStream out) {
|
||||||
this.in=in;
|
this.in = in;
|
||||||
this.out=out;
|
this.out = out;
|
||||||
setName("StreamForwarder");
|
setName("StreamForwarder");
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
byte[] buffer = new byte[NETWORK_BUFFER_SIZE];
|
byte[] buffer = new byte[NETWORK_BUFFER_SIZE];
|
||||||
try {
|
try {
|
||||||
int len;
|
int len;
|
||||||
while ((len=in.read(buffer)) != -1) {
|
while ((len = in.read(buffer)) != -1) {
|
||||||
out.write(buffer, 0, len);
|
out.write(buffer, 0, len);
|
||||||
|
|
||||||
if (len > 0)
|
if (len > 0) updateActivity();
|
||||||
updateActivity();
|
|
||||||
|
|
||||||
if (in.available()==0) {
|
if (in.available() == 0) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(I2PTunnel.PACKET_DELAY);
|
Thread.sleep(I2PTunnel.PACKET_DELAY);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (in.available()==0) {
|
if (in.available() == 0) {
|
||||||
out.flush(); // make sure the data get though
|
out.flush(); // make sure the data get though
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SocketException ex) {
|
} catch (SocketException ex) {
|
||||||
// this *will* occur when the other threads closes the socket
|
// this *will* occur when the other threads closes the socket
|
||||||
synchronized(finishLock) {
|
synchronized (finishLock) {
|
||||||
if (!finished)
|
if (!finished)
|
||||||
_log.error("Error reading and writing", ex);
|
_log.error("Error reading and writing", ex);
|
||||||
else
|
else
|
||||||
_log.warn("You may ignore this", ex);
|
_log.warn("You may ignore this", ex);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
if (!finished)
|
if (!finished)
|
||||||
_log.error("Error forwarding", ex);
|
_log.error("Error forwarding", ex);
|
||||||
else
|
else
|
||||||
_log.warn("You may ignore this", ex);
|
_log.warn("You may ignore this", ex);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
out.close();
|
out.close();
|
||||||
in.close();
|
in.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Error closing streams", ex);
|
_log.error("Error closing streams", ex);
|
||||||
}
|
}
|
||||||
synchronized(finishLock) {
|
synchronized (finishLock) {
|
||||||
finished=true;
|
finished = true;
|
||||||
finishLock.notifyAll();
|
finishLock.notifyAll();
|
||||||
// the main thread will close sockets etc. now
|
// the main thread will close sockets etc. now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -26,8 +26,7 @@ import net.i2p.util.EventDispatcher;
|
|||||||
import net.i2p.util.I2PThread;
|
import net.i2p.util.I2PThread;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
|
||||||
public class I2PTunnelServer extends I2PTunnelTask
|
public class I2PTunnelServer extends I2PTunnelTask implements Runnable {
|
||||||
implements Runnable {
|
|
||||||
|
|
||||||
private final static Log _log = new Log(I2PTunnelServer.class);
|
private final static Log _log = new Log(I2PTunnelServer.class);
|
||||||
|
|
||||||
@ -41,100 +40,92 @@ public class I2PTunnelServer extends I2PTunnelTask
|
|||||||
|
|
||||||
private Logging l;
|
private Logging l;
|
||||||
|
|
||||||
public I2PTunnelServer(InetAddress host, int port,
|
public I2PTunnelServer(InetAddress host, int port, String privData, Logging l, EventDispatcher notifyThis) {
|
||||||
String privData, Logging l,
|
super(host + ":" + port + " <- " + privData, notifyThis);
|
||||||
EventDispatcher notifyThis) {
|
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(privData));
|
||||||
super(host+":"+port+" <- "+privData, notifyThis);
|
init(host, port, bais, privData, l);
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decode(privData));
|
|
||||||
init(host, port, bais, privData, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public I2PTunnelServer(InetAddress host, int port,
|
public I2PTunnelServer(InetAddress host, int port, File privkey, String privkeyname, Logging l,
|
||||||
File privkey, String privkeyname,
|
EventDispatcher notifyThis) {
|
||||||
Logging l, EventDispatcher notifyThis) {
|
super(host + ":" + port + " <- " + privkeyname, notifyThis);
|
||||||
super(host+":"+port+" <- "+privkeyname, notifyThis);
|
try {
|
||||||
try {
|
init(host, port, new FileInputStream(privkey), privkeyname, l);
|
||||||
init(host, port, new FileInputStream(privkey), privkeyname, l);
|
} catch (IOException ioe) {
|
||||||
} catch (IOException ioe) {
|
_log.error("Error starting server", ioe);
|
||||||
_log.error("Error starting server", ioe);
|
notifyEvent("openServerResult", "error");
|
||||||
notifyEvent("openServerResult", "error");
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
public I2PTunnelServer(InetAddress host, int port,
|
|
||||||
InputStream privData, String privkeyname,
|
|
||||||
Logging l, EventDispatcher notifyThis) {
|
|
||||||
super(host+":"+port+" <- "+privkeyname, notifyThis);
|
|
||||||
init(host, port, privData, privkeyname, l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init(InetAddress host, int port, InputStream privData,
|
public I2PTunnelServer(InetAddress host, int port, InputStream privData, String privkeyname, Logging l,
|
||||||
String privkeyname, Logging l) {
|
EventDispatcher notifyThis) {
|
||||||
this.l=l;
|
super(host + ":" + port + " <- " + privkeyname, notifyThis);
|
||||||
this.remoteHost=host;
|
init(host, port, privData, privkeyname, l);
|
||||||
this.remotePort=port;
|
|
||||||
I2PClient client = I2PClientFactory.createClient();
|
|
||||||
Properties props = new Properties();
|
|
||||||
props.putAll(System.getProperties());
|
|
||||||
synchronized(slock) {
|
|
||||||
sockMgr = I2PSocketManagerFactory.createManager
|
|
||||||
(privData, I2PTunnel.host,
|
|
||||||
Integer.parseInt(I2PTunnel.port), props);
|
|
||||||
|
|
||||||
}
|
|
||||||
l.log("Ready!");
|
|
||||||
notifyEvent("openServerResult", "ok");
|
|
||||||
open=true;
|
|
||||||
Thread t = new I2PThread(this);
|
|
||||||
t.setName("Server");
|
|
||||||
t.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void init(InetAddress host, int port, InputStream privData, String privkeyname, Logging l) {
|
||||||
|
this.l = l;
|
||||||
|
this.remoteHost = host;
|
||||||
|
this.remotePort = port;
|
||||||
|
I2PClient client = I2PClientFactory.createClient();
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.putAll(System.getProperties());
|
||||||
|
synchronized (slock) {
|
||||||
|
sockMgr = I2PSocketManagerFactory.createManager(privData, I2PTunnel.host, Integer.parseInt(I2PTunnel.port),
|
||||||
|
props);
|
||||||
|
|
||||||
|
}
|
||||||
|
l.log("Ready!");
|
||||||
|
notifyEvent("openServerResult", "ok");
|
||||||
|
open = true;
|
||||||
|
Thread t = new I2PThread(this);
|
||||||
|
t.setName("Server");
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean close(boolean forced) {
|
public boolean close(boolean forced) {
|
||||||
if (!open) return true;
|
if (!open) return true;
|
||||||
synchronized(lock) {
|
synchronized (lock) {
|
||||||
if (!forced && sockMgr.listSockets().size() != 0) {
|
if (!forced && sockMgr.listSockets().size() != 0) {
|
||||||
l.log("There are still active connections!");
|
l.log("There are still active connections!");
|
||||||
for (Iterator it = sockMgr.listSockets().iterator();
|
for (Iterator it = sockMgr.listSockets().iterator(); it.hasNext();) {
|
||||||
it.hasNext();) {
|
l.log("->" + it.next());
|
||||||
l.log("->"+it.next());
|
}
|
||||||
}
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
l.log("Shutting down server " + toString());
|
||||||
l.log("Shutting down server "+toString());
|
try {
|
||||||
try {
|
if (i2pss != null) i2pss.close();
|
||||||
if (i2pss != null) i2pss.close();
|
sockMgr.getSession().destroySession();
|
||||||
sockMgr.getSession().destroySession();
|
} catch (I2PException ex) {
|
||||||
} catch (I2PException ex) {
|
_log.error("Error destroying the session", ex);
|
||||||
_log.error("Error destroying the session", ex);
|
System.exit(1);
|
||||||
System.exit(1);
|
}
|
||||||
}
|
l.log("Server shut down.");
|
||||||
l.log("Server shut down.");
|
open = false;
|
||||||
open=false;
|
return true;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
I2PServerSocket i2pss = sockMgr.getServerSocket();
|
I2PServerSocket i2pss = sockMgr.getServerSocket();
|
||||||
while (true) {
|
while (true) {
|
||||||
I2PSocket i2ps = i2pss.accept();
|
I2PSocket i2ps = i2pss.accept();
|
||||||
//local is fast, so synchronously. Does not need that many
|
//local is fast, so synchronously. Does not need that many
|
||||||
//threads.
|
//threads.
|
||||||
try {
|
try {
|
||||||
Socket s = new Socket(remoteHost, remotePort);
|
Socket s = new Socket(remoteHost, remotePort);
|
||||||
new I2PTunnelRunner(s, i2ps, slock, null);
|
new I2PTunnelRunner(s, i2ps, slock, null);
|
||||||
} catch (SocketException ex) {
|
} catch (SocketException ex) {
|
||||||
i2ps.close();
|
i2ps.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (I2PException ex) {
|
} catch (I2PException ex) {
|
||||||
_log.error("Error while waiting for I2PConnections", ex);
|
_log.error("Error while waiting for I2PConnections", ex);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Error while waiting for I2PConnections", ex);
|
_log.error("Error while waiting for I2PConnections", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,49 +27,86 @@ public abstract class I2PTunnelTask implements EventDispatcher {
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
protected I2PTunnelTask(String name, EventDispatcher notifyThis) {
|
protected I2PTunnelTask(String name, EventDispatcher notifyThis) {
|
||||||
attachEventDispatcher(notifyThis);
|
attachEventDispatcher(notifyThis);
|
||||||
this.name=name;
|
this.name = name;
|
||||||
this.id = -1;
|
this.id = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** for apps that use multiple I2PTunnel instances */
|
/** for apps that use multiple I2PTunnel instances */
|
||||||
public void setTunnel(I2PTunnel pTunnel) { tunnel = pTunnel; }
|
public void setTunnel(I2PTunnel pTunnel) {
|
||||||
|
tunnel = pTunnel;
|
||||||
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOpen() {return open;}
|
public boolean isOpen() {
|
||||||
|
return open;
|
||||||
|
}
|
||||||
|
|
||||||
public void setId(int id) {
|
public void setId(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setName(String name) {
|
protected void setName(String name) {
|
||||||
this.name=name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void routerDisconnected() { tunnel.routerDisconnected(); }
|
protected void routerDisconnected() {
|
||||||
|
tunnel.routerDisconnected();
|
||||||
|
}
|
||||||
|
|
||||||
public abstract boolean close(boolean forced);
|
public abstract boolean close(boolean forced);
|
||||||
|
|
||||||
public void disconnected(I2PSession session) { routerDisconnected(); }
|
public void disconnected(I2PSession session) {
|
||||||
public void errorOccurred(I2PSession session, String message,
|
routerDisconnected();
|
||||||
Throwable error) {}
|
}
|
||||||
public void reportAbuse(I2PSession session, int severity) {}
|
|
||||||
|
public void errorOccurred(I2PSession session, String message, Throwable error) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reportAbuse(I2PSession session, int severity) {
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Required by the EventDispatcher interface */
|
/* Required by the EventDispatcher interface */
|
||||||
public EventDispatcher getEventDispatcher() { return _event; }
|
public EventDispatcher getEventDispatcher() {
|
||||||
public void attachEventDispatcher(EventDispatcher e) { _event.attachEventDispatcher(e.getEventDispatcher()); }
|
return _event;
|
||||||
public void detachEventDispatcher(EventDispatcher e) { _event.detachEventDispatcher(e.getEventDispatcher()); }
|
}
|
||||||
public void notifyEvent(String e, Object a) { _event.notifyEvent(e,a); }
|
|
||||||
public Object getEventValue(String n) { return _event.getEventValue(n); }
|
public void attachEventDispatcher(EventDispatcher e) {
|
||||||
public Set getEvents() { return _event.getEvents(); }
|
_event.attachEventDispatcher(e.getEventDispatcher());
|
||||||
public void ignoreEvents() { _event.ignoreEvents(); }
|
}
|
||||||
public void unIgnoreEvents() { _event.unIgnoreEvents(); }
|
|
||||||
public Object waitEventValue(String n) { return _event.waitEventValue(n); }
|
public void detachEventDispatcher(EventDispatcher e) {
|
||||||
|
_event.detachEventDispatcher(e.getEventDispatcher());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void notifyEvent(String e, Object a) {
|
||||||
|
_event.notifyEvent(e, a);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getEventValue(String n) {
|
||||||
|
return _event.getEventValue(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set getEvents() {
|
||||||
|
return _event.getEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ignoreEvents() {
|
||||||
|
_event.ignoreEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unIgnoreEvents() {
|
||||||
|
_event.unIgnoreEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object waitEventValue(String n) {
|
||||||
|
return _event.waitEventValue(n);
|
||||||
|
}
|
||||||
}
|
}
|
@ -22,17 +22,17 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
|
|||||||
|
|
||||||
private static final int PING_COUNT = 3;
|
private static final int PING_COUNT = 3;
|
||||||
private static final int CPING_COUNT = 5;
|
private static final int CPING_COUNT = 5;
|
||||||
private static final int PING_TIMEOUT= 5000;
|
private static final int PING_TIMEOUT = 5000;
|
||||||
|
|
||||||
private static final long PING_DISTANCE=1000;
|
private static final long PING_DISTANCE = 1000;
|
||||||
|
|
||||||
private int MAX_SIMUL_PINGS=10; // not really final...
|
private int MAX_SIMUL_PINGS = 10; // not really final...
|
||||||
|
|
||||||
private boolean countPing=false;
|
private boolean countPing = false;
|
||||||
|
|
||||||
private I2PSocketManager sockMgr;
|
private I2PSocketManager sockMgr;
|
||||||
private Logging l;
|
private Logging l;
|
||||||
private boolean finished=false;
|
private boolean finished = false;
|
||||||
private String command;
|
private String command;
|
||||||
private long timeout = PING_TIMEOUT;
|
private long timeout = PING_TIMEOUT;
|
||||||
|
|
||||||
@ -40,190 +40,183 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
|
|||||||
private int simulPings = 0;
|
private int simulPings = 0;
|
||||||
private long lastPingTime = 0;
|
private long lastPingTime = 0;
|
||||||
|
|
||||||
private Object lock = new Object(), slock = new Object();
|
private Object lock = new Object(), slock = new Object();
|
||||||
|
|
||||||
//public I2Ping(String cmd, Logging l,
|
//public I2Ping(String cmd, Logging l,
|
||||||
// boolean ownDest) {
|
// boolean ownDest) {
|
||||||
// I2Ping(cmd, l, (EventDispatcher)null);
|
// I2Ping(cmd, l, (EventDispatcher)null);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public I2Ping(String cmd, Logging l,
|
public I2Ping(String cmd, Logging l, boolean ownDest, EventDispatcher notifyThis) {
|
||||||
boolean ownDest, EventDispatcher notifyThis) {
|
super("I2Ping [" + cmd + "]", notifyThis);
|
||||||
super("I2Ping ["+cmd+"]", notifyThis);
|
this.l = l;
|
||||||
this.l=l;
|
command = cmd;
|
||||||
command=cmd;
|
synchronized (slock) {
|
||||||
synchronized(slock) {
|
if (ownDest) {
|
||||||
if (ownDest) {
|
sockMgr = I2PTunnelClient.buildSocketManager();
|
||||||
sockMgr = I2PTunnelClient.buildSocketManager();
|
} else {
|
||||||
} else {
|
sockMgr = I2PTunnelClient.getSocketManager();
|
||||||
sockMgr = I2PTunnelClient.getSocketManager();
|
}
|
||||||
}
|
}
|
||||||
}
|
Thread t = new I2PThread(this);
|
||||||
Thread t = new I2PThread(this);
|
t.setName("Client");
|
||||||
t.setName("Client");
|
t.start();
|
||||||
t.start();
|
open = true;
|
||||||
open=true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
l.log("*** I2Ping results:");
|
l.log("*** I2Ping results:");
|
||||||
try {
|
try {
|
||||||
runCommand(command);
|
runCommand(command);
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
l.log("*** Interrupted");
|
l.log("*** Interrupted");
|
||||||
_log.error("Pinger interrupted",ex);
|
_log.error("Pinger interrupted", ex);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
_log.error("Pinger exception",ex);
|
_log.error("Pinger exception", ex);
|
||||||
}
|
}
|
||||||
l.log("*** Finished.");
|
l.log("*** Finished.");
|
||||||
synchronized(lock) {
|
synchronized (lock) {
|
||||||
finished=true;
|
finished = true;
|
||||||
}
|
}
|
||||||
close(false);
|
close(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runCommand(String cmd) throws InterruptedException,
|
public void runCommand(String cmd) throws InterruptedException, IOException {
|
||||||
IOException {
|
if (cmd.startsWith("-t ")) { // timeout
|
||||||
if (cmd.startsWith("-t ")) { // timeout
|
cmd = cmd.substring(3);
|
||||||
cmd = cmd.substring(3);
|
int pos = cmd.indexOf(" ");
|
||||||
int pos = cmd.indexOf(" ");
|
if (pos == -1) {
|
||||||
if (pos == -1) {
|
l.log("Syntax error");
|
||||||
l.log("Syntax error");
|
return;
|
||||||
return;
|
} else {
|
||||||
} else {
|
timeout = Long.parseLong(cmd.substring(0, pos));
|
||||||
timeout = Long.parseLong(cmd.substring(0, pos));
|
cmd = cmd.substring(pos + 1);
|
||||||
cmd=cmd.substring(pos+1);
|
}
|
||||||
}
|
}
|
||||||
}
|
if (cmd.startsWith("-m ")) { // max simultaneous pings
|
||||||
if (cmd.startsWith("-m ")) { // max simultaneous pings
|
cmd = cmd.substring(3);
|
||||||
cmd = cmd.substring(3);
|
int pos = cmd.indexOf(" ");
|
||||||
int pos = cmd.indexOf(" ");
|
if (pos == -1) {
|
||||||
if (pos == -1) {
|
l.log("Syntax error");
|
||||||
l.log("Syntax error");
|
return;
|
||||||
return;
|
} else {
|
||||||
} else {
|
MAX_SIMUL_PINGS = Integer.parseInt(cmd.substring(0, pos));
|
||||||
MAX_SIMUL_PINGS = Integer.parseInt(cmd.substring(0, pos));
|
cmd = cmd.substring(pos + 1);
|
||||||
cmd=cmd.substring(pos+1);
|
}
|
||||||
}
|
}
|
||||||
}
|
if (cmd.startsWith("-c ")) { // "count" ping
|
||||||
if (cmd.startsWith("-c ")) { // "count" ping
|
countPing = true;
|
||||||
countPing=true;
|
cmd = cmd.substring(3);
|
||||||
cmd=cmd.substring(3);
|
}
|
||||||
}
|
if (cmd.equals("-h")) { // ping all hosts
|
||||||
if (cmd.equals("-h")) { // ping all hosts
|
cmd = "-l hosts.txt";
|
||||||
cmd="-l hosts.txt";
|
}
|
||||||
}
|
if (cmd.startsWith("-l ")) { // ping a list of hosts
|
||||||
if (cmd.startsWith("-l ")) { // ping a list of hosts
|
BufferedReader br = new BufferedReader(new FileReader(cmd.substring(3)));
|
||||||
BufferedReader br = new BufferedReader
|
String line;
|
||||||
(new FileReader(cmd.substring(3)));
|
List pingHandlers = new ArrayList();
|
||||||
String line;
|
while ((line = br.readLine()) != null) {
|
||||||
List pingHandlers = new ArrayList();
|
if (line.startsWith("#")) continue; // comments
|
||||||
while ((line = br.readLine()) != null) {
|
if (line.startsWith(";")) continue;
|
||||||
if (line.startsWith("#")) continue; // comments
|
if (line.startsWith("!")) continue;
|
||||||
if (line.startsWith(";")) continue;
|
if (line.indexOf("=") != -1) { // maybe file is hosts.txt?
|
||||||
if (line.startsWith("!")) continue;
|
line = line.substring(0, line.indexOf("="));
|
||||||
if (line.indexOf("=") != -1) { // maybe file is hosts.txt?
|
}
|
||||||
line=line.substring(0,line.indexOf("="));
|
pingHandlers.add(new PingHandler(line));
|
||||||
}
|
}
|
||||||
pingHandlers.add(new PingHandler(line));
|
br.close();
|
||||||
}
|
for (Iterator it = pingHandlers.iterator(); it.hasNext();) {
|
||||||
br.close();
|
Thread t = (Thread) it.next();
|
||||||
for (Iterator it= pingHandlers.iterator(); it.hasNext(); ) {
|
t.join();
|
||||||
Thread t = (Thread) it.next();
|
}
|
||||||
t.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Thread t = new PingHandler(cmd);
|
Thread t = new PingHandler(cmd);
|
||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean close(boolean forced) {
|
public boolean close(boolean forced) {
|
||||||
if (!open) return true;
|
if (!open) return true;
|
||||||
synchronized(lock) {
|
synchronized (lock) {
|
||||||
if (!forced && !finished) {
|
if (!forced && !finished) {
|
||||||
l.log("There are still pings running!");
|
l.log("There are still pings running!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
l.log("Closing pinger "+toString());
|
l.log("Closing pinger " + toString());
|
||||||
l.log("Pinger closed.");
|
l.log("Pinger closed.");
|
||||||
open=false;
|
open = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean ping(Destination dest) throws I2PException {
|
public boolean ping(Destination dest) throws I2PException {
|
||||||
try {
|
try {
|
||||||
synchronized(simulLock) {
|
synchronized (simulLock) {
|
||||||
while (simulPings >= MAX_SIMUL_PINGS) {
|
while (simulPings >= MAX_SIMUL_PINGS) {
|
||||||
simulLock.wait();
|
simulLock.wait();
|
||||||
}
|
}
|
||||||
simulPings++;
|
simulPings++;
|
||||||
while (lastPingTime + PING_DISTANCE >
|
while (lastPingTime + PING_DISTANCE > System.currentTimeMillis()) {
|
||||||
System.currentTimeMillis()) {
|
// no wait here, to delay all pingers
|
||||||
// no wait here, to delay all pingers
|
Thread.sleep(PING_DISTANCE / 2);
|
||||||
Thread.sleep(PING_DISTANCE/2);
|
}
|
||||||
}
|
lastPingTime = System.currentTimeMillis();
|
||||||
lastPingTime=System.currentTimeMillis();
|
}
|
||||||
}
|
boolean sent = sockMgr.ping(dest, PING_TIMEOUT);
|
||||||
boolean sent = sockMgr.ping(dest, PING_TIMEOUT);
|
synchronized (simulLock) {
|
||||||
synchronized(simulLock) {
|
simulPings--;
|
||||||
simulPings--;
|
simulLock.notifyAll();
|
||||||
simulLock.notifyAll();
|
}
|
||||||
}
|
return sent;
|
||||||
return sent;
|
} catch (InterruptedException ex) {
|
||||||
} catch (InterruptedException ex) {
|
_log.error("Interrupted", ex);
|
||||||
_log.error("Interrupted", ex);
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class PingHandler extends I2PThread {
|
public class PingHandler extends I2PThread {
|
||||||
private String destination;
|
private String destination;
|
||||||
|
|
||||||
public PingHandler(String dest) {
|
public PingHandler(String dest) {
|
||||||
this.destination=dest;
|
this.destination = dest;
|
||||||
setName("PingHandler for " + dest);
|
setName("PingHandler for " + dest);
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Destination dest=I2PTunnel.destFromName(destination);
|
Destination dest = I2PTunnel.destFromName(destination);
|
||||||
if (dest == null) {
|
if (dest == null) {
|
||||||
synchronized(lock) { // Logger is not thread safe
|
synchronized (lock) { // Logger is not thread safe
|
||||||
l.log("Unresolvable: "+destination+"");
|
l.log("Unresolvable: " + destination + "");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int cnt = countPing ? CPING_COUNT : PING_COUNT;
|
int cnt = countPing ? CPING_COUNT : PING_COUNT;
|
||||||
StringBuffer pingResults = new StringBuffer
|
StringBuffer pingResults = new StringBuffer(2 * cnt + destination.length() + 3);
|
||||||
(2*cnt+ destination.length()+3);
|
for (int i = 0; i < cnt; i++) {
|
||||||
for (int i=0;i<cnt; i++) {
|
boolean sent;
|
||||||
boolean sent;
|
sent = ping(dest);
|
||||||
sent = ping(dest);
|
if (countPing) {
|
||||||
if (countPing) {
|
if (!sent) {
|
||||||
if (!sent) {
|
pingResults.append(i).append(" ");
|
||||||
pingResults.append(i).append(" ");
|
break;
|
||||||
break;
|
} else if (i == cnt - 1) {
|
||||||
} else if (i == cnt - 1) {
|
pingResults.append("+ ");
|
||||||
pingResults.append("+ ");
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
pingResults.append(sent ? "+ " : "- ");
|
||||||
pingResults.append(sent?"+ ":"- ");
|
}
|
||||||
}
|
// System.out.println(sent+" -> "+destination);
|
||||||
// System.out.println(sent+" -> "+destination);
|
}
|
||||||
}
|
pingResults.append(" ").append(destination);
|
||||||
pingResults.append(" ").append(destination);
|
synchronized (lock) { // Logger is not thread safe
|
||||||
synchronized(lock) { // Logger is not thread safe
|
l.log(pingResults.toString());
|
||||||
l.log(pingResults.toString());
|
}
|
||||||
}
|
} catch (I2PException ex) {
|
||||||
} catch (I2PException ex) {
|
_log.error("Error pinging " + destination, ex);
|
||||||
_log.error("Error pinging " + destination, ex);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,7 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
package net.i2p.i2ptunnel;
|
package net.i2p.i2ptunnel;
|
||||||
|
|
||||||
|
|
||||||
public interface Logging {
|
public interface Logging {
|
||||||
public void log(String s);
|
public void log(String s);
|
||||||
}
|
}
|
@ -153,105 +153,112 @@ public class TunnelManager implements Runnable {
|
|||||||
private boolean _keepAccepting;
|
private boolean _keepAccepting;
|
||||||
|
|
||||||
public TunnelManager(int listenPort) {
|
public TunnelManager(int listenPort) {
|
||||||
this(null, listenPort);
|
this(null, listenPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TunnelManager(String listenHost, int listenPort) {
|
public TunnelManager(String listenHost, int listenPort) {
|
||||||
_tunnel = new I2PTunnel();
|
_tunnel = new I2PTunnel();
|
||||||
_keepAccepting = true;
|
_keepAccepting = true;
|
||||||
try {
|
try {
|
||||||
if (listenHost != null) {
|
if (listenHost != null) {
|
||||||
_socket = new ServerSocket(listenPort, 0, InetAddress.getByName(listenHost));
|
_socket = new ServerSocket(listenPort, 0, InetAddress.getByName(listenHost));
|
||||||
_log.info("Listening for tunnel management clients on " + listenHost + ":" + listenPort);
|
_log.info("Listening for tunnel management clients on " + listenHost + ":" + listenPort);
|
||||||
} else {
|
} else {
|
||||||
_socket = new ServerSocket(listenPort);
|
_socket = new ServerSocket(listenPort);
|
||||||
_log.info("Listening for tunnel management clients on localhost:" + listenPort);
|
_log.info("Listening for tunnel management clients on localhost:" + listenPort);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
_log.error("Error starting up tunnel management listener on " + listenPort, e);
|
_log.error("Error starting up tunnel management listener on " + listenPort, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String args[]) {
|
public static void main(String args[]) {
|
||||||
int port = 7676;
|
int port = 7676;
|
||||||
String host = null;
|
String host = null;
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
try {
|
try {
|
||||||
port = Integer.parseInt(args[0]);
|
port = Integer.parseInt(args[0]);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
_log.error("Usage: TunnelManager [host] [port]");
|
_log.error("Usage: TunnelManager [host] [port]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (args.length == 2) {
|
} else if (args.length == 2) {
|
||||||
host = args[0];
|
host = args[0];
|
||||||
try {
|
try {
|
||||||
port = Integer.parseInt(args[1]);
|
port = Integer.parseInt(args[1]);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
_log.error("Usage: TunnelManager [host] [port]");
|
_log.error("Usage: TunnelManager [host] [port]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TunnelManager mgr = new TunnelManager(host, port);
|
TunnelManager mgr = new TunnelManager(host, port);
|
||||||
Thread t = new I2PThread(mgr, "Listener");
|
Thread t = new I2PThread(mgr, "Listener");
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
if (_socket == null) {
|
if (_socket == null) {
|
||||||
_log.error("Unable to start listening, since the socket was not bound. Already running?");
|
_log.error("Unable to start listening, since the socket was not bound. Already running?");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_log.debug("Running");
|
_log.debug("Running");
|
||||||
try {
|
try {
|
||||||
while (_keepAccepting) {
|
while (_keepAccepting) {
|
||||||
Socket socket = _socket.accept();
|
Socket socket = _socket.accept();
|
||||||
_log.debug("Client accepted");
|
_log.debug("Client accepted");
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
Thread t = new I2PThread(new TunnelManagerClientRunner(this, socket));
|
Thread t = new I2PThread(new TunnelManagerClientRunner(this, socket));
|
||||||
t.setName("TunnelManager Client");
|
t.setName("TunnelManager Client");
|
||||||
t.setPriority(I2PThread.MIN_PRIORITY);
|
t.setPriority(I2PThread.MIN_PRIORITY);
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
_log.error("Error accepting connections", ioe);
|
_log.error("Error accepting connections", ioe);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
_log.error("Other error?!", e);
|
_log.error("Other error?!", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (_socket != null) try { _socket.close(); } catch (IOException ioe) {}
|
if (_socket != null) try {
|
||||||
}
|
_socket.close();
|
||||||
try { Thread.sleep(5000); } catch (InterruptedException ie) {}
|
} catch (IOException ioe) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void error(String msg, OutputStream out) throws IOException {
|
public void error(String msg, OutputStream out) throws IOException {
|
||||||
out.write(msg.getBytes());
|
out.write(msg.getBytes());
|
||||||
out.write('\n');
|
out.write('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processQuit(OutputStream out) throws IOException {
|
public void processQuit(OutputStream out) throws IOException {
|
||||||
out.write("Nice try".getBytes());
|
out.write("Nice try".getBytes());
|
||||||
out.write('\n');
|
out.write('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processList(OutputStream out) throws IOException {
|
public void processList(OutputStream out) throws IOException {
|
||||||
BufferLogger buf = new BufferLogger();
|
BufferLogger buf = new BufferLogger();
|
||||||
long startCommand = Clock.getInstance().now();
|
long startCommand = Clock.getInstance().now();
|
||||||
_tunnel.runCommand("list", buf);
|
_tunnel.runCommand("list", buf);
|
||||||
Object obj = _tunnel.waitEventValue("listDone");
|
Object obj = _tunnel.waitEventValue("listDone");
|
||||||
long endCommand = Clock.getInstance().now();
|
long endCommand = Clock.getInstance().now();
|
||||||
String str = buf.getBuffer();
|
String str = buf.getBuffer();
|
||||||
_log.debug("ListDone complete after " + (endCommand-startCommand) + "ms: [" + str + "]");
|
_log.debug("ListDone complete after " + (endCommand - startCommand) + "ms: [" + str + "]");
|
||||||
out.write(str.getBytes());
|
out.write(str.getBytes());
|
||||||
out.write('\n');
|
out.write('\n');
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processListenOn(String ip, OutputStream out) throws IOException {
|
public void processListenOn(String ip, OutputStream out) throws IOException {
|
||||||
BufferLogger buf = new BufferLogger();
|
BufferLogger buf = new BufferLogger();
|
||||||
_tunnel.runCommand("listen_on " + ip, buf);
|
_tunnel.runCommand("listen_on " + ip, buf);
|
||||||
String status = (String)_tunnel.waitEventValue("listen_onResult");
|
String status = (String) _tunnel.waitEventValue("listen_onResult");
|
||||||
out.write((status + "\n").getBytes());
|
out.write((status + "\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -260,44 +267,44 @@ public class TunnelManager implements Runnable {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void processLookup(String name, OutputStream out) throws IOException {
|
public void processLookup(String name, OutputStream out) throws IOException {
|
||||||
BufferLogger buf = new BufferLogger();
|
BufferLogger buf = new BufferLogger();
|
||||||
_tunnel.runCommand("lookup " + name, buf);
|
_tunnel.runCommand("lookup " + name, buf);
|
||||||
String rv = (String)_tunnel.waitEventValue("lookupResult");
|
String rv = (String) _tunnel.waitEventValue("lookupResult");
|
||||||
out.write(rv.getBytes());
|
out.write(rv.getBytes());
|
||||||
out.write('\n');
|
out.write('\n');
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processTestDestination(String destKey, OutputStream out) throws IOException {
|
public void processTestDestination(String destKey, OutputStream out) throws IOException {
|
||||||
try {
|
try {
|
||||||
Destination d = new Destination();
|
Destination d = new Destination();
|
||||||
d.fromBase64(destKey);
|
d.fromBase64(destKey);
|
||||||
out.write("valid\n".getBytes());
|
out.write("valid\n".getBytes());
|
||||||
} catch (DataFormatException dfe) {
|
} catch (DataFormatException dfe) {
|
||||||
out.write("invalid\n".getBytes());
|
out.write("invalid\n".getBytes());
|
||||||
}
|
}
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processConvertPrivate(String priv, OutputStream out) throws IOException {
|
public void processConvertPrivate(String priv, OutputStream out) throws IOException {
|
||||||
try {
|
try {
|
||||||
Destination dest = new Destination();
|
Destination dest = new Destination();
|
||||||
dest.fromBase64(priv);
|
dest.fromBase64(priv);
|
||||||
String str = dest.toBase64();
|
String str = dest.toBase64();
|
||||||
out.write(str.getBytes());
|
out.write(str.getBytes());
|
||||||
out.write('\n');
|
out.write('\n');
|
||||||
} catch (DataFormatException dfe) {
|
} catch (DataFormatException dfe) {
|
||||||
_log.error("Error converting private data", dfe);
|
_log.error("Error converting private data", dfe);
|
||||||
out.write("Error converting private key\n".getBytes());
|
out.write("Error converting private key\n".getBytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processClose(String which, boolean forced, OutputStream out) throws IOException {
|
public void processClose(String which, boolean forced, OutputStream out) throws IOException {
|
||||||
BufferLogger buf = new BufferLogger();
|
BufferLogger buf = new BufferLogger();
|
||||||
_tunnel.runCommand((forced?"close forced ":"close ") + which, buf);
|
_tunnel.runCommand((forced ? "close forced " : "close ") + which, buf);
|
||||||
String str = (String)_tunnel.waitEventValue("closeResult");
|
String str = (String) _tunnel.waitEventValue("closeResult");
|
||||||
out.write((str + "\n").getBytes());
|
out.write((str + "\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -306,119 +313,114 @@ public class TunnelManager implements Runnable {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void processGenKey(OutputStream out) throws IOException {
|
public void processGenKey(OutputStream out) throws IOException {
|
||||||
BufferLogger buf = new BufferLogger();
|
BufferLogger buf = new BufferLogger();
|
||||||
_tunnel.runCommand("gentextkeys", buf);
|
_tunnel.runCommand("gentextkeys", buf);
|
||||||
String priv = (String)_tunnel.waitEventValue("privateKey");
|
String priv = (String) _tunnel.waitEventValue("privateKey");
|
||||||
String pub = (String)_tunnel.waitEventValue("publicDestination");
|
String pub = (String) _tunnel.waitEventValue("publicDestination");
|
||||||
out.write((pub + "\t" + priv).getBytes());
|
out.write((pub + "\t" + priv).getBytes());
|
||||||
out.write('\n');
|
out.write('\n');
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processOpenClient(int listenPort, String peer, OutputStream out) throws IOException {
|
public void processOpenClient(int listenPort, String peer, OutputStream out) throws IOException {
|
||||||
BufferLogger buf = new BufferLogger();
|
BufferLogger buf = new BufferLogger();
|
||||||
_tunnel.runCommand("client " + listenPort + " " + peer, buf);
|
_tunnel.runCommand("client " + listenPort + " " + peer, buf);
|
||||||
Integer taskId = (Integer)_tunnel.waitEventValue("clientTaskId");
|
Integer taskId = (Integer) _tunnel.waitEventValue("clientTaskId");
|
||||||
if (taskId.intValue() < 0) {
|
if (taskId.intValue() < 0) {
|
||||||
out.write("error\n".getBytes());
|
out.write("error\n".getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String rv = (String)_tunnel.waitEventValue("openClientResult");
|
String rv = (String) _tunnel.waitEventValue("openClientResult");
|
||||||
if (rv.equals("error")) {
|
if (rv.equals("error")) {
|
||||||
out.write((rv + "\n").getBytes());
|
out.write((rv + "\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listenPort != 0) {
|
if (listenPort != 0) {
|
||||||
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Integer port = (Integer)_tunnel.waitEventValue("clientLocalPort");
|
Integer port = (Integer) _tunnel.waitEventValue("clientLocalPort");
|
||||||
out.write((rv + " " + port.intValue() + " [" + taskId.intValue()
|
out.write((rv + " " + port.intValue() + " [" + taskId.intValue() + "]\n").getBytes());
|
||||||
+ "]\n").getBytes());
|
buf.ignoreFurtherActions();
|
||||||
buf.ignoreFurtherActions();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processOpenHTTPClient(int listenPort,
|
public void processOpenHTTPClient(int listenPort, String proxy, OutputStream out) throws IOException {
|
||||||
String proxy,
|
BufferLogger buf = new BufferLogger();
|
||||||
OutputStream out) throws IOException {
|
_tunnel.runCommand("httpclient " + listenPort + " " + proxy, buf);
|
||||||
BufferLogger buf = new BufferLogger();
|
Integer taskId = (Integer) _tunnel.waitEventValue("httpclientTaskId");
|
||||||
_tunnel.runCommand("httpclient " + listenPort + " " + proxy, buf);
|
if (taskId.intValue() < 0) {
|
||||||
Integer taskId = (Integer)_tunnel.waitEventValue("httpclientTaskId");
|
out.write("error\n".getBytes());
|
||||||
if (taskId.intValue() < 0) {
|
buf.ignoreFurtherActions();
|
||||||
out.write("error\n".getBytes());
|
return;
|
||||||
buf.ignoreFurtherActions();
|
}
|
||||||
return;
|
String rv = (String) _tunnel.waitEventValue("openHTTPClientResult");
|
||||||
}
|
if (rv.equals("error")) {
|
||||||
String rv = (String)_tunnel.waitEventValue("openHTTPClientResult");
|
out.write((rv + "\n").getBytes());
|
||||||
if (rv.equals("error")) {
|
buf.ignoreFurtherActions();
|
||||||
out.write((rv + "\n").getBytes());
|
return;
|
||||||
buf.ignoreFurtherActions();
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (listenPort != 0) {
|
if (listenPort != 0) {
|
||||||
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Integer port = (Integer)_tunnel.waitEventValue("clientLocalPort");
|
Integer port = (Integer) _tunnel.waitEventValue("clientLocalPort");
|
||||||
out.write((rv + " " + port.intValue() + " [" + taskId.intValue()
|
out.write((rv + " " + port.intValue() + " [" + taskId.intValue() + "]\n").getBytes());
|
||||||
+ "]\n").getBytes());
|
buf.ignoreFurtherActions();
|
||||||
buf.ignoreFurtherActions();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processOpenSOCKSTunnel(int listenPort,
|
public void processOpenSOCKSTunnel(int listenPort, OutputStream out) throws IOException {
|
||||||
OutputStream out) throws IOException {
|
BufferLogger buf = new BufferLogger();
|
||||||
BufferLogger buf = new BufferLogger();
|
_tunnel.runCommand("sockstunnel " + listenPort, buf);
|
||||||
_tunnel.runCommand("sockstunnel " + listenPort, buf);
|
Integer taskId = (Integer) _tunnel.waitEventValue("sockstunnelTaskId");
|
||||||
Integer taskId = (Integer)_tunnel.waitEventValue("sockstunnelTaskId");
|
if (taskId.intValue() < 0) {
|
||||||
if (taskId.intValue() < 0) {
|
out.write("error\n".getBytes());
|
||||||
out.write("error\n".getBytes());
|
buf.ignoreFurtherActions();
|
||||||
buf.ignoreFurtherActions();
|
return;
|
||||||
return;
|
}
|
||||||
}
|
String rv = (String) _tunnel.waitEventValue("openSOCKSTunnelResult");
|
||||||
String rv = (String)_tunnel.waitEventValue("openSOCKSTunnelResult");
|
if (rv.equals("error")) {
|
||||||
if (rv.equals("error")) {
|
out.write((rv + "\n").getBytes());
|
||||||
out.write((rv + "\n").getBytes());
|
buf.ignoreFurtherActions();
|
||||||
buf.ignoreFurtherActions();
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (listenPort != 0) {
|
if (listenPort != 0) {
|
||||||
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Integer port = (Integer)_tunnel.waitEventValue("clientLocalPort");
|
Integer port = (Integer) _tunnel.waitEventValue("clientLocalPort");
|
||||||
out.write((rv + " " + port.intValue() + " [" + taskId.intValue()
|
out.write((rv + " " + port.intValue() + " [" + taskId.intValue() + "]\n").getBytes());
|
||||||
+ "]\n").getBytes());
|
buf.ignoreFurtherActions();
|
||||||
buf.ignoreFurtherActions();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processOpenServer(String serverHost, int serverPort, String privateKeys, OutputStream out) throws IOException {
|
public void processOpenServer(String serverHost, int serverPort, String privateKeys, OutputStream out)
|
||||||
BufferLogger buf = new BufferLogger();
|
throws IOException {
|
||||||
_tunnel.runCommand("textserver " + serverHost + " " + serverPort + " " + privateKeys, buf);
|
BufferLogger buf = new BufferLogger();
|
||||||
Integer taskId = (Integer)_tunnel.waitEventValue("serverTaskId");
|
_tunnel.runCommand("textserver " + serverHost + " " + serverPort + " " + privateKeys, buf);
|
||||||
if (taskId.intValue() < 0) {
|
Integer taskId = (Integer) _tunnel.waitEventValue("serverTaskId");
|
||||||
out.write("error\n".getBytes());
|
if (taskId.intValue() < 0) {
|
||||||
buf.ignoreFurtherActions();
|
out.write("error\n".getBytes());
|
||||||
return;
|
buf.ignoreFurtherActions();
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
String rv = (String)_tunnel.waitEventValue("openServerResult");
|
String rv = (String) _tunnel.waitEventValue("openServerResult");
|
||||||
|
|
||||||
if (rv.equals("error")) {
|
if (rv.equals("error")) {
|
||||||
out.write((rv + "\n").getBytes());
|
out.write((rv + "\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
out.write((rv + " [" + taskId.intValue() + "]\n").getBytes());
|
||||||
buf.ignoreFurtherActions();
|
buf.ignoreFurtherActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -426,8 +428,8 @@ public class TunnelManager implements Runnable {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void unknownCommand(String command, OutputStream out) throws IOException {
|
public void unknownCommand(String command, OutputStream out) throws IOException {
|
||||||
out.write("Unknown command: ".getBytes());
|
out.write("Unknown command: ".getBytes());
|
||||||
out.write(command.getBytes());
|
out.write(command.getBytes());
|
||||||
out.write("\n".getBytes());
|
out.write("\n".getBytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,25 +23,27 @@ class TunnelManagerClientRunner implements Runnable {
|
|||||||
private Socket _clientSocket;
|
private Socket _clientSocket;
|
||||||
|
|
||||||
public TunnelManagerClientRunner(TunnelManager mgr, Socket socket) {
|
public TunnelManagerClientRunner(TunnelManager mgr, Socket socket) {
|
||||||
_clientSocket = socket;
|
_clientSocket = socket;
|
||||||
_mgr = mgr;
|
_mgr = mgr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
_log.debug("Client running");
|
_log.debug("Client running");
|
||||||
try {
|
try {
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(_clientSocket.getInputStream()));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(_clientSocket.getInputStream()));
|
||||||
OutputStream out = _clientSocket.getOutputStream();
|
OutputStream out = _clientSocket.getOutputStream();
|
||||||
|
|
||||||
String cmd = reader.readLine();
|
String cmd = reader.readLine();
|
||||||
if (cmd != null)
|
if (cmd != null) processCommand(cmd, out);
|
||||||
processCommand(cmd, out);
|
} catch (IOException ioe) {
|
||||||
} catch (IOException ioe) {
|
_log.error("Error processing client commands", ioe);
|
||||||
_log.error("Error processing client commands", ioe);
|
} finally {
|
||||||
} finally {
|
if (_clientSocket != null) try {
|
||||||
if (_clientSocket != null) try { _clientSocket.close(); } catch (IOException ioe) {}
|
_clientSocket.close();
|
||||||
}
|
} catch (IOException ioe) {
|
||||||
_log.debug("Client closed");
|
}
|
||||||
|
}
|
||||||
|
_log.debug("Client closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,144 +51,144 @@ class TunnelManagerClientRunner implements Runnable {
|
|||||||
* sending the results to the output stream
|
* sending the results to the output stream
|
||||||
*/
|
*/
|
||||||
private void processCommand(String command, OutputStream out) throws IOException {
|
private void processCommand(String command, OutputStream out) throws IOException {
|
||||||
_log.debug("Processing [" + command + "]");
|
_log.debug("Processing [" + command + "]");
|
||||||
StringTokenizer tok = new StringTokenizer(command);
|
StringTokenizer tok = new StringTokenizer(command);
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.unknownCommand(command, out);
|
_mgr.unknownCommand(command, out);
|
||||||
} else {
|
} else {
|
||||||
String cmd = tok.nextToken();
|
String cmd = tok.nextToken();
|
||||||
if ("quit".equalsIgnoreCase(cmd)) {
|
if ("quit".equalsIgnoreCase(cmd)) {
|
||||||
_mgr.processQuit(out);
|
_mgr.processQuit(out);
|
||||||
} else if ("lookup".equalsIgnoreCase(cmd)) {
|
} else if ("lookup".equalsIgnoreCase(cmd)) {
|
||||||
if (tok.hasMoreTokens())
|
if (tok.hasMoreTokens())
|
||||||
_mgr.processLookup(tok.nextToken(), out);
|
_mgr.processLookup(tok.nextToken(), out);
|
||||||
else
|
else
|
||||||
_mgr.error("Usage: lookup <hostname>", out);
|
_mgr.error("Usage: lookup <hostname>", out);
|
||||||
} else if ("testdestination".equalsIgnoreCase(cmd)) {
|
} else if ("testdestination".equalsIgnoreCase(cmd)) {
|
||||||
if (tok.hasMoreTokens())
|
if (tok.hasMoreTokens())
|
||||||
_mgr.processTestDestination(tok.nextToken(), out);
|
_mgr.processTestDestination(tok.nextToken(), out);
|
||||||
else
|
else
|
||||||
_mgr.error("Usage: testdestination <publicDestination>", out);
|
_mgr.error("Usage: testdestination <publicDestination>", out);
|
||||||
} else if ("convertprivate".equalsIgnoreCase(cmd)) {
|
} else if ("convertprivate".equalsIgnoreCase(cmd)) {
|
||||||
if (tok.hasMoreTokens())
|
if (tok.hasMoreTokens())
|
||||||
_mgr.processConvertPrivate(tok.nextToken(), out);
|
_mgr.processConvertPrivate(tok.nextToken(), out);
|
||||||
else
|
else
|
||||||
_mgr.error("Usage: convertprivate <privateData>", out);
|
_mgr.error("Usage: convertprivate <privateData>", out);
|
||||||
} else if ("close".equalsIgnoreCase(cmd)) {
|
} else if ("close".equalsIgnoreCase(cmd)) {
|
||||||
if (tok.hasMoreTokens()) {
|
if (tok.hasMoreTokens()) {
|
||||||
String closeArg;
|
String closeArg;
|
||||||
if ((closeArg = tok.nextToken()).equals("forced")) {
|
if ((closeArg = tok.nextToken()).equals("forced")) {
|
||||||
if (tok.hasMoreTokens()) {
|
if (tok.hasMoreTokens()) {
|
||||||
_mgr.processClose(tok.nextToken(), true, out);
|
_mgr.processClose(tok.nextToken(), true, out);
|
||||||
} else {
|
} else {
|
||||||
_mgr.error("Usage: close [forced] <jobnumber>|all", out);
|
_mgr.error("Usage: close [forced] <jobnumber>|all", out);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_mgr.processClose(closeArg, false, out);
|
_mgr.processClose(closeArg, false, out);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_mgr.error("Usage: close [forced] <jobnumber>|all", out);
|
_mgr.error("Usage: close [forced] <jobnumber>|all", out);
|
||||||
}
|
}
|
||||||
} else if ("genkey".equalsIgnoreCase(cmd)) {
|
} else if ("genkey".equalsIgnoreCase(cmd)) {
|
||||||
_mgr.processGenKey(out);
|
_mgr.processGenKey(out);
|
||||||
} else if ("list".equalsIgnoreCase(cmd)) {
|
} else if ("list".equalsIgnoreCase(cmd)) {
|
||||||
_mgr.processList(out);
|
_mgr.processList(out);
|
||||||
} else if ("listen_on".equalsIgnoreCase(cmd)) {
|
} else if ("listen_on".equalsIgnoreCase(cmd)) {
|
||||||
if (tok.hasMoreTokens()) {
|
if (tok.hasMoreTokens()) {
|
||||||
_mgr.processListenOn(tok.nextToken(), out);
|
_mgr.processListenOn(tok.nextToken(), out);
|
||||||
} else {
|
} else {
|
||||||
_mgr.error("Usage: listen_on <ip>", out);
|
_mgr.error("Usage: listen_on <ip>", out);
|
||||||
}
|
}
|
||||||
} else if ("openclient".equalsIgnoreCase(cmd)) {
|
} else if ("openclient".equalsIgnoreCase(cmd)) {
|
||||||
int listenPort = 0;
|
int listenPort = 0;
|
||||||
String peer = null;
|
String peer = null;
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: openclient <listenPort> <peer>", out);
|
_mgr.error("Usage: openclient <listenPort> <peer>", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String portStr = tok.nextToken();
|
String portStr = tok.nextToken();
|
||||||
listenPort = Integer.parseInt(portStr);
|
listenPort = Integer.parseInt(portStr);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
_mgr.error("Bad listen port", out);
|
_mgr.error("Bad listen port", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: openclient <listenport> <peer>", out);
|
_mgr.error("Usage: openclient <listenport> <peer>", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
peer = tok.nextToken();
|
peer = tok.nextToken();
|
||||||
_mgr.processOpenClient(listenPort, peer, out);
|
_mgr.processOpenClient(listenPort, peer, out);
|
||||||
} else if ("openhttpclient".equalsIgnoreCase(cmd)) {
|
} else if ("openhttpclient".equalsIgnoreCase(cmd)) {
|
||||||
int listenPort = 0;
|
int listenPort = 0;
|
||||||
String proxy = "squid.i2p";
|
String proxy = "squid.i2p";
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: openhttpclient <listenPort> [<proxy>]",out);
|
_mgr.error("Usage: openhttpclient <listenPort> [<proxy>]", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String portStr = tok.nextToken();
|
String portStr = tok.nextToken();
|
||||||
listenPort = Integer.parseInt(portStr);
|
listenPort = Integer.parseInt(portStr);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
_mgr.error("Bad listen port", out);
|
_mgr.error("Bad listen port", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (tok.hasMoreTokens()) {
|
if (tok.hasMoreTokens()) {
|
||||||
proxy = tok.nextToken();
|
proxy = tok.nextToken();
|
||||||
}
|
}
|
||||||
if (tok.hasMoreTokens()) {
|
if (tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: openclient <listenport> [<proxy>]",out);
|
_mgr.error("Usage: openclient <listenport> [<proxy>]", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_mgr.processOpenHTTPClient(listenPort, proxy, out);
|
_mgr.processOpenHTTPClient(listenPort, proxy, out);
|
||||||
} else if ("opensockstunnel".equalsIgnoreCase(cmd)) {
|
} else if ("opensockstunnel".equalsIgnoreCase(cmd)) {
|
||||||
int listenPort = 0;
|
int listenPort = 0;
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: opensockstunnel <listenPort>",out);
|
_mgr.error("Usage: opensockstunnel <listenPort>", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String portStr = tok.nextToken();
|
String portStr = tok.nextToken();
|
||||||
listenPort = Integer.parseInt(portStr);
|
listenPort = Integer.parseInt(portStr);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
_mgr.error("Bad listen port", out);
|
_mgr.error("Bad listen port", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (tok.hasMoreTokens()) {
|
if (tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: opensockstunnel <listenport>",out);
|
_mgr.error("Usage: opensockstunnel <listenport>", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_mgr.processOpenSOCKSTunnel(listenPort, out);
|
_mgr.processOpenSOCKSTunnel(listenPort, out);
|
||||||
} else if ("openserver".equalsIgnoreCase(cmd)) {
|
} else if ("openserver".equalsIgnoreCase(cmd)) {
|
||||||
int listenPort = 0;
|
int listenPort = 0;
|
||||||
String serverHost = null;
|
String serverHost = null;
|
||||||
String serverKeys = null;
|
String serverKeys = null;
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: openserver <serverHost> <serverPort> <serverKeys>", out);
|
_mgr.error("Usage: openserver <serverHost> <serverPort> <serverKeys>", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
serverHost = tok.nextToken();
|
serverHost = tok.nextToken();
|
||||||
|
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: openserver <serverHost> <serverPort> <serverKeys>", out);
|
_mgr.error("Usage: openserver <serverHost> <serverPort> <serverKeys>", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String portStr = tok.nextToken();
|
String portStr = tok.nextToken();
|
||||||
listenPort = Integer.parseInt(portStr);
|
listenPort = Integer.parseInt(portStr);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
_mgr.error("Bad listen port", out);
|
_mgr.error("Bad listen port", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!tok.hasMoreTokens()) {
|
if (!tok.hasMoreTokens()) {
|
||||||
_mgr.error("Usage: openserver <serverHost> <serverPort> <serverKeys>", out);
|
_mgr.error("Usage: openserver <serverHost> <serverPort> <serverKeys>", out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
serverKeys = tok.nextToken();
|
serverKeys = tok.nextToken();
|
||||||
_mgr.processOpenServer(serverHost, listenPort, serverKeys, out);
|
_mgr.processOpenServer(serverHost, listenPort, serverKeys, out);
|
||||||
} else {
|
} else {
|
||||||
_mgr.unknownCommand(command, out);
|
_mgr.unknownCommand(command, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -26,31 +26,30 @@ public class I2PSOCKSTunnel extends I2PTunnelClientBase {
|
|||||||
// I2PSOCKSTunnel(localPort, l, ownDest, (EventDispatcher)null);
|
// I2PSOCKSTunnel(localPort, l, ownDest, (EventDispatcher)null);
|
||||||
//}
|
//}
|
||||||
|
|
||||||
public I2PSOCKSTunnel(int localPort, Logging l, boolean ownDest,
|
public I2PSOCKSTunnel(int localPort, Logging l, boolean ownDest, EventDispatcher notifyThis) {
|
||||||
EventDispatcher notifyThis) {
|
super(localPort, ownDest, l, notifyThis, "SOCKSHandler");
|
||||||
super(localPort, ownDest, l, notifyThis, "SOCKSHandler");
|
|
||||||
|
|
||||||
if (waitEventValue("openBaseClientResult").equals("error")) {
|
if (waitEventValue("openBaseClientResult").equals("error")) {
|
||||||
notifyEvent("openSOCKSTunnelResult", "error");
|
notifyEvent("openSOCKSTunnelResult", "error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setName(getLocalPort() + " -> SOCKSTunnel");
|
setName(getLocalPort() + " -> SOCKSTunnel");
|
||||||
|
|
||||||
startRunning();
|
startRunning();
|
||||||
|
|
||||||
notifyEvent("openSOCKSTunnelResult", "ok");
|
notifyEvent("openSOCKSTunnelResult", "ok");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void clientConnectionRun(Socket s) {
|
protected void clientConnectionRun(Socket s) {
|
||||||
try {
|
try {
|
||||||
SOCKSServer serv = SOCKSServerFactory.createSOCKSServer(s);
|
SOCKSServer serv = SOCKSServerFactory.createSOCKSServer(s);
|
||||||
Socket clientSock = serv.getClientSocket();
|
Socket clientSock = serv.getClientSocket();
|
||||||
I2PSocket destSock = serv.getDestinationI2PSocket();
|
I2PSocket destSock = serv.getDestinationI2PSocket();
|
||||||
new I2PTunnelRunner (clientSock, destSock, sockLock, null);
|
new I2PTunnelRunner(clientSock, destSock, sockLock, null);
|
||||||
} catch (SOCKSException e) {
|
} catch (SOCKSException e) {
|
||||||
_log.error("Error from SOCKS connection: " + e.getMessage());
|
_log.error("Error from SOCKS connection: " + e.getMessage());
|
||||||
closeSocket(s);
|
closeSocket(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -42,66 +42,61 @@ public class SOCKS5Server extends SOCKSServer {
|
|||||||
* @param clientSock client socket
|
* @param clientSock client socket
|
||||||
*/
|
*/
|
||||||
public SOCKS5Server(Socket clientSock) {
|
public SOCKS5Server(Socket clientSock) {
|
||||||
this.clientSock = clientSock;
|
this.clientSock = clientSock;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Socket getClientSocket() throws SOCKSException {
|
public Socket getClientSocket() throws SOCKSException {
|
||||||
setupServer();
|
setupServer();
|
||||||
|
|
||||||
return clientSock;
|
return clientSock;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setupServer() throws SOCKSException {
|
protected void setupServer() throws SOCKSException {
|
||||||
if (setupCompleted) {
|
if (setupCompleted) { return; }
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
DataInputStream in;
|
DataInputStream in;
|
||||||
DataOutputStream out;
|
DataOutputStream out;
|
||||||
try {
|
try {
|
||||||
in = new DataInputStream(clientSock.getInputStream());
|
in = new DataInputStream(clientSock.getInputStream());
|
||||||
out = new DataOutputStream(clientSock.getOutputStream());
|
out = new DataOutputStream(clientSock.getOutputStream());
|
||||||
|
|
||||||
init(in, out);
|
init(in, out);
|
||||||
manageRequest(in, out);
|
manageRequest(in, out);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new SOCKSException("Connection error ("
|
throw new SOCKSException("Connection error (" + e.getMessage() + ")");
|
||||||
+ e.getMessage() + ")");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
setupCompleted = true;
|
setupCompleted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SOCKS5 connection initialization. This method assumes that
|
* SOCKS5 connection initialization. This method assumes that
|
||||||
* SOCKS "VER" field has been stripped from the input stream.
|
* SOCKS "VER" field has been stripped from the input stream.
|
||||||
*/
|
*/
|
||||||
private void init (DataInputStream in,
|
private void init(DataInputStream in, DataOutputStream out) throws IOException, SOCKSException {
|
||||||
DataOutputStream out) throws IOException, SOCKSException {
|
int nMethods = in.readByte() & 0xff;
|
||||||
int nMethods = in.readByte() & 0xff;
|
boolean methodOk = false;
|
||||||
boolean methodOk = false;
|
int method = Method.NO_ACCEPTABLE_METHODS;
|
||||||
int method = Method.NO_ACCEPTABLE_METHODS;
|
|
||||||
|
|
||||||
for (int i = 0; i < nMethods; ++i) {
|
for (int i = 0; i < nMethods; ++i) {
|
||||||
method = in.readByte() & 0xff;
|
method = in.readByte() & 0xff;
|
||||||
if (method == Method.NO_AUTH_REQUIRED) {
|
if (method == Method.NO_AUTH_REQUIRED) {
|
||||||
// That's fine, we do support this method
|
// That's fine, we do support this method
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean canContinue = false;
|
boolean canContinue = false;
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case Method.NO_AUTH_REQUIRED:
|
case Method.NO_AUTH_REQUIRED:
|
||||||
_log.debug("no authentication required");
|
_log.debug("no authentication required");
|
||||||
sendInitReply(Method.NO_AUTH_REQUIRED, out);
|
sendInitReply(Method.NO_AUTH_REQUIRED, out);
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
_log.debug("no suitable authentication methods found ("
|
_log.debug("no suitable authentication methods found (" + Integer.toHexString(method) + ")");
|
||||||
+ Integer.toHexString(method)+ ")");
|
sendInitReply(Method.NO_ACCEPTABLE_METHODS, out);
|
||||||
sendInitReply(Method.NO_ACCEPTABLE_METHODS, out);
|
throw new SOCKSException("Unsupported authentication method");
|
||||||
throw new SOCKSException("Unsupported authentication method");
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -110,121 +105,105 @@ public class SOCKS5Server extends SOCKSServer {
|
|||||||
* initialization, integrity/confidentiality encapsulations, etc)
|
* initialization, integrity/confidentiality encapsulations, etc)
|
||||||
* has been stripped out of the input/output streams.
|
* has been stripped out of the input/output streams.
|
||||||
*/
|
*/
|
||||||
private void manageRequest(DataInputStream in,
|
private void manageRequest(DataInputStream in, DataOutputStream out) throws IOException, SOCKSException {
|
||||||
DataOutputStream out) throws IOException, SOCKSException {
|
int socksVer = in.readByte() & 0xff;
|
||||||
int socksVer = in.readByte() & 0xff;
|
if (socksVer != SOCKS_VERSION_5) {
|
||||||
if (socksVer != SOCKS_VERSION_5) {
|
_log.debug("error in SOCKS5 request (protocol != 5? wtf?)");
|
||||||
_log.debug("error in SOCKS5 request (protocol != 5? wtf?)");
|
throw new SOCKSException("Invalid protocol version in request");
|
||||||
throw new SOCKSException("Invalid protocol version in request");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int command = in.readByte() & 0xff;
|
int command = in.readByte() & 0xff;
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case Command.CONNECT:
|
case Command.CONNECT:
|
||||||
break;
|
break;
|
||||||
case Command.BIND:
|
case Command.BIND:
|
||||||
_log.debug("BIND command is not supported!");
|
_log.debug("BIND command is not supported!");
|
||||||
sendRequestReply(Reply.COMMAND_NOT_SUPPORTED,
|
sendRequestReply(Reply.COMMAND_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
|
||||||
AddressType.DOMAINNAME, null,
|
throw new SOCKSException("BIND command not supported");
|
||||||
"0.0.0.0", 0, out);
|
case Command.UDP_ASSOCIATE:
|
||||||
throw new SOCKSException("BIND command not supported");
|
_log.debug("UDP ASSOCIATE command is not supported!");
|
||||||
case Command.UDP_ASSOCIATE:
|
sendRequestReply(Reply.COMMAND_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
|
||||||
_log.debug("UDP ASSOCIATE command is not supported!");
|
throw new SOCKSException("UDP ASSOCIATE command not supported");
|
||||||
sendRequestReply(Reply.COMMAND_NOT_SUPPORTED,
|
default:
|
||||||
AddressType.DOMAINNAME, null,
|
_log.debug("unknown command in request (" + Integer.toHexString(command) + ")");
|
||||||
"0.0.0.0", 0, out);
|
throw new SOCKSException("Invalid command in request");
|
||||||
throw new SOCKSException("UDP ASSOCIATE command not supported");
|
}
|
||||||
default:
|
|
||||||
_log.debug("unknown command in request ("
|
|
||||||
+ Integer.toHexString(command) + ")");
|
|
||||||
throw new SOCKSException("Invalid command in request");
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
// Reserved byte, should be 0x00
|
// Reserved byte, should be 0x00
|
||||||
byte rsv = in.readByte();
|
byte rsv = in.readByte();
|
||||||
}
|
}
|
||||||
|
|
||||||
int addressType = in.readByte() & 0xff;
|
int addressType = in.readByte() & 0xff;
|
||||||
switch (addressType) {
|
switch (addressType) {
|
||||||
case AddressType.IPV4:
|
case AddressType.IPV4:
|
||||||
connHostName = new String("");
|
connHostName = new String("");
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
int octet = in.readByte() & 0xff;
|
int octet = in.readByte() & 0xff;
|
||||||
connHostName += Integer.toString(octet);
|
connHostName += Integer.toString(octet);
|
||||||
if (i != 3) {
|
if (i != 3) {
|
||||||
connHostName += ".";
|
connHostName += ".";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_log.warn("IPV4 address type in request: " + connHostName
|
_log.warn("IPV4 address type in request: " + connHostName + ". Is your client secure?");
|
||||||
+ ". Is your client secure?");
|
break;
|
||||||
break;
|
case AddressType.DOMAINNAME:
|
||||||
case AddressType.DOMAINNAME:
|
{
|
||||||
{
|
int addrLen = in.readByte() & 0xff;
|
||||||
int addrLen = in.readByte() & 0xff;
|
if (addrLen == 0) {
|
||||||
if (addrLen == 0) {
|
_log.debug("0-sized address length? wtf?");
|
||||||
_log.debug("0-sized address length? wtf?");
|
throw new SOCKSException("Illegal DOMAINNAME length");
|
||||||
throw new SOCKSException("Illegal DOMAINNAME length");
|
}
|
||||||
}
|
byte addr[] = new byte[addrLen];
|
||||||
byte addr[] = new byte[addrLen];
|
in.readFully(addr);
|
||||||
in.readFully(addr);
|
connHostName = new String(addr);
|
||||||
connHostName = new String(addr);
|
}
|
||||||
}
|
_log.debug("DOMAINNAME address type in request: " + connHostName);
|
||||||
_log.debug("DOMAINNAME address type in request: " + connHostName);
|
break;
|
||||||
break;
|
case AddressType.IPV6:
|
||||||
case AddressType.IPV6:
|
_log.warn("IP V6 address type in request! Is your client secure?" + " (IPv6 is not supported, anyway :-)");
|
||||||
_log.warn("IP V6 address type in request! Is your client secure?"
|
sendRequestReply(Reply.ADDRESS_TYPE_NOT_SUPPORTED, AddressType.DOMAINNAME, null, "0.0.0.0", 0, out);
|
||||||
+ " (IPv6 is not supported, anyway :-)");
|
throw new SOCKSException("IPV6 addresses not supported");
|
||||||
sendRequestReply(Reply.ADDRESS_TYPE_NOT_SUPPORTED,
|
default:
|
||||||
AddressType.DOMAINNAME, null,
|
_log.debug("unknown address type in request (" + Integer.toHexString(command) + ")");
|
||||||
"0.0.0.0", 0, out);
|
throw new SOCKSException("Invalid addresses type in request");
|
||||||
throw new SOCKSException("IPV6 addresses not supported");
|
}
|
||||||
default:
|
|
||||||
_log.debug("unknown address type in request ("
|
|
||||||
+ Integer.toHexString(command) + ")");
|
|
||||||
throw new SOCKSException("Invalid addresses type in request");
|
|
||||||
}
|
|
||||||
|
|
||||||
connPort = in.readUnsignedShort();
|
connPort = in.readUnsignedShort();
|
||||||
if (connPort == 0) {
|
if (connPort == 0) {
|
||||||
_log.debug("trying to connect to TCP port 0? Dropping!");
|
_log.debug("trying to connect to TCP port 0? Dropping!");
|
||||||
throw new SOCKSException("Invalid port number in request");
|
throw new SOCKSException("Invalid port number in request");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void confirmConnection() throws SOCKSException {
|
protected void confirmConnection() throws SOCKSException {
|
||||||
DataInputStream in;
|
DataInputStream in;
|
||||||
DataOutputStream out;
|
DataOutputStream out;
|
||||||
try {
|
try {
|
||||||
out = new DataOutputStream(clientSock.getOutputStream());
|
out = new DataOutputStream(clientSock.getOutputStream());
|
||||||
|
|
||||||
sendRequestReply(Reply.SUCCEEDED,
|
sendRequestReply(Reply.SUCCEEDED, AddressType.IPV4, InetAddress.getByName("127.0.0.1"), null, 1, out);
|
||||||
AddressType.IPV4,
|
} catch (IOException e) {
|
||||||
InetAddress.getByName("127.0.0.1"),
|
throw new SOCKSException("Connection error (" + e.getMessage() + ")");
|
||||||
null, 1, out);
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
throw new SOCKSException("Connection error ("
|
|
||||||
+ e.getMessage() + ")");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send the specified reply during SOCKS5 initialization
|
* Send the specified reply during SOCKS5 initialization
|
||||||
*/
|
*/
|
||||||
private void sendInitReply(int replyCode,
|
private void sendInitReply(int replyCode, DataOutputStream out) throws IOException {
|
||||||
DataOutputStream out) throws IOException {
|
ByteArrayOutputStream reps = new ByteArrayOutputStream();
|
||||||
ByteArrayOutputStream reps = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
reps.write(SOCKS_VERSION_5);
|
reps.write(SOCKS_VERSION_5);
|
||||||
reps.write(replyCode);
|
reps.write(replyCode);
|
||||||
|
|
||||||
byte[] reply = reps.toByteArray();
|
byte[] reply = reps.toByteArray();
|
||||||
|
|
||||||
if (_log.shouldLog(Log.DEBUG)) {
|
if (_log.shouldLog(Log.DEBUG)) {
|
||||||
_log.debug("Sending init reply:\n" + HexDump.dump(reply));
|
_log.debug("Sending init reply:\n" + HexDump.dump(reply));
|
||||||
}
|
}
|
||||||
|
|
||||||
out.write(reply);
|
out.write(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -232,78 +211,72 @@ public class SOCKS5Server extends SOCKSServer {
|
|||||||
* one of inetAddr or domainName can be null, depending on
|
* one of inetAddr or domainName can be null, depending on
|
||||||
* addressType.
|
* addressType.
|
||||||
*/
|
*/
|
||||||
private void sendRequestReply(int replyCode,
|
private void sendRequestReply(int replyCode, int addressType, InetAddress inetAddr, String domainName,
|
||||||
int addressType,
|
int bindPort, DataOutputStream out) throws IOException {
|
||||||
InetAddress inetAddr,
|
ByteArrayOutputStream reps = new ByteArrayOutputStream();
|
||||||
String domainName,
|
DataOutputStream dreps = new DataOutputStream(reps);
|
||||||
int bindPort,
|
|
||||||
DataOutputStream out) throws IOException {
|
|
||||||
ByteArrayOutputStream reps = new ByteArrayOutputStream();
|
|
||||||
DataOutputStream dreps = new DataOutputStream(reps);
|
|
||||||
|
|
||||||
dreps.write(SOCKS_VERSION_5);
|
dreps.write(SOCKS_VERSION_5);
|
||||||
dreps.write(replyCode);
|
dreps.write(replyCode);
|
||||||
|
|
||||||
// Reserved byte, should be 0x00
|
// Reserved byte, should be 0x00
|
||||||
dreps.write(0x00);
|
dreps.write(0x00);
|
||||||
|
|
||||||
dreps.write(addressType);
|
dreps.write(addressType);
|
||||||
|
|
||||||
switch (addressType) {
|
switch (addressType) {
|
||||||
case AddressType.IPV4:
|
case AddressType.IPV4:
|
||||||
dreps.write(inetAddr.getAddress());
|
dreps.write(inetAddr.getAddress());
|
||||||
break;
|
break;
|
||||||
case AddressType.DOMAINNAME:
|
case AddressType.DOMAINNAME:
|
||||||
dreps.writeByte(domainName.length());
|
dreps.writeByte(domainName.length());
|
||||||
dreps.writeBytes(domainName);
|
dreps.writeBytes(domainName);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
_log.error("unknown address type passed to sendReply() ("
|
_log.error("unknown address type passed to sendReply() (" + Integer.toHexString(addressType) + ")! wtf?");
|
||||||
+ Integer.toHexString(addressType) + ")! wtf?");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
dreps.writeShort(bindPort);
|
dreps.writeShort(bindPort);
|
||||||
|
|
||||||
byte[] reply = reps.toByteArray();
|
byte[] reply = reps.toByteArray();
|
||||||
|
|
||||||
if (_log.shouldLog(Log.DEBUG)) {
|
if (_log.shouldLog(Log.DEBUG)) {
|
||||||
_log.debug("Sending request reply:\n" + HexDump.dump(reply));
|
_log.debug("Sending request reply:\n" + HexDump.dump(reply));
|
||||||
}
|
}
|
||||||
|
|
||||||
out.write(reply);
|
out.write(reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Some namespaces to enclose SOCKS protocol codes
|
* Some namespaces to enclose SOCKS protocol codes
|
||||||
*/
|
*/
|
||||||
private class Method {
|
private class Method {
|
||||||
private static final int NO_AUTH_REQUIRED = 0x00;
|
private static final int NO_AUTH_REQUIRED = 0x00;
|
||||||
private static final int NO_ACCEPTABLE_METHODS = 0xff;
|
private static final int NO_ACCEPTABLE_METHODS = 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AddressType {
|
private class AddressType {
|
||||||
private static final int IPV4 = 0x01;
|
private static final int IPV4 = 0x01;
|
||||||
private static final int DOMAINNAME = 0x03;
|
private static final int DOMAINNAME = 0x03;
|
||||||
private static final int IPV6 = 0x04;
|
private static final int IPV6 = 0x04;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Command {
|
private class Command {
|
||||||
private static final int CONNECT = 0x01;
|
private static final int CONNECT = 0x01;
|
||||||
private static final int BIND = 0x02;
|
private static final int BIND = 0x02;
|
||||||
private static final int UDP_ASSOCIATE = 0x03;
|
private static final int UDP_ASSOCIATE = 0x03;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Reply {
|
private class Reply {
|
||||||
private static final int SUCCEEDED = 0x00;
|
private static final int SUCCEEDED = 0x00;
|
||||||
private static final int GENERAL_SOCKS_SERVER_FAILURE = 0x01;
|
private static final int GENERAL_SOCKS_SERVER_FAILURE = 0x01;
|
||||||
private static final int CONNECTION_NOT_ALLOWED_BY_RULESET = 0x02;
|
private static final int CONNECTION_NOT_ALLOWED_BY_RULESET = 0x02;
|
||||||
private static final int NETWORK_UNREACHABLE = 0x03;
|
private static final int NETWORK_UNREACHABLE = 0x03;
|
||||||
private static final int HOST_UNREACHABLE = 0x04;
|
private static final int HOST_UNREACHABLE = 0x04;
|
||||||
private static final int CONNECTION_REFUSED = 0x05;
|
private static final int CONNECTION_REFUSED = 0x05;
|
||||||
private static final int TTL_EXPIRED = 0x06;
|
private static final int TTL_EXPIRED = 0x06;
|
||||||
private static final int COMMAND_NOT_SUPPORTED = 0x07;
|
private static final int COMMAND_NOT_SUPPORTED = 0x07;
|
||||||
private static final int ADDRESS_TYPE_NOT_SUPPORTED = 0x08;
|
private static final int ADDRESS_TYPE_NOT_SUPPORTED = 0x08;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,10 +14,10 @@ package net.i2p.i2ptunnel.socks;
|
|||||||
public class SOCKSException extends Exception {
|
public class SOCKSException extends Exception {
|
||||||
|
|
||||||
public SOCKSException() {
|
public SOCKSException() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SOCKSException(String s) {
|
public SOCKSException(String s) {
|
||||||
super(s);
|
super(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -47,7 +47,6 @@ public abstract class SOCKSServer {
|
|||||||
*/
|
*/
|
||||||
public abstract Socket getClientSocket() throws SOCKSException;
|
public abstract Socket getClientSocket() throws SOCKSException;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Confirm to the client that the connection has succeeded
|
* Confirm to the client that the connection has succeeded
|
||||||
*/
|
*/
|
||||||
@ -60,40 +59,39 @@ public abstract class SOCKSServer {
|
|||||||
* @return an I2PSocket connected with the destination
|
* @return an I2PSocket connected with the destination
|
||||||
*/
|
*/
|
||||||
public I2PSocket getDestinationI2PSocket() throws SOCKSException {
|
public I2PSocket getDestinationI2PSocket() throws SOCKSException {
|
||||||
setupServer();
|
setupServer();
|
||||||
|
|
||||||
if (connHostName == null) {
|
if (connHostName == null) {
|
||||||
_log.error("BUG: destination host name has not been initialized!");
|
_log.error("BUG: destination host name has not been initialized!");
|
||||||
throw new SOCKSException("BUG! See the logs!");
|
throw new SOCKSException("BUG! See the logs!");
|
||||||
}
|
}
|
||||||
if (connPort == 0) {
|
if (connPort == 0) {
|
||||||
_log.error("BUG: destination port has not been initialized!");
|
_log.error("BUG: destination port has not been initialized!");
|
||||||
throw new SOCKSException("BUG! See the logs!");
|
throw new SOCKSException("BUG! See the logs!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: here we should read our config file, select an
|
// FIXME: here we should read our config file, select an
|
||||||
// outproxy, and instantiate the proper socket class that
|
// outproxy, and instantiate the proper socket class that
|
||||||
// handles the outproxy itself (SOCKS4a, SOCKS5, HTTP CONNECT...).
|
// handles the outproxy itself (SOCKS4a, SOCKS5, HTTP CONNECT...).
|
||||||
I2PSocket destSock;
|
I2PSocket destSock;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (connHostName.toLowerCase().endsWith(".i2p")) {
|
if (connHostName.toLowerCase().endsWith(".i2p")) {
|
||||||
_log.debug("connecting to " + connHostName + "...");
|
_log.debug("connecting to " + connHostName + "...");
|
||||||
I2PSocketManager sm = I2PSocketManagerFactory.createManager();
|
I2PSocketManager sm = I2PSocketManagerFactory.createManager();
|
||||||
destSock = sm.connect(I2PTunnel.destFromName(connHostName),
|
destSock = sm.connect(I2PTunnel.destFromName(connHostName), new I2PSocketOptions());
|
||||||
new I2PSocketOptions());
|
confirmConnection();
|
||||||
confirmConnection();
|
_log.debug("connection confirmed - exchanging data...");
|
||||||
_log.debug("connection confirmed - exchanging data...");
|
} else {
|
||||||
} else {
|
_log.error("We don't support outproxies (yet)");
|
||||||
_log.error("We don't support outproxies (yet)");
|
throw new SOCKSException("Ouproxies not supported (yet)");
|
||||||
throw new SOCKSException("Ouproxies not supported (yet)");
|
}
|
||||||
}
|
} catch (DataFormatException e) {
|
||||||
} catch (DataFormatException e) {
|
throw new SOCKSException("Error in destination format");
|
||||||
throw new SOCKSException("Error in destination format");
|
} catch (I2PException e) {
|
||||||
} catch (I2PException e) {
|
throw new SOCKSException("I2P error (" + e.getMessage() + ")");
|
||||||
throw new SOCKSException("I2P error (" + e.getMessage() + ")");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return destSock;
|
return destSock;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -27,27 +27,26 @@ public class SOCKSServerFactory {
|
|||||||
* @param s a Socket used to choose the SOCKS server type
|
* @param s a Socket used to choose the SOCKS server type
|
||||||
*/
|
*/
|
||||||
public static SOCKSServer createSOCKSServer(Socket s) throws SOCKSException {
|
public static SOCKSServer createSOCKSServer(Socket s) throws SOCKSException {
|
||||||
SOCKSServer serv;
|
SOCKSServer serv;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DataInputStream in = new DataInputStream(s.getInputStream());
|
DataInputStream in = new DataInputStream(s.getInputStream());
|
||||||
int socksVer = in.readByte();
|
int socksVer = in.readByte();
|
||||||
|
|
||||||
switch (socksVer) {
|
switch (socksVer) {
|
||||||
case 0x05: // SOCKS version 5
|
case 0x05:
|
||||||
serv = new SOCKS5Server(s);
|
// SOCKS version 5
|
||||||
break;
|
serv = new SOCKS5Server(s);
|
||||||
default:
|
break;
|
||||||
_log.debug("SOCKS protocol version not supported ("
|
default:
|
||||||
+ Integer.toHexString(socksVer) + ")");
|
_log.debug("SOCKS protocol version not supported (" + Integer.toHexString(socksVer) + ")");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
_log.debug("error reading SOCKS protocol version");
|
_log.debug("error reading SOCKS protocol version");
|
||||||
throw new SOCKSException("Connection error ("
|
throw new SOCKSException("Connection error (" + e.getMessage() + ")");
|
||||||
+ e.getMessage() + ")");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return serv;
|
return serv;
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user