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.

348 lines
9.0 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 "rcp-passwd=${rcp-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 "$rcp_user" ]; then
  208. append_setting "rpc-user=${rpc_user}"
  209. append_setting "rcp-passwd=${rcp-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. fi
  223. if [ ."$enable_dht" = ."true" ]; then
  224. dht_file="${config_dir}/dht.dat.${section}"
  225. _create_file "$dht_file" || {
  226. _err "Can't create DHT file: ${dht_file}"
  227. return 1
  228. }
  229. append_setting "enable-dht=true"
  230. append_setting "dht-file-path=${dht_file}"
  231. fi
  232. if [ ."$enable_dht6" = ."true" ] && [ ."$disable_ipv6" != ."true" ]; then
  233. dht6_file="${config_dir}/dht6.dat.${section}"
  234. _create_file "$dht6_file" || {
  235. _err "Can't create DHT6 file: ${dht6_file}"
  236. return 1
  237. }
  238. append_setting "enable-dht6=true"
  239. append_setting "dht-file-path6=${dht6_file}"
  240. fi
  241. if [ -n "$bt_tracker" ]; then
  242. local bt_tracker_list; local t
  243. for t in $bt_tracker; do
  244. if [ -z "$bt_tracker_list" ]; then
  245. bt_tracker_list="$t"
  246. else
  247. bt_tracker_list="${bt_tracker_list},${t}"
  248. fi
  249. done
  250. append_setting "bt-tracker=${bt_tracker_list}"
  251. fi
  252. append_options "auto_save_interval" "bt_enable_lpd" "bt_max_open_files" "bt_max_peers" \
  253. "bt_remove_unselected_file" "bt_request_peer_speed_limit" "bt_save_metadata" "bt_seed_unverified" \
  254. "bt_stop_timeout" "certificate" "connect_timeout" "dht_listen_port" "disable_ipv6" "disk_cache" \
  255. "enable_peer_exchange" "event_poll" "file_allocation" "follow_torrent" "force_save" "http_accept_gzip" \
  256. "http_no_cache" "listen_port" "lowest_speed_limit" "max_concurrent_downloads" "max_connection_per_server" \
  257. "max_download_limit" "max_overall_download_limit" "max_overall_upload_limit" "max_tries" \
  258. "max_upload_limit" "min_split_size" "pause" "pause_metadata" "peer_id_prefix" "private_key" \
  259. "retry_wait" "rpc_listen_port" "save_session_interval" "seed_ratio" "seed_time" "split" "timeout" \
  260. "user_agent"
  261. config_list_foreach "$section" "header" append_header
  262. config_list_foreach "$section" "extra_settings" append_setting
  263. sed '/^$/d' "$config_file_tmp" >"$config_file"
  264. rm -f "$config_file_tmp"
  265. _reset_dir_mode "$config_dir"
  266. _change_file_mode 600 "$config_file"
  267. if [ -n "$user" ]; then
  268. if ( user_exists "$user" && _change_owner "$user" "$config_dir" "$log" ); then
  269. _info "Aria2 will run with uer '${user}'."
  270. if [ "$user" != "root" ]; then
  271. _info "Please make sure user '${user}' has write access to downlod dir: ${dir}"
  272. fi
  273. else
  274. _info "Set run user to '${user}' failed, default user will be used."
  275. user=
  276. fi
  277. fi
  278. procd_open_instance "${NAME}.${section}"
  279. procd_set_param command "$PROG"
  280. procd_append_param command --conf-path="${config_file}"
  281. procd_set_param respawn
  282. procd_set_param stdout 1
  283. procd_set_param stderr 1
  284. procd_set_param file "$config_file"
  285. [ -n "$user" ] && \
  286. procd_set_param user "$user"
  287. procd_add_jail "${NAME}.${section}" log
  288. procd_add_jail_mount "$config_file"
  289. procd_add_jail_mount_rw "$dir" "$config_dir" "$log"
  290. procd_close_instance
  291. }
  292. service_triggers() {
  293. procd_add_reload_trigger "$NAME"
  294. }
  295. start_service() {
  296. config_load "$NAME"
  297. config_foreach aria2_start "aria2"
  298. }