|
20 | 20 | import com.google.common.collect.Lists; |
21 | 21 | import com.google.common.primitives.Ints; |
22 | 22 | import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; |
| 23 | +import com.viaversion.viaversion.libs.fastutil.ints.IntArrayList; |
| 24 | +import com.viaversion.viaversion.libs.fastutil.ints.IntImmutableList; |
| 25 | +import com.viaversion.viaversion.libs.fastutil.ints.IntList; |
| 26 | +import java.net.InetAddress; |
| 27 | +import java.net.InetSocketAddress; |
| 28 | +import java.net.UnknownHostException; |
23 | 29 | import java.util.ArrayList; |
24 | 30 | import java.util.Arrays; |
25 | 31 | import java.util.List; |
26 | 32 | import java.util.regex.Pattern; |
| 33 | +import java.util.stream.Collectors; |
27 | 34 | import org.apache.commons.lang3.StringUtils; |
| 35 | +import org.jetbrains.annotations.NotNull; |
| 36 | +import org.jetbrains.annotations.Nullable; |
| 37 | +import org.jetbrains.annotations.VisibleForTesting; |
28 | 38 |
|
29 | 39 | // Based on VIAaaS parser |
30 | 40 | public class AddressParser { |
31 | | - public Integer protocol; |
32 | | - public String viaSuffix; |
33 | | - public String serverAddress; |
34 | | - public String viaOptions; |
| 41 | + private static final String VIA = "viafabric"; |
| 42 | + private static final Pattern DOT_SUFFIX = Pattern.compile("\\.$"); |
| 43 | + private static final int MAX_PORT = 65535; |
35 | 44 |
|
36 | | - public AddressParser parse(String address) { |
37 | | - return parse(address, "viafabric"); |
| 45 | + // Retaining a list for now; not exposing it to the end consumer. |
| 46 | + // An idea tossed about is to retry the server for the given protocols. |
| 47 | + @Nullable |
| 48 | + @VisibleForTesting |
| 49 | + final IntList protocols; |
| 50 | + @NotNull |
| 51 | + private final String serverAddress; |
| 52 | + @Nullable |
| 53 | + private final Integer port; |
| 54 | + |
| 55 | + private AddressParser(@NotNull String address, @Nullable Integer port) { |
| 56 | + this(address, null, port); |
38 | 57 | } |
39 | 58 |
|
40 | | - public String getSuffixWithOptions() { |
41 | | - if (viaOptions != null && !viaOptions.isEmpty()) { |
42 | | - return viaOptions + "." + viaSuffix; |
43 | | - } |
44 | | - return viaSuffix; |
| 59 | + private AddressParser(@NotNull String address, @Nullable IntList protocols, @Nullable Integer port) { |
| 60 | + this.serverAddress = address; |
| 61 | + this.port = port; |
| 62 | + this.protocols = protocols; |
| 63 | + } |
| 64 | + |
| 65 | + @NotNull |
| 66 | + public static AddressParser parse(String address) { |
| 67 | + return parse(address, VIA); |
45 | 68 | } |
46 | 69 |
|
47 | | - public AddressParser parse(String address, String viaHostName) { |
| 70 | + @NotNull |
| 71 | + private static AddressParser parse(String address, String viaHostName) { |
| 72 | + int portIndex = address.lastIndexOf(':'); |
| 73 | + Integer port = portIndex >= 0 ? Ints.tryParse(address.substring(portIndex + 1)) : null; |
| 74 | + |
| 75 | + if (port != null && port >= 0 && port < MAX_PORT + 1) { |
| 76 | + if (address.charAt(portIndex - 1) == '.') { |
| 77 | + // Let's not allocate an intermediate string. |
| 78 | + portIndex -= 1; |
| 79 | + } else if (port < 10000 && |
| 80 | + // I don't like this but there's not really a better way of doing this |
| 81 | + address.lastIndexOf(':', portIndex - 1) > Math.max( |
| 82 | + address.lastIndexOf(']', portIndex - 1), |
| 83 | + address.lastIndexOf('.', portIndex - 1) |
| 84 | + )) { |
| 85 | + // We parsed an IPv6, a port isn't ideal here. |
| 86 | + port = null; |
| 87 | + } |
| 88 | + |
| 89 | + // Keeping a sane flow control. |
| 90 | + if (port != null) { |
| 91 | + // Truncate the port off, as that interferes. |
| 92 | + address = address.substring(0, portIndex); |
| 93 | + } |
| 94 | + } |
| 95 | + |
48 | 96 | address = StringUtils.removeEnd(address, "."); |
49 | | - String suffixRemoved = StringUtils.removeEnd(address, "." + viaHostName); |
50 | 97 |
|
51 | | - if (suffixRemoved.equals(address)) { |
52 | | - serverAddress = address; |
53 | | - return this; |
| 98 | + String truncated = StringUtils.removeEnd(address, '.' + viaHostName); |
| 99 | + if (!address.equals(truncated)) { |
| 100 | + return parseSuffix(truncated, port); |
| 101 | + } |
| 102 | + |
| 103 | + truncated = StringUtils.removeStart(address, viaHostName + '.'); |
| 104 | + if (!address.equals(truncated)) { |
| 105 | + final AddressParser addr = parsePrefix(truncated, port); |
| 106 | + if (addr != null) { |
| 107 | + return addr; |
| 108 | + } |
54 | 109 | } |
55 | 110 |
|
| 111 | + return new AddressParser(address, port); |
| 112 | + } |
| 113 | + |
| 114 | + @NotNull |
| 115 | + private static AddressParser parseSuffix(String address, Integer port) { |
56 | 116 | boolean stopOptions = false; |
57 | | - List<String> optionsParts = new ArrayList<>(); |
| 117 | + IntList protocolParts = new IntArrayList(); |
58 | 118 | List<String> serverParts = new ArrayList<>(); |
59 | 119 |
|
60 | | - for (String part : Lists.reverse(Arrays.asList(suffixRemoved.split(Pattern.quote("."))))) { |
61 | | - if (!stopOptions && parseOption(part)) { |
62 | | - optionsParts.add(part); |
| 120 | + Integer protocol; |
| 121 | + for (String part : Lists.reverse(Arrays.asList(address.split("\\.")))) { |
| 122 | + if (!stopOptions && (protocol = parseSuffixOption(part)) != null) { |
| 123 | + protocolParts.add(protocol.intValue()); |
63 | 124 | continue; |
64 | 125 | } |
65 | 126 | stopOptions = true; |
66 | 127 | serverParts.add(part); |
67 | 128 | } |
68 | 129 |
|
69 | | - serverAddress = String.join(".", Lists.reverse(serverParts)); |
70 | | - viaOptions = String.join(".", Lists.reverse(optionsParts)); |
71 | | - viaSuffix = viaHostName; |
| 130 | + return new AddressParser( |
| 131 | + String.join(".", Lists.reverse(serverParts)), |
| 132 | + new IntImmutableList(Lists.reverse(protocolParts)), |
| 133 | + port |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + // Fail condition = returns null; caller must fall through. |
| 138 | + @Nullable |
| 139 | + private static AddressParser parsePrefix(String address, Integer port) { |
| 140 | + IntList protocols = new IntArrayList(); |
| 141 | + int index = 0, lastIndex, colonIndex = address.indexOf(';'); |
| 142 | + |
| 143 | + if (colonIndex < 0) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + while ((index = address.indexOf('+', lastIndex = index)) >= 0 && index < colonIndex) { |
| 148 | + parseAndAdd(address.substring(lastIndex, index), protocols); |
| 149 | + index++; |
| 150 | + } |
| 151 | + |
| 152 | + parseAndAdd(address.substring(lastIndex, colonIndex), protocols); |
72 | 153 |
|
73 | | - return this; |
| 154 | + return new AddressParser( |
| 155 | + address.substring(colonIndex + 1), |
| 156 | + new IntImmutableList(protocols), |
| 157 | + port |
| 158 | + ); |
74 | 159 | } |
75 | 160 |
|
76 | | - public boolean parseOption(String part) { |
| 161 | + private static void parseAndAdd(String part, IntList protocols) { |
| 162 | + final Integer protocol = parseSchemeOption(part); |
| 163 | + if (protocol != null) { |
| 164 | + protocols.add(protocol.intValue()); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private static Integer parseSuffixOption(String part) { |
77 | 169 | String option; |
78 | 170 | if (part.length() < 2) { |
79 | | - return false; |
| 171 | + return null; |
80 | 172 | } else if (part.startsWith("_")) { |
81 | 173 | option = String.valueOf(part.charAt(1)); |
82 | 174 | } else if (part.charAt(1) == '_') { |
83 | 175 | option = String.valueOf(part.charAt(0)); |
84 | 176 | } else { |
85 | | - return false; |
| 177 | + return null; |
86 | 178 | } |
87 | 179 |
|
88 | 180 | String arg = part.substring(2); |
89 | 181 | if ("v".equals(option)) { |
90 | | - parseProtocol(arg); |
| 182 | + return parseProtocol(arg); |
| 183 | + } |
| 184 | + |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + private static Integer parseSchemeOption(String part) { |
| 189 | + if (part.length() < 2) { |
| 190 | + return null; |
| 191 | + } |
| 192 | + if (!part.startsWith("v")) { |
| 193 | + return null; |
| 194 | + } |
| 195 | + return parseProtocol(part.substring(1)); |
| 196 | + } |
| 197 | + |
| 198 | + private static Integer parseProtocol(String arg) { |
| 199 | + final Integer protocol = Ints.tryParse(arg); |
| 200 | + if (protocol != null) { |
| 201 | + return protocol; |
| 202 | + } |
| 203 | + ProtocolVersion ver = ProtocolVersion.getClosest(arg.replace('_', '.')); |
| 204 | + if (ver != null) { |
| 205 | + return ver.getVersion(); |
91 | 206 | } |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + private static String toProtocolName(int protocol) { |
| 211 | + if (protocol < 0 || !ProtocolVersion.isRegistered(protocol)) { |
| 212 | + return Integer.toString(protocol); |
| 213 | + } |
| 214 | + return ProtocolVersion.getProtocol(protocol).getIncludedVersions().iterator().next(); |
| 215 | + } |
| 216 | + |
| 217 | + public String getSuffixWithOptions() { |
| 218 | + if (protocols == null) { |
| 219 | + return ""; |
| 220 | + } |
| 221 | + if (protocols.isEmpty()) { |
| 222 | + return VIA; |
| 223 | + } |
| 224 | + return protocols.intStream() |
| 225 | + .mapToObj(AddressParser::toProtocolName) |
| 226 | + .map(str -> str.replace('.', '_')) |
| 227 | + .collect(Collectors.joining("._v", "_v", "." + VIA)); |
| 228 | + } |
| 229 | + |
| 230 | + public String getPrefixWithOptions() { |
| 231 | + if (protocols == null) { |
| 232 | + return ""; |
| 233 | + } |
| 234 | + if (protocols.isEmpty()) { |
| 235 | + return VIA; |
| 236 | + } |
| 237 | + return protocols.intStream() |
| 238 | + .mapToObj(AddressParser::toProtocolName) |
| 239 | + .collect(Collectors.joining("+v", VIA + ".v", "")); |
| 240 | + } |
| 241 | + |
| 242 | + public boolean hasViaMetadata() { |
| 243 | + return protocols != null; |
| 244 | + } |
| 245 | + |
| 246 | + public boolean hasProtocol() { |
| 247 | + return protocols != null && !protocols.isEmpty(); |
| 248 | + } |
| 249 | + |
| 250 | + public Integer protocol() { |
| 251 | + if (protocols != null && !protocols.isEmpty()) { |
| 252 | + return protocols.getInt(0); |
| 253 | + } |
| 254 | + return null; |
| 255 | + } |
| 256 | + |
| 257 | + @NotNull |
| 258 | + public String serverAddress() { |
| 259 | + return this.serverAddress; |
| 260 | + } |
| 261 | + |
| 262 | + @Nullable |
| 263 | + public Integer port() { |
| 264 | + return this.port; |
| 265 | + } |
| 266 | + |
| 267 | + public String toAddress() { |
| 268 | + if (port != null) { |
| 269 | + return serverAddress + ':' + port; |
| 270 | + } |
| 271 | + return serverAddress; |
| 272 | + } |
92 | 273 |
|
93 | | - return true; |
| 274 | + public String toSuffixedViaAddress() { |
| 275 | + final String address = this.addAddressSuffix(serverAddress); |
| 276 | + if (port != null) { |
| 277 | + return address + ':' + port; |
| 278 | + } |
| 279 | + return address; |
| 280 | + } |
| 281 | + |
| 282 | + public InetAddress resolve() throws UnknownHostException { |
| 283 | + return this.addAddressSuffix(InetAddress.getByName(serverAddress)); |
| 284 | + } |
| 285 | + |
| 286 | + public InetSocketAddress addAddressSuffix(InetSocketAddress address) throws UnknownHostException { |
| 287 | + return new InetSocketAddress(this.addAddressSuffix(address.getAddress()), address.getPort()); |
94 | 288 | } |
95 | 289 |
|
96 | | - public void parseProtocol(String arg) { |
97 | | - protocol = Ints.tryParse(arg); |
98 | | - if (protocol == null) { |
99 | | - ProtocolVersion ver = ProtocolVersion.getClosest(arg.replace("_", ".")); |
100 | | - if (ver != null) protocol = ver.getVersion(); |
| 290 | + public InetAddress addAddressSuffix(InetAddress address) throws UnknownHostException { |
| 291 | + return InetAddress.getByAddress(this.addAddressSuffix(address.getHostName()), address.getAddress()); |
| 292 | + } |
| 293 | + |
| 294 | + public String addAddressSuffix(String input) { |
| 295 | + if (!this.hasViaMetadata()) { |
| 296 | + return input; |
101 | 297 | } |
| 298 | + return DOT_SUFFIX.matcher(input).replaceAll("") + '.' + this.getSuffixWithOptions(); |
| 299 | + } |
| 300 | + |
| 301 | + @Override |
| 302 | + public String toString() { |
| 303 | + return "AddressParser{" + this.toSuffixedViaAddress() + '}'; |
102 | 304 | } |
103 | 305 | } |
0 commit comments