Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f7948263c5 | |||
| fc42bc92ff | |||
| b8f693ef82 |
@@ -2,9 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"google.golang.org/grpc/metadata"
|
||||
@@ -15,6 +14,7 @@ type Meta struct {
|
||||
IDENTITY string `json:"identity"`
|
||||
EXTEND map[string]string `json:"extend"`
|
||||
CLIENT string `json:"client"`
|
||||
ROLE string `json:"role"`
|
||||
}
|
||||
|
||||
// 解析Context中MetaData的数据
|
||||
@@ -30,31 +30,28 @@ func ParseMetaCtx(ctx context.Context, opts *ParseOptions) (*Meta, error) {
|
||||
return nil, errcode.ErrJWTAuthNotFound
|
||||
}
|
||||
|
||||
// 安全获取 metadata 中的值
|
||||
identityValues := md.Get("authorization_identity")
|
||||
clientValues := md.Get("client")
|
||||
|
||||
if len(identityValues) == 0 {
|
||||
var Authorizations []string = md.Get("authorization")
|
||||
if len(Authorizations) == 0 || Authorizations[0] == "" {
|
||||
return nil, errcode.ErrJWTAuthNotFound
|
||||
}
|
||||
|
||||
claims, err := encipher.ParseTokenAes(Authorizations[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clientValues := md.Get("client")
|
||||
|
||||
if len(clientValues) == 0 {
|
||||
return nil, errcode.ErrJWTAuthNotFound
|
||||
}
|
||||
|
||||
meta := &Meta{
|
||||
IDENTITY: md["authorization_identity"][0],
|
||||
ID: claims.ID,
|
||||
IDENTITY: claims.Identity,
|
||||
CLIENT: md["client"][0],
|
||||
}
|
||||
|
||||
if id, err := strconv.Atoi(md["authorization_id"][0]); err != nil {
|
||||
return nil, errcode.ErrJWTAuthKeyId
|
||||
} else {
|
||||
meta.ID = uint(id)
|
||||
}
|
||||
|
||||
data := make(map[string]string)
|
||||
if err := json.Unmarshal([]byte(md["authorization_extend"][0]), &data); err == nil {
|
||||
meta.EXTEND = data
|
||||
EXTEND: claims.Extend,
|
||||
ROLE: claims.Role,
|
||||
}
|
||||
|
||||
if opts != nil {
|
||||
|
||||
58
utils/net.go
58
utils/net.go
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func IsPublicIP(ipString string) bool {
|
||||
@@ -32,24 +33,55 @@ func IsPublicIP(ipString string) bool {
|
||||
}
|
||||
|
||||
// Get Location IP .
|
||||
func GetLocationIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
func GetLocationIP() (localIp string) {
|
||||
localIp = "127.0.0.1"
|
||||
// Get all network interfaces
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return ""
|
||||
return
|
||||
}
|
||||
ip := ""
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
ip = ipnet.IP.String()
|
||||
break
|
||||
|
||||
for _, iface := range interfaces {
|
||||
// Skip the loopback interface
|
||||
if iface.Flags&net.FlagLoopback != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get addresses associated with the interface
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
// Check if the address is an IPNet
|
||||
ipnet, ok := addr.(*net.IPNet)
|
||||
if !ok || ipnet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get the IP address
|
||||
ip := ipnet.IP.To4()
|
||||
if ip == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip IP addresses in the 169.254.x.x range
|
||||
if strings.HasPrefix(ip.String(), "169.254") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip IP addresses in the 169.254.x.x range
|
||||
if strings.HasPrefix(ip.String(), "26.26") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Return the first valid IP address found
|
||||
return ip.String()
|
||||
}
|
||||
}
|
||||
if ip == "" {
|
||||
return ""
|
||||
}
|
||||
return ip
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func LocalIPv4s() ([]string, error) {
|
||||
|
||||
Reference in New Issue
Block a user