├── gfwlist2privoxy ├── gfwlist2privoxy-mac-zsh └── README.md /gfwlist2privoxy: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Desc: convert gfwlist.txt into privoxy.action 4 | # Usage: bash gfwlist2privoxy 5 | # Dependency: base64, curl(https support), perl5 v5.10.0+ 6 | 7 | # check dependency 8 | command -v curl &>/dev/null || { echo "curl is not installed in this system" 1>&2; exit 1; } 9 | command -v perl &>/dev/null || { echo "perl is not installed in this system" 1>&2; exit 1; } 10 | command -v base64 &>/dev/null || { echo "base64 is not installed in this system" 1>&2; exit 1; } 11 | 12 | # parse command line 13 | gfwlist_action='gfwlist.action' 14 | test $# -lt 1 && { echo "Usage: bash gfwlist2privoxy "; exit 1; } 15 | perl -e 'exit 1 if $ARGV[0] !~ /^\d+\.\d+\.\d+\.\d+:\d+$/' "$1" || { echo "Invalid address format: '$1'" 1>&2; exit 1; } 16 | echo -e "# Generated by gfwlist2privoxy at $(date '+%F %T')\n{+forward-override{forward-socks5 $1 .}}" >${gfwlist_action} 17 | 18 | # convert gfwlist.txt 19 | temporary_file=$(mktemp) 20 | base64 -d /dev/null && base64='base64 -d' 21 | base64 --decode /dev/null && base64='base64 --decode' 22 | [ "$base64" ] || { echo "[ERR] Command not found: 'base64'" 1>&2; exit 1; } 23 | curl -4sSkL https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt | $base64 | perl -pe ' 24 | if (/URL Keywords/i) { $null = <> until $null =~ /^!/ } 25 | s#^\s*+$|^!.*+$|^@@.*+$|^\[AutoProxy.*+$|^/.*/$##i; 26 | s@^\|\|?|\|$@@; 27 | s@^https?:/?/?@@i; 28 | s@(?:/|%).*+$@@; 29 | s@^\*\.@.@; 30 | s@\.?\*$@.@; 31 | s@(?=[^0-9a-zA-Z.*-]).*+$@@ unless /^\d+\.\d+\.\d+\.\d+(?::\d+)?$/; 32 | s@^(?!\.|\s*+$|\d+\.\d+\.\d+\.\d+(?::\d+)?$)@.@; 33 | s@^\.[^.]++$@@; 34 | s@^\s*+$@@' >${temporary_file} 35 | echo -e '.google.\n.blogspot.\n.twimg.edgesuite.net' >>${temporary_file} 36 | 37 | # conversion completed 38 | cat ${temporary_file} | perl -ne 'print if /^\d+\.\d+\.\d+\.\d+(?::\d+)?$/' | sort -n | uniq >>${gfwlist_action} 39 | cat ${temporary_file} | perl -ne 'print unless /^\d+\.\d+\.\d+\.\d+(?::\d+)?$/' | sort | uniq >>${gfwlist_action} 40 | rm -fr ${temporary_file} 41 | cat << EOF 42 | Generated file: '${gfwlist_action}'. Please put it in privoxy config directory. 43 | Usually, the directory is '/etc/privoxy'. If yes, then exec following command: 44 | 45 | mv -f ${gfwlist_action} /etc/privoxy 46 | echo 'actionsfile ${gfwlist_action}' >>/etc/privoxy/config 47 | service privoxy restart (via SysVinit) 48 | systemctl restart privoxy.service (via Systemd) 49 | 50 | Enjoy it! 51 | EOF 52 | -------------------------------------------------------------------------------- /gfwlist2privoxy-mac-zsh: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | 3 | ## gfwlist.action 文件 4 | gfwlist_action='gfwlist.action' 5 | 6 | ## socks5 代理地址,必须为 ip:port 形式 7 | socks5_proxy=$1 8 | if [[ ! "${socks5_proxy}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}$ ]]; then 9 | echo -e "\e[35minvalid address:\e[0m \"${socks5_proxy}\"" 1>&2 10 | echo -e "\e[37mUsage:\e[0m \e[32m$0\e[0m \e[36m'address:port'\e[0m" 1>&2 11 | exit 1 12 | else 13 | echo "{+forward-override{forward-socks5 ${socks5_proxy} .}}" > ${gfwlist_action} 14 | fi 15 | 16 | ## 用到的临时文件 17 | gfwlist_txt=$(mktemp) 18 | gfwlist_regex=$(mktemp) 19 | gfwlist_scheme=$(mktemp) 20 | gfwlist_begin=$(mktemp) 21 | gfwlist_main=$(mktemp) 22 | gfwlist_temp=$(mktemp) 23 | 24 | ## 获取 gfwlist.txt 25 | curl -4sSkL https://raw.github.com/gfwlist/gfwlist/master/gfwlist.txt | base64 --decode | egrep -v '^$|^!|^@@|^\[AutoProxy' > ${gfwlist_txt} 26 | 27 | ## 分离不同的语法 28 | cat ${gfwlist_txt} | egrep '^/' > ${gfwlist_regex} # '/regex/' 正则 29 | cat ${gfwlist_txt} | egrep '^\|\|' > ${gfwlist_scheme} # '||pattern' 协议符 30 | cat ${gfwlist_txt} | egrep '^\|[^\|]' > ${gfwlist_begin} # '|pattern' 边界符 31 | cat ${gfwlist_txt} | egrep -v '^/|^\|\||^\|[^\|]' > ${gfwlist_main} # 与 privoxy.action 语法接近的部分 32 | 33 | ## 处理正则语法 (目前只能手动添加,因为将正则替换为 shell 通配符太复杂了) 34 | echo '.google.' >> ${gfwlist_main} 35 | echo '.blogspot.' >> ${gfwlist_main} 36 | echo '.twimg.edgesuite.net' >> ${gfwlist_main} 37 | 38 | ## 处理协议符,直接删除即可,在 privoxy.action 中没有所谓的协议字段 39 | cat ${gfwlist_scheme} | sed -E 's@^\|\|(.*)$@\1@g' >> ${gfwlist_main} 40 | 41 | ## 处理边界符,删除边界符,然后删除可能有的协议字段 42 | cat ${gfwlist_begin} | sed -E 's@^\|(.*)$@\1@g' | sed -E '\@^https?://@ s@^https?://(.*)$@\1@g' >> ${gfwlist_main} 43 | 44 | ## 处理 gfwlist_main 文件,去除尾部的 uri 部分,只保留域名 45 | cat ${gfwlist_main} | sed -E '\@/@ s@^([^/]*).*$@\1@g' | sort | uniq -i > ${gfwlist_temp} 46 | 47 | ## 处理 ipv4 地址 48 | cat ${gfwlist_temp} | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' >> ${gfwlist_action} 49 | 50 | ## 处理域名,在开头添加 '.',然后删除重复内容 51 | cat ${gfwlist_temp} | grep -Ev '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' | sed -E '\@^\.@! s@^(.*)$@.\1@g' | sort | uniq -i >> ${gfwlist_action} 52 | 53 | ## 处理完毕,删除临时文件 54 | rm -fr ${gfwlist_txt} ${gfwlist_regex} ${gfwlist_scheme} ${gfwlist_begin} ${gfwlist_main} ${gfwlist_temp} 55 | 56 | ## 打印接下来要执行的命令(应用 gfwlist_action) 57 | echo -e "\e[37m# Please execute the following command:\e[0m" 58 | echo -e "\e[36mcp -af ${gfwlist_action} /etc/privoxy/\e[0m" 59 | echo -e "\e[36mecho \"actionsfile ${gfwlist_action}\" >> /etc/privoxy/config\e[0m" 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 适用于 privoxy 的 gfwlist.pac(socks5 代理) 2 | - 由于 macOS 的命令行参数与 Linux 命令行参数部分不一致,macOS 用户请使用 `gfwlist2privoxy-mac-zsh` 代替 `gfwlist2privoxy`。使用 `zsh` 能得到更好的提示效果。-- [c-rainstorm](https://github.com/c-rainstorm) 3 | - 现使用 Perl5 替代 sed、grep 等正则处理工具,应该不存在所谓参数不一致问题了,请先尝试 `gfwlist2privoxy`。-- [zfl9](https://github.com/zfl9) 4 | 5 | ## 脚本依赖 6 | - base64 7 | - curl (https) 8 | - Perl5 v5.10.0+ 9 | 10 | ## 脚本用法 11 | - `curl -4sSkLO https://raw.github.com/zfl9/gfwlist2privoxy/master/gfwlist2privoxy` 12 | - `bash gfwlist2privoxy 127.0.0.1:1080`,注意将 `127.0.0.1:1080` 替换为你的 socks5 地址 13 | - `mv -f gfwlist.action /etc/privoxy/`,将 gfwlist.action 移动到 privoxy 配置文件目录 14 | - `echo 'actionsfile gfwlist.action' >>/etc/privoxy/config`,应用 gfwlist.action 配置文件 15 | - `systemctl restart privoxy.service`,重启 privoxy 服务,留意 privoxy 运行状态,检查是否有错误 16 | 17 | > 更多信息,请参见: https://github.com/zfl9/gfwlist2privoxy/wiki 18 | 19 | ## Adblock Plus 规则 20 | - 注释符:`!`开头的行均为注释行,会被 Adblock Plus 忽略,当然还有特殊注释,此处略过 21 | - 通配符:`*`匹配任意长度字符,并且默认假设字符串两边存在`*`,即`ad`与`*ad*`是一样的 22 | - 边界符:`|`位于模式首尾,用于取消首尾默认的`*`通配符,比如`|https://www.google.com/ad` 23 | - 子域符:`||`位于模式头部,用于匹配`http://*.`、`https://*.`协议字符串及子域部分(含当前域名) 24 | - 分隔符:`^`匹配 url 分隔符,如`http://192.168.1.1:8080/?a=1&b=2`中的红色部分以及地址结尾(~~见下图~~) 25 | - 正则符:`/regex/`使用 Unix 路径分隔符括住正则表达式(Perl 正则)(应避免大量正则的使用,性能低) 26 | - 例外符:`@@`开头的模式,表示定义一个例外规则,如`@@||google.com^`、`@@/https?:\/\/.*\.google\..*/` 27 | 28 | ## privoxy.action 规则 29 | - 协议部分:privoxy 已经默认假设存在`http://`、`https://`,因此不能再定义协议字符串 30 | - host 部分:使用 glob 模式,即`*`任意长度字符、`?`任意单个字符、`[set]`集合、`[^set]`集合(取反) 31 | - host 部分可以指定端口号,语法和平时指定端口号一样,比如:`www.zfl9.com:8989`、`192.168.1.1:8080` 32 | - host 部分除了可以是域名外,还可以是 IPv4、IPv6 地址,如果为 IPv6 地址,需用`<>`括起来(取消歧义) 33 | - uri 部分:使用 regex 模式(POSIX 扩展正则表达式,ERE,POSIX 1003.2),不支持非贪婪匹配等高级特性 34 | 35 | host 部分的例子: 36 | - `/`,匹配所有 URL 请求,这是特殊模式 37 | - `:8080`,匹配所有目的端口为 8080 的请求 38 | - `192.168.1.1`,匹配目的主机 192.168.1.1 的所有请求 39 | - `<2001:db8::1>`,匹配目的主机 2001:db8::1 的所有请求 40 | - `www.zfl9.com/`,匹配 www.zfl9.com 下的所有请求 41 | - `www.zfl9.com`,匹配 www.zfl9.com 下的所有请求,末尾的 / 可以省略 42 | - `www.`,匹配所有以 www. 开头的域名下的所有请求(包括 www) 43 | - `.google.`,匹配所有包含 .google. 内容的域名下的所有请求(包括 google) 44 | - `.google.com`,匹配所有以 .google.com 结尾的域名下的所有请求(包括 google.com) 45 | 46 | privoxy.action 典型写法: 47 | ``` bash 48 | # 定义别名,可包含除空格、Tab、=、{} 外的任意字符 49 | {{alias}} 50 | # 代理(socks5) 51 | socks5 = +forward-override{forward-socks5 127.0.0.1:1080 .} 52 | # 直连 53 | direct = +forward-override{forward .} 54 | 55 | # 后面的规则会覆盖前面的规则,因此例外规则必须写在后面 56 | # 所有网站走代理 (一般情况写在前面) 57 | {socks5} 58 | / 59 | 60 | # 以下网站走直连 (特殊情况写在后面) 61 | {direct} 62 | .ip.cn 63 | .chinaz.com 64 | ``` 65 | 66 | 更多 privoxy 用法请参考:[privoxy - 用户手册](https://www.privoxy.org/user-manual/) 67 | --------------------------------------------------------------------------------