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.

174 lines
4.4 KiB

  1. #!/bin/sh /etc/rc.common
  2. #
  3. # Copyright (C) 2019 Chao Liu <expiron18@gmail.com>
  4. #
  5. # This is free software, licensed under the GNU General Public License v3.
  6. # See /LICENSE for more information.
  7. #
  8. USE_PROCD=1
  9. START=99
  10. confdir=/var/etc/kcptun
  11. bindir=/usr/bin
  12. mkjson_server_conf() {
  13. [ "$disabled" = 0 ] || return 1
  14. [ -n "$listen" ] || return 1
  15. [ -n "$target" ] || return 1
  16. [ -n "$target_port" ] || return 1
  17. json_add_string listen ":$listen"
  18. json_add_string target "$target:$target_port"
  19. json_add_boolean pprof "$pprof"
  20. }
  21. mkjson_client_conf() {
  22. [ "$disabled" = 0 ] || return 1
  23. [ -n "$local_port" ] || return 1
  24. [ -n "$server" ] || return 1
  25. [ -n "$server_port" ] || return 1
  26. json_add_string localaddr "$bind_address:$local_port"
  27. json_add_string remoteaddr "$server:$server_port"
  28. [ -z "$conn" ] || json_add_int conn "$conn"
  29. [ -z "$autoexpire" ] || json_add_int autoexpire "$autoexpire"
  30. [ -z "$scavengettl" ] || json_add_int scavengettl "$scavengettl"
  31. }
  32. kcptun() {
  33. local cfg="$1"
  34. local cfgtype="$2"
  35. local bin="$bindir/kcptun-$cfgtype"
  36. local confjson="$confdir/$cfgtype.$cfg.json"
  37. [ -x "$bin" ] || return
  38. eval "$("validate_${cfgtype}_section" "$cfg" validate_mklocal)"
  39. "validate_${cfgtype}_section" "$cfg" || return
  40. [ "$disabled" = 0 ] || return
  41. json_init
  42. mkjson_${cfgtype}_conf || return
  43. [ -z "$crypt" ] || json_add_string crypt "$crypt"
  44. [ -z "$key" ] || json_add_string key "$key"
  45. [ -z "$mode" ] || json_add_string mode "$mode"
  46. [ -z "$mtu" ] || json_add_int mtu "$mtu"
  47. [ -z "$sndwnd" ] || json_add_int sndwnd "$sndwnd"
  48. [ -z "$rcvwnd" ] || json_add_int rcvwnd "$rcvwnd"
  49. [ -z "$datashard" ] || json_add_int datashard "$datashard"
  50. [ -z "$parityshard" ] || json_add_int parityshard "$parityshard"
  51. [ -z "$dscp" ] || json_add_int dscp "$dscp"
  52. json_add_boolean nocomp "$nocomp"
  53. [ -z "$sockbuf" ] || json_add_int sockbuf "$sockbuf"
  54. [ -z "$smuxver" ] || json_add_int smuxver "$smuxver"
  55. [ -z "$smuxbuf" ] || json_add_int smuxbuf "$smuxbuf"
  56. [ -z "$streambuf" ] || json_add_int streambuf "$streambuf"
  57. [ -z "$keepalive" ] || json_add_int keepalive "$keepalive"
  58. [ -z "$snmplog" ] || json_add_string snmplog "$snmplog"
  59. [ -z "$snmpperiod" ] || json_add_int snmpperiod "$snmpperiod"
  60. json_add_boolean quiet "$quiet"
  61. json_dump -i > "$confjson"
  62. procd_open_instance "$cfgtype.$cfg"
  63. procd_set_param command "$bin" -c "$confjson"
  64. [ -z "$gogc" ] || procd_set_param env GOGC="$gogc"
  65. [ -z "$syslog" ] || procd_set_param stderr 1
  66. [ -z "$user" ] || procd_set_param user "$user"
  67. procd_set_param file "$confjson"
  68. procd_set_param respawn
  69. procd_close_instance
  70. }
  71. start_service() {
  72. local cfgtype
  73. mkdir -p "$confdir"
  74. config_load kcptun
  75. for cfgtype in server client; do
  76. config_foreach kcptun "$cfgtype" "$cfgtype"
  77. done
  78. }
  79. stop_service() {
  80. rm -rf "$confdir"
  81. }
  82. service_triggers() {
  83. procd_add_reload_interface_trigger wan
  84. procd_add_reload_trigger kcptun
  85. procd_open_validate
  86. validate_server_section
  87. validate_client_section
  88. procd_close_validate
  89. }
  90. validate_mklocal() {
  91. local tuple opts
  92. shift 2
  93. for tuple in "$@"; do
  94. opts="${tuple%%:*} $opts"
  95. done
  96. [ -z "$opts" ] || echo "local $opts"
  97. }
  98. validate() {
  99. uci_validate_section kcptun "$@"
  100. }
  101. validate_common_options() {
  102. local cfgtype="$1"; shift
  103. local cfg="$1"; shift
  104. local func="$1"; shift
  105. local crypt_methods='"aes", "aes-128", "aes-192", "salsa20", "blowfish", "twofish", "cast5", "3des", "tea", "xtea", "xor", "sm4", "none"'
  106. local mode_profiles='"fast3", "fast2", "fast", "normal", "manual"'
  107. "${func:-validate}" "$cfgtype" "$cfg" "$@" \
  108. 'disabled:bool:0' \
  109. 'key:string' \
  110. "crypt:or($crypt_methods)" \
  111. "mode:or($mode_profiles)" \
  112. 'mtu:uinteger' \
  113. 'sndwnd:uinteger' \
  114. 'rcvwnd:uinteger' \
  115. 'datashard:uinteger' \
  116. 'parityshard:uinteger' \
  117. 'dscp:uinteger' \
  118. 'nocomp:bool' \
  119. 'sockbuf:uinteger' \
  120. 'smuxver:uinteger' \
  121. 'smuxbuf:uinteger' \
  122. 'streambuf:uinteger' \
  123. 'keepalive:uinteger' \
  124. 'snmplog:string' \
  125. 'snmpperiod:uinteger' \
  126. 'quiet:bool' \
  127. 'gogc:uinteger' \
  128. 'syslog:bool:1' \
  129. 'user:string:nobody'
  130. }
  131. validate_server_options() {
  132. validate_common_options server "$@" \
  133. 'listen:port' \
  134. 'target:host' \
  135. 'target_port:port' \
  136. 'pprof:bool'
  137. }
  138. validate_client_options() {
  139. validate_common_options client "$@" \
  140. 'bind_address:ipaddr' \
  141. 'local_port:port' \
  142. 'server:host' \
  143. 'server_port:port' \
  144. 'conn:uinteger' \
  145. 'autoexpire:uinteger' \
  146. 'scavengettl:uinteger'
  147. }
  148. validate_server_section() {
  149. validate_server_options "$1" "$2"
  150. }
  151. validate_client_section() {
  152. validate_client_options "$1" "$2"
  153. }