Reseed: Add support for reseeding via outproxy or Orchid (ticket #1841)

InternalSocket: Add support for some methods needed for reseed to work
This commit is contained in:
zzz
2017-11-21 01:29:31 +00:00
parent 07a83bf310
commit 8c7898de1e
7 changed files with 154 additions and 49 deletions

View File

@ -8,6 +8,8 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import net.i2p.I2PAppContext;
/**
* A simple in-JVM Socket using Piped Streams.
* We use port numbers just like regular sockets.
@ -17,11 +19,13 @@ import java.nio.channels.SocketChannel;
public class InternalSocket extends Socket {
private InputStream _is;
private OutputStream _os;
private final int _port;
/** server side */
InternalSocket(InputStream is, OutputStream os) {
_is = is;
_os = os;
_port = 1;
}
/**
@ -31,6 +35,7 @@ public class InternalSocket extends Socket {
public InternalSocket(int port) throws IOException {
if (port <= 0)
throw new IOException("bad port number");
_port = port;
InternalServerSocket.internalConnect(port, this);
}
@ -39,7 +44,7 @@ public class InternalSocket extends Socket {
* @param port &gt; 0
*/
public static Socket getSocket(String host, int port) throws IOException {
if (System.getProperty("router.version") != null &&
if (I2PAppContext.getGlobalContext().isRouterContext() &&
(host.equals("127.0.0.1") || host.equals("localhost"))) {
try {
return new InternalSocket(port);
@ -103,7 +108,8 @@ public class InternalSocket extends Socket {
return 0;
}
// everything below here unsupported
// everything below here unsupported unless otherwise noted
/** @deprecated unsupported */
@Deprecated
@Override
@ -146,12 +152,16 @@ public class InternalSocket extends Socket {
public InetAddress getLocalAddress() {
throw new UnsupportedOperationException();
}
/** @deprecated unsupported */
@Deprecated
/**
* Supported as of 0.9.33, prior to that threw UnsupportedOperationException
* @return 1 if connected, -1 if not
*/
@Override
public int getLocalPort() {
throw new UnsupportedOperationException();
return isConnected() ? 1 : -1;
}
/** @deprecated unsupported */
@Deprecated
@Override
@ -164,11 +174,14 @@ public class InternalSocket extends Socket {
public boolean getOOBInline() {
throw new UnsupportedOperationException();
}
/** @deprecated unsupported */
@Deprecated
/**
* Supported as of 0.9.33, prior to that threw UnsupportedOperationException
* @return if connected: actual port for clients, 1 for servers; -1 if not
*/
@Override
public int getPort() {
throw new UnsupportedOperationException();
return isConnected() ? _port : 0;
}
/** @deprecated unsupported */
@Deprecated
@ -194,12 +207,16 @@ public class InternalSocket extends Socket {
public int getSendBufferSize() {
throw new UnsupportedOperationException();
}
/** @deprecated unsupported */
@Deprecated
/**
* Supported as of 0.9.33, prior to that threw UnsupportedOperationException
* @return -1 always
*/
@Override
public int getSoLinger() {
throw new UnsupportedOperationException();
return -1;
}
/** @deprecated unsupported */
@Deprecated
@Override
@ -218,12 +235,15 @@ public class InternalSocket extends Socket {
public boolean isBound() {
throw new UnsupportedOperationException();
}
/** @deprecated unsupported */
@Deprecated
/**
* Supported as of 0.9.33, prior to that threw UnsupportedOperationException
*/
@Override
public boolean isConnected() {
throw new UnsupportedOperationException();
public synchronized boolean isConnected() {
return _is != null || _os != null;
}
/** @deprecated unsupported */
@Deprecated
@Override

View File

@ -147,7 +147,10 @@ public class SSLEepGet extends EepGet {
/**
* Use a proxy.
*
* @param proxyPort must be valid, -1 disallowed, no default
* @param proxyHost Must be valid hostname or literal IPv4/v6.
* If type is INTERNAL, set to "localhost".
* @param proxyPort Must be valid, -1 disallowed, no default.
* If type is INTERNAL, set to 4444.
* @since 0.9.33
*/
public SSLEepGet(I2PAppContext ctx, ProxyType type, String proxyHost, int proxyPort,
@ -158,7 +161,10 @@ public class SSLEepGet extends EepGet {
/**
* Use a proxy.
*
* @param proxyPort must be valid, -1 disallowed, no default
* @param proxyHost Must be valid hostname or literal IPv4/v6.
* If type is INTERNAL, set to "localhost".
* @param proxyPort Must be valid, -1 disallowed, no default.
* If type is INTERNAL, set to 4444.
* @param state an SSLState retrieved from a previous SSLEepGet with getSSLState(), or null.
* This makes repeated fetches from the same host MUCH faster,
* and prevents repeated key store loads even for different hosts.
@ -183,7 +189,10 @@ public class SSLEepGet extends EepGet {
/**
* Use a proxy.
*
* @param proxyPort must be valid, -1 disallowed, no default
* @param proxyHost Must be valid hostname or literal IPv4/v6.
* If type is INTERNAL, set to "localhost".
* @param proxyPort Must be valid, -1 disallowed, no default.
* If type is INTERNAL, set to 4444.
* @since 0.9.33
*/
public SSLEepGet(I2PAppContext ctx, ProxyType type, String proxyHost, int proxyPort,
@ -194,7 +203,10 @@ public class SSLEepGet extends EepGet {
/**
* Use a proxy.
*
* @param proxyPort must be valid, -1 disallowed, no default
* @param proxyHost Must be valid hostname or literal IPv4/v6.
* If type is INTERNAL, set to "localhost".
* @param proxyPort Must be valid, -1 disallowed, no default.
* If type is INTERNAL, set to 4444.
* @param state an SSLState retrieved from a previous SSLEepGet with getSSLState(), or null.
* This makes repeated fetches from the same host MUCH faster,
* and prevents repeated key store loads even for different hosts.
@ -276,6 +288,10 @@ public class SSLEepGet extends EepGet {
ptype = ProxyType.SOCKS4;
} else if (y.equals("SOCKS5")) {
ptype = ProxyType.SOCKS5;
} else if (y.equals("I2P")) {
ptype = ProxyType.INTERNAL;
proxyHost = "localhost";
proxyPort = 4444;
} else {
error = true;
}
@ -333,8 +349,8 @@ public class SSLEepGet extends EepGet {
private static void usage() {
System.err.println("Usage: SSLEepGet [-psyz] https://url\n" +
" -p proxyHost[:proxyPort] // default port 8080 for HTTPS and 1080 for SOCKS\n" +
" -y HTTPS|SOCKS4|SOCKS5 // proxy type, default HTTPS if proxyHost is set\n" +
" -p proxyHost[:proxyPort] // default port 8080 for HTTPS and 1080 for SOCKS; default localhost:4444 for I2P\n" +
" -y HTTPS|SOCKS4|SOCKS5|I2P // proxy type, default HTTPS if proxyHost is set\n" +
" -s save unknown certs\n" +
" -s -s save all certs\n" +
" -z bypass hostname verification");
@ -698,6 +714,10 @@ public class SSLEepGet extends EepGet {
httpProxyConnect(host, port);
break;
case INTERNAL:
internalHttpProxyConnect(host, port);
break;
case SOCKS4:
socksProxyConnect(false, host, port);
break;
@ -707,7 +727,6 @@ public class SSLEepGet extends EepGet {
break;
case HTTPS:
case INTERNAL:
case TRANSPARENT:
default:
throw new IOException("Unsupported proxy type " + _proxyType);
@ -803,6 +822,35 @@ public class SSLEepGet extends EepGet {
} else {
_proxy = new Socket(_proxyHost, _proxyPort);
}
httpProxyConnect(_proxy, host, port);
}
/**
* Connect to a HTTP proxy.
* Proxy address must be in _proxyHost and _proxyPort.
* Side effects: Sets _proxy, _proxyIn, _proxyOut,
* and other globals via readHeaders()
*
* @param port what the proxy should connect to, probably 4444
* @since 0.9.33
*/
private void internalHttpProxyConnect(String host, int port) throws IOException {
// connect to the proxy
_proxy = InternalSocket.getSocket(_proxyHost, _proxyPort);
httpProxyConnect(_proxy, host, port);
}
/**
* Connect to a HTTP proxy.
* Proxy address must be in _proxyHost and _proxyPort.
* Side effects: Sets _proxyIn, _proxyOut,
* and other globals via readHeaders()
*
* @param host what the proxy should connect to
* @param port what the proxy should connect to
* @since 0.9.33
*/
private void httpProxyConnect(Socket proxy, String host, int port) throws IOException {
_proxyIn = _proxy.getInputStream();
_proxyOut = _proxy.getOutputStream();
StringBuilder buf = new StringBuilder(64);