tun_darwin.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2025 The frp 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 vnet
  15. import (
  16. "context"
  17. "fmt"
  18. "net"
  19. "os/exec"
  20. "golang.zx2c4.com/wireguard/tun"
  21. )
  22. const (
  23. defaultTunName = "utun"
  24. defaultMTU = 1420
  25. )
  26. func openTun(_ context.Context, addr string) (tun.Device, error) {
  27. dev, err := tun.CreateTUN(defaultTunName, defaultMTU)
  28. if err != nil {
  29. return nil, err
  30. }
  31. name, err := dev.Name()
  32. if err != nil {
  33. return nil, err
  34. }
  35. ip, ipNet, err := net.ParseCIDR(addr)
  36. if err != nil {
  37. return nil, err
  38. }
  39. // Calculate a peer IP for the point-to-point tunnel
  40. peerIP := generatePeerIP(ip)
  41. // Configure the interface with proper point-to-point addressing
  42. if err = exec.Command("ifconfig", name, "inet", ip.String(), peerIP.String(), "mtu", fmt.Sprint(defaultMTU), "up").Run(); err != nil {
  43. return nil, err
  44. }
  45. // Add default route for the tunnel subnet
  46. routes := []net.IPNet{*ipNet}
  47. if err = addRoutes(name, routes); err != nil {
  48. return nil, err
  49. }
  50. return dev, nil
  51. }
  52. // generatePeerIP creates a peer IP for the point-to-point tunnel
  53. // by incrementing the last octet of the IP
  54. func generatePeerIP(ip net.IP) net.IP {
  55. // Make a copy to avoid modifying the original
  56. peerIP := make(net.IP, len(ip))
  57. copy(peerIP, ip)
  58. // Increment the last octet
  59. peerIP[len(peerIP)-1]++
  60. return peerIP
  61. }
  62. // addRoutes configures system routes for the TUN interface
  63. func addRoutes(ifName string, routes []net.IPNet) error {
  64. for _, route := range routes {
  65. routeStr := route.String()
  66. if err := exec.Command("route", "add", "-net", routeStr, "-interface", ifName).Run(); err != nil {
  67. return err
  68. }
  69. }
  70. return nil
  71. }