chore: 初始开源发布
iotlcm-admin-svr:管理后台服务。 - Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件) - 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、 MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位; 私网 IP → 文档地址段;库名 xiujie_admin → iotlcm - 真实手机号/邮箱 → 合法 11 位编造号 / example.com - 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、 私有依赖 go get 行、明文 registry 登录口令 - 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名 - Dockerfile 改用公共基础镜像 - 拼错的 RADEME.md → README.md,README 重写 - 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用 - 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
package utility
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gcharset"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
)
|
||||
|
||||
type ipLookupEntry struct {
|
||||
Value string
|
||||
RefreshAt time.Time
|
||||
}
|
||||
|
||||
type asyncIPLookup struct {
|
||||
cache *gcache.Cache
|
||||
mutex sync.Mutex
|
||||
pending map[string]bool
|
||||
workers chan struct{}
|
||||
lookup func(context.Context, string) (string, error)
|
||||
timeout time.Duration
|
||||
ttl time.Duration
|
||||
retryDelay time.Duration
|
||||
}
|
||||
|
||||
var (
|
||||
cityLookup = newAsyncIPLookup(lookupCityByIP, time.Second, 24*time.Hour, time.Minute, 4)
|
||||
publicIPLookup = newAsyncIPLookup(func(ctx context.Context, _ string) (string, error) {
|
||||
return lookupPublicIP(ctx)
|
||||
}, 2*time.Second, time.Hour, time.Minute, 1)
|
||||
)
|
||||
|
||||
func newAsyncIPLookup(lookup func(context.Context, string) (string, error), timeout, ttl, retryDelay time.Duration, workers int) *asyncIPLookup {
|
||||
return &asyncIPLookup{
|
||||
cache: gcache.New(4096),
|
||||
pending: make(map[string]bool),
|
||||
workers: make(chan struct{}, workers),
|
||||
lookup: lookup,
|
||||
timeout: timeout,
|
||||
ttl: ttl,
|
||||
retryDelay: retryDelay,
|
||||
}
|
||||
}
|
||||
|
||||
func (lookup *asyncIPLookup) Get(key string) string {
|
||||
lookup.mutex.Lock()
|
||||
defer lookup.mutex.Unlock()
|
||||
entry := ipLookupEntry{}
|
||||
if value, err := lookup.cache.Get(context.Background(), key); err == nil && value != nil {
|
||||
entry, _ = value.Val().(ipLookupEntry)
|
||||
}
|
||||
if time.Now().Before(entry.RefreshAt) || lookup.pending[key] {
|
||||
return entry.Value
|
||||
}
|
||||
select {
|
||||
case lookup.workers <- struct{}{}:
|
||||
lookup.pending[key] = true
|
||||
go lookup.refresh(key, entry)
|
||||
default:
|
||||
}
|
||||
return entry.Value
|
||||
}
|
||||
|
||||
func (lookup *asyncIPLookup) refresh(key string, entry ipLookupEntry) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), lookup.timeout)
|
||||
defer cancel()
|
||||
value, err := lookup.lookup(ctx, key)
|
||||
delay := lookup.retryDelay
|
||||
if err == nil && value != "" {
|
||||
entry.Value = value
|
||||
delay = lookup.ttl
|
||||
}
|
||||
entry.RefreshAt = time.Now().Add(delay)
|
||||
retention := delay
|
||||
if entry.Value != "" {
|
||||
retention = 2 * lookup.ttl
|
||||
}
|
||||
lookup.mutex.Lock()
|
||||
defer lookup.mutex.Unlock()
|
||||
_ = lookup.cache.Set(context.Background(), key, entry, retention)
|
||||
delete(lookup.pending, key)
|
||||
<-lookup.workers
|
||||
}
|
||||
|
||||
func GetCachedPublicIP() string {
|
||||
return publicIPLookup.Get("public-ip")
|
||||
}
|
||||
|
||||
func lookupCityByIP(ctx context.Context, ip string) (string, error) {
|
||||
query := url.Values{"json": {"true"}, "ip": {ip}}
|
||||
payload, err := readIPLookupResponse(ctx, "https://whois.pconline.com.cn/ipJson.jsp?"+query.Encode())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return parseCityByIPResponse(payload)
|
||||
}
|
||||
|
||||
func parseCityByIPResponse(payload []byte) (string, error) {
|
||||
text, err := gcharset.ToUTF8("GBK", string(payload))
|
||||
if err != nil {
|
||||
return "", gerror.Wrap(err, "decode IP location response failed")
|
||||
}
|
||||
var response struct {
|
||||
Code *int `json:"code"`
|
||||
Province string `json:"pro"`
|
||||
City string `json:"city"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(text), &response); err != nil {
|
||||
return "", gerror.Wrap(err, "parse IP location response failed")
|
||||
}
|
||||
if response.Code == nil || *response.Code != 0 {
|
||||
return "", gerror.New("IP location lookup failed")
|
||||
}
|
||||
if strings.EqualFold(response.Province, response.City) {
|
||||
return strings.TrimSpace(response.Province), nil
|
||||
}
|
||||
return strings.TrimSpace(response.Province + " " + response.City), nil
|
||||
}
|
||||
|
||||
func lookupPublicIP(ctx context.Context) (string, error) {
|
||||
payload, err := readIPLookupResponse(ctx, "https://ifconfig.co/ip")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ip := net.ParseIP(strings.TrimSpace(string(payload)))
|
||||
if ip == nil || !ip.IsGlobalUnicast() || ip.IsPrivate() {
|
||||
return "", gerror.New("invalid public IP response")
|
||||
}
|
||||
return ip.String(), nil
|
||||
}
|
||||
|
||||
func readIPLookupResponse(ctx context.Context, endpoint string) ([]byte, error) {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "create IP lookup request failed")
|
||||
}
|
||||
client := &http.Client{Timeout: 2 * time.Second}
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "IP lookup request failed")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, gerror.Newf("IP lookup HTTP status: %d", response.StatusCode)
|
||||
}
|
||||
const maxBodySize = 64 * 1024
|
||||
payload, err := io.ReadAll(io.LimitReader(response.Body, maxBodySize+1))
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "read IP lookup response failed")
|
||||
}
|
||||
if len(payload) > maxBodySize {
|
||||
return nil, gerror.New("IP lookup response is too large")
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
Reference in New Issue
Block a user