Skip to content

Commit

Permalink
Update deploy_start to v0.6.3
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Oct 27, 2021
1 parent ade760e commit d0b98e2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 33 deletions.
4 changes: 2 additions & 2 deletions deploy_side_router
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ if ./v2ray -version; then
set +e; systemctl stop v2ray; set -e
cp v2ray v2ctl geosite.dat geoip-only-cn-private.dat /opt/sbin/
cp systemd/system/v2ray.service /etc/systemd/system/v2ray.service
replace_regex 'RestartPreventExitStatus=23' 'RestartPreventExitStatus=23
replace_regex1 'RestartPreventExitStatus=23' 'RestartPreventExitStatus=23
# Added by user
LimitNPROC=500
LimitNOFILE=1000000' /etc/systemd/system/v2ray.service
replace_string '/usr/local/bin/v2ray -config /usr/local/etc/v2ray/config.json' '/opt/sbin/v2ray -config /opt/etc/v2ray.json' /etc/systemd/system/v2ray.service
replace_string1 '/usr/local/bin/v2ray -config /usr/local/etc/v2ray/config.json' '/opt/sbin/v2ray -config /opt/etc/v2ray.json' /etc/systemd/system/v2ray.service
# systemctl daemon-reload && systemctl start v2ray
else
echo 'Not valid v2ray version is supported by current router, please download correct version.'
Expand Down
76 changes: 60 additions & 16 deletions deploy_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ function replace_escape1() {
printf %s "${REPLY%$'\n'}"
}

# 基于 perl 的 replace 函数,目的是为了解决 sed 无法很好的全文匹配的问题。
# 这几个函数的做法,是一次性读取文件所有内容作为一个字符串,再使用 PCRE 语法匹配/替换

function match_multiline() {
escaped_regex=$(echo "$1" |sed 's#/#\\\/#g')
Expand All @@ -380,41 +382,80 @@ function match_multiline() {
}

function perl_replace() {
local regexp replace
regexp=$1
# 注意 replace 当中的特殊变量, 例如, $& $1 $2 的手动转义.
local regexp=$1
# 注意:$1 在 perl 里面是一个矢量, 因此它有 $[ 会出错,因为 perl 会认为在通过 []
# 方法读取矢量的元素,所以记得在 placement 中 [ 也要转义。
# 写完一定测试一下,perl 变量引用: http://www.perlmonks.org/?node_id=353259
replace=$2
escaped_replace=$(echo "$replace" |sed 's#"#\\"#g')
local replace=$2
local escaped_replace=$(echo "$replace" |sed 's#"#\\"#g')

perl -i -ne "s$regexp$replacegs; print \$_; unless ($& eq \"\") {print STDERR \"\`\033[0;33m$&\033[0m' was replaced with \`\033[0;33m${escaped_replace}\033[0m'\n\"};" "${@:3}"
# 和 sed 类似,就是 g, 表示是否全局替换,不加只替换第一个
local replace_all_matched=$3
# 就是 s, 新增的话, . 也匹配 new_line
local match_newline=$4

if [ -z "$replace_all_matched" ]; then
globally=''
else
globally=' globally'
fi

perl -i -ne "s$regexp$replace${replace_all_matched}${match_newline}; print \$_; unless ($& eq \"\") {print STDERR \"\`\033[0;33m$&\033[0m' was replaced with \`\033[0;33m${escaped_replace}\033[0m'${globally} for \`$6'!\n\"};" "$5" "$6"
}

# 为了支持多行匹配,使用 perl 正则, 比 sed 好用一百倍!
function replace_multiline () {
local regexp replace file
regexp=$1
replace=$2
file=$3
local regexp=$1
local replace=$2
local file=$3

perl_replace "$regexp" "$replace" -0 "$file"
# 这个 -0 必须的,-0 表示,将空白字符作为 input record separators ($/)
# 这也意味着,它会将文件内的所有内容整体作为一个字符串一次性读取。
# 感觉类似于 -0777 (file slurp mode) ?
perl_replace "$regexp" "$replace" "g" "s" -0777 "$file"
}

function replace_multiline1 () {
local regexp=$1
local replace=$2
local file=$3

perl_replace "$regexp" "$replace" "" "s" -0777 "$file"
}

# 这个和 multiline 的区别仅仅在于,multi 里面 . 也匹配 newline, regex 不会
function replace_regex () {
local regexp="$1"
local replace="$2"
local regexp=$1
local replace=$2
local file=$3

perl_replace "$regexp" "$replace" "g" "" -0777 "$file"
}

function replace_regex1 () {
local regexp=$1
local replace=$2
local file=$3

perl_replace "$regexp" "$replace" -0 "$file"
perl_replace "$regexp" "$replace" "" "" -0777 "$file"
}

function replace_string () {
# 转化输入的字符串为 literal 形式
local regexp="\\Q$1\\E"
local replace="$2"
local replace=$2
local file=$3

perl_replace "$regexp" "$replace" "g" "" -0777 "$file"
}

function replace_string1 () {
# 转化输入的字符串为 literal 形式
local regexp="\\Q$1\\E"
local replace=$2
local file=$3

perl_replace "$regexp" "$replace" -0 "$file"
perl_replace "$regexp" "$replace" "" "" -0777 "$file"
}

function update_config () {
Expand Down Expand Up @@ -496,6 +537,9 @@ function download_and_extract () {
xz|txz)
curl "$1" |tar -Jxvf - -C "$dest" --strip-components=1
;;
zst|zstd)
curl "$1" |tar --use-compress-program zstd -xvf - -C "$dest" --strip-components=1
;;
lzma)
curl "$1" |tar --lzma -xvf - -C "$dest" --strip-components=1
;;
Expand Down
51 changes: 36 additions & 15 deletions router/opt/etc/toggle_proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,45 @@ function match_multiline() {
}

