backend.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 k8s
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "path"
  21. "strconv"
  22. "strings"
  23. "time"
  24. crdutils "github.com/ant31/crd-validation/pkg"
  25. v1 "k8s.io/api/core/v1"
  26. "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
  27. apiextensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
  28. apierrors "k8s.io/apimachinery/pkg/api/errors"
  29. "k8s.io/apimachinery/pkg/labels"
  30. "k8s.io/apimachinery/pkg/types"
  31. "k8s.io/apimachinery/pkg/util/strategicpatch"
  32. "k8s.io/apimachinery/pkg/util/validation"
  33. v1informers "k8s.io/client-go/informers/core/v1"
  34. "k8s.io/client-go/kubernetes"
  35. v1listers "k8s.io/client-go/listers/core/v1"
  36. "k8s.io/client-go/tools/cache"
  37. "github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1"
  38. kiloclient "github.com/squat/kilo/pkg/k8s/clientset/versioned"
  39. v1alpha1informers "github.com/squat/kilo/pkg/k8s/informers/kilo/v1alpha1"
  40. v1alpha1listers "github.com/squat/kilo/pkg/k8s/listers/kilo/v1alpha1"
  41. "github.com/squat/kilo/pkg/mesh"
  42. "github.com/squat/kilo/pkg/wireguard"
  43. )
  44. const (
  45. // Backend is the name of this mesh backend.
  46. Backend = "kubernetes"
  47. endpointAnnotationKey = "kilo.squat.ai/endpoint"
  48. forceEndpointAnnotationKey = "kilo.squat.ai/force-endpoint"
  49. forceInternalIPAnnotationKey = "kilo.squat.ai/force-internal-ip"
  50. internalIPAnnotationKey = "kilo.squat.ai/internal-ip"
  51. keyAnnotationKey = "kilo.squat.ai/key"
  52. lastSeenAnnotationKey = "kilo.squat.ai/last-seen"
  53. leaderAnnotationKey = "kilo.squat.ai/leader"
  54. locationAnnotationKey = "kilo.squat.ai/location"
  55. persistentKeepaliveKey = "kilo.squat.ai/persistent-keepalive"
  56. wireGuardIPAnnotationKey = "kilo.squat.ai/wireguard-ip"
  57. // RegionLabelKey is the key for the well-known Kubernetes topology region label.
  58. RegionLabelKey = "topology.kubernetes.io/region"
  59. jsonPatchSlash = "~1"
  60. jsonRemovePatch = `{"op": "remove", "path": "%s"}`
  61. )
  62. type backend struct {
  63. nodes *nodeBackend
  64. peers *peerBackend
  65. }
  66. // Nodes implements the mesh.Backend interface.
  67. func (b *backend) Nodes() mesh.NodeBackend {
  68. return b.nodes
  69. }
  70. // Peers implements the mesh.Backend interface.
  71. func (b *backend) Peers() mesh.PeerBackend {
  72. return b.peers
  73. }
  74. type nodeBackend struct {
  75. client kubernetes.Interface
  76. events chan *mesh.NodeEvent
  77. informer cache.SharedIndexInformer
  78. lister v1listers.NodeLister
  79. topologyLabel string
  80. }
  81. type peerBackend struct {
  82. client kiloclient.Interface
  83. extensionsClient apiextensions.Interface
  84. events chan *mesh.PeerEvent
  85. informer cache.SharedIndexInformer
  86. lister v1alpha1listers.PeerLister
  87. }
  88. // New creates a new instance of a mesh.Backend.
  89. func New(c kubernetes.Interface, kc kiloclient.Interface, ec apiextensions.Interface, topologyLabel string) mesh.Backend {
  90. ni := v1informers.NewNodeInformer(c, 5*time.Minute, nil)
  91. pi := v1alpha1informers.NewPeerInformer(kc, 5*time.Minute, nil)
  92. return &backend{
  93. &nodeBackend{
  94. client: c,
  95. events: make(chan *mesh.NodeEvent),
  96. informer: ni,
  97. lister: v1listers.NewNodeLister(ni.GetIndexer()),
  98. topologyLabel: topologyLabel,
  99. },
  100. &peerBackend{
  101. client: kc,
  102. extensionsClient: ec,
  103. events: make(chan *mesh.PeerEvent),
  104. informer: pi,
  105. lister: v1alpha1listers.NewPeerLister(pi.GetIndexer()),
  106. },
  107. }
  108. }
  109. // CleanUp removes configuration applied to the backend.
  110. func (nb *nodeBackend) CleanUp(name string) error {
  111. patch := []byte("[" + strings.Join([]string{
  112. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(endpointAnnotationKey, "/", jsonPatchSlash, 1))),
  113. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(internalIPAnnotationKey, "/", jsonPatchSlash, 1))),
  114. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(keyAnnotationKey, "/", jsonPatchSlash, 1))),
  115. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(lastSeenAnnotationKey, "/", jsonPatchSlash, 1))),
  116. fmt.Sprintf(jsonRemovePatch, path.Join("/metadata", "annotations", strings.Replace(wireGuardIPAnnotationKey, "/", jsonPatchSlash, 1))),
  117. }, ",") + "]")
  118. if _, err := nb.client.CoreV1().Nodes().Patch(name, types.JSONPatchType, patch); err != nil {
  119. return fmt.Errorf("failed to patch node: %v", err)
  120. }
  121. return nil
  122. }
  123. // Get gets a single Node by name.
  124. func (nb *nodeBackend) Get(name string) (*mesh.Node, error) {
  125. n, err := nb.lister.Get(name)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return translateNode(n, nb.topologyLabel), nil
  130. }
  131. // Init initializes the backend; for this backend that means
  132. // syncing the informer cache.
  133. func (nb *nodeBackend) Init(stop <-chan struct{}) error {
  134. go nb.informer.Run(stop)
  135. if ok := cache.WaitForCacheSync(stop, func() bool {
  136. return nb.informer.HasSynced()
  137. }); !ok {
  138. return errors.New("failed to sync node cache")
  139. }
  140. nb.informer.AddEventHandler(
  141. cache.ResourceEventHandlerFuncs{
  142. AddFunc: func(obj interface{}) {
  143. n, ok := obj.(*v1.Node)
  144. if !ok {
  145. // Failed to decode Node; ignoring...
  146. return
  147. }
  148. nb.events <- &mesh.NodeEvent{Type: mesh.AddEvent, Node: translateNode(n, nb.topologyLabel)}
  149. },
  150. UpdateFunc: func(old, obj interface{}) {
  151. n, ok := obj.(*v1.Node)
  152. if !ok {
  153. // Failed to decode Node; ignoring...
  154. return
  155. }
  156. o, ok := old.(*v1.Node)
  157. if !ok {
  158. // Failed to decode Node; ignoring...
  159. return
  160. }
  161. nb.events <- &mesh.NodeEvent{Type: mesh.UpdateEvent, Node: translateNode(n, nb.topologyLabel), Old: translateNode(o, nb.topologyLabel)}
  162. },
  163. DeleteFunc: func(obj interface{}) {
  164. n, ok := obj.(*v1.Node)
  165. if !ok {
  166. // Failed to decode Node; ignoring...
  167. return
  168. }
  169. nb.events <- &mesh.NodeEvent{Type: mesh.DeleteEvent, Node: translateNode(n, nb.topologyLabel)}
  170. },
  171. },
  172. )
  173. return nil
  174. }
  175. // List gets all the Nodes in the cluster.
  176. func (nb *nodeBackend) List() ([]*mesh.Node, error) {
  177. ns, err := nb.lister.List(labels.Everything())
  178. if err != nil {
  179. return nil, err
  180. }
  181. nodes := make([]*mesh.Node, len(ns))
  182. for i := range ns {
  183. nodes[i] = translateNode(ns[i], nb.topologyLabel)
  184. }
  185. return nodes, nil
  186. }
  187. // Set sets the fields of a node.
  188. func (nb *nodeBackend) Set(name string, node *mesh.Node) error {
  189. old, err := nb.lister.Get(name)
  190. if err != nil {
  191. return fmt.Errorf("failed to find node: %v", err)
  192. }
  193. n := old.DeepCopy()
  194. n.ObjectMeta.Annotations[endpointAnnotationKey] = node.Endpoint.String()
  195. if node.InternalIP == nil {
  196. n.ObjectMeta.Annotations[internalIPAnnotationKey] = ""
  197. } else {
  198. n.ObjectMeta.Annotations[internalIPAnnotationKey] = node.InternalIP.String()
  199. }
  200. n.ObjectMeta.Annotations[keyAnnotationKey] = string(node.Key)
  201. n.ObjectMeta.Annotations[lastSeenAnnotationKey] = strconv.FormatInt(node.LastSeen, 10)
  202. if node.WireGuardIP == nil {
  203. n.ObjectMeta.Annotations[wireGuardIPAnnotationKey] = ""
  204. } else {
  205. n.ObjectMeta.Annotations[wireGuardIPAnnotationKey] = node.WireGuardIP.String()
  206. }
  207. oldData, err := json.Marshal(old)
  208. if err != nil {
  209. return err
  210. }
  211. newData, err := json.Marshal(n)
  212. if err != nil {
  213. return err
  214. }
  215. patch, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
  216. if err != nil {
  217. return fmt.Errorf("failed to create patch for node %q: %v", n.Name, err)
  218. }
  219. if _, err = nb.client.CoreV1().Nodes().Patch(name, types.StrategicMergePatchType, patch); err != nil {
  220. return fmt.Errorf("failed to patch node: %v", err)
  221. }
  222. return nil
  223. }
  224. // Watch returns a chan of node events.
  225. func (nb *nodeBackend) Watch() <-chan *mesh.NodeEvent {
  226. return nb.events
  227. }
  228. // translateNode translates a Kubernetes Node to a mesh.Node.
  229. func translateNode(node *v1.Node, topologyLabel string) *mesh.Node {
  230. if node == nil {
  231. return nil
  232. }
  233. _, subnet, err := net.ParseCIDR(node.Spec.PodCIDR)
  234. // The subnet should only ever fail to parse if the pod CIDR has not been set,
  235. // so in this case set the subnet to nil and let the node be updated.
  236. if err != nil {
  237. subnet = nil
  238. }
  239. _, leader := node.ObjectMeta.Annotations[leaderAnnotationKey]
  240. // Allow the region to be overridden by an explicit location.
  241. location, ok := node.ObjectMeta.Annotations[locationAnnotationKey]
  242. if !ok {
  243. location = node.ObjectMeta.Labels[topologyLabel]
  244. }
  245. // Allow the endpoint to be overridden.
  246. endpoint := parseEndpoint(node.ObjectMeta.Annotations[forceEndpointAnnotationKey])
  247. if endpoint == nil {
  248. endpoint = parseEndpoint(node.ObjectMeta.Annotations[endpointAnnotationKey])
  249. }
  250. // Allow the internal IP to be overridden.
  251. internalIP := normalizeIP(node.ObjectMeta.Annotations[forceInternalIPAnnotationKey])
  252. if internalIP == nil {
  253. internalIP = normalizeIP(node.ObjectMeta.Annotations[internalIPAnnotationKey])
  254. }
  255. // Set the ForceInternalIP flag, if force-internal-ip annotation was set to "".
  256. noInternalIP := false
  257. if s, ok := node.ObjectMeta.Annotations[forceInternalIPAnnotationKey]; ok && (s == "" || s == "-") {
  258. noInternalIP = true
  259. internalIP = nil
  260. }
  261. // Set Wireguard PersistentKeepalive setting for the node.
  262. var persistentKeepalive int64
  263. if keepAlive, ok := node.ObjectMeta.Annotations[persistentKeepaliveKey]; !ok {
  264. persistentKeepalive = 0
  265. } else {
  266. if persistentKeepalive, err = strconv.ParseInt(keepAlive, 10, 64); err != nil {
  267. persistentKeepalive = 0
  268. }
  269. }
  270. var lastSeen int64
  271. if ls, ok := node.ObjectMeta.Annotations[lastSeenAnnotationKey]; !ok {
  272. lastSeen = 0
  273. } else {
  274. if lastSeen, err = strconv.ParseInt(ls, 10, 64); err != nil {
  275. lastSeen = 0
  276. }
  277. }
  278. return &mesh.Node{
  279. // Endpoint and InternalIP should only ever fail to parse if the
  280. // remote node's agent has not yet set its IP address;
  281. // in this case the IP will be nil and
  282. // the mesh can wait for the node to be updated.
  283. // It is valid for the InternalIP to be nil,
  284. // if the given node only has public IP addresses.
  285. Endpoint: endpoint,
  286. NoInternalIP: noInternalIP,
  287. InternalIP: internalIP,
  288. Key: []byte(node.ObjectMeta.Annotations[keyAnnotationKey]),
  289. LastSeen: lastSeen,
  290. Leader: leader,
  291. Location: location,
  292. Name: node.Name,
  293. PersistentKeepalive: int(persistentKeepalive),
  294. Subnet: subnet,
  295. // WireGuardIP can fail to parse if the node is not a leader or if
  296. // the node's agent has not yet reconciled. In either case, the IP
  297. // will parse as nil.
  298. WireGuardIP: normalizeIP(node.ObjectMeta.Annotations[wireGuardIPAnnotationKey]),
  299. }
  300. }
  301. // translatePeer translates a Peer CRD to a mesh.Peer.
  302. func translatePeer(peer *v1alpha1.Peer) *mesh.Peer {
  303. if peer == nil {
  304. return nil
  305. }
  306. var aips []*net.IPNet
  307. for _, aip := range peer.Spec.AllowedIPs {
  308. aip := normalizeIP(aip)
  309. // Skip any invalid IPs.
  310. if aip == nil {
  311. continue
  312. }
  313. aips = append(aips, aip)
  314. }
  315. var endpoint *wireguard.Endpoint
  316. if peer.Spec.Endpoint != nil {
  317. ip := net.ParseIP(peer.Spec.Endpoint.IP)
  318. if ip4 := ip.To4(); ip4 != nil {
  319. ip = ip4
  320. } else {
  321. ip = ip.To16()
  322. }
  323. if peer.Spec.Endpoint.Port > 0 && (ip != nil || peer.Spec.Endpoint.DNS != "") {
  324. endpoint = &wireguard.Endpoint{
  325. DNSOrIP: wireguard.DNSOrIP{
  326. DNS: peer.Spec.Endpoint.DNS,
  327. IP: ip,
  328. },
  329. Port: peer.Spec.Endpoint.Port,
  330. }
  331. }
  332. }
  333. var key []byte
  334. if len(peer.Spec.PublicKey) > 0 {
  335. key = []byte(peer.Spec.PublicKey)
  336. }
  337. var psk []byte
  338. if len(peer.Spec.PresharedKey) > 0 {
  339. psk = []byte(peer.Spec.PresharedKey)
  340. }
  341. var pka int
  342. if peer.Spec.PersistentKeepalive > 0 {
  343. pka = peer.Spec.PersistentKeepalive
  344. }
  345. return &mesh.Peer{
  346. Name: peer.Name,
  347. Peer: wireguard.Peer{
  348. AllowedIPs: aips,
  349. Endpoint: endpoint,
  350. PersistentKeepalive: pka,
  351. PresharedKey: psk,
  352. PublicKey: key,
  353. },
  354. }
  355. }
  356. // CleanUp removes configuration applied to the backend.
  357. func (pb *peerBackend) CleanUp(name string) error {
  358. return nil
  359. }
  360. // Get gets a single Peer by name.
  361. func (pb *peerBackend) Get(name string) (*mesh.Peer, error) {
  362. p, err := pb.lister.Get(name)
  363. if err != nil {
  364. return nil, err
  365. }
  366. return translatePeer(p), nil
  367. }
  368. // Init initializes the backend; for this backend that means
  369. // syncing the informer cache.
  370. func (pb *peerBackend) Init(stop <-chan struct{}) error {
  371. // Register CRD.
  372. crd := crdutils.NewCustomResourceDefinition(crdutils.Config{
  373. SpecDefinitionName: "github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1.Peer",
  374. EnableValidation: true,
  375. ResourceScope: string(v1beta1.ClusterScoped),
  376. Group: v1alpha1.GroupName,
  377. Kind: v1alpha1.PeerKind,
  378. Version: v1alpha1.SchemeGroupVersion.Version,
  379. Plural: v1alpha1.PeerPlural,
  380. ShortNames: v1alpha1.PeerShortNames,
  381. GetOpenAPIDefinitions: v1alpha1.GetOpenAPIDefinitions,
  382. })
  383. crd.Spec.Subresources.Scale = nil
  384. crd.Spec.Subresources.Status = nil
  385. _, err := pb.extensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
  386. if err != nil && !apierrors.IsAlreadyExists(err) {
  387. return fmt.Errorf("failed to create CRD: %v", err)
  388. }
  389. go pb.informer.Run(stop)
  390. if ok := cache.WaitForCacheSync(stop, func() bool {
  391. return pb.informer.HasSynced()
  392. }); !ok {
  393. return errors.New("failed to sync peer cache")
  394. }
  395. pb.informer.AddEventHandler(
  396. cache.ResourceEventHandlerFuncs{
  397. AddFunc: func(obj interface{}) {
  398. p, ok := obj.(*v1alpha1.Peer)
  399. if !ok || p.Validate() != nil {
  400. // Failed to decode Peer; ignoring...
  401. return
  402. }
  403. pb.events <- &mesh.PeerEvent{Type: mesh.AddEvent, Peer: translatePeer(p)}
  404. },
  405. UpdateFunc: func(old, obj interface{}) {
  406. p, ok := obj.(*v1alpha1.Peer)
  407. if !ok || p.Validate() != nil {
  408. // Failed to decode Peer; ignoring...
  409. return
  410. }
  411. o, ok := old.(*v1alpha1.Peer)
  412. if !ok || o.Validate() != nil {
  413. // Failed to decode Peer; ignoring...
  414. return
  415. }
  416. pb.events <- &mesh.PeerEvent{Type: mesh.UpdateEvent, Peer: translatePeer(p), Old: translatePeer(o)}
  417. },
  418. DeleteFunc: func(obj interface{}) {
  419. p, ok := obj.(*v1alpha1.Peer)
  420. if !ok || p.Validate() != nil {
  421. // Failed to decode Peer; ignoring...
  422. return
  423. }
  424. pb.events <- &mesh.PeerEvent{Type: mesh.DeleteEvent, Peer: translatePeer(p)}
  425. },
  426. },
  427. )
  428. return nil
  429. }
  430. // List gets all the Peers in the cluster.
  431. func (pb *peerBackend) List() ([]*mesh.Peer, error) {
  432. ps, err := pb.lister.List(labels.Everything())
  433. if err != nil {
  434. return nil, err
  435. }
  436. peers := make([]*mesh.Peer, len(ps))
  437. for i := range ps {
  438. // Skip invalid peers.
  439. if ps[i].Validate() != nil {
  440. continue
  441. }
  442. peers[i] = translatePeer(ps[i])
  443. }
  444. return peers, nil
  445. }
  446. // Set sets the fields of a peer.
  447. func (pb *peerBackend) Set(name string, peer *mesh.Peer) error {
  448. old, err := pb.lister.Get(name)
  449. if err != nil {
  450. return fmt.Errorf("failed to find peer: %v", err)
  451. }
  452. p := old.DeepCopy()
  453. p.Spec.AllowedIPs = make([]string, len(peer.AllowedIPs))
  454. for i := range peer.AllowedIPs {
  455. p.Spec.AllowedIPs[i] = peer.AllowedIPs[i].String()
  456. }
  457. if peer.Endpoint != nil {
  458. var ip string
  459. if peer.Endpoint.IP != nil {
  460. ip = peer.Endpoint.IP.String()
  461. }
  462. p.Spec.Endpoint = &v1alpha1.PeerEndpoint{
  463. DNSOrIP: v1alpha1.DNSOrIP{
  464. IP: ip,
  465. DNS: peer.Endpoint.DNS,
  466. },
  467. Port: peer.Endpoint.Port,
  468. }
  469. }
  470. p.Spec.PersistentKeepalive = peer.PersistentKeepalive
  471. p.Spec.PresharedKey = string(peer.PresharedKey)
  472. p.Spec.PublicKey = string(peer.PublicKey)
  473. if _, err = pb.client.KiloV1alpha1().Peers().Update(p); err != nil {
  474. return fmt.Errorf("failed to update peer: %v", err)
  475. }
  476. return nil
  477. }
  478. // Watch returns a chan of peer events.
  479. func (pb *peerBackend) Watch() <-chan *mesh.PeerEvent {
  480. return pb.events
  481. }
  482. func normalizeIP(ip string) *net.IPNet {
  483. i, ipNet, err := net.ParseCIDR(ip)
  484. if err != nil || ipNet == nil {
  485. return nil
  486. }
  487. if ip4 := i.To4(); ip4 != nil {
  488. ipNet.IP = ip4
  489. return ipNet
  490. }
  491. ipNet.IP = i.To16()
  492. return ipNet
  493. }
  494. func parseEndpoint(endpoint string) *wireguard.Endpoint {
  495. if len(endpoint) == 0 {
  496. return nil
  497. }
  498. parts := strings.Split(endpoint, ":")
  499. if len(parts) < 2 {
  500. return nil
  501. }
  502. portRaw := parts[len(parts)-1]
  503. hostRaw := strings.Trim(strings.Join(parts[:len(parts)-1], ":"), "[]")
  504. port, err := strconv.ParseUint(portRaw, 10, 32)
  505. if err != nil {
  506. return nil
  507. }
  508. if len(validation.IsValidPortNum(int(port))) != 0 {
  509. return nil
  510. }
  511. ip := net.ParseIP(hostRaw)
  512. if ip == nil {
  513. if len(validation.IsDNS1123Subdomain(hostRaw)) == 0 {
  514. return &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{DNS: hostRaw}, Port: uint32(port)}
  515. }
  516. return nil
  517. }
  518. if ip4 := ip.To4(); ip4 != nil {
  519. ip = ip4
  520. } else {
  521. ip = ip.To16()
  522. }
  523. return &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: ip}, Port: uint32(port)}
  524. }