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.

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