#!/bin/sh
# Copyright (C) Juewuy

echo "***********************************************"
echo "**                 欢迎使用                  **"
echo "**             Clash for Miwifi              **"
echo "**                             by  Juewuy    **"
echo "***********************************************"
Mark1=""
Mark2="\033[31m未设置开机启动！\033[0m"
Mark3="Tun模式"

clashlink(){
echo 施工中，敬请期待！
clashsh
} 
clashadv(){
echo -----------------------------------------------
echo -e "\033[33m欢迎使用高级模式菜单：\033[0m"
echo 1 切换代理模式（Tun/Redir）
echo 2 更新clash核心文件（施工中）
echo 3 更新GeoIP数据库（施工中）
echo 4 更新管理脚本（施工中）
echo 9 卸载clash
echo 0 返回上级菜单 
read -p "请输入对应数字 > " num
if [[ $num -le 9 ]] > /dev/null 2>&1; then 
  if [[ $num == 0 ]]; then
    clashsh
  
  elif [[ $num == 1 ]]; then
    echo -----------------------------------------------
    echo -e "当前代理模式为：\033[47;30m $Mark3 \033[0m"
	echo -e "\033[33m切换模式时会重启clash服务，可能会导致短时间的网络中断！\033[0m"
	echo "1 Tun模式：  支持UDP转发且延迟低"
	echo "             但CPU及内存占用更高"
	echo "             适合外服游戏用户"
	echo "2 Redir模式：CPU以及内存占用较低"
	echo "             但不支持UDP流量转发"
	echo "             日常使用推荐此模式"
	echo 0 返回上级菜单 
	read -p "请输入对应数字 > " num
	if [[ $num == 0 ]]; then
	  clashadv
	elif [[ $num == 1 ]]; then
	  if [[ $Mark3 = "Redir模式" ]]; then
		sed -i '1,20s/Redir模式/Tun模式/' /bin/clash		#修改Mark3标记
	    sed -i '1,40s/tun: {enable: false/tun: {enable: true/' /etc/clash/config.yaml		#修改配置文件
		if [ $Number1 -gt 0 ];then
		echo -----------------------------------------------
		echo -e "\033[33m正在重启clash进程……\033[0m"
		/etc/init.d/clash stop > /dev/null 2>&1
		fi	  
		/etc/init.d/clash start
		Number2=`ps |grep -w /etc/clash/clash|grep -v grep|wc -l`	  
		if [ $Number2 -gt 0 ];then
		echo -----------------------------------------------
		echo -e "\033[32mclash服务已启动！\033[0m"
		echo -e "\033[33mclash已成功切换为：\033[47;34m Tun模式! \033[0m"
		echo -e 可以使用 "\033[32mhttp://clash.razord.top\033[0m"（IP为网关IP，端口为9999）管理clash内置规则
		Mark3="Tun模式"
		clashsh
		else
		echo -----------------------------------------------
		echo -e "\033[31mclash服务启动失败！请检查配置文件！\033[0m"
		clashsh
		fi  
	  else
	    echo -----------------------------------------------
		echo -e "\033[33m当前已经处于Tun模式，无需重复设置！\033[0m"
		clashadv
	  fi
	  
	elif [[ $num == 2 ]]; then
	  if [[ $Mark3 = "Tun模式" ]]; then
		sed -i '1,20s/Tun模式/Redir模式/' /bin/clash		#修改Mark3标记
	  	sed -i '1,40s/tun: {enable: true/tun: {enable: false/' /etc/clash/config.yaml		#修改配置文件
		if [ $Number1 -gt 0 ];then
		echo -----------------------------------------------
		echo -e "\033[33m正在重启clash进程……\033[0m"
		/etc/init.d/clash stop > /dev/null 2>&1
		fi	  
		/etc/init.d/clash start
		Number2=`ps |grep -w /etc/clash/clash|grep -v grep|wc -l`	  
		if [ $Number2 -gt 0 ];then
		echo -----------------------------------------------
		echo -e "\033[32mclash服务已启动！\033[0m"
		echo -e "\033[33mclash已成功切换为：\033[47;34m Redir模式! \033[0m"
		echo -e 可以使用 "\033[32mhttp://clash.razord.top\033[0m"（IP为网关IP，端口为9999）管理clash内置规则
		Mark3="Redir模式"
		clashsh
		else
		echo -----------------------------------------------
		echo -e "\033[31mclash服务启动失败！请检查配置文件！\033[0m"
		clashsh
		fi  
	  else
	    echo -----------------------------------------------
		echo -e "\033[33m当前已经处于Redir模式，无需重复设置！\033[0m"
		clashadv
	  fi
	else
	  echo -e "\033[31m请输入正确的数字！\033[0m"
      clashadv
	fi
	
  elif [[ $num == 9 ]]; then
    read -p "确认卸载clash？（警告：该操作不可逆！）[1/0] " res
	if [ "$res" = '1' ]; then
    /etc/init.d/clash disable
    /etc/init.d/clash stop
    rm -rf /etc/clash
    rm /etc/init.d/clash
    rm /bin/clash
    echo 已卸载clash相关文件！
	fi
    exit;
  else
    echo -e "\033[31m暂未支持的选项！\033[0m"
    clashadv
  fi
else
  echo -e "\033[31m请输入正确的数字！\033[0m"
  clashsh
fi
exit;
}
clashsh(){
Number1=`ps |grep -w /etc/clash/clash|grep -v grep|wc -l`
if [ $Number1 -gt 0 ];then
Mark1="\033[32m正在运行（$Mark3）\033[0m"
else
Mark1="\033[31m没有运行（$Mark3）\033[0m"
fi
echo -----------------------------------------------
echo -e "Clash服务$Mark1,$Mark2"
echo 1 启动/重启clash服务
echo 2 测试代理服务器连通性
echo 3 停止clash服务
echo 4 允许clash开机启动
echo 5 禁止clash开机启动
echo 6 使用链接导入节点/订阅
echo 7 高级设置
echo 0 退出脚本
read -p "请输入对应数字 > " num
if [[ $num -le 7 ]] > /dev/null 2>&1; then 
  if [[ $num == 0 ]]; then
  exit;
  
  elif [[ $num == 1 ]]; then
    if [ $Number1 -gt 0 ];then
	echo -----------------------------------------------
	/etc/init.d/clash stop > /dev/null 2>&1
	echo -e "\033[31mClash服务已停止！\033[0m"
	sleep 1
	fi
    /etc/init.d/clash start
    Number2=`ps |grep -w /etc/clash/clash|grep -v grep|wc -l`
      if [ $Number2 -gt 0 ];then
	  echo -----------------------------------------------
      echo -e "\033[32mclash服务已启动！\033[0m"
	  echo 可以使用 http://clash.razord.top （IP为网关IP，端口为9999）管理clash内置规则
	  clashsh
      else
	  echo -----------------------------------------------
      echo -e "\033[31mclash服务启动失败！请检查配置文件！\033[0m"
	  clashsh
      fi
	  exit;
  
  elif [[ $num == 2 ]]; then
      echo 注意：连续多次测试可能会导致IP暂时被GitHub封禁！
      delay=`curl -kx socks5://127.0.0.1:7891 -o /dev/null -s -w '%{time_total}' 'https://github.com' & { sleep 3 ; kill $! & }` > /dev/null 2>&1
	  delay0=`expr length x$delay`
	  #echo $delay
	  if [ $delay0 == 1 ];then
      echo ----------------------------------------------
      echo '连接超时（>3s），请检查节点信息！'
	  clashsh
	  else
	  echo -----------------------------------------------
	  echo 连接成功！响应时间为：$delay秒
	  clashsh
	  fi

  elif [[ $num == 3 ]]; then
  /etc/init.d/clash stop > /dev/null 2>&1
  echo -----------------------------------------------
  echo -e "\033[31mClash服务已停止！\033[0m"
  echo -----------------------------------------------
  exit;

  elif [[ $num == 4 ]]; then
  /etc/init.d/clash enable
  sed -i '1,20s/1m未/2m已/' /bin/clash
  echo -----------------------------------------------
  echo -e "\033[32m已设置Clash开机启动！\033[0m"
  Mark2="\033[32m已设置开机启动！\033[0m"
  clashsh

  elif [[ $num == 5 ]]; then
  /etc/init.d/clash disable
  sed -i '1,20s/2m已/1m未/' /bin/clash
  echo -----------------------------------------------
  echo -e "\033[33m已禁止Clash开机启动！\033[0m"
  Mark2="\033[31m未设置开机启动！\033[0m"
  clashsh
    
  elif [[ $num == 6 ]]; then
  clashlink

  elif [[ $num == 7 ]]; then
  clashadv
  
  else
  echo -e "\033[31m请输入正确的数字！\033[0m"
  fi
  exit 1
else
echo -e "\033[31m请输入正确的数字！\033[0m"
fi
exit 1
}
clashsh