| 1 |
package com.github.markusbernhardt.proxy.selector.pac; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.math.BigInteger; |
| 5 |
import java.net.Inet4Address; |
| 6 |
import java.net.Inet6Address; |
| 7 |
import java.net.InetAddress; |
| 8 |
import java.net.NetworkInterface; |
| 9 |
import java.net.UnknownHostException; |
| 10 |
import java.util.Arrays; |
| 11 |
import java.util.Calendar; |
| 12 |
import java.util.Collections; |
| 13 |
import java.util.Comparator; |
| 14 |
import java.util.Date; |
| 15 |
import java.util.Enumeration; |
| 16 |
import java.util.HashMap; |
| 17 |
import java.util.List; |
| 18 |
import java.util.Map; |
| 19 |
import java.util.StringTokenizer; |
| 20 |
import java.util.TimeZone; |
| 21 |
import java.util.TreeMap; |
| 22 |
|
| 23 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 24 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class PacScriptMethods implements ScriptMethods { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static final String OVERRIDE_LOCAL_IP = "com.btr.proxy.pac.overrideLocalIP"; |
| 38 |
|
| 39 |
private final static String GMT = "GMT"; |
| 40 |
|
| 41 |
private final static List<String> DAYS = Collections |
| 42 |
.unmodifiableList(Arrays.asList("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT")); |
| 43 |
|
| 44 |
private final static List<String> MONTH = Collections.unmodifiableList( |
| 45 |
Arrays.asList("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")); |
| 46 |
|
| 47 |
private Calendar currentTime; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public PacScriptMethods() { |
| 54 |
super(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public boolean isPlainHostName(String host) { |
| 64 |
return host.indexOf(".") < 0; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public boolean dnsDomainIs(String host, String domain) { |
| 78 |
return host.endsWith(domain); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public boolean localHostOrDomainIs(String host, String domain) { |
| 94 |
return domain.startsWith(host); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public boolean isResolvable(String host) { |
| 106 |
try { |
| 107 |
InetAddress.getByName(host).getHostAddress(); |
| 108 |
return true; |
| 109 |
} catch (UnknownHostException ex) { |
| 110 |
Logger.log(JavaxPacScriptParser.class, LogLevel.DEBUG, "Hostname not resolveable {0}.", host); |
| 111 |
} |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public boolean isInNet(String host, String pattern, String mask) { |
| 136 |
host = dnsResolve(host); |
| 137 |
if (host == null || host.length() == 0) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
long lhost = parseIpAddressToLong(host); |
| 141 |
long lpattern = parseIpAddressToLong(pattern); |
| 142 |
long lmask = parseIpAddressToLong(mask); |
| 143 |
return (lhost & lmask) == lpattern; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private long parseIpAddressToLong(String address) { |
| 155 |
long result = 0; |
| 156 |
String[] parts = address.split("\\."); |
| 157 |
long shift = 24; |
| 158 |
for (String part : parts) { |
| 159 |
long lpart = Long.parseLong(part); |
| 160 |
|
| 161 |
result |= (lpart << shift); |
| 162 |
shift -= 8; |
| 163 |
} |
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public String dnsResolve(String host) { |
| 177 |
try { |
| 178 |
InetAddress ina = InetAddress.getByName(host); |
| 179 |
return ina.getHostAddress(); |
| 180 |
} catch (UnknownHostException e) { |
| 181 |
Logger.log(JavaxPacScriptParser.class, LogLevel.DEBUG, "DNS name not resolvable {0}.", host); |
| 182 |
} |
| 183 |
return ""; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public String myIpAddress() { |
| 194 |
return getLocalAddressOfType(Inet4Address.class); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
private String getLocalAddressOfType(Class<? extends InetAddress> cl) { |
| 208 |
try { |
| 209 |
String overrideIP = System.getProperty(OVERRIDE_LOCAL_IP); |
| 210 |
if (overrideIP != null && overrideIP.trim().length() > 0) { |
| 211 |
return overrideIP.trim(); |
| 212 |
} |
| 213 |
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| 214 |
while (interfaces.hasMoreElements()) { |
| 215 |
NetworkInterface current = interfaces.nextElement(); |
| 216 |
if (!current.isUp() || current.isLoopback() || current.isVirtual()) { |
| 217 |
continue; |
| 218 |
} |
| 219 |
Enumeration<InetAddress> addresses = current.getInetAddresses(); |
| 220 |
while (addresses.hasMoreElements()) { |
| 221 |
InetAddress adr = addresses.nextElement(); |
| 222 |
if (cl.isInstance(adr)) { |
| 223 |
Logger.log(JavaxPacScriptParser.class, LogLevel.TRACE, "Local address resolved to {0}", adr); |
| 224 |
return adr.getHostAddress(); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
return ""; |
| 229 |
} catch (IOException e) { |
| 230 |
Logger.log(JavaxPacScriptParser.class, LogLevel.DEBUG, "Local address not resolvable."); |
| 231 |
return ""; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public int dnsDomainLevels(String host) { |
| 245 |
int count = 0; |
| 246 |
int startPos = 0; |
| 247 |
while ((startPos = host.indexOf(".", startPos + 1)) > -1) { |
| 248 |
count++; |
| 249 |
} |
| 250 |
return count; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public boolean shExpMatch(String str, String shexp) { |
| 266 |
StringTokenizer tokenizer = new StringTokenizer(shexp, "*"); |
| 267 |
int startPos = 0; |
| 268 |
while (tokenizer.hasMoreTokens()) { |
| 269 |
String token = tokenizer.nextToken(); |
| 270 |
int temp = str.indexOf(token, startPos); |
| 271 |
|
| 272 |
|
| 273 |
if (startPos == 0 && !shexp.startsWith("*") && temp != 0) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
if (!tokenizer.hasMoreTokens() && !shexp.endsWith("*") && !str.endsWith(token)) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
if (temp == -1) { |
| 282 |
return false; |
| 283 |
} else { |
| 284 |
startPos = temp + token.length(); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public boolean weekdayRange(String wd1, String wd2, String gmt) { |
| 311 |
boolean useGmt = GMT.equalsIgnoreCase(wd2) || GMT.equalsIgnoreCase(gmt); |
| 312 |
Calendar cal = getCurrentTime(useGmt); |
| 313 |
|
| 314 |
int currentDay = cal.get(Calendar.DAY_OF_WEEK) - 1; |
| 315 |
int from = DAYS.indexOf(wd1 == null ? null : wd1.toUpperCase()); |
| 316 |
int to = DAYS.indexOf(wd2 == null ? null : wd2.toUpperCase()); |
| 317 |
if (to == -1) { |
| 318 |
to = from; |
| 319 |
} |
| 320 |
|
| 321 |
if (to < from) { |
| 322 |
return currentDay >= from || currentDay <= to; |
| 323 |
} else { |
| 324 |
return currentDay >= from && currentDay <= to; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public void setCurrentTime(Calendar cal) { |
| 339 |
this.currentTime = cal; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
private Calendar getCurrentTime(boolean useGmt) { |
| 353 |
if (this.currentTime != null) { |
| 354 |
return (Calendar) this.currentTime.clone(); |
| 355 |
} |
| 356 |
return Calendar.getInstance(useGmt ? TimeZone.getTimeZone(GMT) : TimeZone.getDefault()); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public boolean dateRange(Object day1, Object month1, Object year1, Object day2, Object month2, Object year2, |
| 388 |
Object gmt) { |
| 389 |
|
| 390 |
|
| 391 |
Map<String, Integer> params = new HashMap<String, Integer>(); |
| 392 |
parseDateParam(params, day1); |
| 393 |
parseDateParam(params, month1); |
| 394 |
parseDateParam(params, year1); |
| 395 |
parseDateParam(params, day2); |
| 396 |
parseDateParam(params, month2); |
| 397 |
parseDateParam(params, year2); |
| 398 |
parseDateParam(params, gmt); |
| 399 |
|
| 400 |
|
| 401 |
boolean useGmt = params.get("gmt") != null; |
| 402 |
Calendar cal = getCurrentTime(useGmt); |
| 403 |
Date current = cal.getTime(); |
| 404 |
|
| 405 |
|
| 406 |
if (params.get("day1") != null) { |
| 407 |
cal.set(Calendar.DAY_OF_MONTH, params.get("day1")); |
| 408 |
} |
| 409 |
if (params.get("month1") != null) { |
| 410 |
cal.set(Calendar.MONTH, params.get("month1")); |
| 411 |
} |
| 412 |
if (params.get("year1") != null) { |
| 413 |
cal.set(Calendar.YEAR, params.get("year1")); |
| 414 |
} |
| 415 |
Date from = cal.getTime(); |
| 416 |
|
| 417 |
|
| 418 |
Date to; |
| 419 |
if (params.get("day2") != null) { |
| 420 |
cal.set(Calendar.DAY_OF_MONTH, params.get("day2")); |
| 421 |
} |
| 422 |
if (params.get("month2") != null) { |
| 423 |
cal.set(Calendar.MONTH, params.get("month2")); |
| 424 |
} |
| 425 |
if (params.get("year2") != null) { |
| 426 |
cal.set(Calendar.YEAR, params.get("year2")); |
| 427 |
} |
| 428 |
to = cal.getTime(); |
| 429 |
|
| 430 |
|
| 431 |
if (to.before(from)) { |
| 432 |
cal.add(Calendar.MONTH, +1); |
| 433 |
to = cal.getTime(); |
| 434 |
} |
| 435 |
|
| 436 |
if (to.before(from)) { |
| 437 |
cal.add(Calendar.YEAR, +1); |
| 438 |
cal.add(Calendar.MONTH, -1); |
| 439 |
to = cal.getTime(); |
| 440 |
} |
| 441 |
|
| 442 |
return current.compareTo(from) >= 0 && current.compareTo(to) <= 0; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
private void parseDateParam(Map<String, Integer> params, Object value) { |
| 456 |
if (value instanceof Number) { |
| 457 |
int n = ((Number) value).intValue(); |
| 458 |
if (n <= 31) { |
| 459 |
|
| 460 |
if (params.get("day1") == null) { |
| 461 |
params.put("day1", n); |
| 462 |
} else { |
| 463 |
params.put("day2", n); |
| 464 |
} |
| 465 |
} else { |
| 466 |
|
| 467 |
if (params.get("year1") == null) { |
| 468 |
params.put("year1", n); |
| 469 |
} else { |
| 470 |
params.put("year2", n); |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
if (value instanceof String) { |
| 476 |
int n = MONTH.indexOf(((String) value).toUpperCase()); |
| 477 |
if (n > -1) { |
| 478 |
|
| 479 |
if (params.get("month1") == null) { |
| 480 |
params.put("month1", n); |
| 481 |
} else { |
| 482 |
params.put("month2", n); |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
if (GMT.equalsIgnoreCase(String.valueOf(value))) { |
| 488 |
params.put("gmt", 1); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public boolean timeRange(Object hour1, Object min1, Object sec1, Object hour2, Object min2, Object sec2, |
| 526 |
Object gmt) { |
| 527 |
boolean useGmt = GMT.equalsIgnoreCase(String.valueOf(min1)) || GMT.equalsIgnoreCase(String.valueOf(sec1)) |
| 528 |
|| GMT.equalsIgnoreCase(String.valueOf(min2)) || GMT.equalsIgnoreCase(String.valueOf(gmt)); |
| 529 |
|
| 530 |
Calendar cal = getCurrentTime(useGmt); |
| 531 |
cal.set(Calendar.MILLISECOND, 0); |
| 532 |
Date current = cal.getTime(); |
| 533 |
Date from; |
| 534 |
Date to; |
| 535 |
if (sec2 instanceof Number) { |
| 536 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) hour1).intValue()); |
| 537 |
cal.set(Calendar.MINUTE, ((Number) min1).intValue()); |
| 538 |
cal.set(Calendar.SECOND, ((Number) sec1).intValue()); |
| 539 |
from = cal.getTime(); |
| 540 |
|
| 541 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) hour2).intValue()); |
| 542 |
cal.set(Calendar.MINUTE, ((Number) min2).intValue()); |
| 543 |
cal.set(Calendar.SECOND, ((Number) sec2).intValue()); |
| 544 |
to = cal.getTime(); |
| 545 |
} else if (hour2 instanceof Number) { |
| 546 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) hour1).intValue()); |
| 547 |
cal.set(Calendar.MINUTE, ((Number) min1).intValue()); |
| 548 |
cal.set(Calendar.SECOND, 0); |
| 549 |
from = cal.getTime(); |
| 550 |
|
| 551 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) sec1).intValue()); |
| 552 |
cal.set(Calendar.MINUTE, ((Number) hour2).intValue()); |
| 553 |
cal.set(Calendar.SECOND, 59); |
| 554 |
to = cal.getTime(); |
| 555 |
} else if (min1 instanceof Number) { |
| 556 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) hour1).intValue()); |
| 557 |
cal.set(Calendar.MINUTE, 0); |
| 558 |
cal.set(Calendar.SECOND, 0); |
| 559 |
from = cal.getTime(); |
| 560 |
|
| 561 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) min1).intValue()); |
| 562 |
cal.set(Calendar.MINUTE, 59); |
| 563 |
cal.set(Calendar.SECOND, 59); |
| 564 |
to = cal.getTime(); |
| 565 |
} else { |
| 566 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) hour1).intValue()); |
| 567 |
cal.set(Calendar.MINUTE, 0); |
| 568 |
cal.set(Calendar.SECOND, 0); |
| 569 |
from = cal.getTime(); |
| 570 |
|
| 571 |
cal.set(Calendar.HOUR_OF_DAY, ((Number) hour1).intValue()); |
| 572 |
cal.set(Calendar.MINUTE, 59); |
| 573 |
cal.set(Calendar.SECOND, 59); |
| 574 |
to = cal.getTime(); |
| 575 |
} |
| 576 |
|
| 577 |
if (to.before(from)) { |
| 578 |
cal.setTime(to); |
| 579 |
cal.add(Calendar.DATE, +1); |
| 580 |
to = cal.getTime(); |
| 581 |
} |
| 582 |
|
| 583 |
return current.compareTo(from) >= 0 && current.compareTo(to) <= 0; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
public boolean isResolvableEx(String host) { |
| 595 |
return isResolvable(host); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
private static final BigInteger HIGH_32_INT = new BigInteger(new byte[] { -1, -1, -1, -1 }); |
| 600 |
private static final BigInteger HIGH_128_INT = new BigInteger( |
| 601 |
new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }); |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
public boolean isInNetEx(String ipOrHost, String cidr) { |
| 612 |
if (ipOrHost == null || ipOrHost.length() == 0 || cidr == null || cidr.length() == 0) { |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
try { |
| 617 |
|
| 618 |
String[] cidrParts = cidr.split("/"); |
| 619 |
if (cidrParts.length != 2) { |
| 620 |
return false; |
| 621 |
} |
| 622 |
String cidrRange = cidrParts[0]; |
| 623 |
int cidrBits = Integer.parseInt(cidrParts[1]); |
| 624 |
|
| 625 |
byte[] addressBytes = InetAddress.getByName(ipOrHost).getAddress(); |
| 626 |
BigInteger ip = new BigInteger(addressBytes); |
| 627 |
BigInteger mask = addressBytes.length == 4 ? HIGH_32_INT.shiftLeft(32 - cidrBits) |
| 628 |
: HIGH_128_INT.shiftLeft(128 - cidrBits); |
| 629 |
|
| 630 |
byte[] rangeBytes = InetAddress.getByName(cidrRange).getAddress(); |
| 631 |
BigInteger range = new BigInteger(rangeBytes); |
| 632 |
BigInteger lowIP = range.and(mask); |
| 633 |
BigInteger highIP = lowIP.add(mask.not()); |
| 634 |
|
| 635 |
return lowIP.compareTo(ip) <= 0 && highIP.compareTo(ip) >= 0; |
| 636 |
} catch (UnknownHostException e) { |
| 637 |
return false; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
public String dnsResolveEx(String host) { |
| 648 |
StringBuilder result = new StringBuilder(); |
| 649 |
try { |
| 650 |
InetAddress[] list = InetAddress.getAllByName(host); |
| 651 |
for (InetAddress inetAddress : list) { |
| 652 |
result.append(inetAddress.getHostAddress()); |
| 653 |
result.append("; "); |
| 654 |
} |
| 655 |
} catch (UnknownHostException e) { |
| 656 |
Logger.log(JavaxPacScriptParser.class, LogLevel.DEBUG, "DNS name not resolvable {0}.", host); |
| 657 |
} |
| 658 |
return result.toString(); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
public String myIpAddressEx() { |
| 668 |
return getLocalAddressOfType(Inet6Address.class); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
public String sortIpAddressList(String ipAddressList) { |
| 678 |
if (ipAddressList == null || ipAddressList.trim().length() == 0) { |
| 679 |
return ""; |
| 680 |
} |
| 681 |
try { |
| 682 |
String[] ipAddressToken = ipAddressList.split(";"); |
| 683 |
TreeMap<byte[], String> sorting = new TreeMap<byte[], String>(new Comparator<byte[]>() { |
| 684 |
public int compare(byte[] b1, byte[] b2) { |
| 685 |
if (b1.length != b2.length) { |
| 686 |
return b2.length - b1.length; |
| 687 |
} |
| 688 |
return new BigInteger(b1).compareTo(new BigInteger(b2)); |
| 689 |
} |
| 690 |
}); |
| 691 |
|
| 692 |
for (String ip : ipAddressToken) { |
| 693 |
String cleanIP = ip.trim(); |
| 694 |
sorting.put(InetAddress.getByName(cleanIP).getAddress(), cleanIP); |
| 695 |
} |
| 696 |
|
| 697 |
StringBuilder result = new StringBuilder(); |
| 698 |
for (String ip : sorting.values()) { |
| 699 |
if (result.length() > 0) { |
| 700 |
result.append(";"); |
| 701 |
} |
| 702 |
result.append(ip); |
| 703 |
} |
| 704 |
return result.toString(); |
| 705 |
} catch (Exception e) { |
| 706 |
Logger.log(JavaxPacScriptParser.class, LogLevel.DEBUG, "Cannot sort invalid IP list: {0}.", ipAddressList); |
| 707 |
return ""; |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
public String getClientVersion() { |
| 718 |
return "1.0"; |
| 719 |
} |
| 720 |
|
| 721 |
} |