From 7cb71c501f4310a6edac3f1aebcb9c6ab42f8a95 Mon Sep 17 00:00:00 2001 From: mzz2017 <2017@duck.com> Date: Wed, 5 Apr 2023 18:00:37 +0800 Subject: [PATCH] fix(vmess): bool fuzzy parsing for sharing link --- common/json/fuzzy_decoder.go | 51 ++++++++++++++++++++++++++++++++++++ main.go | 3 +++ 2 files changed, 54 insertions(+) create mode 100644 common/json/fuzzy_decoder.go diff --git a/common/json/fuzzy_decoder.go b/common/json/fuzzy_decoder.go new file mode 100644 index 0000000..3dbc7e7 --- /dev/null +++ b/common/json/fuzzy_decoder.go @@ -0,0 +1,51 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) 2023, daeuniverse Organization + */ + +package json + +import ( + jsoniter "github.com/json-iterator/go" + "unsafe" +) + +type FuzzyBoolDecoder struct { +} + +func (decoder *FuzzyBoolDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) { + valueType := iter.WhatIsNext() + switch valueType { + case jsoniter.NumberValue: + *((*bool)(ptr)) = iter.ReadFloat64() != 0 + case jsoniter.StringValue: + str := iter.ReadString() + switch str { + case "", "0": + *((*bool)(ptr)) = false + default: + *((*bool)(ptr)) = true + } + case jsoniter.BoolValue: + *((*bool)(ptr)) = iter.ReadBool() + // In order to stay consistent with the other decoders here, leaving arrays and objects out for now. + // case jsoniter.ObjectValue: + // iter.Skip() + // *((*bool)(ptr)) = true + // case jsoniter.ArrayValue: + // var nonEmptyArray bool + // iter.ReadArrayCB( + // func(*jsoniter.Iterator) bool { + // iter.Skip() + // nonEmptyArray = true + // return true + // }, + // ) + // *((*bool)(ptr)) = nonEmptyArray + case jsoniter.NilValue: + iter.Skip() + *((*bool)(ptr)) = false + default: + iter.ReportError("FuzzyBoolDecoder", "not number, string or bool") + } +} \ No newline at end of file diff --git a/main.go b/main.go index 5df770b..12d7d09 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ package main import ( "github.com/daeuniverse/dae/cmd" + "github.com/daeuniverse/dae/common/json" + jsoniter "github.com/json-iterator/go" "github.com/json-iterator/go/extra" "net/http" "os" @@ -16,6 +18,7 @@ import ( ) func main() { + jsoniter.RegisterTypeDecoder("bool", &json.FuzzyBoolDecoder{}) extra.RegisterFuzzyDecoders() http.DefaultClient.Timeout = 30 * time.Second