github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/pcsutil/getip/getip_netease.go (about)

     1  package getip
     2  
     3  import (
     4  	"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/jsonhelper"
     5  	"github.com/qjfoidnh/BaiduPCS-Go/requester"
     6  	"net"
     7  	"net/http"
     8  )
     9  
    10  type (
    11  	// IPResNetease 网易服务器获取ip返回的结果
    12  	IPResNetease struct {
    13  		Result  string `json:"result"`
    14  		Code    int    `json:"code"`
    15  		Message string `json:"message"`
    16  	}
    17  )
    18  
    19  func IPInfoFromNeteaseByClient(c *requester.HTTPClient) (ipAddr string, err error) {
    20  	resp, err := c.Req(http.MethodGet, "http://mam.netease.com/api/config/getClientIp", nil, nil)
    21  	if resp != nil {
    22  		defer resp.Body.Close()
    23  	}
    24  	if err != nil {
    25  		return
    26  	}
    27  
    28  	res := &IPResNetease{}
    29  	err = jsonhelper.UnmarshalData(resp.Body, res)
    30  	if err != nil {
    31  		return
    32  	}
    33  
    34  	ip := net.ParseIP(res.Result)
    35  	if ip == nil {
    36  		err = ErrParseIP
    37  		return
    38  	}
    39  
    40  	ipAddr = res.Result
    41  	return
    42  }
    43  
    44  // IPInfoFromNetease 从网易服务器获取ip
    45  func IPInfoFromNetease() (ipAddr string, err error) {
    46  	c := requester.NewHTTPClient()
    47  	return IPInfoFromNeteaseByClient(c)
    48  }