add https protocol for bitcoin core connections over tls

This commit is contained in:
Craig Raw 2022-12-05 11:57:25 +02:00
parent 0e26f8fce1
commit aa8380eb03
10 changed files with 55 additions and 17 deletions

View file

@ -149,10 +149,11 @@ public class Bwt {
if(config.getCoreServer() != null) { if(config.getCoreServer() != null) {
bwtConfig.bitcoindUrl = config.getCoreServer().getUrl(); bwtConfig.bitcoindUrl = config.getCoreServer().getUrl();
try { try {
HostAndPort hostAndPort = Protocol.HTTP.getServerHostAndPort(bwtConfig.bitcoindUrl); Protocol protocol = config.getCoreServer().getProtocol();
HostAndPort hostAndPort = protocol.getServerHostAndPort(bwtConfig.bitcoindUrl);
if(hostAndPort.getHost().endsWith(".local")) { if(hostAndPort.getHost().endsWith(".local")) {
InetAddress inetAddress = InetAddress.getByName(hostAndPort.getHost()); InetAddress inetAddress = InetAddress.getByName(hostAndPort.getHost());
bwtConfig.bitcoindUrl = Protocol.HTTP.toUrlString(inetAddress.getHostAddress(), hostAndPort.getPort()); bwtConfig.bitcoindUrl = protocol.toUrlString(inetAddress.getHostAddress(), hostAndPort.getPort());
} }
} catch(Exception e) { } catch(Exception e) {
//ignore //ignore

View file

@ -12,7 +12,7 @@ import java.security.cert.CertificateException;
import java.util.Locale; import java.util.Locale;
public enum Protocol { public enum Protocol {
TCP { TCP(50001) {
@Override @Override
public CloseableTransport getTransport(HostAndPort server) { public CloseableTransport getTransport(HostAndPort server) {
if(isOnionAddress(server)) { if(isOnionAddress(server)) {
@ -38,7 +38,7 @@ public enum Protocol {
return getTransport(server, proxy); return getTransport(server, proxy);
} }
}, },
SSL { SSL(50002) {
@Override @Override
public CloseableTransport getTransport(HostAndPort server) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { public CloseableTransport getTransport(HostAndPort server) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
if(isOnionAddress(server)) { if(isOnionAddress(server)) {
@ -67,7 +67,7 @@ public enum Protocol {
return new ProxyTcpOverTlsTransport(server, serverCert, proxy); return new ProxyTcpOverTlsTransport(server, serverCert, proxy);
} }
}, },
HTTP { HTTP(80) {
@Override @Override
public CloseableTransport getTransport(HostAndPort server) { public CloseableTransport getTransport(HostAndPort server) {
throw new UnsupportedOperationException("No transport supported for HTTP"); throw new UnsupportedOperationException("No transport supported for HTTP");
@ -87,8 +87,39 @@ public enum Protocol {
public CloseableTransport getTransport(HostAndPort server, File serverCert, HostAndPort proxy) { public CloseableTransport getTransport(HostAndPort server, File serverCert, HostAndPort proxy) {
throw new UnsupportedOperationException("No transport supported for HTTP"); throw new UnsupportedOperationException("No transport supported for HTTP");
} }
},
HTTPS(443) {
@Override
public CloseableTransport getTransport(HostAndPort server) {
throw new UnsupportedOperationException("No transport supported for HTTPS");
}
@Override
public CloseableTransport getTransport(HostAndPort server, File serverCert) {
throw new UnsupportedOperationException("No transport supported for HTTPS");
}
@Override
public CloseableTransport getTransport(HostAndPort server, HostAndPort proxy) {
throw new UnsupportedOperationException("No transport supported for HTTPS");
}
@Override
public CloseableTransport getTransport(HostAndPort server, File serverCert, HostAndPort proxy) {
throw new UnsupportedOperationException("No transport supported for HTTPS");
}
}; };
private final int defaultPort;
Protocol(int defaultPort) {
this.defaultPort = defaultPort;
}
public int getDefaultPort() {
return defaultPort;
}
public abstract CloseableTransport getTransport(HostAndPort server) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException; public abstract CloseableTransport getTransport(HostAndPort server) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
public abstract CloseableTransport getTransport(HostAndPort server, File serverCert) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException; public abstract CloseableTransport getTransport(HostAndPort server, File serverCert) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
@ -154,6 +185,9 @@ public enum Protocol {
if(url.startsWith("http://")) { if(url.startsWith("http://")) {
return HTTP; return HTTP;
} }
if(url.startsWith("https://")) {
return HTTPS;
}
return null; return null;
} }

View file

@ -32,7 +32,7 @@ public class ProxyTcpOverTlsTransport extends TcpOverTlsTransport {
protected void createSocket() throws IOException { protected void createSocket() throws IOException {
InetSocketAddress proxyAddr = new InetSocketAddress(proxy.getHost(), proxy.getPortOrDefault(DEFAULT_PROXY_PORT)); InetSocketAddress proxyAddr = new InetSocketAddress(proxy.getHost(), proxy.getPortOrDefault(DEFAULT_PROXY_PORT));
socket = new Socket(new Proxy(Proxy.Type.SOCKS, proxyAddr)); socket = new Socket(new Proxy(Proxy.Type.SOCKS, proxyAddr));
socket.connect(new InetSocketAddress(server.getHost(), server.getPortOrDefault(DEFAULT_PORT))); socket.connect(new InetSocketAddress(server.getHost(), server.getPortOrDefault(Protocol.SSL.getDefaultPort())));
socket = sslSocketFactory.createSocket(socket, proxy.getHost(), proxy.getPortOrDefault(DEFAULT_PROXY_PORT), true); socket = sslSocketFactory.createSocket(socket, proxy.getHost(), proxy.getPortOrDefault(DEFAULT_PROXY_PORT), true);
startHandshake((SSLSocket)socket); startHandshake((SSLSocket)socket);
} }

View file

@ -17,8 +17,6 @@ import java.security.cert.Certificate;
public class TcpOverTlsTransport extends TcpTransport { public class TcpOverTlsTransport extends TcpTransport {
private static final Logger log = LoggerFactory.getLogger(TcpOverTlsTransport.class); private static final Logger log = LoggerFactory.getLogger(TcpOverTlsTransport.class);
public static final int DEFAULT_PORT = 50002;
protected final SSLSocketFactory sslSocketFactory; protected final SSLSocketFactory sslSocketFactory;
public TcpOverTlsTransport(HostAndPort server) throws NoSuchAlgorithmException, KeyManagementException, CertificateException, KeyStoreException, IOException { public TcpOverTlsTransport(HostAndPort server) throws NoSuchAlgorithmException, KeyManagementException, CertificateException, KeyStoreException, IOException {
@ -88,7 +86,7 @@ public class TcpOverTlsTransport extends TcpTransport {
protected void createSocket() throws IOException { protected void createSocket() throws IOException {
socket = sslSocketFactory.createSocket(); socket = sslSocketFactory.createSocket();
socket.connect(new InetSocketAddress(server.getHost(), server.getPortOrDefault(DEFAULT_PORT))); socket.connect(new InetSocketAddress(server.getHost(), server.getPortOrDefault(Protocol.SSL.getDefaultPort())));
startHandshake((SSLSocket)socket); startHandshake((SSLSocket)socket);
} }

View file

@ -26,7 +26,6 @@ import java.util.concurrent.locks.ReentrantLock;
public class TcpTransport implements CloseableTransport, TimeoutCounter { public class TcpTransport implements CloseableTransport, TimeoutCounter {
private static final Logger log = LoggerFactory.getLogger(TcpTransport.class); private static final Logger log = LoggerFactory.getLogger(TcpTransport.class);
public static final int DEFAULT_PORT = 50001;
public static final int DEFAULT_MAX_TIMEOUT = 34; public static final int DEFAULT_MAX_TIMEOUT = 34;
private static final int[] BASE_READ_TIMEOUT_SECS = {3, 8, 16, DEFAULT_MAX_TIMEOUT}; private static final int[] BASE_READ_TIMEOUT_SECS = {3, 8, 16, DEFAULT_MAX_TIMEOUT};
private static final int[] SLOW_READ_TIMEOUT_SECS = {34, 68, 124, 208}; private static final int[] SLOW_READ_TIMEOUT_SECS = {34, 68, 124, 208};
@ -255,7 +254,7 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter {
protected void createSocket() throws IOException { protected void createSocket() throws IOException {
socket = socketFactory.createSocket(); socket = socketFactory.createSocket();
socket.connect(new InetSocketAddress(server.getHost(), server.getPortOrDefault(DEFAULT_PORT))); socket.connect(new InetSocketAddress(server.getHost(), server.getPortOrDefault(Protocol.TCP.getDefaultPort())));
} }
public boolean isClosed() { public boolean isClosed() {

View file

@ -42,7 +42,7 @@ public class TorTcpOverTlsTransport extends TcpOverTlsTransport {
log.error("Could not set socket connected status", e); log.error("Could not set socket connected status", e);
} }
socket = sslSocketFactory.createSocket(socket, server.getHost(), server.getPortOrDefault(DEFAULT_PORT), true); socket = sslSocketFactory.createSocket(socket, server.getHost(), server.getPortOrDefault(Protocol.SSL.getDefaultPort()), true);
startHandshake((SSLSocket)socket); startHandshake((SSLSocket)socket);
} }
} }

View file

@ -552,10 +552,10 @@ public class ServerPreferencesController extends PreferencesDetailController {
if(Config.get().getServerType() == ServerType.ELECTRUM_SERVER) { if(Config.get().getServerType() == ServerType.ELECTRUM_SERVER) {
if(useSslOriginal == null) { if(useSslOriginal == null) {
Integer portAsInteger = getPort(electrumPort.getText()); Integer portAsInteger = getPort(electrumPort.getText());
if(!electrumUseSsl.isSelected() && portAsInteger != null && portAsInteger == TcpOverTlsTransport.DEFAULT_PORT) { if(!electrumUseSsl.isSelected() && portAsInteger != null && portAsInteger == Protocol.SSL.getDefaultPort()) {
useSslOriginal = false; useSslOriginal = false;
electrumUseSsl.setSelected(true); electrumUseSsl.setSelected(true);
} else if(electrumUseSsl.isSelected() && portAsInteger != null && portAsInteger == TcpTransport.DEFAULT_PORT) { } else if(electrumUseSsl.isSelected() && portAsInteger != null && portAsInteger == Protocol.TCP.getDefaultPort()) {
useSslOriginal = true; useSslOriginal = true;
electrumUseSsl.setSelected(false); electrumUseSsl.setSelected(false);
} }
@ -746,7 +746,8 @@ public class ServerPreferencesController extends PreferencesDetailController {
String hostAsString = getHost(coreHost.getText()); String hostAsString = getHost(coreHost.getText());
Integer portAsInteger = getPort(corePort.getText()); Integer portAsInteger = getPort(corePort.getText());
if(hostAsString != null && portAsInteger != null && isValidPort(portAsInteger)) { if(hostAsString != null && portAsInteger != null && isValidPort(portAsInteger)) {
config.setCoreServer(new Server(Protocol.HTTP.toUrlString(hostAsString, portAsInteger))); Protocol protocol = portAsInteger == Protocol.HTTPS.getDefaultPort() ? Protocol.HTTPS : Protocol.HTTP;
config.setCoreServer(new Server(protocol.toUrlString(hostAsString, portAsInteger)));
} else if(hostAsString != null) { } else if(hostAsString != null) {
config.setCoreServer(new Server(Protocol.HTTP.toUrlString(hostAsString))); config.setCoreServer(new Server(Protocol.HTTP.toUrlString(hostAsString)));
} }

View file

@ -126,7 +126,8 @@ public class BitcoinCoreDialog extends ServerUrlDialog {
} }
protected Protocol getProtocol() { protected Protocol getProtocol() {
return Protocol.HTTP; Integer portAsInteger = getServerPort();
return portAsInteger != null && portAsInteger == Protocol.HTTPS.getDefaultPort() ? Protocol.HTTPS : Protocol.HTTP;
} }
protected void setProtocol(Protocol protocol) { protected void setProtocol(Protocol protocol) {

View file

@ -23,7 +23,7 @@ public class PrivateElectrumDialog extends ServerUrlDialog {
Panel mainPanel = new Panel(new GridLayout(3).setHorizontalSpacing(2).setVerticalSpacing(0)); Panel mainPanel = new Panel(new GridLayout(3).setHorizontalSpacing(2).setVerticalSpacing(0));
if(Config.get().getElectrumServer() == null) { if(Config.get().getElectrumServer() == null) {
Config.get().setElectrumServer(new Server("tcp://127.0.0.1:50001")); Config.get().setElectrumServer(new Server(Protocol.TCP.toUrlString("127.0.0.1", Protocol.TCP.getDefaultPort())));
} }
addUrlComponents(mainPanel, Config.get().getRecentElectrumServers(), Config.get().getElectrumServer()); addUrlComponents(mainPanel, Config.get().getRecentElectrumServers(), Config.get().getElectrumServer());
addLine(mainPanel); addLine(mainPanel);

View file

@ -103,6 +103,10 @@ public abstract class ServerUrlDialog extends ServerProxyDialog {
return null; return null;
} }
protected Integer getServerPort() {
return getPort(port.getText());
}
private String getAlias() { private String getAlias() {
return alias.getText().isEmpty() ? null : alias.getText(); return alias.getText().isEmpty() ? null : alias.getText();
} }