go 通过淘宝api,查询出口ip详细信息

package main

import (
    "encoding/json"
    "errors"
    "io/ioutil"
    "net"
    "net/http"
    "strings"

    log "qiniupkg.com/x/log.v7"

    "time"

    "fmt"

    "github.com/gin-gonic/gin"
    "mars.qiniu.com/collections"
    "mars.qiniu.com/models"
)

// TaoBaoAPI to get the taobao api
func TaoBaoAPI(ip string, ipj *models.IPJson) (err error) {
    fmt.Printf(ip)
    client := &http.Client{}
    request, _ := http.NewRequest("GET", "http://ip.taobao.com/service/getIpInfo.php?ip="+ip, nil)
    resp, err := client.Do(request)
    if err != nil {
        return
    }
    defer resp.Body.Close()
    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return
    }
    fmt.Printf(string(data[:]))
    return json.Unmarshal(data, ipj)
}

//IPGet is to get the ip by ip info
func IPGet(c *gin.Context) {
    j := &models.IPJson{}
    ip, err := collections.IPMgr.Find(&models.IP{IP: ClientIP(c)})
    if err != nil {
        if err.Error() == "not found" {
            if err := TaoBaoAPI(ClientIP(c), j); err != nil {
                log.Error(err.Error())
                c.JSON(404, err)
                c.Abort()
                return
            }
            if j.Code == 1 {
                log.Error(errors.New("taobao not found"))
                c.JSON(404, err)
                c.Abort()
                return
            }
            if err := collections.IPMgr.Upsert(&j.Data); err != nil {
                log.Error(err.Error())
                c.JSON(500, err)
                c.Abort()
                return
            }
            c.JSON(200, j.Data)
            return
        }
        log.Error(err.Error())
        c.JSON(404, err)
        c.Abort()
        return
    }
    if (time.Now().Day() - ip.UpdatedAt.Day()) > 20 {
        if err := TaoBaoAPI(ClientIP(c), j); err != nil {
            log.Error(err.Error())
        } else {
            if j.Code == 1 {
                log.Error(errors.New("taobao not found"))
            } else {
                if err := collections.IPMgr.Upsert(&j.Data); err != nil {
                    log.Error(err.Error())
                } else {
                    c.JSON(200, j.Data)
                    return
                }
            }
        }
    }
    c.JSON(200, ip)
}

//ClientIP is to get remote ip
func ClientIP(c *gin.Context) string {
    clientIP := c.Request.Header.Get("X-Forwarded-For")
    if index := strings.IndexByte(clientIP, ','); index >= 0 {
        clientIP = clientIP[0:index]
    }
    clientIP = strings.TrimSpace(clientIP)
    if len(clientIP) > 0 {

        return clientIP
    }
    clientIP = strings.TrimSpace(c.Request.Header.Get("X-Real-Ip"))
    if len(clientIP) > 0 {
        c.JSON(200, gin.H{"ip": clientIP})
        return clientIP
    }

    if ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr)); err == nil {
        return ip
    }
    return "127.0.0.1"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值