|
|
@ -9,7 +9,7 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
"github.com/bilibili/kratos/pkg/conf/env" |
|
|
|
"github.com/bilibili/kratos/pkg/conf/env" |
|
|
|
"github.com/bilibili/kratos/pkg/log" |
|
|
|
"github.com/bilibili/kratos/pkg/log" |
|
|
|
criticalityPkg "github.com/bilibili/kratos/pkg/net/criticality" |
|
|
|
"github.com/bilibili/kratos/pkg/net/criticality" |
|
|
|
"github.com/bilibili/kratos/pkg/net/metadata" |
|
|
|
"github.com/bilibili/kratos/pkg/net/metadata" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors" |
|
|
|
"github.com/pkg/errors" |
|
|
@ -18,36 +18,17 @@ import ( |
|
|
|
const ( |
|
|
|
const ( |
|
|
|
// http head
|
|
|
|
// http head
|
|
|
|
_httpHeaderUser = "x1-bmspy-user" |
|
|
|
_httpHeaderUser = "x1-bmspy-user" |
|
|
|
_httpHeaderColor = "x1-bmspy-color" |
|
|
|
|
|
|
|
_httpHeaderTimeout = "x1-bmspy-timeout" |
|
|
|
_httpHeaderTimeout = "x1-bmspy-timeout" |
|
|
|
_httpHeaderMirror = "x1-bmspy-mirror" |
|
|
|
|
|
|
|
_httpHeaderRemoteIP = "x-backend-bm-real-ip" |
|
|
|
_httpHeaderRemoteIP = "x-backend-bm-real-ip" |
|
|
|
_httpHeaderRemoteIPPort = "x-backend-bm-real-ipport" |
|
|
|
_httpHeaderRemoteIPPort = "x-backend-bm-real-ipport" |
|
|
|
_httpHeaderCriticality = "x-backend-bili-criticality" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
|
_httpHeaderMetadata = "x-bili-metadata-" |
|
|
|
_httpHeaderMetadata = "x-bm-metadata-" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var _outgoingHeader = map[string]string{ |
|
|
|
var _parser = map[string]func(string) interface{}{ |
|
|
|
metadata.Color: _httpHeaderColor, |
|
|
|
"mirror": func(mirrorStr string) interface{} { |
|
|
|
metadata.Criticality: _httpHeaderCriticality, |
|
|
|
|
|
|
|
metadata.Mirror: _httpHeaderMirror, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func setMetadata(req *http.Request, key string, value interface{}) { |
|
|
|
|
|
|
|
strV, ok := value.(string) |
|
|
|
|
|
|
|
if !ok { |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
header := fmt.Sprintf("%s%s", _httpHeaderMetadata, strings.ReplaceAll(key, "_", "-")) |
|
|
|
|
|
|
|
req.Header.Set(header, strV) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// mirror return true if x-bmspy-mirror in http header and its value is 1 or true.
|
|
|
|
|
|
|
|
func mirror(req *http.Request) bool { |
|
|
|
|
|
|
|
mirrorStr := req.Header.Get(_httpHeaderMirror) |
|
|
|
|
|
|
|
if mirrorStr == "" { |
|
|
|
if mirrorStr == "" { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
@ -60,30 +41,41 @@ func mirror(req *http.Request) bool { |
|
|
|
log.Warn("blademaster: request mirrorStr value :%s is false", mirrorStr) |
|
|
|
log.Warn("blademaster: request mirrorStr value :%s is false", mirrorStr) |
|
|
|
} |
|
|
|
} |
|
|
|
return val |
|
|
|
return val |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
"criticality": func(in string) interface{} { |
|
|
|
|
|
|
|
if crtl := criticality.Criticality(in); crtl != criticality.EmptyCriticality { |
|
|
|
|
|
|
|
return string(crtl) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return string(criticality.Critical) |
|
|
|
// setCaller set caller into http request.
|
|
|
|
}, |
|
|
|
func setCaller(req *http.Request) { |
|
|
|
|
|
|
|
req.Header.Set(_httpHeaderUser, env.AppID) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// caller get caller from http request.
|
|
|
|
func parseMetadataTo(req *http.Request, to metadata.MD) { |
|
|
|
func caller(req *http.Request) string { |
|
|
|
for rawKey := range req.Header { |
|
|
|
return req.Header.Get(_httpHeaderUser) |
|
|
|
key := strings.ReplaceAll(strings.TrimLeft(strings.ToLower(rawKey), _httpHeaderMetadata), "-", "_") |
|
|
|
|
|
|
|
rawValue := req.Header.Get(rawKey) |
|
|
|
|
|
|
|
var value interface{} = rawValue |
|
|
|
|
|
|
|
parser, ok := _parser[key] |
|
|
|
|
|
|
|
if ok { |
|
|
|
|
|
|
|
value = parser(rawValue) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
to[key] = value |
|
|
|
// setColor set color into http request.
|
|
|
|
} |
|
|
|
func setColor(req *http.Request, color string) { |
|
|
|
return |
|
|
|
req.Header.Set(_httpHeaderColor, color) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// color get color from http request.
|
|
|
|
func setMetadata(req *http.Request, key string, value interface{}) { |
|
|
|
func color(req *http.Request) string { |
|
|
|
strV, ok := value.(string) |
|
|
|
c := req.Header.Get(_httpHeaderColor) |
|
|
|
if !ok { |
|
|
|
if c == "" { |
|
|
|
return |
|
|
|
c = env.Color |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return c |
|
|
|
header := fmt.Sprintf("%s%s", _httpHeaderMetadata, strings.ReplaceAll(key, "_", "-")) |
|
|
|
|
|
|
|
req.Header.Set(header, strV) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// setCaller set caller into http request.
|
|
|
|
|
|
|
|
func setCaller(req *http.Request) { |
|
|
|
|
|
|
|
req.Header.Set(_httpHeaderUser, env.AppID) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// setTimeout set timeout into http request.
|
|
|
|
// setTimeout set timeout into http request.
|
|
|
@ -102,12 +94,6 @@ func timeout(req *http.Request) time.Duration { |
|
|
|
return time.Duration(timeout) * time.Millisecond |
|
|
|
return time.Duration(timeout) * time.Millisecond |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// criticality get criticality from http request.
|
|
|
|
|
|
|
|
func criticality(req *http.Request) criticalityPkg.Criticality { |
|
|
|
|
|
|
|
raw := req.Header.Get(_httpHeaderCriticality) |
|
|
|
|
|
|
|
return criticalityPkg.Parse(raw) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// remoteIP implements a best effort algorithm to return the real client IP, it parses
|
|
|
|
// remoteIP implements a best effort algorithm to return the real client IP, it parses
|
|
|
|
// x-backend-bm-real-ip or X-Real-IP or X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
|
|
|
// x-backend-bm-real-ip or X-Real-IP or X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
|
|
|
|
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
|
|
|
|
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
|
|
|
|