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) {
bwtConfig.bitcoindUrl = config.getCoreServer().getUrl();
try {
HostAndPort hostAndPort = Protocol.HTTP.getServerHostAndPort(bwtConfig.bitcoindUrl);
Protocol protocol = config.getCoreServer().getProtocol();
HostAndPort hostAndPort = protocol.getServerHostAndPort(bwtConfig.bitcoindUrl);
if(hostAndPort.getHost().endsWith(".local")) {
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) {
//ignore

View file

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

View file

@ -32,7 +32,7 @@ public class ProxyTcpOverTlsTransport extends TcpOverTlsTransport {
protected void createSocket() throws IOException {
InetSocketAddress proxyAddr = new InetSocketAddress(proxy.getHost(), proxy.getPortOrDefault(DEFAULT_PROXY_PORT));
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);
startHandshake((SSLSocket)socket);
}

View file

@ -17,8 +17,6 @@ import java.security.cert.Certificate;
public class TcpOverTlsTransport extends TcpTransport {
private static final Logger log = LoggerFactory.getLogger(TcpOverTlsTransport.class);
public static final int DEFAULT_PORT = 50002;
protected final SSLSocketFactory sslSocketFactory;
public TcpOverTlsTransport(HostAndPort server) throws NoSuchAlgorithmException, KeyManagementException, CertificateException, KeyStoreException, IOException {
@ -88,7 +86,7 @@ public class TcpOverTlsTransport extends TcpTransport {
protected void createSocket() throws IOException {
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);
}

View file

@ -26,7 +26,6 @@ import java.util.concurrent.locks.ReentrantLock;
public class TcpTransport implements CloseableTransport, TimeoutCounter {
private static final Logger log = LoggerFactory.getLogger(TcpTransport.class);
public static final int DEFAULT_PORT = 50001;
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[] SLOW_READ_TIMEOUT_SECS = {34, 68, 124, 208};
@ -255,7 +254,7 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter {
protected void createSocket() throws IOException {
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() {

View file

@ -42,7 +42,7 @@ public class TorTcpOverTlsTransport extends TcpOverTlsTransport {
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);
}
}

View file

@ -552,10 +552,10 @@ public class ServerPreferencesController extends PreferencesDetailController {
if(Config.get().getServerType() == ServerType.ELECTRUM_SERVER) {
if(useSslOriginal == null) {
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;
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;
electrumUseSsl.setSelected(false);
}
@ -746,7 +746,8 @@ public class ServerPreferencesController extends PreferencesDetailController {
String hostAsString = getHost(coreHost.getText());
Integer portAsInteger = getPort(corePort.getText());
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) {
config.setCoreServer(new Server(Protocol.HTTP.toUrlString(hostAsString)));
}

View file

@ -126,7 +126,8 @@ public class BitcoinCoreDialog extends ServerUrlDialog {
}
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) {

View file

@ -23,7 +23,7 @@ public class PrivateElectrumDialog extends ServerUrlDialog {
Panel mainPanel = new Panel(new GridLayout(3).setHorizontalSpacing(2).setVerticalSpacing(0));
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());
addLine(mainPanel);

View file

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