github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/scripts/openwrt/yuhaiin (about)

     1  #!/bin/sh /etc/rc.common
     2  # Copyright (C) 2007-2012 OpenWrt.org
     3  
     4  START=99
     5  
     6  USE_PROCD=1
     7  PROG=${HOME}/yuhaiin_alpine
     8  
     9  HOST=${HOST:-0.0.0.0:50051}
    10  CONFIG_PATH=${YUHAIIN_CONFIG_PATH:-${HOME}/yuhaiin}
    11  CONFIG_FILE=${CONFIG_PATH}/config.json
    12  
    13  TUN_NAME=$(jq -r '(.server.servers.tun.tun.name) | split("tun://")[] | split("fd://")[]' ${CONFIG_FILE})
    14  TUN_NAME=${TUN_NAME:-yuhaiin}
    15  
    16  PREFIX_ADDR=$(jq -r '(.server.servers.tun.tun.gateway)' ${CONFIG_FILE})
    17  PREFIX_ADDR=${PREFIX_ADDR:-172.19.0.1}
    18  MASK_SIZE=${MASK_SIZE:-24}
    19  
    20  PREFIX=${PREFIX_ADDR}/${MASK_SIZE}
    21  
    22  TABLE=${TABLE:-233}
    23  LAN_IPS=${LAN_IPS:-"192.168.2.145 192.168.122.27"}
    24  
    25  RULES="
    26  default
    27  "
    28  
    29  start_service() {
    30      if [ -z "$(ip tuntap list | grep "${TUN_NAME}: tun")" ]; then
    31          logger "add tun device ${TUN_NAME}"
    32          ip tuntap add mode tun dev ${TUN_NAME}
    33      fi
    34  
    35      if [ -z "$(ip addr show dev ${TUN_NAME} to ${PREFIX})" ]; then
    36          logger "add addr ${PREFIX} to dev ${TUN_NAME}"
    37          ip addr add ${PREFIX} dev ${TUN_NAME}
    38      fi
    39  
    40      if [ -z "$(ip link show ${TUN_NAME} up)" ]; then
    41          logger "set dev ${TUN_NAME} up"
    42          ip link set dev ${TUN_NAME} up
    43      fi
    44  
    45      ip route flush table ${TABLE}
    46  
    47      for rule in ${RULES}; do
    48          logger "add rule $rule to table ${TABLE}"
    49          ip route replace $rule dev ${TUN_NAME} table ${TABLE}
    50      done
    51  
    52      for addr in ${LAN_IPS}; do
    53          logger "add lan $addr"
    54          ip rule add from ${addr} lookup ${TABLE} priority 30001
    55      done
    56  
    57      if [ -n "$(iptables -C FORWARD -o ${TUN_NAME} -j ACCEPT 2>&1)" ]; then
    58          logger "iptables -I FORWARD -o ${TUN_NAME} -j ACCEPT"
    59          iptables -I FORWARD -o ${TUN_NAME} -j ACCEPT
    60      fi
    61  
    62      if [ -n "$(iptables -C FORWARD -i ${TUN_NAME} -j ACCEPT 2>&1)" ]; then
    63          logger "iptables -I FORWARD -i ${TUN_NAME} -j ACCEPT"
    64          iptables -I FORWARD -i ${TUN_NAME} -j ACCEPT
    65      fi
    66  
    67      procd_open_instance yuhaiin
    68      procd_set_param command $PROG -host $HOST -path $CONFIG_PATH
    69      procd_set_param stdout 1
    70      procd_set_param stderr 1
    71  
    72      procd_close_instance
    73  }
    74  
    75  stop_service() {
    76      procd_send_signal $PROG
    77  
    78      iptables -D FORWARD -o ${TUN_NAME} -j ACCEPT
    79      iptables -D FORWARD -i ${TUN_NAME} -j ACCEPT
    80  
    81      for addr in ${LAN_IPS}; do
    82          logger "del lan $addr"
    83          ip rule del from ${addr} lookup ${TABLE} priority 30001
    84      done
    85  
    86      ip route flush table ${TABLE}
    87  }
    88  
    89  restart_service() {
    90      stop
    91      start
    92  }