github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_geo_ip.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "fmt" 10 11 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 12 "github.com/GuanceCloud/platypus/pkg/ast" 13 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 14 "github.com/GuanceCloud/platypus/pkg/errchain" 15 ) 16 17 // var ipdbInstance ipdb.IPdb 18 19 var geoDefaultVal = "unknown" 20 21 // func Geo(ip string) (*ipdb.IPdbRecord, error) { 22 // if ipdbInstance != nil { 23 // return ipdbInstance.Geo(ip) 24 // } else { 25 // return &ipdb.IPdbRecord{}, nil 26 // } 27 // } 28 29 // func InitIPdb(instance ipdb.IPdb) { 30 // ipdbInstance = instance 31 // } 32 33 func GeoIPChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 34 if len(funcExpr.Param) != 1 { 35 return runtime.NewRunError(ctx, fmt.Sprintf( 36 "func `%s' expected 1 args", funcExpr.Name), funcExpr.NamePos) 37 } 38 39 if _, err := getKeyName(funcExpr.Param[0]); err != nil { 40 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 41 } 42 43 return nil 44 } 45 46 func GeoIP(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 47 if len(funcExpr.Param) != 1 { 48 return runtime.NewRunError(ctx, fmt.Sprintf( 49 "func `%s' expected 1 args", funcExpr.Name), funcExpr.NamePos) 50 } 51 key, err := getKeyName(funcExpr.Param[0]) 52 if err != nil { 53 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 54 } 55 56 ipStr, err := ctx.GetKeyConv2Str(key) 57 if err != nil { 58 l.Debugf("key `%v' not exist, ignored", key) 59 return nil //nolint:nilerr 60 } 61 62 pt, err := getPoint(ctx.InData()) 63 if err != nil { 64 return nil 65 } 66 67 if dic, err := GeoIPHandle(pt.GetIPDB(), ipStr); err != nil { 68 l.Debugf("GeoIPHandle: %s, ignored", err) 69 return nil 70 } else { 71 for k, v := range dic { 72 _ = addKey2PtWithVal(ctx.InData(), k, v, ast.String, ptinput.KindPtDefault) 73 } 74 } 75 76 return nil 77 }