Skip to content

Instantly share code, notes, and snippets.

@imagelife
Forked from wd/config.json
Created March 28, 2018 05:05
Show Gist options
  • Save imagelife/ec9b735ba5ecaecefb982398b9b1d9a9 to your computer and use it in GitHub Desktop.
Save imagelife/ec9b735ba5ecaecefb982398b9b1d9a9 to your computer and use it in GitHub Desktop.
路由器梅林固件上面使用 v2ray 的方案
{
"log": {
"loglevel": "none"
},
"inbound": {
"port": 23456,
"listen": "127.0.0.1",
"protocol": "socks",
"settings": {
"udp": true
}
},
"inboundDetour":
[{
"port": 3333,
"listen": "0.0.0.0",
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true
}
}],
"outbound": {
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "这里输入你的服务器信息",
"port": 443,
"users": [
{
"id": "这里输入你的id,其他配置根据你需要配置",
"alterId": 64,
"security": ""
}
]
}
]
},
"streamSettings":
{
"network": "tcp",
"security": "tls",
"tlsSettings": { "allowInsecure": true }
}
},
"policy": {
"levels": {
"0": {"uplinkOnly": 0}
}
}
}
#!/bin/sh
CUR_VER=""
NEW_VER=""
ARCH=""
VDIS="arm"
ZIPFILE="/tmp/v2ray/v2ray.zip"
V2RAY_DIR="/jffs/v2ray"
CMD_INSTALL=""
CMD_UPDATE=""
SOFTWARE_UPDATED=0
#######color code########
RED="31m"
GREEN="32m"
YELLOW="33m"
BLUE="36m"
sysArch(){
ARCH=$(uname -m)
if [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then
VDIS="32"
elif [[ "$ARCH" == *"armv7"* ]] || [[ "$ARCH" == "armv6l" ]]; then
VDIS="arm"
elif [[ "$ARCH" == *"armv8"* ]] || [[ "$ARCH" == "aarch64" ]]; then
VDIS="arm64"
elif [[ "$ARCH" == *"mips64le"* ]]; then
VDIS="mips64le"
elif [[ "$ARCH" == *"mips64"* ]]; then
VDIS="mips64"
elif [[ "$ARCH" == *"mipsle"* ]]; then
VDIS="mipsle"
elif [[ "$ARCH" == *"mips"* ]]; then
VDIS="mips"
elif [[ "$ARCH" == *"s390x"* ]]; then
VDIS="s390x"
fi
return 0
}
colorEcho(){
COLOR=$1
echo -e "\033[${COLOR}${@:2}\033[0m"
}
downloadV2Ray(){
rm -rf /tmp/v2ray
mkdir -p /tmp/v2ray
colorEcho ${BLUE} "Downloading V2Ray."
DOWNLOAD_LINK="https://github.com/v2ray/v2ray-core/releases/download/${NEW_VER}/v2ray-linux-${VDIS}.zip"
curl -L -H "Cache-Control: no-cache" -o ${ZIPFILE} ${DOWNLOAD_LINK}
if [ $? != 0 ];then
colorEcho ${RED} "Failed to download! Please check your network or try again."
exit 1
fi
return 0
}
installSoftware(){
COMPONENT=$1
if [[ -n `command -v $COMPONENT` ]]; then
return 0
fi
getPMT
if [[ $? -eq 1 ]]; then
colorEcho $YELLOW "The system package manager tool isn't APT or YUM, please install ${COMPONENT} manually."
exit
fi
colorEcho $GREEN "Installing $COMPONENT"
if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
colorEcho ${BLUE} "Updating software repo"
$CMD_UPDATE
SOFTWARE_UPDATED=1
fi
colorEcho ${BLUE} "Installing ${COMPONENT}"
$CMD_INSTALL $COMPONENT
if [[ $? -ne 0 ]]; then
colorEcho ${RED} "Install ${COMPONENT} fail, please install it manually."
exit
fi
return 0
}
extract(){
colorEcho ${BLUE}"Extracting V2Ray package to /tmp/v2ray."
mkdir -p /tmp/v2ray
unzip $1 -d "/tmp/v2ray/"
if [[ $? -ne 0 ]]; then
colorEcho ${RED} "Extracting V2Ray faile!"
exit
fi
return 0
}
# 1: new V2Ray. 0: no
getVersion(){
CUR_VER=`$V2RAY_DIR/v2ray -version 2>/dev/null | head -n 1 | cut -d " " -f2`
TAG_URL="https://api.github.com/repos/v2ray/v2ray-core/releases/latest"
NEW_VER=`curl ${PROXY} -s ${TAG_URL} --connect-timeout 10| grep 'tag_name' | cut -d\" -f4`
if [[ $? -ne 0 ]] || [[ $NEW_VER == "" ]]; then
colorEcho ${RED} "Network error! Please check your network or try again."
exit
elif [[ "$NEW_VER" != "$CUR_VER" ]];then
return 1
fi
return 0
}
copyFile() {
NAME=$1
MANDATE=$2
ERROR=`cp "/tmp/v2ray/v2ray-${NEW_VER}-linux-${VDIS}/${NAME}" "$V2RAY_DIR/${NAME}"`
if [[ $? -ne 0 ]]; then
colorEcho ${YELLOW} "${ERROR}"
if [ "$MANDATE" = true ]; then
exit
fi
fi
}
installV2Ray(){
# Install V2Ray binary to $V2RAY_DIR
mkdir -p $V2RAY_DIR
copyFile v2ray true
makeExecutable v2ray
copyFile v2ctl false
makeExecutable v2ctl
copyFile geoip.dat false
copyFile geosite.dat false
return 0
}
makeExecutable() {
chmod +x "$V2RAY_DIR/$1"
}
updateWatchDog() {
colorEcho ${GREEN} "Update v2ray_watchdog.sh"
echo "IyEvYmluL3NoCgp2MnJheT0nL2pmZnMvdjJyYXkvdjJyYXknCgppc192MnJheV9hbGl2ZSgpIHsKICAgIHYycmF5X2NvdW50PWBwcyAtdyB8Z3JlcCAnL2pmZnMvdjJyYXkvdjJyYXknfGdyZXAgLXYgZ3JlcHxncmVwIC12IHdhdGNoZG9nfHdjIC1sYAogICAgaWYgWyAiJHYycmF5X2NvdW50IiAtZXEgMSBdO3RoZW4KICAgICAgICByZXR1cm4gMCAgIyB3b3JrIG9rCiAgICBlbHNlCiAgICAgICAgcmV0dXJuIDEKICAgIGZpCn0KCnJlc3RhcnRfdjJyYXkoKSB7CiAgICBraWxsYWxsIHYycmF5ID4vZGV2L251bGwgMj4mMQogICAgJHYycmF5ICYKICAgIGVjaG8gJCQgPiAvdG1wL3YycmF5LnBpZAp9CgpzdG9wX3NzKCkgewogICAga2lsbGFsbCBzcy1sb2NhbCA+L2Rldi9udWxsIDI+JjEKICAgIGtpbGxhbGwgc3MtcmVkaXIgPi9kZXYvbnVsbCAyPiYxCn0KCm1haW4oKXsKICAgIGlzX3YycmF5X2FsaXZlCiAgICBpZiBbICIkPyIgLW5lIDAgXTt0aGVuCiAgICAgICAgc3RvcF9zcwogICAgICAgIHJlc3RhcnRfdjJyYXkKICAgICAgICBlY2hvICJ2MnJheSByZXN0YXJ0IGF0ICIkKGRhdGUpID4+IC90bXAvdjJyYXkubG9nCiAgICBmaQp9CgptYWluCg==" > /tmp/dog.in
base64 -d /tmp/dog.in > "$V2RAY_DIR/v2ray_watchdog.sh"
rm /tmp/dog.in
}
main() {
# dowload via network and extract
updateWatchDog
getVersion
if [[ $? == 0 ]]; then
colorEcho ${GREEN} "Lastest version ${NEW_VER} is already installed."
exit
else
colorEcho ${BLUE} "Installing V2Ray ${NEW_VER} on ${ARCH}"
downloadV2Ray
extract ${ZIPFILE}
fi
installV2Ray
colorEcho ${GREEN} "V2Ray ${NEW_VER} is installed."
rm -rf /tmp/v2ray
return 0
}
main
# 修改 /jffs/scripts/nat-start 和 /jffs/scripts/wan-start 都增加一行 cron 的配置,定时跑 watchdog 检查服务
# 注意:下面的 admin 需要改成你自己的用户名
grep -q 'v2ray_watchdog.sh' /var/spool/cron/crontabs/admin || echo "*/5 * * * * /bin/sh /jffs/v2ray/v2ray_watchdog.sh" >> /var/spool/cron/crontabs/admin
#!/bin/sh
v2ray='/jffs/v2ray/v2ray' # v2ray 的所有文件放到 /jffs/v2ray 下面
is_v2ray_alive() {
v2ray_count=`ps -w |grep '/jffs/v2ray/v2ray'|grep -v grep|grep -v watchdog|wc -l`
if [ "$v2ray_count" -eq 1 ];then
return 0 # work ok
else
return 1
fi
}
restart_v2ray() {
killall v2ray >/dev/null 2>&1
$v2ray &
echo $$ > /tmp/v2ray.pid
}
stop_ss() {
killall ss-local >/dev/null 2>&1
killall ss-redir >/dev/null 2>&1
}
main(){
is_v2ray_alive #判断 v2ray 是不是还活着
if [ "$?" -ne 0 ];then
stop_ss # 停止 ss 相关进程,因为我们会用 v2ray 代替他
restart_v2ray # 启动 v2ray
echo "v2ray restart at "$(date) >> /tmp/v2ray.log
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment