Class PacScriptMethods
java.lang.Object
com.github.markusbernhardt.proxy.selector.pac.PacScriptMethods
- All Implemented Interfaces:
ScriptMethods
Implementation of PAC JavaScript functions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Calendar
private static final String
private static final String
private static final String
private static final BigInteger
private static final BigInteger
private String
private String
private static final String
private static final String
static final String
private static final String
private static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
dateRange
(Object day1, Object month1, Object year1, Object day2, Object month2, Object year2, Object gmt) Only the first parameter is mandatory.boolean
dnsDomainIs
(String host, String domain) Tests if an URL is in a given domain.int
dnsDomainLevels
(String host) Returns the number of DNS domain levels (number of dots) in the host name.dnsResolve
(String host) Resolves the given DNS host name into an IP address, and returns it in the dot separated format as a string.dnsResolveEx
(String host) dnsResolveExgetClientVersionprivate Calendar
getCurrentTime
(boolean useGmt) Gets a calendar set to the current time.private String
getLocalAddressOfType
(Class<? extends InetAddress> cl) Get the current IP address of the computer.boolean
Returns true if the IP address of the host matches the specified IP address pattern.boolean
isInNetEx Implementation from http://fhanik.blogspot.ch/2013/11/ip-magic-check-if-ipv6-address-is.htmlboolean
isPlainHostName
(String host) isPlainHostNameboolean
isResolvable
(String host) Tries to resolve the host name.boolean
isResolvableEx
(String host) isResolvableExboolean
localHostOrDomainIs
(String host, String domain) Is true if the host name matches exactly the specified host name, or if there is no domain name part in the host name, but the unqualified host name matches.Returns the IP address of the host that the process is running on, as a string in the dot-separated integer format.myIpAddressExprivate void
parseDateParam
(Map<String, Integer> params, Object value) Try to guess the type of the given parameter and put it into the params map.private long
parseIpAddressToLong
(String address) Convert a string representation of a IP to a long.void
setCurrentTime
(Calendar cal) Sets a calendar with the current time.boolean
shExpMatch
(String str, String shexp) Returns true if the string matches the specified shell expression.sortIpAddressList
(String ipAddressList) sortIpAddressListboolean
timeRange
(Object hour1, Object min1, Object sec1, Object hour2, Object min2, Object sec2, Object gmt) Some parameters can be left out therefore the meaning of the parameters changes.boolean
weekdayRange
(String wd1, String wd2, String gmt) Only the first parameter is mandatory.
-
Field Details
-
OVERRIDE_LOCAL_IP
- See Also:
-
ipAddress
-
ipAddressEx
-
GMT
- See Also:
-
DAYS
-
MONTH
-
DAY1
- See Also:
-
DAY2
- See Also:
-
MONTH1
- See Also:
-
MONTH2
- See Also:
-
YEAR1
- See Also:
-
YEAR2
- See Also:
-
currentTime
-
HIGH_32_INT
-
HIGH_128_INT
-
-
Constructor Details
-
PacScriptMethods
public PacScriptMethods()Constructor
-
-
Method Details
-
isPlainHostName
isPlainHostName- Specified by:
isPlainHostName
in interfaceScriptMethods
- See Also:
-
dnsDomainIs
Tests if an URL is in a given domain.- Specified by:
dnsDomainIs
in interfaceScriptMethods
- Parameters:
host
- is the host name from the URL.domain
- is the domain name to test the host name against.- Returns:
- true if the domain of host name matches.
-
localHostOrDomainIs
Is true if the host name matches exactly the specified host name, or if there is no domain name part in the host name, but the unqualified host name matches.- Specified by:
localHostOrDomainIs
in interfaceScriptMethods
- Parameters:
host
- the host name from the URL.domain
- fully qualified host name with domain to match against.- Returns:
- true if matches else false.
-
isResolvable
Tries to resolve the host name. Returns true if succeeds.- Specified by:
isResolvable
in interfaceScriptMethods
- Parameters:
host
- is the host name from the URL.- Returns:
- true if resolvable else false.
-
isInNet
Returns true if the IP address of the host matches the specified IP address pattern. Pattern and mask specification is done the same way as for SOCKS configuration. Example: isInNet(host, "198.95.0.0", "255.255.0.0") is true if the IP address of the host matches 198.95.*.*.- Specified by:
isInNet
in interfaceScriptMethods
- Parameters:
host
- a DNS host name, or IP address. If a host name is passed, it will be resolved into an IP address by this function.pattern
- an IP address pattern in the dot-separated format.mask
- mask for the IP address pattern informing which parts of the IP address should be matched against. 0 means ignore, 255 means match.- Returns:
- true if it matches else false.
-
parseIpAddressToLong
Convert a string representation of a IP to a long.- Parameters:
address
- to convert.- Returns:
- the address as long.
-
dnsResolve
Resolves the given DNS host name into an IP address, and returns it in the dot separated format as a string.- Specified by:
dnsResolve
in interfaceScriptMethods
- Parameters:
host
- the host to resolve.- Returns:
- the resolved IP, empty string if not resolvable.
-
myIpAddress
Returns the IP address of the host that the process is running on, as a string in the dot-separated integer format. IP is cached during the pac processing time to avoid requeting it too often.- Specified by:
myIpAddress
in interfaceScriptMethods
- Returns:
- an IP as string.
-
getLocalAddressOfType
Get the current IP address of the computer. This will return the first address of the first network interface that is a "real" IP address of the given type.- Parameters:
cl
- the type of address we are searching for.- Returns:
- the address as string or "" if not found.
-
dnsDomainLevels
Returns the number of DNS domain levels (number of dots) in the host name.- Specified by:
dnsDomainLevels
in interfaceScriptMethods
- Parameters:
host
- is the host name from the URL.- Returns:
- number of DNS domain levels.
-
shExpMatch
Returns true if the string matches the specified shell expression. Actually, currently the patterns are shell expressions, not regular expressions.- Specified by:
shExpMatch
in interfaceScriptMethods
- Parameters:
str
- is any string to compare (e.g. the URL, or the host name).shexp
- is a shell expression to compare against.- Returns:
- true if the string matches, else false.
-
weekdayRange
Only the first parameter is mandatory. Either the second, the third, or both may be left out. If only one parameter is present, the function yields a true value on the weekday that the parameter represents. If the string "GMT" is specified as a second parameter, times are taken to be in GMT, otherwise in local time zone. If both wd1 and wd2 are defined, the condition is true if the current weekday is in between those two weekdays. Bounds are inclusive. If the "GMT" parameter is specified, times are taken to be in GMT, otherwise the local time zone is used.- Specified by:
weekdayRange
in interfaceScriptMethods
- Parameters:
wd1
- weekday 1 is one of SUN MON TUE WED THU FRI SATwd2
- weekday 2 is one of SUN MON TUE WED THU FRI SATgmt
- "GMT" for gmt time format else "undefined"- Returns:
- true if current day matches the criteria.
-
setCurrentTime
Sets a calendar with the current time. If this is set all date and time based methods will use this calendar to determine the current time instead of the real time. This is only be used by unit tests and is not part of the public API.- Parameters:
cal
- a Calendar to set.
-
getCurrentTime
Gets a calendar set to the current time. This is used by the date and time based methods.- Parameters:
useGmt
- flag to indicate if the calendar is to be created in GMT time or local time.- Returns:
- a Calendar set to the current time.
-
dateRange
public boolean dateRange(Object day1, Object month1, Object year1, Object day2, Object month2, Object year2, Object gmt) Only the first parameter is mandatory. All other parameters can be left out therefore the meaning of the parameters changes. The method definition shows the version with the most possible parameters filled. The real meaning of the parameters is guessed from it's value. If "from" and "to" are specified then the bounds are inclusive. If the "GMT" parameter is specified, times are taken to be in GMT, otherwise the local time zone is used.- Specified by:
dateRange
in interfaceScriptMethods
- Parameters:
day1
- is the day of month between 1 and 31 (as an integer).month1
- one of JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DECyear1
- is the full year number, for example 1995 (but not 95). Integer.day2
- is the day of month between 1 and 31 (as an integer).month2
- one of JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DECyear2
- is the full year number, for example 1995 (but not 95). Integer.gmt
- "GMT" for gmt time format else "undefined"- Returns:
- true if the current date matches the given range.
-
parseDateParam
Try to guess the type of the given parameter and put it into the params map.- Parameters:
params
- a map to put the parsed parameters into.value
- to parse and specify the type for.
-
timeRange
public boolean timeRange(Object hour1, Object min1, Object sec1, Object hour2, Object min2, Object sec2, Object gmt) Some parameters can be left out therefore the meaning of the parameters changes. The method definition shows the version with the most possible parameters filled. The real meaning of the parameters is guessed from it's value. If "from" and "to" are specified then the bounds are inclusive. If the "GMT" parameter is specified, times are taken to be in GMT, otherwise the local time zone is used.
timeRange(hour) timeRange(hour1, hour2) timeRange(hour1, min1, hour2, min2) timeRange(hour1, min1, sec1, hour2, min2, sec2) timeRange(hour1, min1, sec1, hour2, min2, sec2, gmt)
- Specified by:
timeRange
in interfaceScriptMethods
- Parameters:
hour1
- is the hour from 0 to 23. (0 is midnight, 23 is 11 pm.)min1
- minutes from 0 to 59.sec1
- seconds from 0 to 59.hour2
- is the hour from 0 to 23. (0 is midnight, 23 is 11 pm.)min2
- minutes from 0 to 59.sec2
- seconds from 0 to 59.gmt
- "GMT" for gmt time format else "undefined"- Returns:
- true if the current time matches the given range.
-
isResolvableEx
isResolvableEx- Specified by:
isResolvableEx
in interfaceScriptMethods
- Parameters:
host
- is the host name from the URL.- Returns:
- true if resolvable else false.
- See Also:
-
isInNetEx
isInNetEx Implementation from http://fhanik.blogspot.ch/2013/11/ip-magic-check-if-ipv6-address-is.html- Specified by:
isInNetEx
in interfaceScriptMethods
- Parameters:
ipOrHost
- an IP4 or IP6 addresscidr
- A string containing colon delimited IP prefix with top n bits specified in the bit field (i.e. 3ffe:8311:ffff::/48 or 123.112.0.0/16).- Returns:
- true if the host is in the given subnet, else false.
- See Also:
-
dnsResolveEx
dnsResolveEx- Specified by:
dnsResolveEx
in interfaceScriptMethods
- Parameters:
host
- the host to resolve- Returns:
- a semicolon separated list of IP6 and IP4 addresses the host name resolves to, empty string if not resolvable.
- See Also:
-
myIpAddressEx
myIpAddressEx- Specified by:
myIpAddressEx
in interfaceScriptMethods
- Returns:
- the list, empty string if not available.
- See Also:
-
sortIpAddressList
sortIpAddressList- Specified by:
sortIpAddressList
in interfaceScriptMethods
- Parameters:
ipAddressList
- the address list.- Returns:
- the sorted list, empty string if sort is not possible
- See Also:
-
getClientVersion
getClientVersion- Specified by:
getClientVersion
in interfaceScriptMethods
- Returns:
- the extension version, currently 1.0
- See Also:
-