Shadowsocks PAC自定义规则介绍

Shadowsocks的PAC自定义规则使用Adblock Plus的部分语法,本文主要介绍几种常用配置方法
原文地址:https://adblockplus.org/en/filter-cheatsheet

一、匹配url部分字符串

示例:/banner/*/img^

解释:*表示0~多个任意字符,^表示分隔符,例如?/等,也可以表示字符结尾

匹配:上述配置匹配以下几个url

http://example.com/banner/foo/img (img后面没有字符了,可以匹配)
http://example.com/banner/foo/bar/img?param (img后面是个“?”,属于分隔符,可以匹配)
http://example.com/banner//img/foo (“*”表示0~任意个字符,这里为0个,img后面是“/”,也是个分隔符,可以匹配)

不匹配:上述配置不能匹配下面的url

http://example.com/banner/img (banner后面只有一个“/”而规则里,只有“*”部分可以没有,此时会出现两个“/”)
http://example.com/banner/foo/imgraph (img后面不是分隔符,而是字母)
http://example.com/banner/foo/img.gif (“.”不算做分隔符)

二、域名匹配

示例:||ads.example.com^

解释:||表示规则为域名,ads.example.com是域名中必须存在的一部分,^表示域名的结尾,通常是“/”或“:”(例如http://xxx.com/、http://xxx.com:8080/)

匹配:上述配置匹配以下url

http://ads.example.com/foo.gif (ads.example.com是域名的一部分)
http://server1.ads.example.com/foo.gif (server1.ads.example.com包含了ads.example.com,即写一个根域名,可以匹配其子域名)
https://ads.example.com:8000/ (域名ads.example.com与配置规则一致)

不匹配:上述配置不匹配下面的url

http://ads.example.com.ua/foo.gif (虽然ads.example.com.ua包含了ads.example.com,但由于规则后面有“^”,所以期望域名必须是以ads.example.com结尾,而不是出现在域名中的一部分)
http://example.com/redirect/http://ads.example.com/ (虽然url有ads.example.com,但这已经不是这个url的域名部分,而属于path的一部分,所以不能匹配)

三、精确匹配

示例:|http://example.com/|

解释:第一个“|”表示url起始,最后一个“|”表示url结束,中间部分必须完全一致才可以匹配

匹配:http://example.com/ (必须完全一样)

不匹配:

http://example.com/foo.gif (与规则不一致)
http://example.info/redirect/http://example.com/ (http://example.com/不符合是url的开头且是url的结尾)

四、例外

@@表示例外,@@||ads.example.com/notbanner^ 表示不匹配||ads.example.com/notbanner规则,例如:
||ads.example.com^
@@||ads.example.com/notbanner^
上面两条规则指定了匹配ads.example.com及子域名下除/notbanner开头的页面
它可以匹配http://ads.example.com/example、http://subdomain.ads.example.com/example,http://subdomain.ads.example.com/notbannerxxx,但不匹配http://ads.example.com/notbanner (第二条排除了)

五、注意

更改完用户规则后,可能需要重启shadowsocks才能使配置生效

解决openssl升级到1.1.0后shadowsocks服务报错问题

本文适用于解决openssl升级到1.1.0以上版本,导致shadowsocks2.8.2启动报undefined symbol: EVP_CIPHER_CTX_cleanup错误。

最近将openssl升级到了1.1.0b版本,编译之后shadowsocks无法启动,报错如下:

Traceback (most recent call last):
  File "/usr/bin/ssserver", line 9, in 
    load_entry_point('shadowsocks==2.8.2', 'console_scripts', 'ssserver')()
  File "/usr/lib/python2.7/site-packages/shadowsocks/server.py", line 34, in main
    config = shell.get_config(False)
  File "/usr/lib/python2.7/site-packages/shadowsocks/shell.py", line 262, in get_config
    check_config(config, is_local)
  File "/usr/lib/python2.7/site-packages/shadowsocks/shell.py", line 124, in check_config
    encrypt.try_cipher(config['password'], config['method'])
  File "/usr/lib/python2.7/site-packages/shadowsocks/encrypt.py", line 44, in try_cipher
    Encryptor(key, method)
  File "/usr/lib/python2.7/site-packages/shadowsocks/encrypt.py", line 83, in __init__
    random_string(self._method_info[1]))
  File "/usr/lib/python2.7/site-packages/shadowsocks/encrypt.py", line 109, in get_cipher
    return m[2](method, key, iv, op)
  File "/usr/lib/python2.7/site-packages/shadowsocks/crypto/openssl.py", line 76, in __init__
    load_openssl()
  File "/usr/lib/python2.7/site-packages/shadowsocks/crypto/openssl.py", line 52, in load_openssl
    libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
  File "/usr/lib64/python2.7/ctypes/__init__.py", line 373, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib64/python2.7/ctypes/__init__.py", line 378, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /usr/local/ssl/lib/libcrypto.so.1.1: undefined symbol: EVP_CIPHER_CTX_cleanup
shadowsocks start failed

这个问题是由于在openssl1.1.0版本中,废弃了EVP_CIPHER_CTX_cleanup函数,如官网中所说:

EVP_CIPHER_CTX was made opaque in OpenSSL 1.1.0. As a result, EVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup() disappeared. EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset().

EVP_CIPHER_CTX_reset函数替代了EVP_CIPHER_CTX_cleanup函数
EVP_CIPHER_CTX_reset函数说明:

EVP_CIPHER_CTX_reset() clears all information from a cipher context and free up any allocated memory associate with it, except the ctx itself. This function should be called anytime ctx is to be reused for another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of calls.

EVP_CIPHER_CTX_cleanup函数说明:

EVP_CIPHER_CTX_cleanup() clears all information from a cipher context and free up any allocated memory associate with it. It should be called after all operations using a cipher are complete so sensitive information does not remain in memory.

可以看出,二者功能基本上相同,都是释放内存,只是应该调用的时机稍有不同,所以用reset代替cleanup问题不大。

修改方法:

  1. 用vi打开文件:vi /usr/lib/python2.7/site-packages/shadowsocks/crypto/openssl.py
  2. 跳转到52行(shadowsocks2.8.2版本,其他版本搜索一下cleanup)
  3. 进入编辑模式
  4. 将第52行libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
    改为libcrypto.EVP_CIPHER_CTX_reset.argtypes = (c_void_p,)
  5. 再次搜索cleanup(全文件共2处,此处位于111行),将libcrypto.EVP_CIPHER_CTX_cleanup(self._ctx)
    改为libcrypto.EVP_CIPHER_CTX_reset(self._ctx)
  6. 保存并推出
  7. 启动shadowsocks服务:service shadowsocks start

之后我们就可以继续愉快的、科学的畅游互联网了。
提示:openssl1.1.0目前兼容性很不好,大部分的软件都不支持
目前支持的有nginx-1.11.5、curl-7.50.3
不支持的有PHP-7.0.12、openssh-7.3p1
所以如果决定使用openssl1.1.0需要考虑很多兼容问题,必须保留1.0.2或1.0.1(不推荐,存在一些已知漏洞,最重要的是如果服务器要开http2,由于新版chrome必须使用ALPN的限制,只有1.0.2版本支持ALPN,所以必须升级到1.0.2)版本以便编译其他程序。