i2ptunnel: Close sockets in finally{}

This commit is contained in:
zzz
2018-02-17 16:04:09 +00:00
parent 509e39b592
commit cd3515923e
10 changed files with 100 additions and 135 deletions

View File

@ -130,21 +130,16 @@ public class I2PTunnelClient extends I2PTunnelClientBase {
} catch (IOException ex) { } catch (IOException ex) {
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info("Error connecting", ex); _log.info("Error connecting", ex);
//l.log("Error connecting: " + ex.getMessage());
closeSocket(s);
if (i2ps != null) {
synchronized (sockLock) {
mySockets.remove(sockLock);
}
}
} catch (I2PException ex) { } catch (I2PException ex) {
if (_log.shouldLog(Log.INFO)) if (_log.shouldLog(Log.INFO))
_log.info("Error connecting", ex); _log.info("Error connecting", ex);
//l.log("Error connecting: " + ex.getMessage()); } finally {
// only because we are running it inline
closeSocket(s); closeSocket(s);
if (i2ps != null) { if (i2ps != null) {
try { i2ps.close(); } catch (IOException ioe) {}
synchronized (sockLock) { synchronized (sockLock) {
mySockets.remove(sockLock); mySockets.remove(i2ps);
} }
} }
} }

View File

@ -154,6 +154,7 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
boolean usingInternalOutproxy = false; boolean usingInternalOutproxy = false;
Outproxy outproxy = null; Outproxy outproxy = null;
long requestId = __requestId.incrementAndGet(); long requestId = __requestId.incrementAndGet();
I2PSocket i2ps = null;
try { try {
s.setSoTimeout(INITIAL_SO_TIMEOUT); s.setSoTimeout(INITIAL_SO_TIMEOUT);
out = s.getOutputStream(); out = s.getOutputStream();
@ -228,7 +229,6 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn(getPrefix(requestId) + "Host wants to be outproxied, but we dont have any!"); _log.warn(getPrefix(requestId) + "Host wants to be outproxied, but we dont have any!");
writeErrorMessage(ERR_NO_OUTPROXY, out); writeErrorMessage(ERR_NO_OUTPROXY, out);
s.close();
return; return;
} }
destination = currentProxy; destination = currentProxy;
@ -237,7 +237,6 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
} }
} else if (host.toLowerCase(Locale.US).equals("localhost")) { } else if (host.toLowerCase(Locale.US).equals("localhost")) {
writeErrorMessage(ERR_LOCALHOST, out); writeErrorMessage(ERR_LOCALHOST, out);
s.close();
return; return;
} else { // full b64 address (hopefully) } else { // full b64 address (hopefully)
destination = host; destination = host;
@ -292,7 +291,6 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
if (method == null || !"CONNECT".equals(method.toUpperCase(Locale.US))) { if (method == null || !"CONNECT".equals(method.toUpperCase(Locale.US))) {
writeErrorMessage(ERR_BAD_PROTOCOL, out); writeErrorMessage(ERR_BAD_PROTOCOL, out);
s.close();
return; return;
} }
@ -309,7 +307,6 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
if (destination == null) { if (destination == null) {
writeErrorMessage(ERR_BAD_PROTOCOL, out); writeErrorMessage(ERR_BAD_PROTOCOL, out);
s.close();
return; return;
} }
@ -323,7 +320,6 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
_log.warn(getPrefix(requestId) + "Auth required, sending 407"); _log.warn(getPrefix(requestId) + "Auth required, sending 407");
} }
out.write(DataHelper.getASCII(getAuthError(result == AuthResult.AUTH_STALE))); out.write(DataHelper.getASCII(getAuthError(result == AuthResult.AUTH_STALE)));
s.close();
return; return;
} }
@ -335,14 +331,13 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
else else
header = getErrorPage("dnfh", ERR_DESTINATION_UNKNOWN); header = getErrorPage("dnfh", ERR_DESTINATION_UNKNOWN);
writeErrorMessage(header, out, targetRequest, usingWWWProxy, destination); writeErrorMessage(header, out, targetRequest, usingWWWProxy, destination);
s.close();
return; return;
} }
I2PSocketOptions sktOpts = getDefaultOptions(); I2PSocketOptions sktOpts = getDefaultOptions();
if (!usingWWWProxy && remotePort > 0) if (!usingWWWProxy && remotePort > 0)
sktOpts.setPort(remotePort); sktOpts.setPort(remotePort);
I2PSocket i2ps = createI2PSocket(clientDest, sktOpts); i2ps = createI2PSocket(clientDest, sktOpts);
byte[] data = null; byte[] data = null;
byte[] response = null; byte[] response = null;
if (usingWWWProxy) if (usingWWWProxy)
@ -357,16 +352,17 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
} catch (IOException ex) { } catch (IOException ex) {
_log.info(getPrefix(requestId) + "Error trying to connect", ex); _log.info(getPrefix(requestId) + "Error trying to connect", ex);
handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId); handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId);
closeSocket(s);
} catch (I2PException ex) { } catch (I2PException ex) {
_log.info("getPrefix(requestId) + Error trying to connect", ex); _log.info("getPrefix(requestId) + Error trying to connect", ex);
handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId); handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId);
closeSocket(s);
} catch (OutOfMemoryError oom) { } catch (OutOfMemoryError oom) {
IOException ex = new IOException("OOM"); IOException ex = new IOException("OOM");
_log.info("getPrefix(requestId) + Error trying to connect", ex); _log.info("getPrefix(requestId) + Error trying to connect", ex);
handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId); handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId);
} finally {
// only because we are running it inline
closeSocket(s); closeSocket(s);
try { i2ps.close(); } catch (IOException ioe) {}
} }
} }

