Refactor configure.sh

This commit is contained in:
Sarina
2024-07-29 17:09:01 +03:30
parent fe47551904
commit 2b0020121a

View File

@ -3,20 +3,28 @@
# Source the path.sh script to load the CONFIG_FILE and CLI_PATH variables # Source the path.sh script to load the CONFIG_FILE and CLI_PATH variables
source /etc/hysteria/core/scripts/path.sh source /etc/hysteria/core/scripts/path.sh
# Check if wg-quick@wgcf.service is active check_warp_service() {
if ! systemctl is-active --quiet wg-quick@wgcf.service; then if ! systemctl is-active --quiet wg-quick@wgcf.service; then
echo "WARP is not active. Please install WARP before configuring." echo "WARP is not active. Please install WARP before configuring."
exit 1 exit 1
fi fi
}
# Check if the config file exists check_config_file() {
if [ -f "$CONFIG_FILE" ]; then if [ ! -f "$CONFIG_FILE" ]; then
# Check the current status of WARP configurations echo "Error: Config file $CONFIG_FILE not found."
exit 1
fi
}
get_status() {
warp_all_status=$(jq -r 'if .acl.inline | index("warps(all)") then "WARP active" else "Direct" end' "$CONFIG_FILE") warp_all_status=$(jq -r 'if .acl.inline | index("warps(all)") then "WARP active" else "Direct" end' "$CONFIG_FILE")
google_openai_status=$(jq -r 'if (.acl.inline | index("warps(geoip:google)")) or (.acl.inline | index("warps(geosite:google)")) or (.acl.inline | index("warps(geosite:netflix)")) or (.acl.inline | index("warps(geosite:spotify)")) or (.acl.inline | index("warps(geosite:openai)")) or (.acl.inline | index("warps(geoip:openai)")) then "WARP active" else "Direct" end' "$CONFIG_FILE") google_openai_status=$(jq -r 'if (.acl.inline | index("warps(geoip:google)")) or (.acl.inline | index("warps(geosite:google)")) or (.acl.inline | index("warps(geosite:netflix)")) or (.acl.inline | index("warps(geosite:spotify)")) or (.acl.inline | index("warps(geosite:openai)")) or (.acl.inline | index("warps(geoip:openai)")) then "WARP active" else "Direct" end' "$CONFIG_FILE")
iran_status=$(jq -r 'if (.acl.inline | index("warps(geosite:ir)")) and (.acl.inline | index("warps(geoip:ir)")) then "Use WARP" else "Reject" end' "$CONFIG_FILE") iran_status=$(jq -r 'if (.acl.inline | index("warps(geosite:ir)")) and (.acl.inline | index("warps(geoip:ir)")) then "Use WARP" else "Reject" end' "$CONFIG_FILE")
adult_content_status=$(jq -r 'if .acl.inline | index("reject(geosite:category-porn)") then "Blocked" else "Not blocked" end' "$CONFIG_FILE") adult_content_status=$(jq -r 'if .acl.inline | index("reject(geosite:category-porn)") then "Blocked" else "Not blocked" end' "$CONFIG_FILE")
}
display_menu() {
echo "===== Configuration Menu =====" echo "===== Configuration Menu ====="
echo "1. Use WARP for all traffic ($warp_all_status)" echo "1. Use WARP for all traffic ($warp_all_status)"
echo "2. Use WARP for Google, OpenAI, etc. ($google_openai_status)" echo "2. Use WARP for Google, OpenAI, etc. ($google_openai_status)"
@ -24,55 +32,74 @@ if [ -f "$CONFIG_FILE" ]; then
echo "4. Block adult content ($adult_content_status)" echo "4. Block adult content ($adult_content_status)"
echo "5. Back to Advance Menu" echo "5. Back to Advance Menu"
echo "===================================" echo "==================================="
}
read -p "Enter your choice: " choice update_config() {
case $choice in local jq_command=$1
1) jq "$jq_command" "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE"
}
configure_warp_all() {
if [ "$warp_all_status" == "WARP active" ]; then if [ "$warp_all_status" == "WARP active" ]; then
jq 'del(.acl.inline[] | select(. == "warps(all)"))' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config 'del(.acl.inline[] | select(. == "warps(all)"))'
echo "Traffic configuration changed to Direct." echo "Traffic configuration changed to Direct."
else else
jq '.acl.inline += ["warps(all)"]' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config '.acl.inline += ["warps(all)"]'
echo "Traffic configuration changed to WARP." echo "Traffic configuration changed to WARP."
fi fi
;; }
2)
configure_google_openai() {
if [ "$google_openai_status" == "WARP active" ]; then if [ "$google_openai_status" == "WARP active" ]; then
jq 'del(.acl.inline[] | select(. == "warps(geoip:google)" or . == "warps(geosite:google)" or . == "warps(geosite:netflix)" or . == "warps(geosite:spotify)" or . == "warps(geosite:openai)" or . == "warps(geoip:openai)"))' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config 'del(.acl.inline[] | select(. == "warps(geoip:google)" or . == "warps(geosite:google)" or . == "warps(geosite:netflix)" or . == "warps(geosite:spotify)" or . == "warps(geosite:openai)" or . == "warps(geoip:openai)"))'
echo "WARP configuration for Google, OpenAI, etc. removed." echo "WARP configuration for Google, OpenAI, etc. removed."
else else
jq '.acl.inline += ["warps(geoip:google)", "warps(geosite:google)", "warps(geosite:netflix)", "warps(geosite:spotify)", "warps(geosite:openai)", "warps(geoip:openai)"]' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config '.acl.inline += ["warps(geoip:google)", "warps(geosite:google)", "warps(geosite:netflix)", "warps(geosite:spotify)", "warps(geosite:openai)", "warps(geoip:openai)"]'
echo "WARP configured for Google, OpenAI, etc." echo "WARP configured for Google, OpenAI, etc."
fi fi
;; }
3)
configure_iran() {
if [ "$iran_status" == "Use WARP" ]; then if [ "$iran_status" == "Use WARP" ]; then
jq '(.acl.inline[] | select(. == "warps(geosite:ir)")) = "reject(geosite:ir)" | (.acl.inline[] | select(. == "warps(geoip:ir)")) = "reject(geoip:ir)"' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config '(.acl.inline[] | select(. == "warps(geosite:ir)")) = "reject(geosite:ir)" | (.acl.inline[] | select(. == "warps(geoip:ir)")) = "reject(geoip:ir)"'
echo "Configuration changed to Reject for geosite:ir and geoip:ir." echo "Configuration changed to Reject for geosite:ir and geoip:ir."
else else
jq '(.acl.inline[] | select(. == "reject(geosite:ir)")) = "warps(geosite:ir)" | (.acl.inline[] | select(. == "reject(geoip:ir)")) = "warps(geoip:ir)"' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config '(.acl.inline[] | select(. == "reject(geosite:ir)")) = "warps(geosite:ir)" | (.acl.inline[] | select(. == "reject(geoip:ir)")) = "warps(geoip:ir)"'
echo "Configuration changed to Use WARP for geosite:ir and geoip:ir." echo "Configuration changed to Use WARP for geosite:ir and geoip:ir."
fi fi
;; }
4)
configure_adult_content() {
if [ "$adult_content_status" == "Blocked" ]; then if [ "$adult_content_status" == "Blocked" ]; then
jq 'del(.acl.inline[] | select(. == "reject(geosite:category-porn)"))' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config 'del(.acl.inline[] | select(. == "reject(geosite:category-porn)"))'
jq '.resolver.tls.addr = "1.1.1.1:853"' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config '.resolver.tls.addr = "1.1.1.1:853"'
echo "Adult content blocking removed and resolver updated." echo "Adult content blocking removed and resolver updated."
else else
jq '.acl.inline += ["reject(geosite:category-porn)"]' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config '.acl.inline += ["reject(geosite:category-porn)"]'
jq '.resolver.tls.addr = "1.1.1.3:853"' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" update_config '.resolver.tls.addr = "1.1.1.3:853"'
echo "Adult content blocked and resolver updated." echo "Adult content blocked and resolver updated."
fi fi
;; }
5)
exit 0 handle_choice() {
;; case $choice in
*) 1) configure_warp_all ;;
echo "Invalid option. Please try again." 2) configure_google_openai ;;
;; 3) configure_iran ;;
4) configure_adult_content ;;
5) exit 0 ;;
*) echo "Invalid option. Please try again." ;;
esac esac
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1 python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
else }
echo "Error: Config file $CONFIG_FILE not found."
fi main() {
check_warp_service
check_config_file
get_status
display_menu
read -p "Enter your choice: " choice
handle_choice
}
main