1+ // Helper function to check if a string is an IP address
2+ function isIP ( host ) {
3+ var parts = host . split ( "." ) ;
4+ if ( parts . length !== 4 ) {
5+ return false ;
6+ }
7+ for ( var i = 0 ; i < 4 ; i ++ ) {
8+ var num = parseInt ( parts [ i ] , 10 ) ;
9+ if ( isNaN ( num ) || num < 0 || num > 255 ) {
10+ return false ;
11+ }
12+ }
13+ return true ;
14+ }
15+
16+ // Helper function to check if IP is in private network range
17+ function isPrivateIP ( host ) {
18+ if ( ! isIP ( host ) ) {
19+ return false ;
20+ }
21+ var parts = host . split ( "." ) ;
22+ var first = parseInt ( parts [ 0 ] , 10 ) ;
23+ var second = parseInt ( parts [ 1 ] , 10 ) ;
24+
25+ // 10.0.0.0/8
26+ if ( first === 10 ) {
27+ return true ;
28+ }
29+ // 172.16.0.0/12
30+ if ( first === 172 && second >= 16 && second <= 31 ) {
31+ return true ;
32+ }
33+ // 192.168.0.0/16
34+ if ( first === 192 && second === 168 ) {
35+ return true ;
36+ }
37+ // 127.0.0.0/8 (localhost)
38+ if ( first === 127 ) {
39+ return true ;
40+ }
41+ return false ;
42+ }
43+
44+ function FindProxyForURL ( url , host ) {
45+ // Enable logging (set to false to disable)
46+ var enableLog = true ;
47+
48+ // Log function for debugging using alert
49+ function log ( message ) {
50+ if ( enableLog ) {
51+ alert ( "PAC: " + message ) ;
52+ }
53+ }
54+
55+ log ( "Checking URL: " + url + ", Host: " + host ) ;
56+
57+ // Direct connect addresses and domains (whitelist)
58+ var direct_list = [
59+ "localhost" ,
60+ "*.cn" , // All .cn domains direct connect (use with caution)
61+ "*.baidu.com" ,
62+ "*.qq.com" ,
63+ "*.taobao.com"
64+ ] ;
65+
66+ // Proxy addresses and domains (proxy list)
67+ var proxy_list = [
68+ "google.com" ,
69+ "*.google.com" ,
70+ "*.youtube.com" ,
71+ "*.facebook.com" ,
72+ "*.twitter.com"
73+ ] ;
74+
75+ // 1. Check if host is localhost or private IP, then direct connect
76+ if ( host === "localhost" || host === "127.0.0.1" || isPrivateIP ( host ) ) {
77+ log ( "Matched private/localhost IP: " + host + " -> DIRECT" ) ;
78+ return "DIRECT" ;
79+ }
80+
81+ // 2. Check if host is in direct list (whitelist), then direct connect
82+ for ( var i = 0 ; i < direct_list . length ; i ++ ) {
83+ if ( shExpMatch ( host , direct_list [ i ] ) ) {
84+ log ( "Matched direct list pattern: " + direct_list [ i ] + " for " + host + " -> DIRECT" ) ;
85+ return "DIRECT" ;
86+ }
87+ }
88+
89+ // 3. Check if host is in proxy list, then use proxy
90+ for ( var i = 0 ; i < proxy_list . length ; i ++ ) {
91+ if ( shExpMatch ( host , proxy_list [ i ] ) ) {
92+ log ( "Matched proxy list pattern: " + proxy_list [ i ] + " for " + host + " -> PROXY" ) ;
93+ return "SOCKS5 127.0.0.1:1088" ; // This port should match your sslocal listening port
94+ }
95+ }
96+
97+ // 4. Default rule: all other traffic direct connect
98+ log ( "No match found for " + host + " -> DIRECT (default)" ) ;
99+ return "DIRECT" ;
100+ }
0 commit comments