install 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. rm -f $PREFIX/etc/init.d/smartdns
  133. if [ $ISWSL -eq 0 ]; then
  134. sed -i '\#%sudo ALL=NOPASSWD: /etc/init.d/smartdns#d' /etc/sudoers 2>/dev/null
  135. fi
  136. if [ $ISSYSTEMD -eq 0 ]; then
  137. SYSTEM_UNIT_PATH="`get_systemd_path`"
  138. if [ ! -z "$SYSTEM_UNIT_PATH" ]; then
  139. rm -f $PREFIX$SYSTEM_UNIT_PATH/smartdns.service
  140. fi
  141. fi
  142. if [ -z "$PREFIX" ]; then
  143. clean_service
  144. fi
  145. }
  146. install_smartdns()
  147. {
  148. local ret
  149. which smartdns >/dev/null 2>&1
  150. if [ $? -eq 0 ]; then
  151. echo "Already installed."
  152. return 1
  153. fi
  154. install_files
  155. ret=$?
  156. if [ $ret -ne 0 ]; then
  157. uninstall_smartdns
  158. return $ret
  159. fi
  160. if [ -z "$PREFIX" ]; then
  161. start_service
  162. fi
  163. if [ $ISWSL -eq 0 ]; then
  164. grep "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" /etc/sudoers >/dev/null 2>&1
  165. if [ $? -ne 0 ]; then
  166. echo "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" >> /etc/sudoers
  167. fi
  168. fi
  169. return 0
  170. }
  171. init_dir()
  172. {
  173. local ID=`id -u`
  174. if [ $ID -ne 0 ]; then
  175. echo "Please run as root."
  176. return 1
  177. fi
  178. SMARTDNS_CONF_DIR=$PREFIX/etc/smartdns
  179. SMARTDNS_INIT_DIR=$PREFIX/etc/init.d
  180. which systemctl >/dev/null 2>&1
  181. ISSYSTEMD="$?"
  182. # Running under WSL (Windows Subsystem for Linux)?
  183. cat /proc/version | grep -E '[Mm]icrosoft' >/dev/null 2>&1;
  184. if [ $? -eq 0 ]; then
  185. ISSYSTEMD=1
  186. ISWSL=0
  187. fi
  188. cd $INST_DIR
  189. }
  190. main()
  191. {
  192. ACTION=""
  193. OPTS=`getopt -o iuhU --long help,prefix: \
  194. -n "" -- "$@"`
  195. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  196. # Note the quotes around `$TEMP': they are essential!
  197. eval set -- "$OPTS"
  198. while true; do
  199. case "$1" in
  200. --prefix)
  201. PREFIX="$2"
  202. shift 2;;
  203. -h | --help )
  204. showhelp
  205. return 0
  206. shift ;;
  207. -i )
  208. ACTION="INSTALL"
  209. shift ;;
  210. -u )
  211. ACTION="UNINSTALL"
  212. shift ;;
  213. -U )
  214. ACTION="UPGRADE"
  215. shift ;;
  216. -- ) shift; break ;;
  217. * ) break ;;
  218. esac
  219. done
  220. init_dir
  221. if [ -z "$ACTION" ]; then
  222. showhelp
  223. return 0
  224. elif [ "$ACTION" = "INSTALL" ]; then
  225. install_smartdns
  226. return $?
  227. elif [ "$ACTION" = "UNINSTALL" ]; then
  228. uninstall_smartdns
  229. return 0
  230. elif [ "$ACTION" = "UPGRADE" ]; then
  231. uninstall_smartdns
  232. install_smartdns
  233. return $?
  234. else
  235. showhelp
  236. return 1
  237. fi
  238. }
  239. main $@
  240. exit $?