frp/pkg/vnet/tun_darwin.go
2025-04-09 11:51:34 +08:00

80 lines
2.1 KiB
Go

// Copyright 2025 The frp Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vnet
import (
"fmt"
"io"
"net"
"os/exec"
)
const (
defaultTunName = "utun"
defaultMTU = 1420
)
func createTun(addr string) (io.ReadWriteCloser, string, net.IP, error) {
ifce, name, err := createTunDevice(defaultTunName, defaultMTU)
if err != nil {
return nil, "", nil, err
}
ip, ipNet, err := net.ParseCIDR(addr)
if err != nil {
return nil, "", nil, err
}
// Calculate a peer IP for the point-to-point tunnel
peerIP := generatePeerIP(ip)
// Configure the interface with proper point-to-point addressing
if err = exec.Command("ifconfig", name, "inet", ip.String(), peerIP.String(), "mtu", fmt.Sprint(defaultMTU), "up").Run(); err != nil {
return nil, "", nil, err
}
// Add default route for the tunnel subnet
routes := []net.IPNet{*ipNet}
if err = addRoutes(name, routes); err != nil {
return nil, "", nil, err
}
return ifce, name, ip, nil
}
// generatePeerIP creates a peer IP for the point-to-point tunnel
// by incrementing the last octet of the IP
func generatePeerIP(ip net.IP) net.IP {
// Make a copy to avoid modifying the original
peerIP := make(net.IP, len(ip))
copy(peerIP, ip)
// Increment the last octet
peerIP[len(peerIP)-1]++
return peerIP
}
// addRoutes configures system routes for the TUN interface
func addRoutes(ifName string, routes []net.IPNet) error {
for _, route := range routes {
routeStr := route.String()
if err := exec.Command("route", "add", "-net", routeStr, "-interface", ifName).Run(); err != nil {
return err
}
}
return nil
}