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.

158 lines
3.6 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2016 OpenWrt.org
  3. START=80
  4. STOP=20
  5. USE_PROCD=1
  6. PROG=/usr/bin/obfsproxy
  7. append_arg() {
  8. local cfg="$1"
  9. local var="$2"
  10. local opt="$3"
  11. local def="$4"
  12. local val
  13. config_get val "$cfg" "$var"
  14. [ -n "$val" -o -n "$def" ] && procd_append_param command "$opt" "${val:-$def}"
  15. }
  16. append_bool() {
  17. local cfg="$1"
  18. local var="$2"
  19. local opt="$3"
  20. local def="$4"
  21. local val
  22. config_get_bool val "$cfg" "$var" "$def"
  23. [ "$val" = 1 ] && procd_append_param command "$opt"
  24. }
  25. append_plain() {
  26. procd_append_param command "$1"
  27. }
  28. append_param() {
  29. local cfg="$1"
  30. local var="$2"
  31. local opt="$3"
  32. local def="$4"
  33. local val
  34. config_get val "$cfg" "$var"
  35. [ -n "$val" -o -n "$def" ] && procd_append_param "$opt" "${val:-$def}"
  36. }
  37. append_proxy_spec() {
  38. local cfg="$1"
  39. local scheme_var="$2"
  40. local username_var="$3"
  41. local password_var="$4"
  42. local host_var="$5"
  43. local port_var="$6"
  44. local opt="$7"
  45. local def="$8"
  46. local scheme
  47. local username
  48. local password
  49. local host
  50. local port
  51. local login
  52. local val
  53. config_get scheme "$cfg" "$scheme_var"
  54. config_get username "$cfg" "$username_var"
  55. config_get password "$cfg" "$password_var"
  56. config_get host "$cfg" "$host_var"
  57. config_get port "$cfg" "$port_var"
  58. [ "$username" ] && login="$username${password:+:$password}@"
  59. [ -n "$scheme" -a -n "$host" -a -n "$port" ] && val="$scheme://$login$host:$port"
  60. [ -n "$val" -o -n "$def" ] && procd_append_param command "$opt" "${val:-$def}"
  61. }
  62. append_host_port() {
  63. local cfg="$1"
  64. local host_var="$2"
  65. local port_var="$3"
  66. local opt="$4"
  67. local def="$5"
  68. local host
  69. local port
  70. local val
  71. config_get host "$cfg" "$host_var"
  72. config_get port "$cfg" "$port_var"
  73. [ -n "$host" -a -n "$port" ] && val="$host:$port"
  74. [ -n "$val" -o -n "$def" ] && {
  75. [ "$opt" ] && procd_append_param command "$opt"
  76. procd_append_param command "${val:-$def}"
  77. }
  78. }
  79. start_instance() {
  80. local cfg="$1"
  81. local lib_dir="/var/lib/obfsproxy/$cfg"
  82. local redirect=0
  83. local enabled
  84. local user
  85. local transport
  86. local password
  87. local log_min_severity
  88. config_get_bool enabled "$cfg" 'enabled' '0'
  89. [ "$enabled" = 0 ] && return 1
  90. config_get user "$cfg" 'user' 'root'
  91. config_get transport "$cfg" 'transport'
  92. [ "$transport" = "scramblesuit" ] && config_get password "$cfg" 'password'
  93. [ -d "$lib_dir" ] || {
  94. mkdir -m 0755 -p "$lib_dir/data"
  95. chmod -R 0700 "$lib_dir"
  96. }
  97. [ "$password" ] && {
  98. echo "$password" > "$lib_dir/secret"
  99. chmod 0600 "$lib_dir/secret"
  100. }
  101. chown -R "$user:" "$lib_dir"
  102. config_get log_min_severity "$cfg" 'log_min_severity'
  103. [ "$log_min_severity" = "debug" ] && redirect=1
  104. procd_open_instance
  105. procd_set_param command "$PROG" --data-dir "$lib_dir/data" --syslog "obfsproxy($cfg)"
  106. append_arg "$cfg" log_file "--log-file"
  107. append_arg "$cfg" log_min_severity "--log-min-severity"
  108. append_bool "$cfg" no_log "--no-log"
  109. append_bool "$cfg" no_safe_logging "--no-safe-logging"
  110. append_proxy_spec "$cfg" proxy_scheme proxy_username proxy_password proxy_host proxy_port "--proxy"
  111. append_param "$cfg" transport command
  112. [ "$transport" = "obfs2" ] && append_arg "$cfg" password "--shared-secret"
  113. [ "$password" ] && procd_append_param command "--password-file" "$lib_dir/secret"
  114. append_param "$cfg" mode command
  115. append_host_port "$cfg" dest_host dest_port "--dest"
  116. append_arg "$cfg" ext_cookie_file "--ext-cookie-file"
  117. append_host_port "$cfg" listen_host listen_port
  118. procd_set_param respawn
  119. procd_set_param stdout $redirect
  120. procd_set_param stderr $redirect
  121. append_param "$cfg" user user
  122. procd_close_instance
  123. }
  124. service_triggers() {
  125. procd_add_reload_trigger obfsproxy
  126. }
  127. start_service() {
  128. config_load obfsproxy
  129. config_foreach start_instance obfsproxy
  130. }