diff options
| author | luccioman <luccioman@users.noreply.github.com> | 2018-04-10 11:15:31 +0200 |
|---|---|---|
| committer | luccioman <luccioman@users.noreply.github.com> | 2018-04-10 11:15:31 +0200 |
| commit | 2af3bf79c7ef06447e741b3c027cd457183dec84 (patch) | |
| tree | 3bbde55bea18d7cfd646a67fe565d21932639b72 /source | |
| parent | d5f9dc0848db8bc4a0033334f4017d3d2d97e822 (diff) | |
Improve rendering of remote Solr admin URLs
- properly handle IPv6 loopback address replacement
- replace loopback address or host only when accessing peer remotely
- replace loopback part with the peer hostname as requested rather than
with its seed public IP as this works better for Intranet mode and when
peer is behind a reverse proxy.
Diffstat (limited to 'source')
3 files changed, 67 insertions, 17 deletions
diff --git a/source/net/yacy/cora/document/id/MultiProtocolURL.java b/source/net/yacy/cora/document/id/MultiProtocolURL.java index 1dc973eca..d681b7f21 100644 --- a/source/net/yacy/cora/document/id/MultiProtocolURL.java +++ b/source/net/yacy/cora/document/id/MultiProtocolURL.java @@ -482,6 +482,33 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU identSearchpart(); escape(); } + + /** + * @param host the new host to apply to the copy + * @return an exact copy of this URL instance but with a new host. The original instance remains unchanged. + * @throws IllegalArgumentException when the host parameter is null or empty. + */ + public MultiProtocolURL ofNewHost(final String host) throws IllegalArgumentException { + if(host == null || host.trim().isEmpty()) { + throw new IllegalArgumentException("Host parameter must not be null"); + } + MultiProtocolURL copy = new MultiProtocolURL(this); + + if (host.indexOf(':') >= 0 && host.charAt(0) != '[') { + copy.host = '[' + host + ']'; // IPv6 host must be enclosed in square brackets + } else { + copy.host = host; + } + + if (!Punycode.isBasic(this.host)) try { + this.host = toPunycode(this.host); + } catch (final PunycodeException e) { + ConcurrentLog.logException(e); + } + + return copy; + + } /** * Resolve '..' segments in the path. diff --git a/source/net/yacy/cora/federate/solr/instance/RemoteInstance.java b/source/net/yacy/cora/federate/solr/instance/RemoteInstance.java index 5e6dc5a50..d3bf67fed 100644 --- a/source/net/yacy/cora/federate/solr/instance/RemoteInstance.java +++ b/source/net/yacy/cora/federate/solr/instance/RemoteInstance.java @@ -21,7 +21,6 @@ package net.yacy.cora.federate.solr.instance; import java.io.IOException; -import java.net.InetAddress; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collection; @@ -58,7 +57,6 @@ import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient; import net.yacy.cora.document.id.MultiProtocolURL; -import net.yacy.cora.protocol.Domains; import net.yacy.cora.protocol.HeaderFramework; import net.yacy.cora.util.CommonPattern; import net.yacy.cora.util.ConcurrentLog; @@ -284,19 +282,35 @@ public class RemoteInstance implements SolrInstance { return o instanceof RemoteInstance && ((RemoteInstance) o).solrurl.equals(this.solrurl); } - /** - * @return the administration URL of the remote Solr instance - */ - public String getAdminInterface() { - final InetAddress localhostExternAddress = Domains.myPublicLocalIP(); - final String localhostExtern = localhostExternAddress == null ? "127.0.0.1" : localhostExternAddress.getHostAddress(); - String u = this.solrurl; - int p = u.indexOf("localhost",0); - if (p < 0) p = u.indexOf("127.0.0.1",0); - if (p < 0) p = u.indexOf("0:0:0:0:0:0:0:1",0); - if (p >= 0) u = u.substring(0, p) + localhostExtern + u.substring(p + 9); - return u; - } + /** + * @param toExternalAddress + * when true, try to replace the eventual loopback host part of the + * Solr URL with the external host name of the hosting machine + * @param externalHost + * the eventual external host name or address to use when + * toExternalAddress is true + * @return the administration URL of the remote Solr instance + */ + public String getAdminInterface(final boolean toExternalAddress, final String externalHost) { + String u = this.solrurl; + if (toExternalAddress && externalHost != null && !externalHost.trim().isEmpty()) { + try { + MultiProtocolURL url = new MultiProtocolURL(u); + + if(url.isLocal()) { + url = url.ofNewHost(externalHost); + u = url.toString(); + } + + } catch (final MalformedURLException ignored) { + /* + * This should not happen as the solrurl attribute has already been parsed in + * the constructor + */ + } + } + return u; + } @Override public String getDefaultCoreName() { diff --git a/source/net/yacy/cora/federate/solr/instance/ShardInstance.java b/source/net/yacy/cora/federate/solr/instance/ShardInstance.java index 62ac1f3a7..b955391d2 100644 --- a/source/net/yacy/cora/federate/solr/instance/ShardInstance.java +++ b/source/net/yacy/cora/federate/solr/instance/ShardInstance.java @@ -81,9 +81,18 @@ public class ShardInstance implements SolrInstance { for (RemoteInstance instance: instances) instance.close(); } - public ArrayList<String> getAdminInterfaces() { + /** + * @param toExternalAddress + * when true, try to replace the eventual loopback host part of the + * Solr instances URLs with the external host name of the hosting machine + * @param externalHost + * the eventual external host name or address to use when + * toExternalAddress is true + * @return the administration URLs of the Solr instances + */ + public ArrayList<String> getAdminInterfaces(final boolean toExternalAddress, final String externalHost) { ArrayList<String> a = new ArrayList<String>(); - for (RemoteInstance i: this.instances) a.add(i.getAdminInterface()); + for (RemoteInstance i: this.instances) a.add(i.getAdminInterface(toExternalAddress, externalHost)); return a; } } |
