ipinfo_private.go 3.88 KB
Newer Older
qiuqunfeng's avatar
qiuqunfeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
package service

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strconv"

	"gitlab.com/tensorsecurity-rd/waf-console/internal/model"
)

var fakePrivateData = `{
    "data": [
        {
            "ioc": "159.203.93.255",
            "host": "10.65.135.204",
            "intelligence": [
                {
                    "judgments": [
                        "Exploit"
                    ],
                    "severity": "low",
                    "ban": {
                        "banned": 1,
                        "reason": "The IP address belongs to DigitalOcean, LLC, it is recommended to assess and handle it accordingly."
                    },
                    "basic": {
                        "carrier": "DigitalOcean, LLC",
                        "location": {
                            "country": "美国",
                            "country_code": "US",
                            "province": "新泽西州",
                            "city": "克利夫顿",
                            "lng": -74.16366,
                            "lat": 40.858402
                        }
                    },
                    "asn": {
                        "number": "14061",
                        "info": "DIGITALOCEAN-ASN - DigitalOcean, LLC, US"
                    },
                    "ioc_type": "ipv4",
                    "confidence_level": "low",
                    "is_malicious": true,
                    "source_name": "微步在线-IP信誉",
                    "update_time": 1719268503000
                }
            ]
        }
    ],
    "response_code": 0,
    "verbose_msg": "success"
}`

type ipServicePrivate struct {
	URL    string
	ApiKey string
	// ipQueryUrl      string
	ipReputationUrl string
	// ipInfoMap       map[string]IpInfo
	// useCachedIPInfo bool
	// ipInfoCache     *expirable.LRU[string, IPInfoPrivate]
}

// func NewIpServicePrivate(url, apiKey string, useCachedIPInfo bool) Service {
// 	var ipReputationUrl string
// 	reputationUrl := os.Getenv("IP_REPUTATION_URL")
// 	logging.Get().Info().Msgf("reputationUrl: %s", reputationUrl)
// 	if reputationUrl != "" {
// 		ipReputationUrl = reputationUrl
// 	} else {
// 		ipReputationUrl = fmt.Sprintf("%s?apikey=%s&resource=", ipReputation, apiKey)
// 	}
// 	logging.Get().Info().Msgf("ipReputationUrl: %s", ipReputationUrl)
// 	return &ipServicePrivate{
// 		URL:             url,
// 		ApiKey:          apiKey,
// 		ipReputationUrl: ipReputationUrl,
// 	}
// }

func (s *ipServicePrivate) QueryIP(ip string) (*model.IPInfo, error) {
	url := s.ipReputationUrl + ip
	respData, err := http.DefaultClient.Get(url)
	if err != nil {
		return nil, err
	}
	defer respData.Body.Close()

	body, err := io.ReadAll(respData.Body)
	if err != nil {
		return nil, fmt.Errorf("query ip info failed: %w", err)
	}

	var resp IPInfoPrivateResp
	if err := json.Unmarshal(body, &resp); err != nil {
		return nil, fmt.Errorf("unmarshal response failed: %w", err)
	}

	if len(resp.Data) == 0 || len(resp.Data[0].Intelligence) == 0 {
		return nil, fmt.Errorf("no data found for ip %s", ip)
	}

	info := resp.Data[0].Intelligence[0]
	tagsClasses := make([]model.TagsClass, 0, 5)
	for k, v := range info.TagsClasses {
		for _, tag := range v {
			tagsClasses = append(tagsClasses, model.TagsClass{
				TagsType: k,
				Tags:     tag,
			})
		}
	}
	return &model.IPInfo{
		Carrier: info.Basic.Carrier,
		Location: model.Location{
			Country:     info.Basic.Location.Country,
			CountryCode: info.Basic.Location.CountryCode,
			Province:    info.Basic.Location.Province,
			City:        info.Basic.Location.City,
			Longitude:   info.Basic.Location.Longitude,
			Latitude:    info.Basic.Location.Latitude,
		},
		Asn: model.ASN{
			Number: func() int { n, _ := strconv.Atoi(info.ASN.Number); return n }(),
			Info:   info.ASN.Info,
		},
		Scene:       info.Scene,
		IsMalicious: info.IsMalicious,
		Judgments:   info.Judgments,
		TagsClasses: tagsClasses,
	}, nil
}