wireguard.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // +build linux
  15. package wireguard
  16. import (
  17. "bytes"
  18. "fmt"
  19. "os/exec"
  20. "github.com/vishvananda/netlink"
  21. )
  22. // DefaultMTU is the the default MTU used by WireGuard.
  23. const DefaultMTU = 1420
  24. type wgLink struct {
  25. a netlink.LinkAttrs
  26. t string
  27. }
  28. func (w wgLink) Attrs() *netlink.LinkAttrs {
  29. return &w.a
  30. }
  31. func (w wgLink) Type() string {
  32. return w.t
  33. }
  34. // New returns a WireGuard interface with the given name.
  35. // If the interface exists, its index is returned.
  36. // Otherwise, a new interface is created.
  37. // The function also returns a boolean to indicate if the interface was created.
  38. func New(name string, mtu uint) (int, bool, error) {
  39. link, err := netlink.LinkByName(name)
  40. if err == nil {
  41. return link.Attrs().Index, false, nil
  42. }
  43. if _, ok := err.(netlink.LinkNotFoundError); !ok {
  44. return 0, false, fmt.Errorf("failed to get links: %v", err)
  45. }
  46. wl := wgLink{a: netlink.NewLinkAttrs(), t: "wireguard"}
  47. wl.a.Name = name
  48. wl.a.MTU = int(mtu)
  49. if err := netlink.LinkAdd(wl); err != nil {
  50. return 0, false, fmt.Errorf("failed to create interface %s: %v", name, err)
  51. }
  52. link, err = netlink.LinkByName(name)
  53. if err != nil {
  54. return 0, false, fmt.Errorf("failed to get interface index: %v", err)
  55. }
  56. return link.Attrs().Index, true, nil
  57. }
  58. // Keys generates a WireGuard private and public key-pair.
  59. func Keys() ([]byte, []byte, error) {
  60. private, err := GenKey()
  61. if err != nil {
  62. return nil, nil, fmt.Errorf("failed to generate private key: %v", err)
  63. }
  64. public, err := PubKey(private)
  65. return private, public, err
  66. }
  67. // GenKey generates a WireGuard private key.
  68. func GenKey() ([]byte, error) {
  69. key, err := exec.Command("wg", "genkey").Output()
  70. return bytes.Trim(key, "\n"), err
  71. }
  72. // PubKey generates a WireGuard public key for a given private key.
  73. func PubKey(key []byte) ([]byte, error) {
  74. cmd := exec.Command("wg", "pubkey")
  75. stdin, err := cmd.StdinPipe()
  76. if err != nil {
  77. return nil, fmt.Errorf("failed to open pipe to stdin: %v", err)
  78. }
  79. go func() {
  80. defer stdin.Close()
  81. stdin.Write(key)
  82. }()
  83. public, err := cmd.Output()
  84. if err != nil {
  85. return nil, fmt.Errorf("failed to generate public key: %v", err)
  86. }
  87. return bytes.Trim(public, "\n"), nil
  88. }
  89. // SetConf applies a WireGuard configuration file to the given interface.
  90. func SetConf(iface string, path string) error {
  91. cmd := exec.Command("wg", "setconf", iface, path)
  92. var stderr bytes.Buffer
  93. cmd.Stderr = &stderr
  94. if err := cmd.Run(); err != nil {
  95. return fmt.Errorf("failed to apply the WireGuard configuration: %s", stderr.String())
  96. }
  97. return nil
  98. }
  99. // ShowConf gets the WireGuard configuration for the given interface.
  100. func ShowConf(iface string) ([]byte, error) {
  101. cmd := exec.Command("wg", "showconf", iface)
  102. var stderr, stdout bytes.Buffer
  103. cmd.Stderr = &stderr
  104. cmd.Stdout = &stdout
  105. if err := cmd.Run(); err != nil {
  106. return nil, fmt.Errorf("failed to read the WireGuard configuration: %s", stderr.String())
  107. }
  108. return stdout.Bytes(), nil
  109. }
  110. // ShowDump gets the WireGuard configuration and runtime information for the given interface.
  111. func ShowDump(iface string) ([]byte, error) {
  112. cmd := exec.Command("wg", "show", iface, "dump")
  113. var stderr, stdout bytes.Buffer
  114. cmd.Stderr = &stderr
  115. cmd.Stdout = &stdout
  116. if err := cmd.Run(); err != nil {
  117. return nil, fmt.Errorf("failed to read the WireGuard dump output: %s", stderr.String())
  118. }
  119. return stdout.Bytes(), nil
  120. }