FelisCatus / switchysharp

Automatically exported from code.google.com/p/switchysharp
GNU General Public License v3.0
140 stars 50 forks source link

使用多层哈希表优化域名规则 #313

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
因为http://code.google.com/p/switchysharp/issues/detail?id=312这个问题, 
我现在退回了自写PAC, 用switchysharp来调用的方法

我写的PAC是仅针对域名做匹配, 针对域名匹配的特性, 
预生成一个树, 对传入的域名做"."分割之后进行逐步查找, 
规则表非常长的情况下这样做性能应该有优势

生成的PAC的示例:
var t = {
    "be": {
        "youtu": 0
    }, 
    "cc": {
        "ptt": 1, 
        "wretch": 1
    }, 
    "cn": {
        "google": 0
    }, 
    "co": {
        "t": 1
    }, 
    "com": {
        "5i01": 1, 
        "appspot": 0, 
        "blogger": 0, 
        "blogspot": 0, 
        "dropbox": 1, 
        "facebook": 1, 
        "fc2": 1, 
        "feedburner": 0, 
        "google": 0, 
        "googleapis": 0, 
        "googleusercontent": 0, 
        "gstatic": 0, 
        "hobbyking": 1, 
        "html5rocks": 1, 
        "imdb": 1, 
        "ipaddress": 1, 
        "mediafire": 1, 
        "mobile01": 1, 
        "staticflickr": 1, 
        "twimg": 1, 
        "twitter": 1, 
        "vimeo": 1, 
        "wordpress": 1, 
        "yimg": 1, 
        "youtube": 0, 
        "ytimg": 0, 
        "zforzelda": 0
    }, 
    "hk": {
        "com": {
            "google": 0
        }
    }, 
    "jp": {
        "blogimg": 1, 
        "livedoor": {
            "blog": {
                "image": 1
            }
        }, 
        "nicovideo": 1
    }, 
    "me": {
        "ip6": 0
    }, 
    "net": {
        "appsfire": 1, 
        "fbcdn": 1
    }, 
    "org": {
        "chinagfw": 1, 
        "failblog": 1, 
        "iphone-dev": 1, 
        "python": 1, 
        "thepiratebay": 1
    }, 
    "tw": {
        "com": {
            "pcdvd": 1
        }
    }, 
    "us": {
        "imageshack": 1
    }
};
var p = ["PROXY proxy.server.0;DIRECT","SOCKS proxy.server.1;DIRECT"];
var d = 'DIRECT';

function FindProxyForURL(url, host)
{
    if (host.length == 0)return d;
    host = host.split('.');
    if (host.length == 0)return d;
    var s = t;
    for (var i = host.length - 1; i >= 0; --i){
        s = s[host[i]];
        if (typeof s == "number"){
            return p[s];
        }else if (typeof s == "undefined"){
            break;
        }
    }
    return d;
}

从域名列表生成js树的代码:
http://code.google.com/p/jz-scripts/source/browse/pac/gen.py

不知道你有没有兴趣

Original issue reported on code.google.com by JimmyZ.Z on 4 Jun 2012 at 1:12

GoogleCodeExporter commented 9 years ago
之前想过,后来发现每次 Chrome 调用 PAC 
的时候那个对象都会被重新生成一次,所以效率有时可能反��
�不如直接判断……
也用相当大的数据测试过,结果时间都花在这个对象的生成��
�了。

我甚至想过用字符串模拟哈希表呢……

Original comment by shyc2...@gmail.com on 5 Jun 2012 at 4:05

GoogleCodeExporter commented 9 years ago
原来你的研究比我深入多了,真是现丑了

Original comment by JimmyZ.Z on 5 Jun 2012 at 4:21

GoogleCodeExporter commented 9 years ago
没关系,多研究点总不会是坏事……无论如何,感谢你提交��
�议~

Original comment by shyc2...@gmail.com on 5 Jun 2012 at 10:19