You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
9.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2016-2017 Hsing-wang Liao <kuoruan@gmail.com>
  3. # Licensed to the public under the Apache License 2.0.
  4. START=99
  5. USE_PROCD=1
  6. NAME=aria2
  7. PROG=/usr/bin/aria2c
  8. _info() {
  9. logger -p daemon.info -t "$NAME" "$*"
  10. }
  11. _err() {
  12. logger -p daemon.err -t "$NAME" "$*"
  13. }
  14. _make_dir() {
  15. local d
  16. for d in "$@"; do
  17. if [ ! -d "$d" ]; then
  18. mkdir -p "$d" 2>/dev/null || return 1
  19. fi
  20. done
  21. return 0
  22. }
  23. _create_file() {
  24. touch "$@" 2>/dev/null
  25. }
  26. _change_owner() {
  27. local u="$1"; shift
  28. local d
  29. for d in "$@"; do
  30. if [ -f "$d" ]; then
  31. chown "$u" "$d" 2>/dev/null || return 1
  32. elif [ -d "$d" ]; then
  33. chown -R "$u" "$d" 2>/dev/null || return 1
  34. fi
  35. done
  36. return 0
  37. }
  38. _change_file_mode() {
  39. local mod="$1"; shift
  40. chmod "$mod" "$@" 2>/dev/null
  41. }
  42. _reset_dir_mode() {
  43. local d
  44. for d in "$@"; do
  45. if [ -d "$d" ]; then
  46. find "$d" -type d -exec chmod 755 {} \; 2>/dev/null
  47. find "$d" -type f -exec chmod 644 {} \; 2>/dev/null
  48. fi
  49. done
  50. }
  51. append_options() {
  52. local o; local v
  53. for o in "$@"; do
  54. v="$(eval echo "\$$o")"
  55. [ -n "$v" ] && \
  56. echo "${o//_/-}=$v" >>"$config_file_tmp"
  57. done
  58. }
  59. append_setting() {
  60. local s="$1"
  61. [ -n "$s" ] && \
  62. echo "$s" >>"$config_file_tmp"
  63. }
  64. append_header() {
  65. local h="$1"
  66. [ -n "$h" ] && \
  67. echo "header=\"${h}\"" >>"$config_file_tmp"
  68. }
  69. aria2_validate() {
  70. uci_validate_section "$NAME" aria2 "$1" \
  71. 'enabled:bool:0' \
  72. 'enable_logging:bool' \
  73. 'enable_proxy:bool' \
  74. 'config_dir:string:/var/etc/aria2' \
  75. 'user:string' \
  76. 'all_proxy:string' \
  77. 'all_proxy_passwd:string' \
  78. 'all_proxy_user:string' \
  79. 'auto_save_interval:range(0,600)' \
  80. 'bt_enable_lpd:or("true","false")' \
  81. 'bt_max_open_files:uinteger' \
  82. 'bt_max_peers:uinteger' \
  83. 'bt_remove_unselected_file:or("true","false")' \
  84. 'bt_request_peer_speed_limit:string' \
  85. 'bt_save_metadata:or("true","false")' \
  86. 'bt_seed_unverified:or("true","false")' \
  87. 'bt_stop_timeout:uinteger' \
  88. 'bt_tracker:list(string)' \
  89. 'ca_certificate:file' \
  90. 'certificate:file' \
  91. 'check_certificate:or("true","false"):true' \
  92. 'connect_timeout:uinteger' \
  93. 'dht_listen_port:string' \
  94. 'dir:string' \
  95. 'disable_ipv6:or("true","false")' \
  96. 'disk_cache:string' \
  97. 'enable_dht:or("true","false"):true' \
  98. 'enable_dht6:or("true","false")' \
  99. 'enable_peer_exchange:or("true","false")' \
  100. 'event_poll:or("epoll","kqueue","port","poll","select")' \
  101. 'file_allocation:or("none","prealloc","trunc","falloc")' \
  102. 'follow_torrent:or("true","false","mem")' \
  103. 'force_save:or("true","false")' \
  104. 'http_accept_gzip:or("true","false")' \
  105. 'http_no_cache:or("true","false")' \
  106. 'listen_port:string' \
  107. 'log:string' \
  108. 'log_level:or("debug","info","notice","warn","error")' \
  109. 'lowest_speed_limit:string' \
  110. 'max_concurrent_downloads:uinteger' \
  111. 'max_connection_per_server:uinteger' \
  112. 'max_download_limit:string' \
  113. 'max_overall_download_limit:string' \
  114. 'max_overall_upload_limit:string' \
  115. 'max_tries:uinteger' \
  116. 'max_upload_limit:string' \
  117. 'min_split_size:string' \
  118. 'pause:or("true","false")' \
  119. 'pause_metadata:or("true","false")' \
  120. 'peer_id_prefix:string' \
  121. 'private_key:file' \
  122. 'retry_wait:uinteger' \
  123. 'rpc_auth_method:or("none","user_pass","token")' \
  124. 'rpc_certificate:file' \
  125. 'rpc_listen_port:range(1024,65535)' \
  126. 'rpc_passwd:string' \
  127. 'rpc_private_key:file' \
  128. 'rpc_secret:string' \
  129. 'rpc_secure:or("true","false")' \
  130. 'rpc_user:string' \
  131. 'save_session_interval:uinteger' \
  132. 'seed_ratio:ufloat' \
  133. 'seed_time:ufloat' \
  134. 'split:uinteger' \
  135. 'timeout:uinteger' \
  136. 'user_agent:string'
  137. }
  138. aria2_start() {
  139. local section="$1"
  140. aria2_validate "$section" || { _err "Validation failed."; return 1; }
  141. [ "$enabled" = "1" ] || { _info "Instance \"${section}\" disabled."; return 1; }
  142. [ -n "$dir" ] || { _err "Please set downlod dir."; return 1; }
  143. [ -d "$dir" ] || { _err "Please create downlod dir first."; return 1; }
  144. config_file="${config_dir}/${NAME}.conf.${section}"
  145. config_file_tmp="${config_dir}/${NAME}.conf.tmp"
  146. session_file="${config_dir}/${NAME}.session.${section}"
  147. _make_dir "$config_dir" || {
  148. _err "Can't create config dir: ${config_dir}"
  149. return 1
  150. }
  151. _create_file "$session_file" "$config_file" "$config_file_tmp" || {
  152. _err "Can't create files: ${session_file}, ${config_file}, ${config_file_tmp}"
  153. return 1
  154. }
  155. # create tmp file
  156. cat >"$config_file_tmp" <<-EOF
  157. # Auto generated file, changes to this file will lost.
  158. EOF
  159. append_setting "dir=${dir}"
  160. append_setting "enable-rpc=true"
  161. append_setting "rpc-allow-origin-all=true"
  162. append_setting "rpc-listen-all=true"
  163. append_setting "quiet=true"
  164. append_setting "continue=true"
  165. append_setting "input-file=${session_file}"
  166. append_setting "save-session=${session_file}"
  167. if [ -z "$enable_logging" ]; then
  168. append_options "log" "log_level"
  169. elif [ "$enable_logging" = "1" ]; then
  170. log=${log:-"/var/log/aria2.log"}
  171. local log_dir
  172. log_dir="$(dirname "$log")"
  173. _make_dir "$log_dir" || {
  174. _err "Can't create log dir: ${log_dir}"
  175. return 1
  176. }
  177. # create or clear log file
  178. echo >"$log"
  179. append_setting "log=${log}"
  180. append_options "log_level"
  181. fi
  182. if [ -z "$enable_proxy" ] || [ "$enable_proxy" = "1" ]; then
  183. append_options "all_proxy" "all_proxy_user" "all_proxy_passwd"
  184. fi
  185. unset_auth_method() {
  186. uci -q batch <<-EOF
  187. set ${NAME}.${section}.rpc_auth_method=""
  188. commit $NAME
  189. EOF
  190. }
  191. if [ -z "$rpc_auth_method" ]; then
  192. if [ -n "$rpc_secret" ]; then
  193. append_setting "rpc-secret=${rpc_secret}"
  194. elif [ -n "$rpc_user" ]; then
  195. append_setting "rpc-user=${rpc_user}"
  196. append_setting "rpc-passwd=${rpc_passwd}"
  197. else
  198. _info "It is recommand to set RPC secret."
  199. fi
  200. elif [ "$rpc_auth_method" = "token" ]; then
  201. if [ -n "$rpc_secret" ]; then
  202. append_setting "rpc-secret=${rpc_secret}"
  203. else
  204. unset_auth_method
  205. fi
  206. elif [ "$rpc_auth_method" = "user_pass" ]; then
  207. if [ -n "$rpc_user" ]; then
  208. append_setting "rpc-user=${rpc_user}"
  209. append_setting "rpc-passwd=${rpc_passwd}"
  210. else
  211. _info "Please set RPC user."
  212. unset_auth_method
  213. fi
  214. fi
  215. if [ ."$rpc_secure" = ."true" ] && [ -n "$rpc_certificate" ]; then
  216. append_setting "rpc-secure=true"
  217. append_options "rpc_certificate" "rpc_private_key"
  218. fi
  219. if [ ."$check_certificate" = ."true" ]; then
  220. append_setting "check-certificate=true"
  221. append_options "ca_certificate"
  222. elif [ ."$check_certificate" = ."false" ]; then
  223. append_setting "check-certificate=false"
  224. fi
  225. if [ ."$enable_dht" = ."true" ]; then
  226. dht_file="${config_dir}/dht.dat.${section}"
  227. _create_file "$dht_file" || {
  228. _err "Can't create DHT file: ${dht_file}"
  229. return 1
  230. }
  231. append_setting "enable-dht=true"
  232. append_setting "dht-file-path=${dht_file}"
  233. fi
  234. if [ ."$enable_dht6" = ."true" ] && [ ."$disable_ipv6" != ."true" ]; then
  235. dht6_file="${config_dir}/dht6.dat.${section}"
  236. _create_file "$dht6_file" || {
  237. _err "Can't create DHT6 file: ${dht6_file}"
  238. return 1
  239. }
  240. append_setting "enable-dht6=true"
  241. append_setting "dht-file-path6=${dht6_file}"
  242. fi
  243. if [ -n "$bt_tracker" ]; then
  244. local bt_tracker_list; local t
  245. for t in $bt_tracker; do
  246. if [ -z "$bt_tracker_list" ]; then
  247. bt_tracker_list="$t"
  248. else
  249. bt_tracker_list="${bt_tracker_list},${t}"
  250. fi
  251. done
  252. append_setting "bt-tracker=${bt_tracker_list}"
  253. fi
  254. append_options "auto_save_interval" "bt_enable_lpd" "bt_max_open_files" "bt_max_peers" \
  255. "bt_remove_unselected_file" "bt_request_peer_speed_limit" "bt_save_metadata" "bt_seed_unverified" \
  256. "bt_stop_timeout" "certificate" "connect_timeout" "dht_listen_port" "disable_ipv6" "disk_cache" \
  257. "enable_peer_exchange" "event_poll" "file_allocation" "follow_torrent" "force_save" "http_accept_gzip" \
  258. "http_no_cache" "listen_port" "lowest_speed_limit" "max_concurrent_downloads" "max_connection_per_server" \
  259. "max_download_limit" "max_overall_download_limit" "max_overall_upload_limit" "max_tries" \
  260. "max_upload_limit" "min_split_size" "pause" "pause_metadata" "peer_id_prefix" "private_key" \
  261. "retry_wait" "rpc_listen_port" "save_session_interval" "seed_ratio" "seed_time" "split" "timeout" \
  262. "user_agent"
  263. config_list_foreach "$section" "header" append_header
  264. config_list_foreach "$section" "extra_settings" append_setting
  265. sed '/^$/d' "$config_file_tmp" >"$config_file"
  266. rm -f "$config_file_tmp"
  267. _reset_dir_mode "$config_dir"
  268. _change_file_mode 600 "$config_file"
  269. if [ -n "$user" ]; then
  270. if ( user_exists "$user" && _change_owner "$user" "$config_dir" "$log" ); then
  271. _info "Aria2 will run with uer '${user}'."
  272. if [ "$user" != "root" ]; then
  273. _info "Please make sure user '${user}' has write access to downlod dir: ${dir}"
  274. fi
  275. else
  276. _info "Set run user to '${user}' failed, default user will be used."
  277. user=
  278. fi
  279. fi
  280. procd_open_instance "${NAME}.${section}"
  281. procd_set_param command "$PROG"
  282. procd_append_param command --conf-path="${config_file}"
  283. procd_set_param respawn
  284. procd_set_param stdout 1
  285. procd_set_param stderr 1
  286. procd_set_param file "$config_file"
  287. [ -n "$user" ] && \
  288. procd_set_param user "$user"
  289. procd_add_jail "${NAME}.${section}" log
  290. procd_add_jail_mount "$config_file"
  291. procd_add_jail_mount_rw "$dir" "$config_dir" "$log"
  292. procd_close_instance
  293. }
  294. service_triggers() {
  295. procd_add_reload_trigger "$NAME"
  296. }
  297. start_service() {
  298. config_load "$NAME"
  299. config_foreach aria2_start "aria2"
  300. }