generate_igp_wg.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. def sanitize_string str
  2. return str.dump[1...-1]
  3. end
  4. def ansible_encrypt to_encrypt
  5. res = `echo -n #{to_encrypt} | ansible-vault encrypt_string --vault-password-file=#{$vault_file}`
  6. if res =~ /(!vault \|[A-Za-z_;.0-9\n\s$]*)$/
  7. return $1
  8. else
  9. raise 'Failed to encrypt'
  10. end
  11. end
  12. class WireGuard
  13. def self.generate_psk
  14. return `wg genpsk`.strip
  15. end
  16. def self.generate_privkey
  17. return `wg genkey`.strip
  18. end
  19. def self.get_pubkey privkey
  20. raise 'Private key must be a string' unless privkey.is_a? String
  21. return `echo -n #{privkey} | wg pubkey`.strip
  22. end
  23. end
  24. def print_template args
  25. puts " - name: <insert name>"
  26. puts " autostart: true"
  27. puts " listen_port: <insert port>"
  28. puts " privkey: #{args[:privkey]}"
  29. puts " addresses:"
  30. puts " - local: <insert address>"
  31. puts " remote: <insert address>"
  32. puts " table: false"
  33. puts " peers:"
  34. puts " - endpoint: <insert endpoint>"
  35. puts " pubkey: #{args[:pubkey]}"
  36. puts " psk: #{args[:psk]}"
  37. puts " allowed_ips:"
  38. puts " - 0.0.0.0/0"
  39. puts " - \"::/0\""
  40. end
  41. if ARGV[0]
  42. $vault_file = ARGV[0].dup.strip.freeze
  43. else
  44. raise "No vault file given"
  45. end
  46. puts "Vault file is #{sanitize_string $vault_file}."
  47. psk = WireGuard.generate_psk
  48. privkey1 = WireGuard.generate_privkey
  49. pubkey1 = WireGuard.get_pubkey privkey1
  50. encrypted_privkey1 = ansible_encrypt privkey1
  51. privkey2 = WireGuard.generate_privkey
  52. pubkey2 = WireGuard.get_pubkey privkey2
  53. encrypted_privkey2 = ansible_encrypt privkey2
  54. puts "---> Peer 1:"
  55. print_template psk:, privkey: encrypted_privkey1, pubkey: pubkey2
  56. puts "---> Peer 2:"
  57. print_template psk:, privkey: encrypted_privkey2, pubkey: pubkey1