View File

@ -398,7 +398,7 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
String currentProxy = null; String currentProxy = null;
long requestId = __requestId.incrementAndGet(); long requestId = __requestId.incrementAndGet();
boolean shout = false; boolean shout = false;
I2PSocket i2ps = null;
try { try {
s.setSoTimeout(INITIAL_SO_TIMEOUT); s.setSoTimeout(INITIAL_SO_TIMEOUT);
out = s.getOutputStream(); out = s.getOutputStream();
@ -555,8 +555,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
reader.drain(); reader.drain();
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -694,11 +692,8 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
"</p>").getBytes("UTF-8")); "</p>").getBytes("UTF-8"));
writeFooter(out); writeFooter(out);
reader.drain(); reader.drain();
// XXX: should closeSocket(s) be in a finally block?
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -814,8 +809,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
reader.drain(); reader.drain();
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -852,8 +845,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
reader.drain(); reader.drain();
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} else if(host.contains(".") || host.startsWith("[")) { } else if(host.contains(".") || host.startsWith("[")) {
@ -905,8 +896,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
reader.drain(); reader.drain();
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -931,8 +920,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
reader.drain(); reader.drain();
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} // end host name processing } // end host name processing
@ -1111,8 +1098,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
writeFooter(out); writeFooter(out);
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -1136,8 +1121,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
writeFooter(out); writeFooter(out);
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -1155,8 +1138,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
} }
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -1200,8 +1181,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
writeErrorMessage(header, out, targetRequest, false, destination); writeErrorMessage(header, out, targetRequest, false, destination);
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -1257,8 +1236,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
writeErrorMessage(header, extraMessage, out, targetRequest, usingWWWProxy, destination, jumpServers); writeErrorMessage(header, extraMessage, out, targetRequest, usingWWWProxy, destination, jumpServers);
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -1270,8 +1247,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
writeErrorMessage(ERR_INTERNAL_SSL, out, targetRequest, false, destination); writeErrorMessage(ERR_INTERNAL_SSL, out, targetRequest, false, destination);
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("SSL to i2p destinations denied by configuration: " + targetRequest); _log.warn("SSL to i2p destinations denied by configuration: " + targetRequest);
@ -1288,8 +1263,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
writeHelperSaveForm(out, destination, ahelperKey, targetRequest, referer); writeHelperSaveForm(out, destination, ahelperKey, targetRequest, referer);
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -1311,8 +1284,6 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
"\r\n").getBytes("UTF-8")); "\r\n").getBytes("UTF-8"));
} catch (IOException ioe) { } catch (IOException ioe) {
// ignore // ignore
} finally {
closeSocket(s);
} }
return; return;
} }
@ -1325,7 +1296,7 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
I2PSocketOptions sktOpts = getDefaultOptions(opts); I2PSocketOptions sktOpts = getDefaultOptions(opts);
if (remotePort > 0) if (remotePort > 0)
sktOpts.setPort(remotePort); sktOpts.setPort(remotePort);
I2PSocket i2ps = createI2PSocket(clientDest, sktOpts); i2ps = createI2PSocket(clientDest, sktOpts);
OnTimeout onTimeout = new OnTimeout(s, s.getOutputStream(), targetRequest, usingWWWProxy, currentProxy, requestId); OnTimeout onTimeout = new OnTimeout(s, s.getOutputStream(), targetRequest, usingWWWProxy, currentProxy, requestId);
Thread t; Thread t;
if (method.toUpperCase(Locale.US).equals("CONNECT")) { if (method.toUpperCase(Locale.US).equals("CONNECT")) {
@ -1350,22 +1321,20 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
if(_log.shouldLog(Log.INFO)) { if(_log.shouldLog(Log.INFO)) {
_log.info(getPrefix(requestId) + "Error trying to connect", ex); _log.info(getPrefix(requestId) + "Error trying to connect", ex);
} }
//l.log("Error connecting: " + ex.getMessage());
handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId); handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId);
closeSocket(s);
} catch(I2PException ex) { } catch(I2PException ex) {
if(_log.shouldLog(Log.INFO)) { if(_log.shouldLog(Log.INFO)) {
_log.info("getPrefix(requestId) + Error trying to connect", ex); _log.info("getPrefix(requestId) + Error trying to connect", ex);
} }
//l.log("Error connecting: " + ex.getMessage());
handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId); handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId);
closeSocket(s);
} catch(OutOfMemoryError oom) { } catch(OutOfMemoryError oom) {
IOException ex = new IOException("OOM"); IOException ex = new IOException("OOM");
_log.error("getPrefix(requestId) + Error trying to connect", oom); _log.error("getPrefix(requestId) + Error trying to connect", oom);
//l.log("Error connecting: " + ex.getMessage());
handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId); handleClientException(ex, out, targetRequest, usingWWWProxy, currentProxy, requestId);
} finally {
// only because we are running it inline
closeSocket(s); closeSocket(s);
try { i2ps.close(); } catch (IOException ioe) {}
} }
} }

