wireguard.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package wireguard
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os/exec"
  19. "github.com/vishvananda/netlink"
  20. )
  21. type wgLink struct {
  22. a netlink.LinkAttrs
  23. t string
  24. }
  25. func (w wgLink) Attrs() *netlink.LinkAttrs {
  26. return &w.a
  27. }
  28. func (w wgLink) Type() string {
  29. return w.t
  30. }
  31. // New returns a WireGuard interface with the given name.
  32. // If the interface exists, its index is returned.
  33. // Otherwise, a new interface is created.
  34. // The function also returns a boolean to indicate if the interface was created.
  35. func New(name string) (int, bool, error) {
  36. link, err := netlink.LinkByName(name)
  37. if err == nil {
  38. return link.Attrs().Index, false, nil
  39. }
  40. if _, ok := err.(netlink.LinkNotFoundError); !ok {
  41. return 0, false, fmt.Errorf("failed to get links: %v", err)
  42. }
  43. wl := wgLink{a: netlink.NewLinkAttrs(), t: "wireguard"}
  44. wl.a.Name = name
  45. if err := netlink.LinkAdd(wl); err != nil {
  46. return 0, false, fmt.Errorf("failed to create interface %s: %v", name, err)
  47. }
  48. link, err = netlink.LinkByName(name)
  49. if err != nil {
  50. return 0, false, fmt.Errorf("failed to get interface index: %v", err)
  51. }
  52. return link.Attrs().Index, true, nil
  53. }
  54. // Keys generates a WireGuard private and public key-pair.
  55. func Keys() ([]byte, []byte, error) {
  56. private, err := GenKey()
  57. if err != nil {
  58. return nil, nil, fmt.Errorf("failed to generate private key: %v", err)
  59. }
  60. public, err := PubKey(private)
  61. return private, public, err
  62. }
  63. // GenKey generates a WireGuard private key.
  64. func GenKey() ([]byte, error) {
  65. key, err := exec.Command("wg", "genkey").Output()
  66. return bytes.Trim(key, "\n"), err
  67. }
  68. // PubKey generates a WireGuard public key for a given private key.
  69. func PubKey(key []byte) ([]byte, error) {
  70. cmd := exec.Command("wg", "pubkey")
  71. stdin, err := cmd.StdinPipe()
  72. if err != nil {
  73. return nil, fmt.Errorf("failed to open pipe to stdin: %v", err)
  74. }
  75. go func() {
  76. defer stdin.Close()
  77. stdin.Write(key)
  78. }()
  79. public, err := cmd.Output()
  80. if err != nil {
  81. return nil, fmt.Errorf("failed to generate public key: %v", err)
  82. }
  83. return bytes.Trim(public, "\n"), nil
  84. }
  85. // SetConf applies a WireGuard configuration file to the given interface.
  86. func SetConf(iface string, path string) error {
  87. cmd := exec.Command("wg", "setconf", iface, path)
  88. var stderr bytes.Buffer
  89. cmd.Stderr = &stderr
  90. if err := cmd.Run(); err != nil {
  91. return fmt.Errorf("failed to apply the WireGuard configuration: %s", stderr.String())
  92. }
  93. return nil
  94. }
  95. // ShowConf gets the WireGuard configuration for the given interface.
  96. func ShowConf(iface string) ([]byte, error) {
  97. cmd := exec.Command("wg", "showconf", iface)
  98. var stderr, stdout bytes.Buffer
  99. cmd.Stderr = &stderr
  100. cmd.Stdout = &stdout
  101. if err := cmd.Run(); err != nil {
  102. return nil, fmt.Errorf("failed to read the WireGuard configuration: %s", stderr.String())
  103. }
  104. return stdout.Bytes(), nil
  105. }