Skip to content

Instantly share code, notes, and snippets.

@cute cute/rule.conf Secret
Last active Sep 22, 2019

Embed
What would you like to do?
[Rule]
DOMAIN,baidu.com,DIRECT,script-filter
FINAL,DIRECT
[URL Rewrite]
^http://baidu.com/robots.txt _ direct script-filter
[Filter]
enable=true
script=LyogdGVzdCBmaWx0ZXIgYXBpICovCnZhciBsb2NhbFN0b3JhZ2UgPSB7fTsKCmZ1bmN0aW9uIHRlc3RGaWx0ZXJSZXNwb25zZSh0eXBlLCBpZCwgdXJsLCBkYXRhLCByZXNwb25zZSkgewogICAgdmFyIHBhcnNlciA9IGxvY2FsU3RvcmFnZVtpZF0gfHwgKGxvY2FsU3RvcmFnZVtpZF0gPSBuZXcgSFRUUFBhcnNlcih0eXBlKSk7CiAgICBwYXJzZXIub25IZWFkZXJzQ29tcGxldGUgPSBmdW5jdGlvbihpbmZvKSB7CiAgICAgICAgY29uc29sZS5sb2coJ29uSGVhZGVyc0NvbXBsZXRlJywgaW5mbyk7CiAgICB9OwogICAgcGFyc2VyLm9uQm9keSA9IGZ1bmN0aW9uKGNodW5rLCBvZmZzZXQsIGxlbmd0aCkgewogICAgICAgIGNvbnNvbGUubG9nKCdvbkJvZHknLCBjaHVuaywgb2Zmc2V0LCBsZW5ndGgpOwogICAgfTsKICAgIHBhcnNlci5vbk1lc3NhZ2VDb21wbGV0ZSA9IGZ1bmN0aW9uKCkgewogICAgICAgIGNvbnNvbGUubG9nKCdvbk1lc3NhZ2VDb21wbGV0ZScpOwogICAgfTsKICAgIHBhcnNlci5leGVjdXRlKGRhdGEpOwp9CgpmdW5jdGlvbiB0ZXN0TW9ja1Jlc3BvbnNlKHR5cGUsIGlkLCB1cmwsIGRhdGEsIHJlc3BvbnNlKSB7CiAgICBpZiAoIWxvY2FsU3RvcmFnZVtpZF0pIHsKICAgICAgICBsb2NhbFN0b3JhZ2VbaWRdID0ge30KICAgIH0KICAgIHZhciBwYXJzZXIgPSBsb2NhbFN0b3JhZ2VbaWRdW3R5cGVdO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R0cCByYXcgZGF0YQogKiBAcGFyYW0ge2Z1bmN0aW9ufTogZnVuY3Rpb24oZGF0YSl7fQogKiBAcmV0dXJucyB7c3RyaW5nfGJvb2xlYW58dW5kZWZpbmVkfQogKi8KZnVuY3Rpb24gc2NyaXB0RmlsdGVyRXhlY3V0ZSh0eXBlLCBpZCwgdXJsLCBkYXRhLCByZXNwb25zZSkgewogICAgY29uc29sZS5sb2coJ2ZpbHRlciAnICsgaWQpOwogICAgY29uc29sZS5sb2coJ3VybCAnICsgdXJsKTsKICAgIGlmICh1cmwuaW5kZXhPZignaHR0cDovL2JhaWR1LmNvbS9yb2JvdHMudHh0PycpID09PSAwKSB7CiAgICAgICAgaWYgKHR5cGUgPT0gJ1JFU1BPTlNFJykgewogICAgICAgICAgICB0ZXN0RmlsdGVyUmVzcG9uc2UodHlwZSwgaWQsIHVybCwgZGF0YSwgcmVzcG9uc2UpOwogICAgICAgIH0KICAgIH0gZWxzZSBpZiAodXJsLmluZGV4T2YoJ2h0dHA6Ly9iYWlkdS5jb20vcm9ib3RzLnR4dCcpID09PSAwKSB7CiAgICAgICAgdGVzdE1vY2tSZXNwb25zZSh0eXBlLCBpZCwgdXJsLCBkYXRhLCByZXNwb25zZSk7CiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9Cn0KCi8qCiAqIFJlbGVhc2UgZmlsdGVyIHZhbHVlcy4KICogQHBhcmFtIHtzdHJpbmd9OiBodHRwIHJlcXVlc3QgVVVJRAogKi8KZnVuY3Rpb24gc2NyaXB0RmlsdGVyRnJlZShpZCkgewogICAgY29uc29sZS5sb2coJ2ZyZWUgJyArIGlkKTsKICAgIGRlbGV0ZSBsb2NhbFN0b3JhZ2VbaWRdOwp9Cg==
/* test filter api */
const localStorage = {};
function testFilterResponse(type, id, url, data, response) {
var parser = localStorage[id] || (localStorage[id] = new HTTPParser(type));
parser.onHeadersComplete = function(info) {
console.log('onHeadersComplete', info);
};
parser.onBody = function(chunk, offset, length) {
console.log('onBody', chunk, offset, length);
};
parser.onMessageComplete = function() {
console.log('onMessageComplete');
};
parser.execute(data);
}
function testMockResponse(type, id, url, data, response) {
if (!localStorage[id]) {
localStorage[id] = {}
}
var parser = localStorage[id][type];
if (!parser) {
parser = localStorage[id][type] = new HTTPParser(type);
parser.onHeadersComplete = function(info) {
console.log('onHeadersComplete', info);
};
parser.onBody = function(chunk, offset, length) {
console.log('onBody =>', chunk, offset, length);
};
parser.onMessageComplete = function() {
console.log('onMessageComplete');
};
}
parser.execute(data);
console.log(parser.info);
var time = new Date().toGMTString();
response('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: ' + time.length + '\r\n\r\n' + time);
}
/**
* Filter HTTP request and response.
* @param {string}: http type, REQUEST or RESPONSE
* @param {string}: http request UUID
* @param {string}: http request url
* @param {string}: http raw data
* @param {function}: function(data){}
* @returns {string|boolean|undefined} string:return modified data, boolean:close connection, undefined:do not modify data
*/
function scriptFilterExecute(type, id, url, data, response) {
console.log('filter ' + id);
console.log('url ' + url);
if (url.indexOf('http://baidu.com/robots.txt?') === 0) {
if (type == 'RESPONSE') {
testFilterResponse(type, id, url, data, response);
}
} else if (url.indexOf('http://baidu.com/robots.txt') === 0) {
testMockResponse(type, id, url, data, response);
return true;
}
}
/*
* Release filter values.
* @param {string}: http request UUID
*/
function scriptFilterFree(id) {
console.log('free ' + id);
delete localStorage[id];
}
@cute

This comment has been minimized.

Copy link
Owner Author

cute commented May 5, 2019

disable gzip:

Content-Encoding: identity
Accept-Encoding: identity

disable chunked:

switch to HTTP/1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.