Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically update the PAC for ShadowsocksX. Only tested on OS X. (Deprecated)
#!/bin/bash
# update_gfwlist.sh
# Author : VincentSit
# Copyright (c) http://xuexuefeng.com
#
# Example usage
#
# ./whatever-you-name-this.sh
#
# Task Scheduling (Optional)
#
# crontab -e
#
# add:
# 30 9 * * * sh /path/whatever-you-name-this.sh
#
# Now it will update the PAC at 9:30 every day.
#
# Remember to chmod +x the script.
GFWLIST="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
PROXY="127.0.0.1:1080"
USER_RULE_NAME="user-rule.txt"
check_module_installed()
{
pip list | grep gfwlist2pac &> /dev/null
if [ $? -eq 1 ]; then
echo "Installing gfwlist2pac."
pip install gfwlist2pac
fi
}
update_gfwlist()
{
echo "Downloading gfwlist."
curl -s "$GFWLIST" --fail --socks5-hostname "$PROXY" --output /tmp/gfwlist.txt
if [[ $? -ne 0 ]]; then
echo "abort due to error occurred."
exit 1
fi
cd ~/.ShadowsocksX || exit 1
if [ -f "gfwlist.js" ]; then
mv gfwlist.js ~/.Trash
fi
if [ ! -f $USER_RULE_NAME ]; then
touch $USER_RULE_NAME
fi
/usr/local/bin/gfwlist2pac \
--input /tmp/gfwlist.txt \
--file gfwlist.js \
--proxy "SOCKS5 $PROXY; SOCKS $PROXY; DIRECT" \
--user-rule $USER_RULE_NAME \
--precise
rm -f /tmp/gfwlist.txt
echo "Updated."
}
check_module_installed
update_gfwlist
@snowleopardw

This comment has been minimized.

Copy link

commented May 16, 2016

sudo easy_install pip
sudo pip install gfwlist2pac

@VincentSit

This comment has been minimized.

Copy link
Owner Author

commented May 17, 2016

Thanks @snowleopardw.

Before running the script, please make sure you have installed the pip.

@Femtometer

This comment has been minimized.

Copy link

commented May 19, 2016

Downloading gfwlist.
update_gfwlist.sh: line 48: cd: /var/root/.ShadowsocksX: No such file or directory
update_gfwlist.sh: line 59: /usr/local/bin/gfwlist2pac: No such file or directory
Updated.


Note, seems directory does not fit others' laptop.

@VincentSit

This comment has been minimized.

Copy link
Owner Author

commented May 20, 2016

Hi @Femtometer

As I mentioned in the description, I only tested on OS X.

Anyone can modify this code based on their needs.

@HoneyLuka

This comment has been minimized.

Copy link

commented May 25, 2016

@VincentSit Thanks

@jingchen-xu

This comment has been minimized.

Copy link

commented May 28, 2016

when I run your script ./update_gfwlist.sh, I get:

Installing gfwlist2pac.
Collecting gfwlist2pac
Installing collected packages: gfwlist2pac
Successfully installed gfwlist2pac-1.1.3
Downloading gfwlist.
Traceback (most recent call last):
  File "/usr/local/bin/gfwlist2pac", line 7, in <module>
    from gfwlist2pac.main import main
  File "/usr/local/lib/python3.5/site-packages/gfwlist2pac/main.py", line 160
    print 'Downloading gfwlist from %s' % gfwlist_url
                                      ^
SyntaxError: Missing parentheses in call to 'print'
Updated.

Is that because I am using python version 3.x ?

@VincentSit

This comment has been minimized.

Copy link
Owner Author

commented May 30, 2016

@xujchen

Hi, sorry for the delay, because Gist comments and mentions don't trigger notifications.

Yes, gfwlist2pac needs python 2.7.

@VincentSit

This comment has been minimized.

Copy link
Owner Author

commented May 31, 2016

For OS X users, if crontab does't running your script, please consider specify PATH variable in the Crontab.

@zwpaper

This comment has been minimized.

Copy link

commented Jun 5, 2016

thanks for the code, but it would be better if could check the version of python.
I install gfwlist2pac by hand, but when i run the code, it download the gfwlist2pac again using pip3
I changed pip to pip2 and it works

@tony-haibo-zhu

This comment has been minimized.

Copy link

commented Jun 19, 2016

Your bash is fine.
But l'm wired of one thing. Here it is when l update latest gfwlist l found couldn't access google whether Autoproxy or Globalproxy. Something like latest gfwlist have some problem. l don't know any people encounter this problem?

@mitoop

This comment has been minimized.

Copy link

commented Jul 21, 2016

@tony-haibo-zhu Just close your ShadowsockX and restart it.

@fuurose

This comment has been minimized.

Copy link

commented Aug 5, 2016

The script returns:

Downloading gfwlist.
abort due to error occurred.

Is this because of an error with the gfwlist2pac install? The gfwlist2pac install returns:
The directory '/Users/xxx/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Does this mean the installation was unsuccessful?

@langjun

This comment has been minimized.

Copy link

commented Aug 10, 2016

Good job!

@janeluck

This comment has been minimized.

Copy link

commented Aug 18, 2016

@xujchen I have the same problem, is there any way to use python3?

@linkdesu

This comment has been minimized.

Copy link

commented Aug 19, 2016

THX!!! Line 63 --precise is useless and will cause error .

@juyfchn

This comment has been minimized.

Copy link

commented Aug 25, 2016

i want to know how to cancel

@d4rkb1ue

This comment has been minimized.

Copy link

commented Oct 13, 2016

@fuurose you should run it like sudo -H update_gfwlist.sh

@d4rkb1ue

This comment has been minimized.

Copy link

commented Oct 13, 2016

@Femtometer you can change line 48 as cd /Users/[YOUR_USER_NAME]/.ShadowsocksX || exit 1 to solve the problem

@chesterlyd

This comment has been minimized.

Copy link

commented Dec 13, 2016

我运行脚本./update_gfwlist.sh以后显示

DEPRECATION: The default format will switch to columns in the future. 
You can use --format=(legacy|columns)
 (or define a format=(legacy|columns)
 in your pip.conf under the [list] section) to disable this warning.\
Downloading gfwlist.
abort due to error occurred.

系统是OS X 10.11 ,请问一下,该怎么解决

@holyen

This comment has been minimized.

Copy link

commented Mar 9, 2017

Desktop xxxx$ python update_gfwlist.sh
File "update_gfwlist.sh", line 28
pip list | grep gfwlist2pac &> /dev/null
^
SyntaxError: invalid syntax

@VincentSit

This comment has been minimized.

Copy link
Owner Author

commented Mar 9, 2017

这个脚本已经不需要了,也不维护了,请自行去下载 ShadowsocksX-NG

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.