wireguard.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "regexp"
  20. "strconv"
  21. "github.com/vishvananda/netlink"
  22. )
  23. type wgLink struct {
  24. a netlink.LinkAttrs
  25. t string
  26. }
  27. func (w wgLink) Attrs() *netlink.LinkAttrs {
  28. return &w.a
  29. }
  30. func (w wgLink) Type() string {
  31. return w.t
  32. }
  33. // New creates a new WireGuard interface.
  34. func New(prefix string) (int, error) {
  35. links, err := netlink.LinkList()
  36. if err != nil {
  37. return 0, fmt.Errorf("failed to list links: %v", err)
  38. }
  39. max := 0
  40. re := regexp.MustCompile(fmt.Sprintf("^%s([0-9]+)$", prefix))
  41. for _, link := range links {
  42. if matches := re.FindStringSubmatch(link.Attrs().Name); len(matches) == 2 {
  43. i, err := strconv.Atoi(matches[1])
  44. if err != nil {
  45. // This should never happen.
  46. return 0, fmt.Errorf("failed to parse digits as an integer: %v", err)
  47. }
  48. if i >= max {
  49. max = i + 1
  50. }
  51. }
  52. }
  53. name := fmt.Sprintf("%s%d", prefix, max)
  54. wl := wgLink{a: netlink.NewLinkAttrs(), t: "wireguard"}
  55. wl.a.Name = name
  56. if err := netlink.LinkAdd(wl); err != nil {
  57. return 0, fmt.Errorf("failed to create interface %s: %v", name, err)
  58. }
  59. link, err := netlink.LinkByName(name)
  60. if err != nil {
  61. return 0, fmt.Errorf("failed to get interface index: %v", err)
  62. }
  63. return link.Attrs().Index, nil
  64. }
  65. // Keys generates a WireGuard private and public key-pair.
  66. func Keys() ([]byte, []byte, error) {
  67. private, err := GenKey()
  68. if err != nil {
  69. return nil, nil, fmt.Errorf("failed to generate private key: %v", err)
  70. }
  71. public, err := PubKey(private)
  72. return private, public, err
  73. }
  74. // GenKey generates a WireGuard private key.
  75. func GenKey() ([]byte, error) {
  76. key, err := exec.Command("wg", "genkey").Output()
  77. return bytes.Trim(key, "\n"), err
  78. }
  79. // PubKey generates a WireGuard public key for a given private key.
  80. func PubKey(key []byte) ([]byte, error) {
  81. cmd := exec.Command("wg", "pubkey")
  82. stdin, err := cmd.StdinPipe()
  83. if err != nil {
  84. return nil, fmt.Errorf("failed to open pipe to stdin: %v", err)
  85. }
  86. go func() {
  87. defer stdin.Close()
  88. stdin.Write(key)
  89. }()
  90. public, err := cmd.Output()
  91. if err != nil {
  92. return nil, fmt.Errorf("failed to generate public key: %v", err)
  93. }
  94. return bytes.Trim(public, "\n"), nil
  95. }
  96. // SetConf applies a WireGuard configuration file to the given interface.
  97. func SetConf(iface string, path string) error {
  98. cmd := exec.Command("wg", "setconf", iface, path)
  99. var stderr bytes.Buffer
  100. cmd.Stderr = &stderr
  101. if err := cmd.Run(); err != nil {
  102. return fmt.Errorf("failed to apply the WireGuard configuration: %s", stderr.String())
  103. }
  104. return nil
  105. }
  106. // ShowConf gets the WireGuard configuration for the given interface.
  107. func ShowConf(iface string) ([]byte, error) {
  108. cmd := exec.Command("wg", "showconf", iface)
  109. var stderr, stdout bytes.Buffer
  110. cmd.Stderr = &stderr
  111. cmd.Stdout = &stdout
  112. if err := cmd.Run(); err != nil {
  113. return nil, fmt.Errorf("failed to read the WireGuard configuration: %s", stderr.String())
  114. }
  115. return stdout.Bytes(), nil
  116. }