| 1 | package managedproxy |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "time" |
| 12 | |
| 13 | "go.yaml.in/yaml/v3" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | |
| 18 | func PhysicalInterface(ctx context.Context) (string, error) { |
| 19 | if runtime.GOOS == "windows" { |
| 20 | return windowsPhysicalInterface(ctx) |
| 21 | } |
| 22 | if runtime.GOOS == "linux" { |
| 23 | return linuxPhysicalInterface(ctx) |
| 24 | } |
| 25 | if runtime.GOOS != "darwin" { |
| 26 | return "", errors.New("物理网卡识别当前支持 macOS") |
| 27 | } |
| 28 | ctx, cancel := context.WithTimeout(ctx, 4*time.Second) |
| 29 | defer cancel() |
| 30 | hardware, err := exec.CommandContext(ctx, "/usr/sbin/networksetup", "-listallhardwareports").Output() |
| 31 | if err != nil { |
| 32 | return "", errors.New("无法读取物理网卡,请检查主机网络后重试") |
| 33 | } |
| 34 | route, _ := exec.CommandContext(ctx, "/sbin/route", "-n", "get", "default").Output() |
| 35 | nwi, _ := exec.CommandContext(ctx, "/usr/sbin/scutil", "--nwi").Output() |
| 36 | usable := map[string]bool{} |
| 37 | interfaces, err := net.Interfaces() |
| 38 | if err != nil { |
| 39 | return "", err |
| 40 | } |
| 41 | for _, iface := range interfaces { |
| 42 | if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { |
| 43 | continue |
| 44 | } |
| 45 | addresses, _ := iface.Addrs() |
| 46 | for _, address := range addresses { |
| 47 | ip, _, err := net.ParseCIDR(address.String()) |
| 48 | if err == nil && ip.To4() != nil && !ip.IsLinkLocalUnicast() && !ip.IsUnspecified() { |
| 49 | usable[iface.Name] = true |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return selectPhysicalInterface(string(route), string(hardware), string(nwi), usable) |
| 54 | } |
| 55 | |
| 56 | func selectPhysicalInterface(route, hardware, nwi string, usable map[string]bool) (string, error) { |
| 57 | physical := map[string]bool{} |
| 58 | for _, line := range strings.Split(hardware, "\n") { |
| 59 | fields := strings.Fields(line) |
| 60 | if len(fields) == 2 && fields[0] == "Device:" && usable[fields[1]] { |
| 61 | physical[fields[1]] = true |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | inIPv4, candidate := false, "" |
| 67 | reachable := []string{} |
| 68 | for _, line := range strings.Split(nwi, "\n") { |
| 69 | line = strings.TrimSpace(line) |
| 70 | if strings.HasPrefix(line, "IPv4 network interface information") { |
| 71 | inIPv4 = true |
| 72 | } else if strings.HasPrefix(line, "IPv6 network interface information") { |
| 73 | break |
| 74 | } |
| 75 | if !inIPv4 { |
| 76 | continue |
| 77 | } |
| 78 | fields := strings.Fields(line) |
| 79 | if len(fields) >= 3 && fields[1] == ":" && fields[2] == "flags" { |
| 80 | candidate = fields[0] |
| 81 | } |
| 82 | if strings.HasPrefix(line, "reach") && strings.Contains(line, "(Reachable)") && physical[candidate] { |
| 83 | reachable = append(reachable, candidate) |
| 84 | } |
| 85 | } |
| 86 | for _, line := range strings.Split(route, "\n") { |
| 87 | fields := strings.Fields(line) |
| 88 | if len(fields) == 2 && fields[0] == "interface:" { |
| 89 | for _, name := range reachable { |
| 90 | if fields[1] == name { |
| 91 | return name, nil |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | if len(reachable) > 0 { |
| 97 | return reachable[0], nil |
| 98 | } |
| 99 | return "", errors.New("未找到可用的物理 IPv4 网卡;Cage 外网保持关闭,请连接网络后重试") |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | func BindInterface(data []byte, name string) ([]byte, error) { |
| 105 | if name == "" { |
| 106 | return nil, errors.New("缺少物理网卡,不能启动代理") |
| 107 | } |
| 108 | var config map[string]any |
| 109 | if err := yaml.Unmarshal(data, &config); err != nil || config == nil { |
| 110 | return nil, errors.New("代理运行配置无效") |
| 111 | } |
| 112 | config["interface-name"] = name |
| 113 | |
| 114 | if nodes, ok := config["proxies"].([]any); ok { |
| 115 | for _, item := range nodes { |
| 116 | if node, ok := item.(map[string]any); ok { |
| 117 | if _, bound := node["interface-name"]; bound { |
| 118 | node["interface-name"] = name |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return yaml.Marshal(config) |
| 124 | } |
| 125 | |
| 126 | func BoundInterface(path string) (string, error) { |
| 127 | data, err := os.ReadFile(path) |
| 128 | if err != nil { |
| 129 | return "", err |
| 130 | } |
| 131 | var config struct { |
| 132 | Interface string `yaml:"interface-name"` |
| 133 | } |
| 134 | if err := yaml.Unmarshal(data, &config); err != nil { |
| 135 | return "", err |
| 136 | } |
| 137 | return config.Interface, nil |
| 138 | } |