Skip to content

Commit

Permalink
简单压缩,生成的文件中相邻的前缀相同前缀字符用波浪线替代,最终文件缩减又110k,目前116k。未经完全测试。
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyi7 committed Dec 5, 2024
1 parent 7b9fb28 commit d554c44
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
23 changes: 23 additions & 0 deletions gfw-pac.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def convert_cidr(cidr):
network_address = network
return hex(int(network_address))[2:]

def longest_common_prefix(str1, str2):
min_length = min(len(str1), len(str2))
for i in range(min_length):
if str1[i] != str2[i]:
return str1[:i]
return str1[:min_length]

def generate_cnip_cidrs():
""" 从文件中读取CIDR地址 """
args = parse_args()
Expand All @@ -42,6 +49,22 @@ def generate_cnip_cidrs():
for cidr in cidrs:
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)
return f"'{cidr_list}'.split(',')"

Expand Down
8 changes: 7 additions & 1 deletion gfw.pac

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions pac-template
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,14 @@ var radixTree = new RadixTree();

(function () {
debug('开始生成 Radix Tree', 'PAC文件载入开始');
lastFullPrefix = ''
for (let i=0; i<cidrs.length; i++) {
var prefix = cidrs[i];
if (prefix.substring(0, 1) !== '~') {
lastFullPrefix = prefix
} else {
prefix = lastFullPrefix.substring(0, lastFullPrefix.length-prefix.length+1) + prefix.substring(1)
}
var bits = (parseInt(prefix, 16)).toString(2);
radixTree.insert(bits);
}
Expand Down

0 comments on commit d554c44

Please sign in to comment.