wireguard.go 3.3 KB

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