install 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 -d $SMARTDNS_UI_WWWROOT
  90. if [ $? -ne 0 ]; then
  91. return 1
  92. fi
  93. install -v -d $SMARTDNS_PLUIGN_DIR
  94. if [ $? -ne 0 ]; then
  95. return 1
  96. fi
  97. install -v -t $SMARTDNS_PLUIGN_DIR $INST_DIR/usr/local/lib/smartdns/smartdns_ui.so
  98. if [ $? -ne 0 ]; then
  99. echo "smartdns-ui plugin not found, skipping copy."
  100. fi
  101. if [ -d "$INST_DIR/usr/local/lib/smartdns" ]; then
  102. cp $INST_DIR/usr/local/lib/smartdns/* $SMARTDNS_PLUIGN_DIR/ -a
  103. if [ $? -ne 0 ]; then
  104. echo "Failed to copy smartdns library files."
  105. return 1
  106. fi
  107. fi
  108. if [ -d "$INST_DIR/usr/share/smartdns/wwwroot/" ]; then
  109. cp $INST_DIR/usr/share/smartdns/wwwroot/* $SMARTDNS_UI_WWWROOT/ -a
  110. if [ $? -ne 0 ]; then
  111. echo "Failed to copy smartdns-webui files."
  112. return 1
  113. fi
  114. fi
  115. cp $INST_DIR/usr/sbin/smartdns $PREFIX/usr/sbin -a
  116. if [ $? -ne 0 ]; then
  117. return 1
  118. fi
  119. chmod +x $PREFIX/usr/sbin/smartdns
  120. if [ -e "$PREFIX$SMARTDNS_CONF_DIR/smartdns.conf" ]; then
  121. cp $INST_DIR/etc/smartdns/smartdns.conf $PREFIX$SMARTDNS_CONF_DIR/smartdns.conf.pkg
  122. else
  123. install -v -m 0640 -t $PREFIX$SMARTDNS_CONF_DIR $INST_DIR/etc/smartdns/smartdns.conf
  124. if [ $? -ne 0 ]; then
  125. return 1
  126. fi
  127. fi
  128. install -v -m 0640 -t $PREFIX/etc/default $INST_DIR/etc/default/smartdns
  129. if [ $? -ne 0 ]; then
  130. return 1
  131. fi
  132. install -v -m 0755 -t $SMARTDNS_INIT_DIR $INST_DIR/etc/init.d/smartdns
  133. if [ $? -ne 0 ]; then
  134. if [ $ISSYSTEMD -ne 0 ]; then
  135. return 1
  136. fi
  137. fi
  138. if [ $ISSYSTEMD -eq 0 ]; then
  139. SYSTEM_UNIT_PATH="`get_systemd_path`"
  140. if [ -z "$SYSTEM_UNIT_PATH" ]; then
  141. echo "cannot find systemd path"
  142. return 1
  143. fi
  144. install -v -m 0644 -t $PREFIX$SYSTEM_UNIT_PATH $INST_DIR/systemd/smartdns.service
  145. if [ $? -ne 0 ]; then
  146. return 1
  147. fi
  148. fi
  149. return 0
  150. }
  151. uninstall_smartdns()
  152. {
  153. if [ -z "$PREFIX" ]; then
  154. stop_service 2>/dev/null
  155. fi
  156. rmdir $PREFIX$SMARTDNS_CONF_DIR 2>/dev/null
  157. rm -f $PREFIX/usr/sbin/smartdns
  158. rm -f $PREFIX/etc/default/smartdns
  159. rm -f $PREFIX/etc/init.d/smartdns
  160. rm -fr $PREFIX/usr/share/smartdns/wwwroot
  161. rmdir $PREFIX/usr/share/smartdns 2>/dev/null
  162. rm -fr $PREFIX/usr/local/lib/smartdns 2>/dev/null
  163. if [ $ISWSL -eq 0 ]; then
  164. sed -i '\#%sudo ALL=NOPASSWD: /etc/init.d/smartdns#d' /etc/sudoers 2>/dev/null
  165. fi
  166. if [ $ISSYSTEMD -eq 0 ]; then
  167. SYSTEM_UNIT_PATH="`get_systemd_path`"
  168. if [ ! -z "$SYSTEM_UNIT_PATH" ]; then
  169. rm -f $PREFIX$SYSTEM_UNIT_PATH/smartdns.service
  170. fi
  171. fi
  172. if [ -z "$PREFIX" ]; then
  173. clean_service
  174. fi
  175. }
  176. install_smartdns()
  177. {
  178. local ret
  179. which smartdns >/dev/null 2>&1
  180. if [ $? -eq 0 ]; then
  181. echo "Already installed."
  182. return 1
  183. fi
  184. install_files
  185. ret=$?
  186. if [ $ret -ne 0 ]; then
  187. uninstall_smartdns
  188. return $ret
  189. fi
  190. if [ -z "$PREFIX" ]; then
  191. start_service
  192. fi
  193. if [ $ISWSL -eq 0 ]; then
  194. grep "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" /etc/sudoers >/dev/null 2>&1
  195. if [ $? -ne 0 ]; then
  196. echo "%sudo ALL=NOPASSWD: /etc/init.d/smartdns" >> /etc/sudoers
  197. fi
  198. fi
  199. return 0
  200. }
  201. init_dir()
  202. {
  203. local ID=`id -u`
  204. if [ $ID -ne 0 ]; then
  205. echo "Please run as root."
  206. return 1
  207. fi
  208. SMARTDNS_CONF_DIR=$PREFIX/etc/smartdns
  209. SMARTDNS_INIT_DIR=$PREFIX/etc/init.d
  210. SMARTDNS_UI_WWWROOT=$PREFIX/usr/share/smartdns/wwwroot
  211. SMARTDNS_PLUIGN_DIR=$PREFIX/usr/local/lib/smartdns
  212. which systemctl >/dev/null 2>&1
  213. ISSYSTEMD="$?"
  214. # Running under WSL (Windows Subsystem for Linux)?
  215. cat /proc/version | grep -E '[Mm]icrosoft' >/dev/null 2>&1;
  216. if [ $? -eq 0 ]; then
  217. ISSYSTEMD=1
  218. ISWSL=0
  219. fi
  220. cd $INST_DIR
  221. }
  222. main()
  223. {
  224. ACTION=""
  225. OPTS=`getopt -o iuhU --long help,prefix: \
  226. -n "" -- "$@"`
  227. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  228. # Note the quotes around `$TEMP': they are essential!
  229. eval set -- "$OPTS"
  230. while true; do
  231. case "$1" in
  232. --prefix)
  233. PREFIX="$2"
  234. shift 2;;
  235. -h | --help )
  236. showhelp
  237. return 0
  238. shift ;;
  239. -i )
  240. ACTION="INSTALL"
  241. shift ;;
  242. -u )
  243. ACTION="UNINSTALL"
  244. shift ;;
  245. -U )
  246. ACTION="UPGRADE"
  247. shift ;;
  248. -- ) shift; break ;;
  249. * ) break ;;
  250. esac
  251. done
  252. init_dir
  253. if [ -z "$ACTION" ]; then
  254. showhelp
  255. return 0
  256. elif [ "$ACTION" = "INSTALL" ]; then
  257. install_smartdns
  258. return $?
  259. elif [ "$ACTION" = "UNINSTALL" ]; then
  260. uninstall_smartdns
  261. return 0
  262. elif [ "$ACTION" = "UPGRADE" ]; then
  263. uninstall_smartdns
  264. install_smartdns
  265. return $?
  266. else
  267. showhelp
  268. return 1
  269. fi
  270. }
  271. main $@
  272. exit $?