View File

@ -49,27 +49,27 @@ public class I2PTunnelHTTPClientRunner extends I2PTunnelRunner {
@Override @Override
protected void close(OutputStream out, InputStream in, OutputStream i2pout, InputStream i2pin, protected void close(OutputStream out, InputStream in, OutputStream i2pout, InputStream i2pin,
Socket s, I2PSocket i2ps, Thread t1, Thread t2) throws InterruptedException { Socket s, I2PSocket i2ps, Thread t1, Thread t2) throws InterruptedException {
try { if (i2pin != null) { try {
i2pin.close(); i2pin.close();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
try { if (i2pout != null) { try {
i2pout.close(); i2pout.close();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
try { if (in != null) { try {
in.close(); in.close();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
try { if (out != null) { try {
out.close(); out.close();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
try { try {
i2ps.close(); i2ps.close();
} catch (IOException ioe) {} } catch (IOException ioe) {}
try { try {
s.close(); s.close();
} catch (IOException ioe) {} } catch (IOException ioe) {}
t1.join(30*1000); if (t1 != null)
t1.join(30*1000);
// t2 = fromI2P now run inline // t2 = fromI2P now run inline
//t2.join(30*1000); //t2.join(30*1000);
} }
} }

View File

@ -155,12 +155,6 @@ public class I2PTunnelIRCClient extends I2PTunnelClientBase {
String msg = ":" + name + " 499 you :" + ex + "\r\n"; String msg = ":" + name + " 499 you :" + ex + "\r\n";
s.getOutputStream().write(DataHelper.getUTF8(msg)); s.getOutputStream().write(DataHelper.getUTF8(msg));
} catch (IOException ioe) {} } catch (IOException ioe) {}
closeSocket(s);
if (i2ps != null) {
synchronized (sockLock) {
mySockets.remove(sockLock);
}
}
} catch (I2PException ex) { } catch (I2PException ex) {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("Error connecting", ex); _log.warn("Error connecting", ex);
@ -172,10 +166,13 @@ public class I2PTunnelIRCClient extends I2PTunnelClientBase {
String msg = ":" + name + " 499 you :" + ex + "\r\n"; String msg = ":" + name + " 499 you :" + ex + "\r\n";
s.getOutputStream().write(DataHelper.getUTF8(msg)); s.getOutputStream().write(DataHelper.getUTF8(msg));
} catch (IOException ioe) {} } catch (IOException ioe) {}
} finally {
// only because we are running it inline
closeSocket(s); closeSocket(s);
if (i2ps != null) { if (i2ps != null) {
try { i2ps.close(); } catch (IOException ioe) {}
synchronized (sockLock) { synchronized (sockLock) {
mySockets.remove(sockLock); mySockets.remove(i2ps);
} }
} }
} }

View File

@ -231,7 +231,7 @@ public class I2PTunnelIRCServer extends I2PTunnelServer implements Runnable {
* case-insensitive manner? * case-insensitive manner?
* *
*/ */
String cloakDest(Destination d) { private String cloakDest(Destination d) {
String hf; String hf;
String hc; String hc;

View File

@ -235,13 +235,21 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
@Override @Override
public void run() { public void run() {
boolean i2pReset = false;
boolean sockReset = false;
InputStream in = null;
OutputStream out = null;
InputStream i2pin = null;
OutputStream i2pout = null;
StreamForwarder toI2P = null;
StreamForwarder fromI2P = null;
try { try {
InputStream in = getSocketIn(); in = getSocketIn();
OutputStream out = getSocketOut(); // = new BufferedOutputStream(s.getOutputStream(), NETWORK_BUFFER_SIZE); out = getSocketOut(); // = new BufferedOutputStream(s.getOutputStream(), NETWORK_BUFFER_SIZE);
// unimplemented in streaming // unimplemented in streaming
//i2ps.setSocketErrorListener(this); //i2ps.setSocketErrorListener(this);
InputStream i2pin = i2ps.getInputStream(); i2pin = i2ps.getInputStream();
OutputStream i2pout = i2ps.getOutputStream(); //new BufferedOutputStream(i2ps.getOutputStream(), MAX_PACKET_SIZE); i2pout = i2ps.getOutputStream(); //new BufferedOutputStream(i2ps.getOutputStream(), MAX_PACKET_SIZE);
if (initialI2PData != null) { if (initialI2PData != null) {
// why synchronize this? we could be in here a LONG time for large initial data // why synchronize this? we could be in here a LONG time for large initial data
//synchronized (slock) { //synchronized (slock) {
@ -274,8 +282,8 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
+ " written to the socket, starting forwarders"); + " written to the socket, starting forwarders");
if (!(s instanceof InternalSocket)) if (!(s instanceof InternalSocket))
in = new BufferedInputStream(in, 2*NETWORK_BUFFER_SIZE); in = new BufferedInputStream(in, 2*NETWORK_BUFFER_SIZE);
StreamForwarder toI2P = new StreamForwarder(in, i2pout, true); toI2P = new StreamForwarder(in, i2pout, true);
StreamForwarder fromI2P = new StreamForwarder(i2pin, out, false); fromI2P = new StreamForwarder(i2pin, out, false);
toI2P.start(); toI2P.start();
// We are already a thread, so run the second one inline // We are already a thread, so run the second one inline
//fromI2P.start(); //fromI2P.start();
@ -288,8 +296,6 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("At least one forwarder completed, closing and joining"); _log.debug("At least one forwarder completed, closing and joining");
boolean i2pReset = false;
boolean sockReset = false;
// this task is useful for the httpclient // this task is useful for the httpclient
if ((onTimeout != null || _onFail != null) && totalReceived <= 0) { if ((onTimeout != null || _onFail != null) && totalReceived <= 0) {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
@ -333,31 +339,6 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
} }
} }
if (i2pReset) {
if (_log.shouldWarn())
_log.warn("Got I2P reset, resetting socket");
try {
s.setSoLinger(true, 0);
} catch (IOException ioe) {}
try {
s.close();
} catch (IOException ioe) {}
try {
i2ps.close();
} catch (IOException ioe) {}
} else if (sockReset) {
if (_log.shouldWarn())
_log.warn("Got socket reset, resetting I2P socket");
try {
i2ps.reset();
} catch (IOException ioe) {}
try {
s.close();
} catch (IOException ioe) {}
} else {
// now one connection is dead - kill the other as well, after making sure we flush
close(out, in, i2pout, i2pin, s, i2ps, toI2P, fromI2P);
}
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("Interrupted", ex); _log.error("Interrupted", ex);
@ -385,40 +366,58 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
_log.error("Internal error", e); _log.error("Internal error", e);
} finally { } finally {
removeRef(); removeRef();
try { if (i2pReset) {
if (s != null) if (_log.shouldWarn())
_log.warn("Got I2P reset, resetting socket");
try {
s.setSoLinger(true, 0);
} catch (IOException ioe) {}
try {
s.close(); s.close();
} catch (IOException ex) { } catch (IOException ioe) {}
if (_log.shouldLog(Log.WARN)) try {
_log.warn("Could not close java socket", ex);
}
if (i2ps != null) {
try {
i2ps.close(); i2ps.close();
} catch (IOException ex) { } catch (IOException ioe) {}
if (_log.shouldLog(Log.WARN)) } else if (sockReset) {
_log.warn("Could not close I2PSocket", ex); if (_log.shouldWarn())
} _log.warn("Got socket reset, resetting I2P socket");
// unimplemented in streaming try {
//i2ps.setSocketErrorListener(null); i2ps.reset();
} catch (IOException ioe) {}
try {
s.close();
} catch (IOException ioe) {}
} else {
// now one connection is dead - kill the other as well, after making sure we flush
try {
close(out, in, i2pout, i2pin, s, i2ps, toI2P, fromI2P);
} catch (InterruptedException ie) {}
} }
} }
} }
/**
* @param out may be null
* @param in may be null
* @param i2pout may be null
* @param i2pin may be null
* @param t1 may be null
* @param t2 may be null
*/
protected void close(OutputStream out, InputStream in, OutputStream i2pout, InputStream i2pin, protected void close(OutputStream out, InputStream in, OutputStream i2pout, InputStream i2pin,
Socket s, I2PSocket i2ps, Thread t1, Thread t2) throws InterruptedException { Socket s, I2PSocket i2ps, Thread t1, Thread t2) throws InterruptedException {
try { if (out != null) { try {
out.flush(); out.flush();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
try { if (i2pout != null) { try {
i2pout.flush(); i2pout.flush();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
try { if (in != null) { try {
in.close(); in.close();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
try { if (i2pin != null) { try {
i2pin.close(); i2pin.close();
} catch (IOException ioe) {} } catch (IOException ioe) {} }
// ok, yeah, there's a race here in theory, if data comes in after flushing and before // ok, yeah, there's a race here in theory, if data comes in after flushing and before
// closing, but its better than before... // closing, but its better than before...
try { try {
@ -427,7 +426,8 @@ public class I2PTunnelRunner extends I2PAppThread implements I2PSocket.SocketErr
try { try {
i2ps.close(); i2ps.close();
} catch (IOException ioe) {} } catch (IOException ioe) {}
t1.join(30*1000); if (t1 != null)
t1.join(30*1000);
// t2 = fromI2P now run inline // t2 = fromI2P now run inline
//t2.join(30*1000); //t2.join(30*1000);
} }

View File

@ -83,18 +83,16 @@ public class I2PTunnelDCCClient extends I2PTunnelClientBase {
t.run(); t.run();
} catch (IOException ex) { } catch (IOException ex) {
_log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex); _log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex);
closeSocket(s);
if (i2ps != null) {
try { i2ps.close(); } catch (IOException ioe) {}
}
notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort())); notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
} catch (I2PException ex) { } catch (I2PException ex) {
_log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex); _log.error("Could not make DCC connection to " + _dest + ':' + _remotePort, ex);
notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
} finally {
// only because we are running it inline
closeSocket(s); closeSocket(s);
if (i2ps != null) { if (i2ps != null) {
try { i2ps.close(); } catch (IOException ioe) {} try { i2ps.close(); } catch (IOException ioe) {}
} }
notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
} }
stop(); stop();
} }

View File

@ -6,6 +6,7 @@
*/ */
package net.i2p.i2ptunnel.socks; package net.i2p.i2ptunnel.socks;
import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -47,6 +48,7 @@ public class I2PSOCKSIRCTunnel extends I2PSOCKSTunnel {
*/ */
@Override @Override
protected void clientConnectionRun(Socket s) { protected void clientConnectionRun(Socket s) {
I2PSocket destSock = null;
try { try {
//_log.error("SOCKS IRC Tunnel Start"); //_log.error("SOCKS IRC Tunnel Start");
try { try {
@ -57,7 +59,7 @@ public class I2PSOCKSIRCTunnel extends I2PSOCKSTunnel {
try { try {
s.setSoTimeout(0); s.setSoTimeout(0);
} catch (SocketException ioe) {} } catch (SocketException ioe) {}
I2PSocket destSock = serv.getDestinationI2PSocket(this); destSock = serv.getDestinationI2PSocket(this);
StringBuffer expectedPong = new StringBuffer(); StringBuffer expectedPong = new StringBuffer();
int id = __clientId.incrementAndGet(); int id = __clientId.incrementAndGet();
Thread in = new I2PAppThread(new IrcInboundFilter(clientSock, destSock, expectedPong, _log), Thread in = new I2PAppThread(new IrcInboundFilter(clientSock, destSock, expectedPong, _log),
@ -72,7 +74,10 @@ public class I2PSOCKSIRCTunnel extends I2PSOCKSTunnel {
} catch (SOCKSException e) { } catch (SOCKSException e) {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("Error from SOCKS connection", e); _log.warn("Error from SOCKS connection", e);
} finally {
// only because we are running it inline
closeSocket(s); closeSocket(s);
if (destSock != null) try { destSock.close(); } catch (IOException ioe) {}
} }
} }
} }

View File

@ -6,6 +6,7 @@
*/ */
package net.i2p.i2ptunnel.socks; package net.i2p.i2ptunnel.socks;
import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.util.ArrayList; import java.util.ArrayList;
@ -57,6 +58,7 @@ public class I2PSOCKSTunnel extends I2PTunnelClientBase {
} }
protected void clientConnectionRun(Socket s) { protected void clientConnectionRun(Socket s) {
I2PSocket destSock = null;
try { try {
try { try {
s.setSoTimeout(INITIAL_SO_TIMEOUT); s.setSoTimeout(INITIAL_SO_TIMEOUT);
@ -66,7 +68,7 @@ public class I2PSOCKSTunnel extends I2PTunnelClientBase {
try { try {
s.setSoTimeout(0); s.setSoTimeout(0);
} catch (SocketException ioe) {} } catch (SocketException ioe) {}
I2PSocket destSock = serv.getDestinationI2PSocket(this); destSock = serv.getDestinationI2PSocket(this);
Thread t = new I2PTunnelRunner(clientSock, destSock, sockLock, null, null, mySockets, Thread t = new I2PTunnelRunner(clientSock, destSock, sockLock, null, null, mySockets,
(I2PTunnelRunner.FailCallback) null); (I2PTunnelRunner.FailCallback) null);
// we are called from an unlimited thread pool, so run inline // we are called from an unlimited thread pool, so run inline
@ -75,7 +77,10 @@ public class I2PSOCKSTunnel extends I2PTunnelClientBase {
} catch (SOCKSException e) { } catch (SOCKSException e) {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("Error from SOCKS connection", e); _log.warn("Error from SOCKS connection", e);
} finally {
// only because we are running it inline
closeSocket(s); closeSocket(s);
if (destSock != null) try { destSock.close(); } catch (IOException ioe) {}
} }
} }