2023-12-29 16:16:48 +00:00
|
|
|
package reflect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-08-08 14:35:46 +00:00
|
|
|
"fmt"
|
2023-12-29 16:16:48 +00:00
|
|
|
"reflect"
|
2024-08-08 14:35:46 +00:00
|
|
|
"strings"
|
2023-12-29 16:16:48 +00:00
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
cnet "github.com/xtls/xray-core/common/net"
|
2023-12-29 16:16:48 +00:00
|
|
|
cserial "github.com/xtls/xray-core/common/serial"
|
2024-08-08 14:35:46 +00:00
|
|
|
"github.com/xtls/xray-core/infra/conf"
|
2023-12-29 16:16:48 +00:00
|
|
|
)
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
func MarshalToJson(v interface{}, insertTypeInfo bool) (string, bool) {
|
|
|
|
if itf := marshalInterface(v, true, insertTypeInfo); itf != nil {
|
2023-12-29 16:16:48 +00:00
|
|
|
if b, err := json.MarshalIndent(itf, "", " "); err == nil {
|
|
|
|
return string(b[:]), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
func marshalTypedMessage(v *cserial.TypedMessage, ignoreNullValue bool, insertTypeInfo bool) interface{} {
|
2024-01-11 15:34:26 +00:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-29 16:16:48 +00:00
|
|
|
tmsg, err := v.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-08 14:35:46 +00:00
|
|
|
r := marshalInterface(tmsg, ignoreNullValue, insertTypeInfo)
|
|
|
|
if msg, ok := r.(map[string]interface{}); ok && insertTypeInfo {
|
2023-12-29 16:16:48 +00:00
|
|
|
msg["_TypedMessage_"] = v.Type
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
func marshalSlice(v reflect.Value, ignoreNullValue bool, insertTypeInfo bool) interface{} {
|
2023-12-29 16:16:48 +00:00
|
|
|
r := make([]interface{}, 0)
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
rv := v.Index(i)
|
|
|
|
if rv.CanInterface() {
|
|
|
|
value := rv.Interface()
|
2024-08-08 14:35:46 +00:00
|
|
|
r = append(r, marshalInterface(value, ignoreNullValue, insertTypeInfo))
|
2023-12-29 16:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
func isNullValue(f reflect.StructField, rv reflect.Value) bool {
|
|
|
|
if rv.Kind() == reflect.String && rv.Len() == 0 {
|
|
|
|
return true
|
|
|
|
} else if !isValueKind(rv.Kind()) && rv.IsNil() {
|
|
|
|
return true
|
|
|
|
} else if tag := f.Tag.Get("json"); strings.Contains(tag, "omitempty") {
|
|
|
|
if !rv.IsValid() || rv.IsZero() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func toJsonName(f reflect.StructField) string {
|
|
|
|
if tags := f.Tag.Get("protobuf"); len(tags) > 0 {
|
|
|
|
for _, tag := range strings.Split(tags, ",") {
|
|
|
|
if before, after, ok := strings.Cut(tag, "="); ok && before == "json" {
|
|
|
|
return after
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tag := f.Tag.Get("json"); len(tag) > 0 {
|
|
|
|
if before, _, ok := strings.Cut(tag, ","); ok {
|
|
|
|
return before
|
|
|
|
} else {
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalStruct(v reflect.Value, ignoreNullValue bool, insertTypeInfo bool) interface{} {
|
2023-12-29 16:16:48 +00:00
|
|
|
r := make(map[string]interface{})
|
|
|
|
t := v.Type()
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
rv := v.Field(i)
|
|
|
|
if rv.CanInterface() {
|
|
|
|
ft := t.Field(i)
|
2024-08-08 14:35:46 +00:00
|
|
|
if !ignoreNullValue || !isNullValue(ft, rv) {
|
|
|
|
name := toJsonName(ft)
|
|
|
|
value := rv.Interface()
|
|
|
|
tv := marshalInterface(value, ignoreNullValue, insertTypeInfo)
|
2023-12-29 16:16:48 +00:00
|
|
|
r[name] = tv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
func marshalMap(v reflect.Value, ignoreNullValue bool, insertTypeInfo bool) interface{} {
|
2023-12-29 16:16:48 +00:00
|
|
|
// policy.level is map[uint32] *struct
|
|
|
|
kt := v.Type().Key()
|
|
|
|
vt := reflect.TypeOf((*interface{})(nil))
|
|
|
|
mt := reflect.MapOf(kt, vt)
|
|
|
|
r := reflect.MakeMap(mt)
|
|
|
|
for _, key := range v.MapKeys() {
|
|
|
|
rv := v.MapIndex(key)
|
|
|
|
if rv.CanInterface() {
|
|
|
|
iv := rv.Interface()
|
2024-08-08 14:35:46 +00:00
|
|
|
tv := marshalInterface(iv, ignoreNullValue, insertTypeInfo)
|
2023-12-29 16:16:48 +00:00
|
|
|
if tv != nil || !ignoreNullValue {
|
|
|
|
r.SetMapIndex(key, reflect.ValueOf(&tv))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalIString(v interface{}) (r string, ok bool) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
r = ""
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if iStringFn, ok := v.(interface{ String() string }); ok {
|
|
|
|
return iStringFn.String(), true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
func serializePortList(portList *cnet.PortList) (interface{}, bool) {
|
|
|
|
if portList == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
n := len(portList.Range)
|
|
|
|
if n == 1 {
|
|
|
|
if first := portList.Range[0]; first.From == first.To {
|
|
|
|
return first.From, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r := make([]string, 0, n)
|
|
|
|
for _, pr := range portList.Range {
|
|
|
|
if pr.From == pr.To {
|
|
|
|
r = append(r, pr.FromPort().String())
|
|
|
|
} else {
|
|
|
|
r = append(r, fmt.Sprintf("%d-%d", pr.From, pr.To))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(r, ","), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalKnownType(v interface{}, ignoreNullValue bool, insertTypeInfo bool) (interface{}, bool) {
|
2023-12-29 16:16:48 +00:00
|
|
|
switch ty := v.(type) {
|
|
|
|
case cserial.TypedMessage:
|
2024-08-08 14:35:46 +00:00
|
|
|
return marshalTypedMessage(&ty, ignoreNullValue, insertTypeInfo), true
|
2023-12-29 16:16:48 +00:00
|
|
|
case *cserial.TypedMessage:
|
2024-08-08 14:35:46 +00:00
|
|
|
return marshalTypedMessage(ty, ignoreNullValue, insertTypeInfo), true
|
2023-12-29 16:16:48 +00:00
|
|
|
case map[string]json.RawMessage:
|
|
|
|
return ty, true
|
|
|
|
case []json.RawMessage:
|
|
|
|
return ty, true
|
2024-08-08 14:35:46 +00:00
|
|
|
case *json.RawMessage, json.RawMessage:
|
2023-12-29 16:16:48 +00:00
|
|
|
return ty, true
|
2024-08-08 14:35:46 +00:00
|
|
|
case *cnet.IPOrDomain:
|
|
|
|
if domain := v.(*cnet.IPOrDomain); domain != nil {
|
|
|
|
return domain.AsAddress().String(), true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
case *cnet.PortList:
|
|
|
|
npl := v.(*cnet.PortList)
|
|
|
|
return serializePortList(npl)
|
|
|
|
case *conf.PortList:
|
|
|
|
cpl := v.(*conf.PortList)
|
|
|
|
return serializePortList(cpl.Build())
|
|
|
|
case cnet.Address:
|
|
|
|
if addr := v.(cnet.Address); addr != nil {
|
|
|
|
return addr.String(), true
|
|
|
|
}
|
|
|
|
return nil, false
|
2023-12-29 16:16:48 +00:00
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValueKind(kind reflect.Kind) bool {
|
2024-01-11 15:34:26 +00:00
|
|
|
switch kind {
|
|
|
|
case reflect.Bool,
|
|
|
|
reflect.Int,
|
|
|
|
reflect.Int8,
|
|
|
|
reflect.Int16,
|
|
|
|
reflect.Int32,
|
|
|
|
reflect.Int64,
|
|
|
|
reflect.Uint,
|
|
|
|
reflect.Uint8,
|
|
|
|
reflect.Uint16,
|
|
|
|
reflect.Uint32,
|
|
|
|
reflect.Uint64,
|
|
|
|
reflect.Uintptr,
|
|
|
|
reflect.Float32,
|
|
|
|
reflect.Float64,
|
|
|
|
reflect.Complex64,
|
|
|
|
reflect.Complex128,
|
|
|
|
reflect.String:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2023-12-29 16:16:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
func marshalInterface(v interface{}, ignoreNullValue bool, insertTypeInfo bool) interface{} {
|
2023-12-29 16:16:48 +00:00
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
if r, ok := marshalKnownType(v, ignoreNullValue, insertTypeInfo); ok {
|
2023-12-29 16:16:48 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
if rv.Kind() == reflect.Ptr {
|
|
|
|
rv = rv.Elem()
|
|
|
|
}
|
|
|
|
k := rv.Kind()
|
|
|
|
if k == reflect.Invalid {
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-08 14:35:46 +00:00
|
|
|
|
|
|
|
if ty := rv.Type().Name(); isValueKind(k) {
|
|
|
|
if k.String() != ty {
|
|
|
|
if s, ok := marshalIString(v); ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
2023-12-29 16:16:48 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
// fmt.Println("kind:", k, "type:", rv.Type().Name())
|
|
|
|
|
2023-12-29 16:16:48 +00:00
|
|
|
switch k {
|
|
|
|
case reflect.Struct:
|
2024-08-08 14:35:46 +00:00
|
|
|
return marshalStruct(rv, ignoreNullValue, insertTypeInfo)
|
2023-12-29 16:16:48 +00:00
|
|
|
case reflect.Slice:
|
2024-08-08 14:35:46 +00:00
|
|
|
return marshalSlice(rv, ignoreNullValue, insertTypeInfo)
|
2023-12-29 16:16:48 +00:00
|
|
|
case reflect.Array:
|
2024-08-08 14:35:46 +00:00
|
|
|
return marshalSlice(rv, ignoreNullValue, insertTypeInfo)
|
2023-12-29 16:16:48 +00:00
|
|
|
case reflect.Map:
|
2024-08-08 14:35:46 +00:00
|
|
|
return marshalMap(rv, ignoreNullValue, insertTypeInfo)
|
2023-12-29 16:16:48 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if str, ok := marshalIString(v); ok {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|