Compare commits
31 Commits
v20241024.
...
v20250417.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e084116209 | ||
|
|
64f75179f5 | ||
|
|
948a002d73 | ||
|
|
046401ec20 | ||
|
|
9cf413c43c | ||
|
|
601b549b31 | ||
|
|
6b65baa275 | ||
|
|
8c52b77a03 | ||
|
|
02f24366f5 | ||
|
|
419485dde2 | ||
|
|
2a1c178d3a | ||
|
|
ab03a97789 | ||
|
|
ac4fb7b19b | ||
|
|
f810a66b0c | ||
|
|
e6129a6dd4 | ||
|
|
f1df8acaba | ||
|
|
7add95746e | ||
|
|
9622bd4456 | ||
|
|
3949a407b0 | ||
|
|
8b51175a34 | ||
|
|
bdc53dd5ca | ||
|
|
f3d1085c2d | ||
|
|
932d427c01 | ||
|
|
d554c444cc | ||
|
|
7b9fb28619 | ||
|
|
74e6d0aa47 | ||
|
|
509f516d1d | ||
|
|
4ae3541f20 | ||
|
|
58d793c13c | ||
|
|
340754f759 | ||
|
|
d36a5097f5 |
13
.github/workflows/auto-generate-pac.yml
vendored
13
.github/workflows/auto-generate-pac.yml
vendored
@@ -20,13 +20,25 @@ jobs:
|
|||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Check if gfw.pac is in the commit
|
||||||
|
id: check-file
|
||||||
|
run: |
|
||||||
|
if git diff --name-only HEAD^ HEAD | grep -q 'gfw\.pac'; then
|
||||||
|
echo "file-exists=true" >> $GITHUB_ENV
|
||||||
|
else
|
||||||
|
echo "file-exists=false" >> $GITHUB_ENV
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Download the latest cn.txt
|
- name: Download the latest cn.txt
|
||||||
|
if: env.file-exists == 'false'
|
||||||
run: curl -L https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/cn.txt -o cidrs-cn.txt
|
run: curl -L https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/cn.txt -o cidrs-cn.txt
|
||||||
|
|
||||||
- name: Run gfw-pac.py script
|
- name: Run gfw-pac.py script
|
||||||
|
if: env.file-exists == 'false'
|
||||||
run: ./gfw-pac.py -f gfw.pac -p "PROXY 127.0.0.1:3128" --proxy-domains=proxy-domains.txt --direct-domains=direct-domains.txt --localtld-domains=local-tlds.txt --ip-file=cidrs-cn.txt
|
run: ./gfw-pac.py -f gfw.pac -p "PROXY 127.0.0.1:3128" --proxy-domains=proxy-domains.txt --direct-domains=direct-domains.txt --localtld-domains=local-tlds.txt --ip-file=cidrs-cn.txt
|
||||||
|
|
||||||
- name: Commit and push changes
|
- name: Commit and push changes
|
||||||
|
if: env.file-exists == 'false'
|
||||||
run: |
|
run: |
|
||||||
last_commit_message=$(git log -1 --pretty=%B)
|
last_commit_message=$(git log -1 --pretty=%B)
|
||||||
commit_message=$([[ "${{ github.event_name }}" == "schedule" ]] && echo "定期更新数据并生成 gfw.pac" || echo "自动生成 gfw.pac: $last_commit_message")
|
commit_message=$([[ "${{ github.event_name }}" == "schedule" ]] && echo "定期更新数据并生成 gfw.pac" || echo "自动生成 gfw.pac: $last_commit_message")
|
||||||
@@ -37,6 +49,7 @@ jobs:
|
|||||||
git push
|
git push
|
||||||
|
|
||||||
- name: Trigger Auto-Release
|
- name: Trigger Auto-Release
|
||||||
|
if: env.file-exists == 'false'
|
||||||
uses: benc-uk/workflow-dispatch@v1
|
uses: benc-uk/workflow-dispatch@v1
|
||||||
with:
|
with:
|
||||||
workflow: Auto Generate Release
|
workflow: Auto Generate Release
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -43,3 +43,5 @@ $RECYCLE.BIN/
|
|||||||
|
|
||||||
# Windows shortcuts
|
# Windows shortcuts
|
||||||
*.lnk
|
*.lnk
|
||||||
|
|
||||||
|
test.js
|
||||||
6322
cidrs-cn.txt
6322
cidrs-cn.txt
File diff suppressed because it is too large
Load Diff
@@ -132,6 +132,7 @@ ocsp.digicert.cn
|
|||||||
ocsp.dcocsp.cn
|
ocsp.dcocsp.cn
|
||||||
api.onedrive.com
|
api.onedrive.com
|
||||||
storage.live.com
|
storage.live.com
|
||||||
|
gr.global.aa-rt.sharepoint.com
|
||||||
skyapi.live.net
|
skyapi.live.net
|
||||||
roaming.officeapps.live.com
|
roaming.officeapps.live.com
|
||||||
blob.core.windows.net
|
blob.core.windows.net
|
||||||
|
|||||||
32
gfw-pac.py
32
gfw-pac.py
@@ -27,16 +27,18 @@ def parse_args():
|
|||||||
def convert_cidr(cidr):
|
def convert_cidr(cidr):
|
||||||
if '/' in cidr:
|
if '/' in cidr:
|
||||||
network = ipaddress.ip_network(cidr.strip(), strict=False)
|
network = ipaddress.ip_network(cidr.strip(), strict=False)
|
||||||
network_address = network.network_address
|
network_address = int(network.network_address) >> (network.max_prefixlen - network.prefixlen)
|
||||||
prefixlen = network.prefixlen
|
|
||||||
else:
|
else:
|
||||||
network = ipaddress.ip_address(cidr.strip())
|
network = ipaddress.ip_address(cidr.strip())
|
||||||
network_address = network
|
network_address = network
|
||||||
prefixlen = network.max_prefixlen
|
return hex(int(network_address))[2:]
|
||||||
if network.version == 4:
|
|
||||||
return hex(int(network_address))[2:] + '/' + str(prefixlen)
|
def longest_common_prefix(str1, str2):
|
||||||
else:
|
min_length = min(len(str1), len(str2))
|
||||||
return network.compressed
|
for i in range(min_length):
|
||||||
|
if str1[i] != str2[i]:
|
||||||
|
return str1[:i]
|
||||||
|
return str1[:min_length]
|
||||||
|
|
||||||
def generate_cnip_cidrs():
|
def generate_cnip_cidrs():
|
||||||
""" 从文件中读取CIDR地址 """
|
""" 从文件中读取CIDR地址 """
|
||||||
@@ -47,6 +49,22 @@ def generate_cnip_cidrs():
|
|||||||
for cidr in cidrs:
|
for cidr in cidrs:
|
||||||
converted_cidrs.append(convert_cidr(cidr))
|
converted_cidrs.append(convert_cidr(cidr))
|
||||||
|
|
||||||
|
converted_cidrs.sort(key=lambda x: (len(x), x), reverse=False)
|
||||||
|
converted_cidrs_clone = converted_cidrs[:]
|
||||||
|
|
||||||
|
lastFullCidr = ''
|
||||||
|
for i in range(len(converted_cidrs)):
|
||||||
|
prevCidr = converted_cidrs_clone[i-1] if i > 0 else ''
|
||||||
|
currentCidr = converted_cidrs[i]
|
||||||
|
if len(prevCidr) != len(currentCidr):
|
||||||
|
lastFullCidr = currentCidr
|
||||||
|
continue
|
||||||
|
prefix = longest_common_prefix(lastFullCidr, currentCidr)
|
||||||
|
if len(prefix) < len(lastFullCidr)//1.2:
|
||||||
|
lastFullCidr = currentCidr
|
||||||
|
continue
|
||||||
|
converted_cidrs[i] = '~' + currentCidr[len(prefix):]
|
||||||
|
|
||||||
cidr_list = ','.join(converted_cidrs)
|
cidr_list = ','.join(converted_cidrs)
|
||||||
return f"'{cidr_list}'.split(',')"
|
return f"'{cidr_list}'.split(',')"
|
||||||
|
|
||||||
|
|||||||
70
pac-template
70
pac-template
@@ -10,35 +10,45 @@ var localTlds = __LOCAL_TLDS__;
|
|||||||
|
|
||||||
var cidrs = __CIDRS__;
|
var cidrs = __CIDRS__;
|
||||||
|
|
||||||
var hasOwnProperty = Object.hasOwnProperty;
|
|
||||||
|
|
||||||
function isIpAddress(ip) {
|
function isIpAddress(ip) {
|
||||||
return /^\d{1,3}(\.\d{1,3}){3}$/.test(ip) || /^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$/.test(ip);
|
return /^\d{1,3}(\.\d{1,3}){3}$/.test(ip) || /^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$/.test(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
function RadixTree() {
|
function RadixTree() {
|
||||||
this.root = {};
|
this.root = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
RadixTree.prototype.insert = function(string) {
|
RadixTree.prototype.insert = function(string) {
|
||||||
var node = this.root;
|
var node = this.root;
|
||||||
for (var i = 0; i < string.length; i++) {
|
for (var i = 0; i < string.length; i++) {
|
||||||
var char = string[i];
|
var char = string[i];
|
||||||
if (!node[char]) {
|
if (!node.has(char)) {
|
||||||
node[char] = {};
|
node.set(char, new Map());
|
||||||
}
|
}
|
||||||
node = node[char];
|
node = node.get(char);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
RadixTree.prototype.to_list = function() {
|
RadixTree.prototype.search = function(string) {
|
||||||
return this.root;
|
var currentNode = this.root;
|
||||||
};
|
var isLastNode = false;
|
||||||
|
for (var i=0; i < string.length; i++) {
|
||||||
|
var char = string[i];
|
||||||
|
if (currentNode.has(char)) {
|
||||||
|
currentNode = currentNode.get(char);
|
||||||
|
isLastNode = currentNode.size === 0;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isLastNode;
|
||||||
|
}
|
||||||
|
|
||||||
function ipToBinary(ip) {
|
function ipToBinary(ip) {
|
||||||
|
var bin = ''
|
||||||
// Check if it's IPv4
|
// Check if it's IPv4
|
||||||
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(ip)) {
|
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(ip)) {
|
||||||
return ip.split('.').map(function(num) {
|
bin = ip.split('.').map(function(num) {
|
||||||
return ("00000000" + parseInt(num, 10).toString(2)).slice(-8);
|
return ("00000000" + parseInt(num, 10).toString(2)).slice(-8);
|
||||||
}).join('');
|
}).join('');
|
||||||
} else if (/^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$/.test(ip)) {
|
} else if (/^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$/.test(ip)) {
|
||||||
@@ -54,25 +64,11 @@ function ipToBinary(ip) {
|
|||||||
var fullAddress = left.concat(Array(zeroGroups + 1).join('0').split('')).concat(right);
|
var fullAddress = left.concat(Array(zeroGroups + 1).join('0').split('')).concat(right);
|
||||||
|
|
||||||
// Convert each group to binary and pad to 16 bits
|
// Convert each group to binary and pad to 16 bits
|
||||||
return fullAddress.map(function(group) {
|
bin = fullAddress.map(function(group) {
|
||||||
return ("0000000000000000" + parseInt(group || '0', 16).toString(2)).slice(-16);
|
return ("0000000000000000" + parseInt(group || '0', 16).toString(2)).slice(-16);
|
||||||
}).join('');
|
}).join('');
|
||||||
}
|
}
|
||||||
}
|
return bin.replace(/^0+/, '');
|
||||||
|
|
||||||
function searchRadixTree(bits) {
|
|
||||||
var currentNode = radixTree;
|
|
||||||
var isLastNode = false;
|
|
||||||
for (var i=0; i<bits.length; i++) {
|
|
||||||
var char = bits[i];
|
|
||||||
if (currentNode[char]) {
|
|
||||||
currentNode = currentNode[char];
|
|
||||||
isLastNode = Object.keys(currentNode).length === 0;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return isLastNode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isInDirectDomain(host) {
|
function isInDirectDomain(host) {
|
||||||
@@ -142,7 +138,7 @@ function FindProxyForURL(url, host) {
|
|||||||
} else if (isPrivateIp(ip)) {
|
} else if (isPrivateIp(ip)) {
|
||||||
debug('域名解析后命中私有 IP 地址', host, ip);
|
debug('域名解析后命中私有 IP 地址', host, ip);
|
||||||
return direct;
|
return direct;
|
||||||
} else if (searchRadixTree(ipToBinary(ip))) {
|
} else if (radixTree.search(ipToBinary(ip))) {
|
||||||
debug('匹配到直连IP', host, ip);
|
debug('匹配到直连IP', host, ip);
|
||||||
return direct;
|
return direct;
|
||||||
}
|
}
|
||||||
@@ -152,7 +148,7 @@ function FindProxyForURL(url, host) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var allowAlert = true
|
var allowAlert = true
|
||||||
function debug(msg, host, ip) {
|
function debug(msg, host='', ip='') {
|
||||||
if (!allowAlert) {
|
if (!allowAlert) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -166,19 +162,17 @@ function debug(msg, host, ip) {
|
|||||||
var radixTree = new RadixTree();
|
var radixTree = new RadixTree();
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
var startTime = new Date().getMilliseconds();
|
debug('开始生成 Radix Tree', 'PAC文件载入开始');
|
||||||
debug('开始生成 Radix Tree', 'PAC文件载入开始', startTime.toString());
|
lastFullPrefix = ''
|
||||||
for (let i=0; i<cidrs.length; i++) {
|
for (let i=0; i<cidrs.length; i++) {
|
||||||
var cidr = cidrs[i];
|
var prefix = cidrs[i];
|
||||||
var [ip, prefixLen] = cidr.split('/');
|
if (prefix.substring(0, 1) !== '~') {
|
||||||
if (!cidr.includes(':')) {
|
lastFullPrefix = prefix
|
||||||
var ip = ip.match(/.{1,2}/g).map(function(byte) {
|
} else {
|
||||||
return parseInt(byte, 16);
|
prefix = lastFullPrefix.substring(0, lastFullPrefix.length-prefix.length+1) + prefix.substring(1)
|
||||||
}).join('.');
|
|
||||||
}
|
}
|
||||||
var bits = ipToBinary(ip).slice(0, prefixLen);
|
var bits = (parseInt(prefix, 16)).toString(2);
|
||||||
radixTree.insert(bits);
|
radixTree.insert(bits);
|
||||||
}
|
}
|
||||||
radixTree = radixTree.to_list();
|
|
||||||
debug('Radix Tree 已生成', 'PAC文件载入完毕', cidrs.length.toString()+'个CIDR条目');
|
debug('Radix Tree 已生成', 'PAC文件载入完毕', cidrs.length.toString()+'个CIDR条目');
|
||||||
})();
|
})();
|
||||||
Reference in New Issue
Block a user