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) {
if (_log.shouldLog(Log.INFO))
_log.info("Error connecting", ex);
//l.log("Error connecting: " + ex.getMessage());
closeSocket(s);
if (i2ps != null) {
synchronized (sockLock) {
mySockets.remove(sockLock);
}
}
} catch (I2PException ex) {
if (_log.shouldLog(Log.INFO))
_log.info("Error connecting", ex);
//l.log("Error connecting: " + ex.getMessage());
} finally {
// only because we are running it inline
closeSocket(s);
if (i2ps != null) {
try { i2ps.close(); } catch (IOException ioe) {}
synchronized (sockLock) {
mySockets.remove(sockLock);
mySockets.remove(i2ps);
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -83,18 +83,16 @@ public class I2PTunnelDCCClient extends I2PTunnelClientBase {
t.run();
} catch (IOException 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()));
} catch (I2PException 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);
if (i2ps != null) {
try { i2ps.close(); } catch (IOException ioe) {}
}
notifyEvent(CONNECT_STOP_EVENT, Integer.valueOf(getLocalPort()));
}
stop();
}

View File

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

View File

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