mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2024-12-22 06:15:51 +00:00
f8f740b0cf
* chore: increase the length of traceId token * fix: size of string builder for trace id * chore: rename --no-banner to --banner * chore: update docs * chore: prepare for next release
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
"strings"
|
|
)
|
|
|
|
type scopeCtxKey struct{}
|
|
|
|
func GetCtxWithScope(ctx context.Context, scope string) context.Context {
|
|
return context.WithValue(ctx, scopeCtxKey{}, scope)
|
|
}
|
|
|
|
func GetScopeFromCtx(ctx context.Context) (string, bool) {
|
|
if scope, ok := ctx.Value(scopeCtxKey{}).(string); ok {
|
|
return scope, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
type traceIdCtxKey struct{}
|
|
|
|
func GetCtxWithTraceId(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, traceIdCtxKey{}, generateTraceId())
|
|
}
|
|
|
|
func GetTraceIdFromCtx(ctx context.Context) (string, bool) {
|
|
if traceId, ok := ctx.Value(traceIdCtxKey{}).(string); ok {
|
|
return traceId, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func generateTraceId() string {
|
|
sb := strings.Builder{}
|
|
sb.Grow(35)
|
|
|
|
var q uint64
|
|
var r uint8
|
|
for i := 0; i < 32; i++ {
|
|
if i%15 == 0 {
|
|
q = rand.Uint64()
|
|
}
|
|
q, r = q>>4, uint8(q&0xF)
|
|
if r > 9 {
|
|
r += 0x27
|
|
}
|
|
sb.WriteByte(r + 0x30)
|
|
if i&7 == 7 && i != 31 {
|
|
sb.WriteByte(0x2D)
|
|
}
|
|
}
|
|
return sb.String()
|
|
}
|