# pac_demo **Repository Path**: qq2820/pac_demo ## Basic Information - **Project Name**: pac_demo - **Description**: https://gitee.com/openharmony/communication_netmanager_base/pulls/3768/files - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-09 - **Last Updated**: 2025-08-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 核心函数 ## FindProxyForURL(url, host) - 这是 PAC 脚本必须实现的函数,用于决定如何处理某个 URL 的代理请求。 参数: url:完整的请求 URL(如 http://example.com/path)。 host:从 URL 中提取的主机名(如 example.com)。 返回值:字符串,指定代理方式,例如:"DIRECT":直接连接,不使用代理。 "PROXY host:port":使用指定的 HTTP 代理。 "SOCKS host:port":使用 SOCKS 代理。 多个选项可以用分号分隔,例如 "PROXY proxy.example.com:8080; DIRECT",表示优先尝试代理,若失败则直接连接。 # 辅助函数 ## isPlainHostName(host) 检查主机名是否为简单主机名(不包含点号的本地主机名,如 localhost 或 server)。 返回值:布尔值,true 表示是简单主机名,false 表示不是。 ```javascript isPlainHostName("www.mozilla.org"); // false isPlainHostName("www"); // true ``` ## dnsDomainIs(host, domain) 检查主机名是否属于某个域名(包括子域名)。 参数:host:主机名。 domain:要检查的域名(如 .example.com)。 返回值:布尔值,true 表示主机名属于该域名。 ```javascript dnsDomainIs("www.mozilla.org", ".mozilla.org"); // true dnsDomainIs("www", ".mozilla.org"); // false ``` ## localHostOrDomainIs(host, domain) 检查主机名是否与指定域名完全匹配(仅主机名部分)或为本地主机。 参数:host:主机名。 domain:要匹配的域名(如 example.com)。 返回值:布尔值,true 表示匹配。 ```javascript localHostOrDomainIs("www.mozilla.org", "www.mozilla.org"); // true (exact match) localHostOrDomainIs("www", "www.mozilla.org"); // true (hostname match, domain not specified) localHostOrDomainIs("www.google.com", "www.mozilla.org"); // false (domain name mismatch) localHostOrDomainIs("home.mozilla.org", "www.mozilla.org"); // false (hostname mismatch) ``` ## isResolvable(host) 检查主机名是否可以通过 DNS 解析为 IP 地址。 返回值:布尔值,true 表示可解析。 ```javascript isResolvable("www.mozilla.org"); // true ``` ## isInNet(host, pattern, mask) 检查主机名的 IP 地址是否在指定网络范围内。 参数:host:主机名。 pattern:网络地址(如 10.0.0.0)。 mask:子网掩码(如 255.255.255.0)。 返回值:布尔值,true 表示主机 IP 在指定网络范围内。 ## dnsResolve(host) 解析主机名对应的 IP 地址。 返回值:字符串(IP 地址)或 null(解析失败)。 ```javascript dnsResolve("www.mozilla.org"); // returns the string "104.16.41.2" ``` ## myIpAddress() 获取本地设备的 IP 地址。 返回值:字符串(设备的 IP 地址,如 "127.0.0.1")。 注意:Android 中通常返回 "127.0.0.1",与 Firefox 行为一致。 ## shExpMatch(str, pattern) 使用 shell 风格的通配符模式匹配字符串(支持 * 和 ?)。 参数:str:要匹配的字符串(如 URL 或主机名)。 pattern:通配符模式(如 *.example.com)。 返回值:布尔值,true 表示匹配成功。 ```javascript shExpMatch("http://home.netscape.com/people/ari/index.html", "*/ari/*"); // returns true shExpMatch("http://home.netscape.com/people/montulli/index.html", "*/ari/*"); // returns false ``` ## weekdayRange(startDay, endDay, [gmt]) 检查当前时间是否在指定工作日范围内。 参数:startDay:起始工作日(如 "MON")。 endDay:结束工作日(如 "FRI")。 gmt:可选,"GMT" 表示使用 GMT 时间,否则使用本地时间。 返回值:布尔值,true 表示当前时间在范围内 ```javascript weekdayRange("MON", "FRI"); // returns true Monday through Friday (local timezone) weekdayRange("MON", "FRI", "GMT"); // returns true Monday through Friday (GMT timezone) weekdayRange("SAT"); // returns true on Saturdays local time weekdayRange("SAT", "GMT"); // returns true on Saturdays GMT time weekdayRange("FRI", "MON"); // returns true Friday and Monday only (note, order does matter!) ``` ## dateRange(...) 检查当前日期是否在指定范围内,支持多种参数格式(如天、月、年)。 参数:可以是 (day)、(day, month)、(day, month, year) 等多种组合。 返回值:布尔值,true 表示当前日期在范围内。 ```javascript dateRange(1); // returns true on the first day of each month, local timezone dateRange(1, "GMT"); // returns true on the first day of each month, GMT timezone dateRange(1, 15); // returns true on the first half of each month dateRange(24, "DEC"); // returns true on 24th of December each year dateRange("JAN", "MAR"); // returns true on the first quarter of the year dateRange(1, "JUN", 15, "AUG"); // returns true from June 1st until August 15th, each year // (including June 1st and August 15th) dateRange(1, "JUN", 1995, 15, "AUG", 1995); // returns true from June 1st, 1995, until August 15th, same year dateRange("OCT", 1995, "MAR", 1996); // returns true from October 1995 until March 1996 // (including the entire month of October 1995 and March 1996) dateRange(1995); // returns true during the entire year of 1995 dateRange(1995, 1997); // returns true from beginning of year 1995 until the end of year 1997 ``` ## timeRange(...) 检查当前时间是否在指定时间范围内。 参数:可以是 (hour)、(hour, min)、(hour, min assaying min, sec) 等。 返回值:布尔值,true 表示当前时间在范围内。 ```javascript timerange(12); // returns true from noon to 1pm timerange(12, 13); // returns true from noon to 1pm timerange(12, "GMT"); // returns true from noon to 1pm, in GMT timezone timerange(9, 17); // returns true from 9am to 5pm timerange(8, 30, 17, 00); // returns true from 8:30am to 5:00pm timerange(0, 0, 0, 0, 0, 30); // returns true between midnight and 30 seconds past midnight ``` ## myIpAddressEx() 返回本地设备的所有 IP 地址(以分号分隔的字符串)。 返回值:字符串(如 "192.168.1.100;10.0.0.1")或空字符串(失败时)。 ## dnsResolveEx(host) 解析主机名的所有 IP 地址。 返回值:字符串(以分号分隔的 IP 地址列表)或空字符串。 ## isResolvableEx(host) 检查主机名是否可解析为至少一个 IP 地址。 返回值:布尔值,true 表示可解析。 ## isInNetEx(host, pattern, mask) 类似于 isInNet,但支持更复杂的网络范围检查。 返回值:布尔值。 ## sortIpAddressList(ipList) 对 IP 地址列表进行排序。 返回值:排序后的 IP 地址列表(字符串)或 false(失败时)。 ## dnsDomainLevels(host) host 从 URL 中得到的主机名。 返回主机名中 DNS 域名级别的整数数量(域名中包含点的个数)。