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.

128 lines
2.8 KiB

  1. #!/bin/sh
  2. # TODO :
  3. # - add root user detection
  4. # - verify curl output to prevent overwriting config file with an HTTP answer
  5. _usage() {
  6. echo "
  7. Usage: $0 <command> --url <wgportal_url> [options...]
  8. Commands :
  9. -d, --download only download config file
  10. -i, --install download config file and install the service (UNIMPLEMENTED)
  11. Parameters:
  12. -u, --url <url> Specify your WG Portal base URL
  13. -f, --filename <file> Specify Wireguard filename (default: /etc/wireguard/wg0.conf)
  14. "
  15. _version
  16. }
  17. _version() {
  18. printf "%s version pre-alpha 0.1 (yes we're very cautious)\n" "$0"
  19. printf "USE AT YOUR OWN RISK and read the code before\n"
  20. }
  21. _startswith() {
  22. _str="$1"
  23. _sub="$2"
  24. echo "$_str" | grep "^$_sub" >/dev/null 2>&1
  25. }
  26. _download() {
  27. [ -z "$WGPORTAL_URL" ] && printf "Please set wg-portal URL (see help)\n" && return 1
  28. [ -z "$WG_CONFFILE" ] && WG_CONFFILE=/etc/wireguard/wg0.conf
  29. trap 'stty echo; exit 99;' INT
  30. if [ -f $WG_CONFFILE -a "$OVERWRITE" != "on" ]; then
  31. printf "WARNING : Wireguard config file already exists\n"
  32. printf "Exiting now to prevent overrid of your actual parameters\n"
  33. printf "You can force config overwriting with :\n"
  34. printf "OVERWRITE=on %s %s\n" "$0" "$_ARGS"
  35. exit 1;
  36. fi
  37. printf "Username: "
  38. IFS= read -r username
  39. printf "Password: "
  40. stty -echo
  41. IFS= read -r password
  42. stty echo
  43. printf "\n"
  44. printf "Peer public key: "
  45. IFS= read -r wgpubkey
  46. printf "\n"
  47. HTTP_STATUS=$(curl -w "%{http_code}" -G -s -o $WG_CONFFILE --user $username:$password --data-urlencode "pkey=$wgpubkey" -X GET $WGPORTAL_URL"api/v1/provisioning/peer" -H "accept: text/plain")
  48. res=$?
  49. if [ "$res" != "0" ]; then
  50. echo "the curl command failed with: $res"
  51. fi
  52. [ $HTTP_STATUS -ne 200 ] && printf "Curl returned HTTP code %s\n" "$HTTP_STATUS" && exit 1
  53. printf "WG config successfully download at %s\n" "$WG_CONFFILE"
  54. }
  55. _install() {
  56. printf "install : This command does nothing ... yet (TDB)\n"
  57. }
  58. _process() {
  59. while [ ${#} -gt 0 ]; do
  60. case "${1}" in
  61. --help | -h)
  62. _usage
  63. return
  64. ;;
  65. --version | -v)
  66. _version
  67. return
  68. ;;
  69. --download | -d)
  70. _CMD="download"
  71. ;;
  72. --url | -u)
  73. WGPORTAL_URL="$2"
  74. shift
  75. ;;
  76. --filename | -f)
  77. WG_CONFFILE="$2"
  78. shift
  79. ;;
  80. --install | -i)
  81. _CMD="install"
  82. ;;
  83. *)
  84. printf "Unknown parameter : %s\n" "$1"
  85. return 1
  86. ;;
  87. esac
  88. shift 1
  89. done
  90. case "${_CMD}" in
  91. download)
  92. _download
  93. ;;
  94. install)
  95. _install
  96. ;;
  97. *)
  98. if [ "$_CMD" ]; then
  99. printf "Invalid command: %s\n" "$_CMD"
  100. fi
  101. esac
  102. }
  103. main() {
  104. [ -z "$1" ] && _usage && return
  105. if _startswith "$1" '-'; then _process "$@"; else "$@"; fi
  106. }
  107. _ARGS="$*"
  108. main "$@"