|
@@ -1,84 +0,0 @@
|
|
|
-package api
|
|
|
-
|
|
|
-import (
|
|
|
- "encoding/json"
|
|
|
- "errors"
|
|
|
- "fmt"
|
|
|
- "io/ioutil"
|
|
|
- "net"
|
|
|
- "net/http"
|
|
|
- "pay/log"
|
|
|
- "strings"
|
|
|
- "time"
|
|
|
-
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
-)
|
|
|
-
|
|
|
-func PrintParam(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
-
|
|
|
- // return utils.IntToDecimal(1)
|
|
|
- return time.Now(), nil
|
|
|
-}
|
|
|
-func GetIp(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
-
|
|
|
- return getIpInfo(c)
|
|
|
-}
|
|
|
-
|
|
|
-const IPV6_API = "https://api.vore.top/api/IPv6?v6="
|
|
|
-const IPV4_API = "https://api.vore.top/api/IPv4?v4="
|
|
|
-
|
|
|
-type IPVRes struct {
|
|
|
- Msg string `json:"msg"`
|
|
|
- IpData *IpArea `json:"ipdata"`
|
|
|
-}
|
|
|
-
|
|
|
-type IpArea struct {
|
|
|
- Province string `json:"info1"`
|
|
|
- City string `json:"info2"`
|
|
|
- District string `json:"info3"`
|
|
|
- Isp string `json:"isp"`
|
|
|
-}
|
|
|
-
|
|
|
-type IpInfo struct {
|
|
|
- Ip string `json:"ip"`
|
|
|
- IPVRes
|
|
|
-}
|
|
|
-
|
|
|
-func getIpInfo(c *gin.Context) (interface{}, error) {
|
|
|
-
|
|
|
- _ips := c.Request.Header.Get("X-Forwarded-For")
|
|
|
- fmt.Println("remoteip-------------:", c.RemoteIP())
|
|
|
- ips := strings.Split(_ips, ",")
|
|
|
- fmt.Println("ips-----------:", ips)
|
|
|
- ipurl := ""
|
|
|
- ipType := net.ParseIP(ips[0])
|
|
|
- if ipType.To4() != nil {
|
|
|
- ipurl = IPV4_API
|
|
|
- } else {
|
|
|
- ipurl = IPV6_API
|
|
|
- }
|
|
|
-
|
|
|
- getInfoReq := fmt.Sprintf("%s%s", ipurl, ips[0])
|
|
|
- fmt.Println("requrl-----------:", getInfoReq)
|
|
|
-
|
|
|
- resp, err := http.Get(getInfoReq)
|
|
|
- if err != nil {
|
|
|
- log.Error(err)
|
|
|
- return nil, errors.New("ipv6获取地域失败")
|
|
|
- }
|
|
|
- defer resp.Body.Close()
|
|
|
-
|
|
|
- body, err := ioutil.ReadAll(resp.Body)
|
|
|
- if err != nil {
|
|
|
- log.Error(err)
|
|
|
- return nil, errors.New("ipv6读取请求数据失败")
|
|
|
- }
|
|
|
- var infos IPVRes
|
|
|
- err = json.Unmarshal(body, &infos)
|
|
|
- if err != nil {
|
|
|
- log.Error(err)
|
|
|
- return nil, errors.New("unmarshal数据失败")
|
|
|
- }
|
|
|
-
|
|
|
- return IpInfo{Ip: ips[0], IPVRes: infos}, nil
|
|
|
-}
|