install 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
  4. #
  5. # smartdns is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # smartdns is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. INST_DIR=$(cd $(dirname $0);pwd)
  18. ISWSL=1 # 1 means not WSL, 0 means wsl
  19. showhelp()
  20. {
  21. echo "Usage: install [OPTION]"
  22. echo "Options:"
  23. echo " -i install smartdns."
  24. echo " -u uninstall smartdns."
  25. echo " -U upgrade install smartdns."
  26. echo " --prefix [dir] prefix directory."
  27. echo " -h show this message."
  28. }
  29. start_service()
  30. {
  31. if [ $ISSYSTEMD -ne 0 ]; then
  32. chkconfig smartdns on >/dev/null 2>&1
  33. service smartdns start
  34. return $?
  35. fi
  36. systemctl daemon-reload
  37. systemctl enable smartdns
  38. systemctl start smartdns
  39. }
  40. stop_service()
  41. {
  42. if [ $ISSYSTEMD -ne 0 ]; then
  43. service smartdns stop
  44. chkconfig smartdns off >/dev/null 2>&1
  45. return 0
  46. fi
  47. systemctl stop smartdns
  48. systemctl disable smartdns
  49. return 0
  50. }
  51. clean_service()
  52. {
  53. if [ $ISSYSTEMD -ne 0 ]; then
  54. return 0
  55. fi
  56. systemctl daemon-reload
  57. }
  58. get_systemd_path()
  59. {
  60. service="`systemctl --no-legend| grep '\.service' | head -n 1 | awk '{print $1}' 2>/dev/null`"
  61. SERVICE_PATH="`systemctl show $service | grep FragmentPath | awk -F'=' '{print $2}' 2>/dev/null`"
  62. if [ ! -z "$SERVICE_PATH" ]; then
  63. SERVICE_PATH="`dirname $SERVICE_PATH 2>/dev/null`"
  64. if [ -d "$SERVICE_PATH" ]; then
  65. echo "$SERVICE_PATH"
  66. return 0
  67. fi
  68. fi
  69. SERVICE_PATH="`pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null`"
  70. if [ ! -z "$SERVICE_PATH" ]; then
  71. if [ -d "$SERVICE_PATH" ]; then
  72. echo "$SERVICE_PATH"
  73. return 0
  74. fi
  75. fi
  76. SERVICE_PATH="/lib/systemd/system"
  77. if [ -d "$SERVICE_PATH" ]; then
  78. echo "$SERVICE_PATH"
  79. return 0
  80. fi
  81. return 1
  82. }
  83. install_files()
  84. {
  85. install -v -d $SMARTDNS_CONF_DIR
  86. if [ $? -ne 0 ]; then
  87. return 1
  88. fi
  89. install -v -m 0755 -t $PREFIX/usr/sbin usr/sbin/smartdns
  90. if [ $? -ne 0 ]; then
  91. return 1
  92. fi
  93. if [ -e "$PREFIX$SMARTDNS_CONF_DIR/smartdns.conf" ]; then
  94. cp etc/smartdns/smartdns.conf $PREFIX$SMARTDNS_CONF_DIR/smartdns.conf.pkg
  95. else
  96. install -v -m 0640 -t $PREFIX$SMARTDNS_CONF_DIR etc/smartdns/smartdns.conf
  97. if [ $? -ne 0 ]; then
  98. return 1
  99. fi
  100. fi
  101. install -v -m 0640 -t $PREFIX/etc/default etc/default/smartdns
  102. if [ $? -ne 0 ]; then
  103. return 1
  104. fi
  105. install -v -m 0755 -t $SMARTDNS_INIT_DIR etc/init.d/smartdns
  106. if [ $? -ne 0 ]; then
  107. if [ $ISSYSTEMD -ne 0 ]; then
  108. return 1
  109. fi
  110. fi
  111. if [ $ISSYSTEMD -eq 0 ]; then
  112. SYSTEM_UNIT_PATH="`get_systemd_path`"
  113. if [ -z "$SYSTEM_UNIT_PATH" ]; then
  114. echo "cannot find systemd path"
  115. return 1
  116. fi
  117. install -v -m 0644 -t $PREFIX$SYSTEM_UNIT_PATH systemd/smartdns.service
  118. if [ $? -ne 0 ]; then
  119. return 1
  120. fi
  121. fi
  122. return 0
  123. }
  124. uninstall_smartdns()
  125. {
  126. if [ -z "$PREFIX" ]; then
  127. stop_service 2>/dev/null
  128. fi
  129. rmdir $PREFIX$SMARTDNS_CONF_DIR 2>/dev/null
  130. rm -f $PREFIX/usr/sbin/smartdns
  131. rm -f $PREFIX/etc/default/smartdns
  132. if [ $ISWSL -eq 0 ]; then
  133. sed -i '\#%sudo ALL=NOPASSWD: /etc/init.d/smartdns#d' /etc/sudoers 2>/dev/null
  134. fi
  135. if [ $ISSYSTEMD -eq 0 ]; then
  136. SYSTEM_UNIT_PATH="`get_systemd_path`"
  137. if [ ! -z "$SYSTEM_UNIT_PATH" ]; then
  138. rm -f $PREFIX$SYSTEM_UNIT_PATH/smartdns.service
  139. fi
  140. fi
  141. if [ -z "$PREFIX" ]; then
  142. clean_service
  143. fi
  144. }
  145. install_smartdns()
  146. {
  147. local ret
  148. which smartdns >/dev/null 2>&1
  149. if [ $? -eq 0 ]; then
  150. echo "Already installed."
  151. return 1
  152. fi
  153. install_files
  154. ret=$?
  155. if [ $ret -ne 0 ]; then
  156. uninstall_smartdns
  157. return $ret
  158. fi
  159. if [ -z "$PREFIX" ]; then
  160. start_service
  161. fi
  162. if [ $ISWSL -eq 0 ]; then
  163. grep "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" /etc/sudoers >/dev/null 2>&1
  164. if [ $? -ne 0 ]; then
  165. echo "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" >> /etc/sudoers
  166. fi
  167. fi
  168. return 0
  169. }
  170. init_dir()
  171. {
  172. local ID=`id -u`
  173. if [ $ID -ne 0 ]; then
  174. echo "Please run as root."
  175. return 1
  176. fi
  177. SMARTDNS_CONF_DIR=$PREFIX/etc/smartdns
  178. SMARTDNS_INIT_DIR=$PREFIX/etc/init.d
  179. which systemctl >/dev/null 2>&1
  180. ISSYSTEMD="$?"
  181. # Running under WSL (Windows Subsystem for Linux)?
  182. cat /proc/version | grep -E '[Mm]icrosoft' >/dev/null 2>&1;
  183. if [ $? -eq 0 ]; then
  184. ISSYSTEMD=1
  185. ISWSL=0
  186. fi
  187. cd $INST_DIR
  188. }
  189. main()
  190. {
  191. ACTION=""
  192. OPTS=`getopt -o iuhU --long help,prefix: \
  193. -n "" -- "$@"`
  194. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  195. # Note the quotes around `$TEMP': they are essential!
  196. eval set -- "$OPTS"
  197. while true; do
  198. case "$1" in
  199. --prefix)
  200. PREFIX="$2"
  201. shift 2;;
  202. -h | --help )
  203. showhelp
  204. return 0
  205. shift ;;
  206. -i )
  207. ACTION="INSTALL"
  208. shift ;;
  209. -u )
  210. ACTION="UNINSTALL"
  211. shift ;;
  212. -U )
  213. ACTION="UPGRADE"
  214. shift ;;
  215. -- ) shift; break ;;
  216. * ) break ;;
  217. esac
  218. done
  219. init_dir
  220. if [ -z "$ACTION" ]; then
  221. showhelp
  222. return 0
  223. elif [ "$ACTION" = "INSTALL" ]; then
  224. install_smartdns
  225. return $?
  226. elif [ "$ACTION" = "UNINSTALL" ]; then
  227. uninstall_smartdns
  228. return 0
  229. elif [ "$ACTION" = "UPGRADE" ]; then
  230. uninstall_smartdns
  231. install_smartdns
  232. return $?
  233. else
  234. showhelp
  235. return 1
  236. fi
  237. }
  238. main $@
  239. exit $?