function perl_replace() {
local regexp replace
regexp=$1
# 注意 replace 当中的特殊变量, 例如, $& $1 $2 的手动转义.
local regexp=$1
# 注意:$1 在 perl 里面是一个矢量, 因此它有 $[ 会出错,因为 perl 会认为在通过 []
# 方法读取矢量的元素,所以记得在 placement 中 [ 也要转义。
# 写完一定测试一下,perl 变量引用: http://www.perlmonks.org/?node_id=353259
replace=$2
escaped_replace=$(echo "$replace" |sed 's#"#\\"#g')
local replace=$2
local escaped_replace=$(echo "$replace" |sed 's#"#\\"#g')

perl -i -ne "s$regexp$replacegs; print \$_; unless ($& eq \"\") {print STDERR \"\`\033[0;33m$&\033[0m' was replaced with \`\033[0;33m${escaped_replace}\033[0m'\n\"};" "$3" "$4"
# 和 sed 类似,就是 g, 表示是否全局替换,不加只替换第一个
local replace_all_matched=$3
# 就是 s, 新增的话, . 也匹配 new_line
local match_newline=$4

if [ -z "$replace_all_matched" ]; then
globally=''
else
globally=' globally'
fi

perl -i -ne "s$regexp$replace${replace_all_matched}${match_newline}; print \$_; unless ($& eq \"\") {print STDERR \"\`\033[0;33m$&\033[0m' was replaced with \`\033[0;33m${escaped_replace}\033[0m'${globally} for \`$6'!\n\"};" "$5" "$6"
}

# 为了支持多行匹配,使用 perl 正则, 比 sed 好用一百倍!
function replace_multiline () {
local regexp replace file
regexp=$1
replace=$2
file=$3
local regexp=$1
local replace=$2
local file=$3

# 这个 -0 必须的,-0 表示,将空白字符作为 input record separators ($/)
# 这也意味着,它会将文件内的所有内容整体作为一个字符串一次性读取。
# 感觉类似于 -0777 (file slurp mode) ?
perl_replace "$regexp" "$replace" "g" "s" -0777 "$file"
}

function replace_multiline1 () {
local regexp=$1
local replace=$2
local file=$3

perl_replace "$regexp" "$replace" -0 "$file"
perl_replace "$regexp" "$replace" "" "s" -0777 "$file"
}

# disalbe_proxy 并没有停止 v2ray 服务.
Expand Down Expand Up @@ -99,14 +120,14 @@ function enable_proxy () {
sed -i 's#"tproxy": ".*"#"tproxy": "tproxy"#' $v2ray_config
if [ -e /opt/etc/use_fakedns ]; then
echo 'Apply fakeDNS config ...'
replace_multiline '("tag":\s*"transparent",.+?)"destOverride": \[.+?\]' '$1"destOverride": ["fakedns"]' $v2ray_config
replace_multiline1 '("tag":\s*"transparent",.+?)"destOverride": \[.+?\]' '$1"destOverride": ["fakedns"]' $v2ray_config
if ! match_multiline '"servers":\s*\[.*?"fakedns",.*?"8.8.4.4",' "$(cat $v2ray_config)"; then
replace_multiline '("servers":\s*\[)(.*?)(\s*)"8.8.4.4",' '$1$3"fakedns",$2$3"8.8.4.4",' $v2ray_config
replace_multiline1 '("servers":\s*\[)(.*?)(\s*)"8.8.4.4",' '$1$3"fakedns",$2$3"8.8.4.4",' $v2ray_config
fi
else
echo 'Apply TProxy config ...'
replace_multiline '("tag":\s*"transparent",.+?)"destOverride": \[.+?\]' '$1"destOverride": ["http", "tls"]' $v2ray_config
replace_multiline '("servers":\s*\[).*?(\s*)"8.8.4.4",' '$1$2"8.8.4.4",' $v2ray_config
replace_multiline1 '("tag":\s*"transparent",.+?)"destOverride": \[.+?\]' '$1"destOverride": ["http", "tls"]' $v2ray_config
replace_multiline1 '("servers":\s*\[).*?(\s*)"8.8.4.4",' '$1$2"8.8.4.4",' $v2ray_config
fi
else
if [ -e /opt/etc/use_fakedns ]; then
Expand Down

0 comments on commit d0b98e2

Please sign in to comment.