refactor: pac转移配置

pull/88/head
xiaojunnuo 2021-08-11 11:21:53 +08:00
parent 09b2e65adb
commit 8b0f2d0f5d
8 changed files with 3391 additions and 23161 deletions

View File

@ -39,23 +39,23 @@ module.exports = {
'/.*/.*/releases/download/': {
redirect: 'download.fastgit.org',
desc: 'release文件加速下载跳转地址',
sni: 'no.sni'
sni: 'baidu.com'
},
'/.*/.*/archive/': {
redirect: 'download.fastgit.org',
sni: 'no.sni'
sni: 'baidu.com'
},
'/.*/.*/blame/': {
redirect: 'hub.fastgit.org',
sni: 'no.sni'
sni: 'baidu.com'
},
'^/[^/]+/[^/]+(/releases(/.*)?)?$': {
script: [
'github'
],
desc: 'clone加速复制链接脚本',
sni: 'no.sni'
sni: 'baidu.com'
},
'/.*': {
proxy: 'github.com',

View File

@ -16,8 +16,9 @@ module.exports = {
},
pac: {
enabled: true,
update: [
'https://gitlab.com/gfwlist/gfwlist/raw/master/gfwlist.txt'
]
// update: [
// 'https://gitlab.com/gfwlist/gfwlist/raw/master/gfwlist.txt'
// ],
customPacFilePath: './extra/pac/pac.txt'
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,14 @@
const url = require('url')
const pac = require('./source/pac')
const matchUtil = require('../../../utils/util.match')
let pacClient = null
function matched (hostname, regexpMap) {
const ret1 = matchUtil.matchHostname(regexpMap, hostname)
if (ret1) {
return true
}
const ret = pac.FindProxyForURL('https://' + hostname, hostname)
const ret = pacClient.FindProxyForURL('https://' + hostname, hostname)
if (ret && ret.indexOf('PROXY ') === 0) {
return true
}
@ -18,6 +19,11 @@ module.exports = function createOverWallIntercept (overWallConfig) {
if (!overWallConfig || overWallConfig.enabled !== true) {
return null
}
if (overWallConfig.pac) {
// 初始化pac
pacClient = pac.createPacClient(overWallConfig.pac.customPacFilePath)
}
let server = overWallConfig.server
let keys = Object.keys(server)
if (keys.length === 0) {

View File

@ -1,8 +1,27 @@
var __PROXY__ = 'PROXY 127.0.0.1:1080;'
var __USERRULES__ = []
const getRules = function () {
let text = require('./pac.txt.js')
const fs = require('fs')
const path = require('path')
const log = require('../../../../utils/util.log')
function createPacClient (pacFilePath) {
var __PROXY__ = 'PROXY 127.0.0.1:1080;'
var __USERRULES__ = []
function readFile (location) {
try {
const filePath = path.resolve(location)
log.debug('read pac path:', filePath)
return fs.readFileSync(location).toString()
} catch (e) {
log.error('读取pac失败')
return ''
}
}
const getRules = function (pacFilePath) {
let text = readFile(pacFilePath)
if (text.indexOf('!---------------------EOF') === -1) {
text = Buffer.from(text, 'base64').toString()
}
const rules = []
const arr = text.split('\n')
for (const line of arr) {
@ -13,63 +32,64 @@ const getRules = function () {
rules.push(row)
}
return rules
}
var __RULES__ = getRules()
/* eslint-disable */
}
var __RULES__ = getRules(pacFilePath)
/* eslint-disable */
// Was generated by gfwlist2pac in precise mode
// https://github.com/clowwindy/gfwlist2pac
// 2019-10-06: More 'javascript' way to interaction with main program
// 2019-02-08: Updated to support shadowsocks-windows user rules.
var proxy = __PROXY__;
var userrules = [];
var rules = [];
var proxy = __PROXY__;
var userrules = [];
var rules = [];
// convert to abp grammar
for (var i = 0; i < __RULES__.length; i++) {
for (var i = 0; i < __RULES__.length; i++) {
var s = __RULES__[i];
if (s.substring(0, 2) == "||") s += "^";
rules.push(s);
}
}
for (var i = 0; i < __USERRULES__.length; i++) {
for (var i = 0; i < __USERRULES__.length; i++) {
var s = __USERRULES__[i];
if (s.substring(0, 2) == "||") s += "^";
userrules.push(s);
}
}
/*
* This file is part of Adblock Plus <http://adblockplus.org/>,
* Copyright (C) 2006-2014 Eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file is part of Adblock Plus <http://adblockplus.org/>,
* Copyright (C) 2006-2014 Eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
function createDict() {
function createDict() {
var result = {};
result.__proto__ = null;
return result;
}
}
function getOwnPropertyDescriptor(obj, key) {
function getOwnPropertyDescriptor(obj, key) {
if (obj.hasOwnProperty(key)) {
return obj[key];
}
return null;
}
}
function extend(subclass, superclass, definition) {
function extend(subclass, superclass, definition) {
if (Object.__proto__) {
definition.__proto__ = superclass.prototype;
subclass.prototype = definition;
@ -85,25 +105,25 @@ function extend(subclass, superclass, definition) {
}
}
}
}
}
function Filter(text) {
function Filter(text) {
this.text = text;
this.subscriptions = [];
}
}
Filter.prototype = {
Filter.prototype = {
text: null,
subscriptions: null,
toString: function () {
return this.text;
}
};
Filter.knownFilters = createDict();
Filter.elemhideRegExp = /^([^\/\*\|\@"!]*?)#(\@)?(?:([\w\-]+|\*)((?:\([\w\-]+(?:[$^*]?=[^\(\)"]*)?\))*)|#([^{}]+))$/;
Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*)?$/;
Filter.optionsRegExp = /\$(~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*)$/;
Filter.fromText = function (text) {
};
Filter.knownFilters = createDict();
Filter.elemhideRegExp = /^([^\/\*\|\@"!]*?)#(\@)?(?:([\w\-]+|\*)((?:\([\w\-]+(?:[$^*]?=[^\(\)"]*)?\))*)|#([^{}]+))$/;
Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*)?$/;
Filter.optionsRegExp = /\$(~?[\w\-]+(?:=[^,\s]+)?(?:,~?[\w\-]+(?:=[^,\s]+)?)*)$/;
Filter.fromText = function (text) {
if (text in Filter.knownFilters) {
return Filter.knownFilters[text];
}
@ -115,29 +135,29 @@ Filter.fromText = function (text) {
}
Filter.knownFilters[ret.text] = ret;
return ret;
};
};
function InvalidFilter(text, reason) {
function InvalidFilter(text, reason) {
Filter.call(this, text);
this.reason = reason;
}
}
extend(InvalidFilter, Filter, {
extend(InvalidFilter, Filter, {
reason: null
});
});
function CommentFilter(text) {
function CommentFilter(text) {
Filter.call(this, text);
}
}
extend(CommentFilter, Filter, {});
extend(CommentFilter, Filter, {});
function ActiveFilter(text, domains) {
function ActiveFilter(text, domains) {
Filter.call(this, text);
this.domainSource = domains;
}
}
extend(ActiveFilter, Filter, {
extend(ActiveFilter, Filter, {
domainSource: null,
domainSeparator: null,
ignoreTrailingDot: true,
@ -232,9 +252,9 @@ extend(ActiveFilter, Filter, {
}
return true;
}
});
});
function RegExpFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys) {
function RegExpFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys) {
ActiveFilter.call(this, text, domains, sitekeys);
if (contentType != null) {
this.contentType = contentType;
@ -254,9 +274,9 @@ function RegExpFilter(text, regexpSource, contentType, matchCase, domains, third
} else {
this.regexpSource = regexpSource;
}
}
}
extend(RegExpFilter, ActiveFilter, {
extend(RegExpFilter, ActiveFilter, {
domainSourceIsUpperCase: true,
length: 1,
domainSeparator: "|",
@ -294,9 +314,9 @@ extend(RegExpFilter, ActiveFilter, {
}
return false;
}
});
RegExpFilter.prototype["0"] = "#this";
RegExpFilter.fromText = function (text) {
});
RegExpFilter.prototype["0"] = "#this";
RegExpFilter.fromText = function (text) {
var blocking = true;
var origText = text;
if (text.indexOf("@@") == 0) {
@ -369,8 +389,8 @@ RegExpFilter.fromText = function (text) {
} catch (e) {
return new InvalidFilter(origText, e);
}
};
RegExpFilter.typeMap = {
};
RegExpFilter.typeMap = {
OTHER: 1,
SCRIPT: 2,
IMAGE: 4,
@ -388,29 +408,29 @@ RegExpFilter.typeMap = {
BACKGROUND: 4,
POPUP: 268435456,
ELEMHIDE: 1073741824
};
RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.ELEMHIDE | RegExpFilter.typeMap.POPUP);
};
RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.ELEMHIDE | RegExpFilter.typeMap.POPUP);
function BlockingFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys, collapse) {
function BlockingFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys, collapse) {
RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys);
this.collapse = collapse;
}
}
extend(BlockingFilter, RegExpFilter, {
extend(BlockingFilter, RegExpFilter, {
collapse: null
});
});
function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys) {
function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys) {
RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, thirdParty, sitekeys);
}
}
extend(WhitelistFilter, RegExpFilter, {});
extend(WhitelistFilter, RegExpFilter, {});
function Matcher() {
function Matcher() {
this.clear();
}
}
Matcher.prototype = {
Matcher.prototype = {
filterByKeyword: null,
keywordByFilter: null,
clear: function () {
@ -522,16 +542,16 @@ Matcher.prototype = {
}
return null;
}
};
};
function CombinedMatcher() {
function CombinedMatcher() {
this.blacklist = new Matcher();
this.whitelist = new Matcher();
this.resultCache = createDict();
}
}
CombinedMatcher.maxCacheEntries = 1000;
CombinedMatcher.prototype = {
CombinedMatcher.maxCacheEntries = 1000;
CombinedMatcher.prototype = {
blacklist: null,
whitelist: null,
resultCache: null,
@ -628,22 +648,22 @@ CombinedMatcher.prototype = {
this.cacheEntries++;
return result;
}
};
};
var userrulesMatcher = new CombinedMatcher();
var defaultMatcher = new CombinedMatcher();
var userrulesMatcher = new CombinedMatcher();
var defaultMatcher = new CombinedMatcher();
var direct = 'DIRECT;';
var direct = 'DIRECT;';
for (var i = 0; i < userrules.length; i++) {
for (var i = 0; i < userrules.length; i++) {
userrulesMatcher.add(Filter.fromText(userrules[i]));
}
}
for (var i = 0; i < rules.length; i++) {
for (var i = 0; i < rules.length; i++) {
defaultMatcher.add(Filter.fromText(rules[i]));
}
}
function FindProxyForURL(url, host) {
function FindProxyForURL(url, host) {
if (userrulesMatcher.matchesAny(url, host) instanceof BlockingFilter) {
return proxy;
}
@ -658,8 +678,14 @@ function FindProxyForURL(url, host) {
return proxy;
}
return direct;
}
return {
FindProxyForURL,
}
}
module.exports = {
FindProxyForURL
createPacClient
}

File diff suppressed because one or more lines are too long