云算提交任务添加ip信息

This commit is contained in:
zhangwei 2025-02-17 19:18:17 +08:00
parent e12815dea8
commit f4de311e65
6 changed files with 57 additions and 0 deletions

View File

@ -166,6 +166,7 @@ type (
StaticWeightMap map[string]int32 `json:"staticWeightMap,optional"`
ReqBody []string `json:"reqBody"`
Replicas int64 `json:"replicas,string"`
UserIp string `json:"userIp"`
}
PodLogsReq {

View File

@ -7,6 +7,7 @@ import (
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"k8s.io/apimachinery/pkg/util/json"
"net/http"
)
@ -19,6 +20,9 @@ func CommitGeneralTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
result.ParamErrorResult(r, w, err)
return
}
// 获取ip信息
ip := utils.GetClientIP(r)
req.UserIp = ip
// 获取token信息
token := r.Header.Get("Authorization")
req.Token = token

View File

@ -140,6 +140,7 @@ func (l *CommitGeneralTaskLogic) CommitGeneralTask(req *types.GeneralTaskReq) er
return err
}
remoteUtil.Evidence(remoteUtil.EvidenceParam{
UserIp: req.UserIp,
Url: l.svcCtx.Config.BlockChain.Url,
ContractAddress: l.svcCtx.Config.BlockChain.ContractAddress,
FunctionName: l.svcCtx.Config.BlockChain.FunctionName,

View File

@ -150,6 +150,7 @@ type GeneralTaskReq struct {
StaticWeightMap map[string]int32 `json:"staticWeightMap,optional"`
ReqBody []string `json:"reqBody"`
Replicas int64 `json:"replicas,string"`
UserIp string `json:"userIp"`
}
type PodLogsReq struct {

48
pkg/utils/ipUtil.go Normal file
View File

@ -0,0 +1,48 @@
package utils
import (
"net"
"net/http"
"strings"
)
func GetClientIP(r *http.Request) string {
// 检查反向代理设置的常用头信息(按优先级排序)
headers := []string{
"X-Forwarded-For",
"X-Real-Ip",
"Proxy-Client-IP",
"WL-Proxy-Client-IP",
"HTTP_CLIENT_IP",
"HTTP_X_FORWARDED_FOR",
}
for _, header := range headers {
ip := r.Header.Get(header)
if ip != "" {
// 处理多IP情况取第一个有效IP
parts := strings.Split(ip, ",")
for _, part := range parts {
ip = strings.TrimSpace(part)
if ip != "" && !strings.EqualFold(ip, "unknown") {
return parseIP(ip)
}
}
}
}
// 回退到直接连接IP带端口时需要处理
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr // 无端口的情况直接返回
}
return parseIP(ip)
}
// 处理特殊格式IP如IPv6本地地址
func parseIP(ip string) string {
if ip == "::1" {
return "127.0.0.1"
}
return ip
}

View File

@ -12,11 +12,13 @@ type EvidenceParam struct {
FunctionName string `json:"functionName"`
Args []string `json:"args"`
Token string `json:"token"`
UserIp string `json:"userIp"`
}
func Evidence(EvidenceParam EvidenceParam) error {
httpClient := resty.New().R()
_, err := httpClient.SetHeader("Content-Type", "application/json").
SetHeader("Proxy-Client-IP", EvidenceParam.UserIp).
SetHeader("Authorization", EvidenceParam.Token).
SetBody(&EvidenceParam).
Post(EvidenceParam.Url)