mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
9
infra/conf/serial/errors.generated.go
Normal file
9
infra/conf/serial/errors.generated.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package serial
|
||||
|
||||
import "github.com/xtls/xray-core/v1/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
82
infra/conf/serial/loader.go
Normal file
82
infra/conf/serial/loader.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package serial
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common/errors"
|
||||
"github.com/xtls/xray-core/v1/core"
|
||||
"github.com/xtls/xray-core/v1/infra/conf"
|
||||
json_reader "github.com/xtls/xray-core/v1/infra/conf/json"
|
||||
)
|
||||
|
||||
type offset struct {
|
||||
line int
|
||||
char int
|
||||
}
|
||||
|
||||
func findOffset(b []byte, o int) *offset {
|
||||
if o >= len(b) || o < 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
line := 1
|
||||
char := 0
|
||||
for i, x := range b {
|
||||
if i == o {
|
||||
break
|
||||
}
|
||||
if x == '\n' {
|
||||
line++
|
||||
char = 0
|
||||
} else {
|
||||
char++
|
||||
}
|
||||
}
|
||||
|
||||
return &offset{line: line, char: char}
|
||||
}
|
||||
|
||||
// DecodeJSONConfig reads from reader and decode the config into *conf.Config
|
||||
// syntax error could be detected.
|
||||
func DecodeJSONConfig(reader io.Reader) (*conf.Config, error) {
|
||||
jsonConfig := &conf.Config{}
|
||||
|
||||
jsonContent := bytes.NewBuffer(make([]byte, 0, 10240))
|
||||
jsonReader := io.TeeReader(&json_reader.Reader{
|
||||
Reader: reader,
|
||||
}, jsonContent)
|
||||
decoder := json.NewDecoder(jsonReader)
|
||||
|
||||
if err := decoder.Decode(jsonConfig); err != nil {
|
||||
var pos *offset
|
||||
cause := errors.Cause(err)
|
||||
switch tErr := cause.(type) {
|
||||
case *json.SyntaxError:
|
||||
pos = findOffset(jsonContent.Bytes(), int(tErr.Offset))
|
||||
case *json.UnmarshalTypeError:
|
||||
pos = findOffset(jsonContent.Bytes(), int(tErr.Offset))
|
||||
}
|
||||
if pos != nil {
|
||||
return nil, newError("failed to read config file at line ", pos.line, " char ", pos.char).Base(err)
|
||||
}
|
||||
return nil, newError("failed to read config file").Base(err)
|
||||
}
|
||||
|
||||
return jsonConfig, nil
|
||||
}
|
||||
|
||||
func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
|
||||
jsonConfig, err := DecodeJSONConfig(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pbConfig, err := jsonConfig.Build()
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse json config").Base(err)
|
||||
}
|
||||
|
||||
return pbConfig, nil
|
||||
}
|
63
infra/conf/serial/loader_test.go
Normal file
63
infra/conf/serial/loader_test.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package serial_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/xtls/xray-core/v1/infra/conf/serial"
|
||||
)
|
||||
|
||||
func TestLoaderError(t *testing.T) {
|
||||
testCases := []struct {
|
||||
Input string
|
||||
Output string
|
||||
}{
|
||||
{
|
||||
Input: `{
|
||||
"log": {
|
||||
// abcd
|
||||
0,
|
||||
"loglevel": "info"
|
||||
}
|
||||
}`,
|
||||
Output: "line 4 char 6",
|
||||
},
|
||||
{
|
||||
Input: `{
|
||||
"log": {
|
||||
// abcd
|
||||
"loglevel": "info",
|
||||
}
|
||||
}`,
|
||||
Output: "line 5 char 5",
|
||||
},
|
||||
{
|
||||
Input: `{
|
||||
"port": 1,
|
||||
"inbounds": [{
|
||||
"protocol": "test"
|
||||
}]
|
||||
}`,
|
||||
Output: "parse json config",
|
||||
},
|
||||
{
|
||||
Input: `{
|
||||
"inbounds": [{
|
||||
"port": 1,
|
||||
"listen": 0,
|
||||
"protocol": "test"
|
||||
}]
|
||||
}`,
|
||||
Output: "line 1 char 1",
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
reader := bytes.NewReader([]byte(testCase.Input))
|
||||
_, err := serial.LoadJSONConfig(reader)
|
||||
errString := err.Error()
|
||||
if !strings.Contains(errString, testCase.Output) {
|
||||
t.Error("unexpected output from json: ", testCase.Input, ". expected ", testCase.Output, ", but actually ", errString)
|
||||
}
|
||||
}
|
||||
}
|
3
infra/conf/serial/serial.go
Normal file
3
infra/conf/serial/serial.go
Normal file
|
@ -0,0 +1,3 @@
|
|||
package serial
|
||||
|
||||
//go:generate go run github.com/xtls/xray-core/v1/common/errors/errorgen
|
Loading…
Add table
Add a link
Reference in a new issue