gitee.com/aurawing/surguard-go@v0.3.1-0.20240409071558-96509a61ecf3/device/postconfig_windows.go (about) 1 package device 2 3 import ( 4 "fmt" 5 "net" 6 "os" 7 "os/exec" 8 "sort" 9 "strconv" 10 "strings" 11 "time" 12 ) 13 14 const DEFAULT_YID_PATH = "C:\\yid.conf" 15 16 var deviceName string 17 var deviceIndex int 18 19 func GetDeviceIndex() int { 20 return deviceIndex 21 } 22 23 //var lock filelock.FileLock 24 25 // func init() { 26 // lockfile := "C:\\surguard.lock" 27 // if _, err := os.Stat(lockfile); err != nil { 28 // if os.IsNotExist(err) { 29 // var file, err = os.Create(lockfile) 30 // if err != nil { 31 // panic(err) 32 // } 33 // file.Close() 34 // } else { 35 // panic(err) 36 // } 37 // } 38 // lock = filelock.FileLock{Path: lockfile, Timeout: time.Second * 5} 39 // } 40 41 func (device *Device) configTunDevice() error { 42 ifBindInterface = true 43 var err error 44 //find tun device name 45 deviceName, err = device.tun.device.Name() 46 if err != nil { 47 device.log.Errorf("configTunDevice: failed to find tun device name: %s", err) 48 return err 49 } 50 deviceIf, err := net.InterfaceByName(deviceName) 51 if err != nil { 52 device.log.Errorf("configTunDevice: failed to find tun device interface: %s", err) 53 return err 54 } 55 deviceIndex = deviceIf.Index 56 interfaceIndex = byte(deviceIndex % 254) 57 58 //bring up tun device 59 device.runCmd(false, "netsh", "interface", "set", "interface", deviceName, "admin=enable") 60 for { 61 if deviceState(device.state.state.Load()) == deviceStateUp { 62 break 63 } else { 64 time.Sleep(500 * time.Millisecond) 65 } 66 } 67 device.log.Verbosef("configTunDevice: device %s is up\n", deviceName) 68 69 device.runCmd(false, "netsh", "interface", "ipv4", "set", "subinterface", deviceName, fmt.Sprintf("mtu=%d", DefaultMTU), "store=persistent") 70 device.log.Verbosef("configTunDevice: set device MTU to %d\n", DefaultMTU) 71 72 ipaddrs, err := device.getIP() 73 if err != nil { 74 device.log.Errorf("configTunDevice: failed to get IP address: %s", err) 75 return err 76 } 77 sort.Strings(ipaddrs) 78 interfaceIP = strings.Join(ipaddrs, ",") 79 interfaceIPArr = make([][4]byte, 0) 80 sgIPArr = make([][4]byte, 0) 81 for i, ipaddr := range ipaddrs { 82 tmp := net.ParseIP(ipaddr) 83 tmp2 := [4]byte{tmp[12], tmp[13], tmp[14], tmp[15]} 84 interfaceIPArr = append(interfaceIPArr, tmp2) 85 sgIPArr = append(sgIPArr, [4]byte{169, 254, interfaceIndex, byte(i + 1)}) 86 staticip := fmt.Sprintf("169.254.%d.%d", interfaceIndex, i+1) 87 device.runCmd(false, "netsh", "interface", "ipv4", "add", "address", "name="+deviceName, staticip, "255.255.255.0") 88 device.log.Verbosef("configTunDevice: set address %s/24 for device %s\n", staticip, deviceName) 89 } 90 91 // device.runCmd(false, "netsh", "interface", "ipv4", "set", "address", "name="+deviceName, "static", "169.254.254.1", "255.255.255.0") 92 // device.log.Verbosef("configTunDevice: device %s has address 169.254.254.1/24\n", deviceName) 93 94 device.runCmd(false, "netsh", "interface", "ipv4", "set", "interface", deviceName, "weakhostsend=enabled") 95 device.runCmd(false, "netsh", "interface", "ipv4", "set", "interface", deviceName, "weakhostreceive=enabled") 96 device.log.Verbosef("configTunDevice: device %s is setup to weakhost mode\n", deviceName) 97 98 //close rp_filter of tun device 99 // err = sysctl.Set("net.ipv4.conf.all.rp_filter", "0") 100 // if err != nil { 101 // device.log.Errorf("configTunDevice: failed to disable net.ipv4.conf.all.rp_filter: %s", err) 102 // return err 103 // } 104 // device.log.Verbosef("configTunDevice: set net.ipv4.conf.all.rp_filter to 0\n") 105 // err = sysctl.Set(fmt.Sprintf("net.ipv4.conf.%s.rp_filter", deviceName), "0") 106 // if err != nil { 107 // device.log.Errorf("configTunDevice: failed to disable net.ipv4.conf.%s.rp_filter: %s", deviceName, err) 108 // return err 109 // } 110 // device.log.Verbosef("configTunDevice: set net.ipv4.conf.%s.rp_filter to 0\n", deviceName) 111 112 // //get fwmark and route table id 113 // rtid := 256 114 // fwmarkStr := os.Getenv(ENV_SG_FWMARK) 115 // tunnelFwmarkStr := os.Getenv(ENV_SG_TUNNEL_FWMARK) 116 117 // if fwmarkStr != "" { 118 // fwmark, err = strconv.Atoi(fwmarkStr) 119 // if err != nil { 120 // device.log.Errorf("configTunDevice: failed to parse fwmark %s: %s", fwmarkStr, err) 121 // } 122 // } 123 // if tunnelFwmarkStr != "" { 124 // tunnelFwmark, err = strconv.Atoi(tunnelFwmarkStr) 125 // if err != nil { 126 // device.log.Errorf("configTunDevice: failed to parse tunnel fwmark %s: %s", fwmarkStr, err) 127 // } 128 // } 129 // rtidStr := os.Getenv(ENV_SG_RT_ID) 130 // if rtidStr != "" { 131 // rtid, err = strconv.Atoi(rtidStr) 132 // if err != nil { 133 // device.log.Errorf("configTunDevice: failed to parse route table ID %s: %s", rtidStr, err) 134 // } 135 // } 136 137 // //modify route table 138 // err = device.modifyRtTables("/etc/iproute2/rt_tables", rtid) 139 // if err != nil { 140 // device.log.Errorf("configTunDevice: failed to modify route table ID %d: %s", rtid, err) 141 // return err 142 // } 143 144 // // set route policy 145 // device.runCmd(false, "ip", "route", "flush", "table", groupName) 146 // device.runCmd(false, "ip", "route", "add", "default", "dev", deviceName, "table", groupName) 147 // device.runCmd(true, "ip", "rule", "delete", "from", "all", "fwmark", strconv.Itoa(fwmark), "lookup", groupName) 148 // device.runCmd(false, "ip", "rule", "add", "from", "all", "fwmark", strconv.Itoa(fwmark), "lookup", groupName) 149 // device.log.Verbosef("configTunDevice: set route policy table %d %s for %s\n", rtid, groupName, deviceName) 150 return nil 151 } 152 153 func (device *Device) initRules() error { 154 return nil 155 } 156 157 func (device *Device) zkChangedCallback(addedMap, deletedMap map[string]string) { 158 for _, dels := range deletedMap { 159 for _, del := range strings.Split(dels, ",") { 160 strs := strings.Split(del, ":") 161 device.runCmd(true, "route", "delete", strs[0]) 162 } 163 } 164 for _, adds := range addedMap { 165 for _, add := range strings.Split(adds, ",") { 166 strs := strings.Split(add, ":") 167 device.runCmd(true, "route", "add", strs[0], "mask", "255.255.255.255", "0.0.0.0", "IF", strconv.Itoa(deviceIndex)) 168 } 169 } 170 } 171 172 func (device *Device) clearConfigOSSpecific() error { 173 return nil 174 } 175 176 func (device *Device) runCmd(ignoreErr bool, args ...string) { 177 cmd := exec.Command(args[0], args[1:]...) 178 cmd.Stderr = os.Stderr 179 cmd.Stdout = os.Stdout 180 cmd.Stdin = os.Stdin 181 err := cmd.Run() 182 if nil != err { 183 if !ignoreErr { 184 device.log.Errorf("Error running %s: %s\n", args[0], err) 185 } else { 186 device.log.Verbosef("Warning: %s\n", err.Error()) 187 } 188 } 189 }