diff --git a/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java b/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java index a259917a..170c4323 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java @@ -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 diff --git a/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java b/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java index 23dcd161..37c34d86 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java @@ -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; } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/ProxyTcpOverTlsTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/ProxyTcpOverTlsTransport.java index f828232f..923df8b4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/ProxyTcpOverTlsTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/ProxyTcpOverTlsTransport.java @@ -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); } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/TcpOverTlsTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/TcpOverTlsTransport.java index a531576a..25a6e8d7 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/TcpOverTlsTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/TcpOverTlsTransport.java @@ -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); } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java index 93518336..66dda50c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java @@ -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() { diff --git a/src/main/java/com/sparrowwallet/sparrow/net/TorTcpOverTlsTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/TorTcpOverTlsTransport.java index 1b289201..ad7f374d 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/TorTcpOverTlsTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/TorTcpOverTlsTransport.java @@ -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); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java index 4365925f..edeb5206 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java @@ -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))); } diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/BitcoinCoreDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/BitcoinCoreDialog.java index cc519a8e..9b211767 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/BitcoinCoreDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/BitcoinCoreDialog.java @@ -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) { diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/PrivateElectrumDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/PrivateElectrumDialog.java index 93151adf..a569b570 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/PrivateElectrumDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/PrivateElectrumDialog.java @@ -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); diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/ServerUrlDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/ServerUrlDialog.java index b2d40b20..2684c342 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/ServerUrlDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/preferences/ServerUrlDialog.java @@ -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(); }