MCPcopy Index your code
hub / github.com/MengMengCode/GetRealityDomain / getLocalIP

Function getLocalIP

main.go:200–225  ·  view source on GitHub ↗

获取本机IP地址

()

Source from the content-addressed store, hash-verified

198
199// 获取本机IP地址
200func getLocalIP() (string, error) {
201 // 使用ipify.org API获取公网IP
202 resp, err := http.Get("https://api.ipify.org/")
203 if err != nil {
204 return "", fmt.Errorf("获取公网IP失败: %v", err)
205 }
206 defer resp.Body.Close()
207
208 if resp.StatusCode != http.StatusOK {
209 return "", fmt.Errorf("API请求失败,状态码: %d", resp.StatusCode)
210 }
211
212 body, err := io.ReadAll(resp.Body)
213 if err != nil {
214 return "", fmt.Errorf("读取响应失败: %v", err)
215 }
216
217 ip := strings.TrimSpace(string(body))
218
219 // 验证返回的是否为有效IP地址
220 if net.ParseIP(ip) == nil {
221 return "", fmt.Errorf("返回的不是有效的IP地址: %s", ip)
222 }
223
224 return ip, nil
225}
226
227// 实际的扫描函数
228func scanAddress(addr string) error {

Callers 1

mainFunction · 0.85

Calls 1

CloseMethod · 0.45

Tested by

no test coverage detected