wireguard.go 3.9 KB

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