ip info mentanced

master
dustoair 3 years ago
parent 95042f5eb3
commit dcd6516467

@ -18,14 +18,46 @@ func main() {
func IPHandle(w http.ResponseWriter, req *http.Request) {
address := req.FormValue("address")
var ipInfo ip2region.IpInfo
if address == "" {
addressType := ""
ipDomain := ""
switch {
case address == "":
//没传address值 则拿请求ip来查询
addressReq, _ := util.GetIP(req)
address = addressReq
addressType = "self"
case util.MatchIP(address):
//传了address值 是ip 则直接查询
addressType = "ip"
default:
//传了address值 不是ip 则查询ip
ipAddress, err := util.GetIPByDomain(address)
if err != nil {
//提供非法域名 则拿请求ip来查询
addressReq, _ := util.GetIP(req)
address = addressReq
addressType = "self"
} else {
//提供正确域名 则查询ip
ipDomain = ipAddress
address = ipAddress
addressType = "domain"
}
}
var ipInfo ip2region.IpInfo
ipInfo, _ = ip2region.GetIPInfo(address, global.SearchType)
ipInfo.Domain = address
switch addressType {
case "ip":
ipInfo.IP = address
ipInfo.Domain = ""
case "domain":
ipInfo.IP = ipDomain
ipInfo.Domain = address
case "self":
ipInfo.IP = address
ipInfo.Domain = ""
}
res, _ := json.Marshal(ipInfo)
fmt.Fprintln(w, string(res))

Loading…
Cancel
Save