mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 07:26:24 -05:00
refine error messages
This commit is contained in:
parent
8afdbbfc9f
commit
d3d2fab1d9
@ -2,6 +2,7 @@ package log
|
||||
|
||||
import (
|
||||
"v2ray.com/core/app/log/internal"
|
||||
"v2ray.com/core/common/errors"
|
||||
)
|
||||
|
||||
// AccessStatus is the status of an access request from clients.
|
||||
@ -20,8 +21,7 @@ var (
|
||||
func InitAccessLogger(file string) error {
|
||||
logger, err := internal.NewFileLogWriter(file)
|
||||
if err != nil {
|
||||
Error("Failed to create access logger on file (", file, "): ", file, err)
|
||||
return err
|
||||
return errors.Base(err).Message("Failed to create access logger on file: ", file)
|
||||
}
|
||||
accessLoggerInstance = logger
|
||||
return nil
|
||||
|
@ -2,17 +2,6 @@
|
||||
// See each sub-package for detail.
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrObjectReleased = errors.New("Object already released.")
|
||||
ErrBadConfiguration = errors.New("Bad configuration.")
|
||||
ErrObjectNotFound = errors.New("Object not found.")
|
||||
ErrDuplicatedName = errors.New("Duplicated name.")
|
||||
)
|
||||
|
||||
// Must panics if err is not nil.
|
||||
func Must(err error) {
|
||||
if err != nil {
|
||||
|
@ -85,7 +85,7 @@ func IPAddress(ip []byte) Address {
|
||||
}
|
||||
return addr
|
||||
default:
|
||||
log.Error("Invalid IP format: ", ip)
|
||||
log.Error("Net: Invalid IP format: ", ip)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@ -7,11 +7,6 @@ import (
|
||||
"v2ray.com/core/common/serial"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidPortRage indicates an error during port range parsing.
|
||||
ErrInvalidPortRange = errors.New("Invalid port range.")
|
||||
)
|
||||
|
||||
// Port represents a network port in TCP and UDP protocol.
|
||||
type Port uint16
|
||||
|
||||
@ -25,7 +20,7 @@ func PortFromBytes(port []byte) Port {
|
||||
// @error when the integer is not positive or larger then 65535
|
||||
func PortFromInt(val uint32) (Port, error) {
|
||||
if val > 65535 {
|
||||
return Port(0), ErrInvalidPortRange
|
||||
return Port(0), errors.New("Net: Invalid port range: ", val)
|
||||
}
|
||||
return Port(val), nil
|
||||
}
|
||||
@ -35,7 +30,7 @@ func PortFromInt(val uint32) (Port, error) {
|
||||
func PortFromString(s string) (Port, error) {
|
||||
val, err := strconv.ParseUint(s, 10, 32)
|
||||
if err != nil {
|
||||
return Port(0), ErrInvalidPortRange
|
||||
return Port(0), errors.New("Net: Invalid port range: ", s)
|
||||
}
|
||||
return PortFromInt(uint32(val))
|
||||
}
|
||||
|
@ -4,9 +4,9 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
|
||||
"v2ray.com/core/common/errors"
|
||||
)
|
||||
|
||||
type ConfigLoader func(input io.Reader) (*Config, error)
|
||||
@ -21,7 +21,7 @@ func RegisterConfigLoader(format ConfigFormat, loader ConfigLoader) error {
|
||||
func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
|
||||
loader, found := configLoaderCache[format]
|
||||
if !found {
|
||||
return nil, common.ErrBadConfiguration
|
||||
return nil, errors.New("Core: ", ConfigFormat_name[int32(format)], " is not loadable.")
|
||||
}
|
||||
return loader(input)
|
||||
}
|
||||
|
54
main/main.go
54
main/main.go
@ -11,7 +11,7 @@ import (
|
||||
"syscall"
|
||||
|
||||
"v2ray.com/core"
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/common/errors"
|
||||
|
||||
_ "v2ray.com/core/main/distro/all"
|
||||
)
|
||||
@ -43,10 +43,9 @@ func GetConfigFormat() core.ConfigFormat {
|
||||
}
|
||||
}
|
||||
|
||||
func startV2Ray() *core.Point {
|
||||
func startV2Ray() (*core.Point, error) {
|
||||
if len(configFile) == 0 {
|
||||
log.Error("Config file is not set.")
|
||||
return nil
|
||||
return nil, errors.New("V2Ray: Config file is not set.")
|
||||
}
|
||||
var configInput io.Reader
|
||||
if configFile == "stdin:" {
|
||||
@ -55,36 +54,22 @@ func startV2Ray() *core.Point {
|
||||
fixedFile := os.ExpandEnv(configFile)
|
||||
file, err := os.Open(fixedFile)
|
||||
if err != nil {
|
||||
log.Error("Config file not readable: ", err)
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("V2Ray: Config file not readable.")
|
||||
}
|
||||
defer file.Close()
|
||||
configInput = file
|
||||
}
|
||||
config, err := core.LoadConfig(GetConfigFormat(), configInput)
|
||||
if err != nil {
|
||||
log.Error("Failed to read config file (", configFile, "): ", configFile, err)
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("V2Ray: Failed to read config file: ", configFile)
|
||||
}
|
||||
|
||||
vPoint, err := core.NewPoint(config)
|
||||
if err != nil {
|
||||
log.Error("Failed to create Point server: ", err)
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("V2Ray: Failed to create initialize.")
|
||||
}
|
||||
|
||||
if *test {
|
||||
fmt.Println("Configuration OK.")
|
||||
return nil
|
||||
}
|
||||
|
||||
err = vPoint.Start()
|
||||
if err != nil {
|
||||
log.Error("Error starting Point server: ", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return vPoint
|
||||
return vPoint, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -96,11 +81,24 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if point := startV2Ray(); point != nil {
|
||||
osSignals := make(chan os.Signal, 1)
|
||||
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
|
||||
<-osSignals
|
||||
point.Close()
|
||||
point, err := startV2Ray()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if *test {
|
||||
fmt.Println("V2Ray: Configuration OK.")
|
||||
return
|
||||
}
|
||||
|
||||
if err := point.Start(); err != nil {
|
||||
fmt.Println("V2Ray: Failed to start. ", err)
|
||||
}
|
||||
|
||||
osSignals := make(chan os.Signal, 1)
|
||||
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
|
||||
<-osSignals
|
||||
point.Close()
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"v2ray.com/core/common/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidProtocolVersion = errors.New("Invalid protocol version.")
|
||||
ErrAlreadyListening = errors.New("Already listening on another port.")
|
||||
)
|
@ -29,7 +29,7 @@ func (v *BlackholeConfig) Build() (*serial.TypedMessage, error) {
|
||||
if v.Response != nil {
|
||||
response, _, err := configLoader.Load(v.Response)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Blackhole: Failed to parse response config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to parse Blackhole response config.")
|
||||
}
|
||||
responseSettings, err := response.(Buildable).Build()
|
||||
if err != nil {
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/app/log"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
)
|
||||
@ -34,7 +33,7 @@ func (v *StringList) UnmarshalJSON(data []byte) error {
|
||||
*v = *NewStringList(strlist)
|
||||
return nil
|
||||
}
|
||||
return errors.New("Unknown format of a string list: " + string(data))
|
||||
return errors.New("Config: Unknown format of a string list: " + string(data))
|
||||
}
|
||||
|
||||
type Address struct {
|
||||
@ -81,7 +80,7 @@ func (v *NetworkList) UnmarshalJSON(data []byte) error {
|
||||
*v = nl
|
||||
return nil
|
||||
}
|
||||
return errors.New("Unknown format of a string list: " + string(data))
|
||||
return errors.New("Config: Unknown format of a string list: " + string(data))
|
||||
}
|
||||
|
||||
func (v *NetworkList) Build() *v2net.NetworkList {
|
||||
@ -115,7 +114,7 @@ func parseStringPort(data []byte) (v2net.Port, v2net.Port, error) {
|
||||
}
|
||||
pair := strings.SplitN(s, "-", 2)
|
||||
if len(pair) == 0 {
|
||||
return v2net.Port(0), v2net.Port(0), v2net.ErrInvalidPortRange
|
||||
return v2net.Port(0), v2net.Port(0), errors.New("Config: Invalid port range: ", s)
|
||||
}
|
||||
if len(pair) == 1 {
|
||||
port, err := v2net.PortFromString(pair[0])
|
||||
@ -159,14 +158,12 @@ func (v *PortRange) UnmarshalJSON(data []byte) error {
|
||||
v.From = uint32(from)
|
||||
v.To = uint32(to)
|
||||
if v.From > v.To {
|
||||
log.Error("Invalid port range ", v.From, " -> ", v.To)
|
||||
return v2net.ErrInvalidPortRange
|
||||
return errors.New("Config: Invalid port range ", v.From, " -> ", v.To)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Error("Invalid port range: ", string(data))
|
||||
return v2net.ErrInvalidPortRange
|
||||
return errors.New("Config: Invalid port range: ", string(data))
|
||||
}
|
||||
|
||||
type User struct {
|
||||
|
@ -114,10 +114,10 @@ func TestOverRangeIntPort(t *testing.T) {
|
||||
|
||||
var portRange PortRange
|
||||
err := json.Unmarshal([]byte("70000"), &portRange)
|
||||
assert.Error(err).Equals(v2net.ErrInvalidPortRange)
|
||||
assert.Error(err).IsNotNil()
|
||||
|
||||
err = json.Unmarshal([]byte("-1"), &portRange)
|
||||
assert.Error(err).Equals(v2net.ErrInvalidPortRange)
|
||||
assert.Error(err).IsNotNil()
|
||||
}
|
||||
|
||||
func TestSingleStringPort(t *testing.T) {
|
||||
@ -147,16 +147,16 @@ func TestOverRangeStringPort(t *testing.T) {
|
||||
|
||||
var portRange PortRange
|
||||
err := json.Unmarshal([]byte("\"65536\""), &portRange)
|
||||
assert.Error(err).Equals(v2net.ErrInvalidPortRange)
|
||||
assert.Error(err).IsNotNil()
|
||||
|
||||
err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
|
||||
assert.Error(err).Equals(v2net.ErrInvalidPortRange)
|
||||
assert.Error(err).IsNotNil()
|
||||
|
||||
err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
|
||||
assert.Error(err).Equals(v2net.ErrInvalidPortRange)
|
||||
assert.Error(err).IsNotNil()
|
||||
|
||||
err = json.Unmarshal([]byte("\"700-600\""), &portRange)
|
||||
assert.Error(err).Equals(v2net.ErrInvalidPortRange)
|
||||
assert.Error(err).IsNotNil()
|
||||
}
|
||||
|
||||
func TestUserParsing(t *testing.T) {
|
||||
|
@ -2,13 +2,8 @@ package conf
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/app/log"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnknownConfigID = errors.New("Unknown config ID.")
|
||||
"v2ray.com/core/common/errors"
|
||||
)
|
||||
|
||||
type ConfigCreator func() interface{}
|
||||
@ -17,7 +12,7 @@ type ConfigCreatorCache map[string]ConfigCreator
|
||||
|
||||
func (v ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
|
||||
if _, found := v[id]; found {
|
||||
return common.ErrDuplicatedName
|
||||
return errors.New("Config: ", id, " already registered.")
|
||||
}
|
||||
|
||||
v[id] = creator
|
||||
@ -27,7 +22,7 @@ func (v ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) er
|
||||
func (v ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
|
||||
creator, found := v[id]
|
||||
if !found {
|
||||
return nil, ErrUnknownConfigID
|
||||
return nil, errors.New("Config: Unknown config id: ", id)
|
||||
}
|
||||
return creator(), nil
|
||||
}
|
||||
@ -49,7 +44,7 @@ func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey strin
|
||||
func (v *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
|
||||
creator, found := v.cache[id]
|
||||
if !found {
|
||||
return nil, ErrUnknownConfigID
|
||||
return nil, errors.New("Config: Unknown config id: ", id)
|
||||
}
|
||||
|
||||
config := creator()
|
||||
@ -66,8 +61,7 @@ func (v *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
|
||||
}
|
||||
rawID, found := obj[v.idKey]
|
||||
if !found {
|
||||
log.Error(v.idKey, " not found in JSON content.")
|
||||
return nil, "", common.ErrObjectNotFound
|
||||
return nil, "", errors.New("Config: ", v.idKey, " not found in JSON context.")
|
||||
}
|
||||
var id string
|
||||
if err := json.Unmarshal(rawID, &id); err != nil {
|
||||
@ -77,8 +71,7 @@ func (v *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
|
||||
if len(v.configKey) > 0 {
|
||||
configValue, found := obj[v.configKey]
|
||||
if !found {
|
||||
log.Error(v.configKey, " not found in JSON content.")
|
||||
return nil, "", common.ErrObjectNotFound
|
||||
return nil, "", errors.New("Config: ", v.configKey, " not found in JSON content.")
|
||||
}
|
||||
rawConfig = configValue
|
||||
}
|
||||
|
@ -5,9 +5,9 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/app/router"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/app/log"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/tools/geoip"
|
||||
|
||||
@ -25,7 +25,7 @@ type RouterConfig struct {
|
||||
|
||||
func (v *RouterConfig) Build() (*router.Config, error) {
|
||||
if v.Settings == nil {
|
||||
return nil, errors.New("Router settings is not specified.")
|
||||
return nil, errors.New("Config: Router settings is not specified.")
|
||||
}
|
||||
config := new(router.Config)
|
||||
|
||||
@ -39,7 +39,10 @@ func (v *RouterConfig) Build() (*router.Config, error) {
|
||||
config.DomainStrategy = router.Config_IpIfNonMatch
|
||||
}
|
||||
for idx, rawRule := range settings.RuleList {
|
||||
rule := ParseRule(rawRule)
|
||||
rule, err := ParseRule(rawRule)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Rule[idx] = rule
|
||||
}
|
||||
return config, nil
|
||||
@ -50,7 +53,7 @@ type RouterRule struct {
|
||||
OutboundTag string `json:"outboundTag"`
|
||||
}
|
||||
|
||||
func parseIP(s string) *router.CIDR {
|
||||
func parseIP(s string) (*router.CIDR, error) {
|
||||
var addr, mask string
|
||||
i := strings.Index(s, "/")
|
||||
if i < 0 {
|
||||
@ -66,38 +69,35 @@ func parseIP(s string) *router.CIDR {
|
||||
if len(mask) > 0 {
|
||||
bits64, err := strconv.ParseUint(mask, 10, 32)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("Config: invalid network mask for router: ", mask)
|
||||
}
|
||||
bits = uint32(bits64)
|
||||
}
|
||||
if bits > 32 {
|
||||
log.Warning("Router: invalid network mask: ", bits)
|
||||
return nil
|
||||
return nil, errors.New("Config: invalid network mask for router: ", bits)
|
||||
}
|
||||
return &router.CIDR{
|
||||
Ip: []byte(ip.IP()),
|
||||
Prefix: bits,
|
||||
}
|
||||
}, nil
|
||||
case v2net.AddressFamilyIPv6:
|
||||
bits := uint32(128)
|
||||
if len(mask) > 0 {
|
||||
bits64, err := strconv.ParseUint(mask, 10, 32)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("Config: invalid network mask for router: ", mask)
|
||||
}
|
||||
bits = uint32(bits64)
|
||||
}
|
||||
if bits > 128 {
|
||||
log.Warning("Router: invalid network mask: ", bits)
|
||||
return nil
|
||||
return nil, errors.New("Config: invalid network mask for router: ", bits)
|
||||
}
|
||||
return &router.CIDR{
|
||||
Ip: []byte(ip.IP()),
|
||||
Prefix: bits,
|
||||
}
|
||||
}, nil
|
||||
default:
|
||||
log.Warning("Router: unsupported address: ", s)
|
||||
return nil
|
||||
return nil, errors.New("Config: unsupported address for router: ", s)
|
||||
}
|
||||
}
|
||||
|
||||
@ -137,10 +137,11 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
|
||||
if rawFieldRule.IP != nil {
|
||||
for _, ip := range *rawFieldRule.IP {
|
||||
ipRule := parseIP(ip)
|
||||
if ipRule != nil {
|
||||
rule.Cidr = append(rule.Cidr, ipRule)
|
||||
ipRule, err := parseIP(ip)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Config: invalid IP: ", ip)
|
||||
}
|
||||
rule.Cidr = append(rule.Cidr, ipRule)
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,10 +155,11 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
|
||||
if rawFieldRule.SourceIP != nil {
|
||||
for _, ip := range *rawFieldRule.SourceIP {
|
||||
ipRule := parseIP(ip)
|
||||
if ipRule != nil {
|
||||
rule.SourceCidr = append(rule.SourceCidr, ipRule)
|
||||
ipRule, err := parseIP(ip)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Config: invalid IP: ", ip)
|
||||
}
|
||||
rule.SourceCidr = append(rule.SourceCidr, ipRule)
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,52 +178,45 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
return rule, nil
|
||||
}
|
||||
|
||||
func ParseRule(msg json.RawMessage) *router.RoutingRule {
|
||||
func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
rawRule := new(RouterRule)
|
||||
err := json.Unmarshal(msg, rawRule)
|
||||
if err != nil {
|
||||
log.Error("Router: Invalid router rule: ", err)
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("Config: Invalid router rule.")
|
||||
}
|
||||
if rawRule.Type == "field" {
|
||||
|
||||
fieldrule, err := parseFieldRule(msg)
|
||||
if err != nil {
|
||||
log.Error("Invalid field rule: ", err)
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("Config: Invalid field rule.")
|
||||
}
|
||||
return fieldrule
|
||||
return fieldrule, nil
|
||||
}
|
||||
if rawRule.Type == "chinaip" {
|
||||
chinaiprule, err := parseChinaIPRule(msg)
|
||||
if err != nil {
|
||||
log.Error("Router: Invalid chinaip rule: ", err)
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("Config: Invalid chinaip rule.")
|
||||
}
|
||||
return chinaiprule
|
||||
return chinaiprule, nil
|
||||
}
|
||||
if rawRule.Type == "chinasites" {
|
||||
chinasitesrule, err := parseChinaSitesRule(msg)
|
||||
if err != nil {
|
||||
log.Error("Invalid chinasites rule: ", err)
|
||||
return nil
|
||||
return nil, errors.Base(err).Message("Config: Invalid chinasites rule.")
|
||||
}
|
||||
return chinasitesrule
|
||||
return chinasitesrule, nil
|
||||
}
|
||||
log.Error("Unknown router rule type: ", rawRule.Type)
|
||||
return nil
|
||||
return nil, errors.New("Config: Unknown router rule type: ", rawRule.Type)
|
||||
}
|
||||
|
||||
func parseChinaIPRule(data []byte) (*router.RoutingRule, error) {
|
||||
rawRule := new(RouterRule)
|
||||
err := json.Unmarshal(data, rawRule)
|
||||
if err != nil {
|
||||
log.Error("Router: Invalid router rule: ", err)
|
||||
return nil, err
|
||||
return nil, errors.Base(err).Message("Config: Invalid router rule.")
|
||||
}
|
||||
var chinaIPs geoip.CountryIPRange
|
||||
if err := proto.Unmarshal(geoip.ChinaIPs, &chinaIPs); err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Base(err).Message("Config: Invalid china ips.")
|
||||
}
|
||||
return &router.RoutingRule{
|
||||
Tag: rawRule.OutboundTag,
|
||||
|
@ -1,11 +1,10 @@
|
||||
package conf_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"context"
|
||||
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
"v2ray.com/core/testing/assert"
|
||||
@ -23,10 +22,11 @@ func makeDomainDestination(domain string) v2net.Destination {
|
||||
func TestChinaIPJson(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rule := ParseRule([]byte(`{
|
||||
rule, err := ParseRule([]byte(`{
|
||||
"type": "chinaip",
|
||||
"outboundTag": "x"
|
||||
}`))
|
||||
assert.Error(err).IsNil()
|
||||
assert.String(rule.Tag).Equals("x")
|
||||
cond, err := rule.BuildCondition()
|
||||
assert.Error(err).IsNil()
|
||||
@ -41,10 +41,11 @@ func TestChinaIPJson(t *testing.T) {
|
||||
func TestChinaSitesJson(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rule := ParseRule([]byte(`{
|
||||
rule, err := ParseRule([]byte(`{
|
||||
"type": "chinasites",
|
||||
"outboundTag": "y"
|
||||
}`))
|
||||
assert.Error(err).IsNil()
|
||||
assert.String(rule.Tag).Equals("y")
|
||||
cond, err := rule.BuildCondition()
|
||||
assert.Error(err).IsNil()
|
||||
@ -59,7 +60,7 @@ func TestChinaSitesJson(t *testing.T) {
|
||||
func TestDomainRule(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rule := ParseRule([]byte(`{
|
||||
rule, err := ParseRule([]byte(`{
|
||||
"type": "field",
|
||||
"domain": [
|
||||
"ooxx.com",
|
||||
@ -69,6 +70,7 @@ func TestDomainRule(t *testing.T) {
|
||||
"network": "tcp",
|
||||
"outboundTag": "direct"
|
||||
}`))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Pointer(rule).IsNotNil()
|
||||
cond, err := rule.BuildCondition()
|
||||
assert.Error(err).IsNil()
|
||||
@ -82,7 +84,7 @@ func TestDomainRule(t *testing.T) {
|
||||
func TestIPRule(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rule := ParseRule([]byte(`{
|
||||
rule, err := ParseRule([]byte(`{
|
||||
"type": "field",
|
||||
"ip": [
|
||||
"10.0.0.0/8",
|
||||
@ -91,6 +93,7 @@ func TestIPRule(t *testing.T) {
|
||||
"network": "tcp",
|
||||
"outboundTag": "direct"
|
||||
}`))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Pointer(rule).IsNotNil()
|
||||
cond, err := rule.BuildCondition()
|
||||
assert.Error(err).IsNil()
|
||||
@ -103,7 +106,7 @@ func TestIPRule(t *testing.T) {
|
||||
func TestSourceIPRule(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rule := ParseRule([]byte(`{
|
||||
rule, err := ParseRule([]byte(`{
|
||||
"type": "field",
|
||||
"source": [
|
||||
"10.0.0.0/8",
|
||||
@ -111,6 +114,7 @@ func TestSourceIPRule(t *testing.T) {
|
||||
],
|
||||
"outboundTag": "direct"
|
||||
}`))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Pointer(rule).IsNotNil()
|
||||
cond, err := rule.BuildCondition()
|
||||
assert.Error(err).IsNil()
|
||||
|
@ -18,7 +18,7 @@ func (v *TransportConfig) Build() (*transport.Config, error) {
|
||||
if v.TCPConfig != nil {
|
||||
ts, err := v.TCPConfig.Build()
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Failed to build TCP config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to build TCP config.")
|
||||
}
|
||||
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
||||
Protocol: internet.TransportProtocol_TCP,
|
||||
@ -29,7 +29,7 @@ func (v *TransportConfig) Build() (*transport.Config, error) {
|
||||
if v.KCPConfig != nil {
|
||||
ts, err := v.KCPConfig.Build()
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Failed to build mKCP config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to build mKCP config.")
|
||||
}
|
||||
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
||||
Protocol: internet.TransportProtocol_MKCP,
|
||||
@ -40,7 +40,7 @@ func (v *TransportConfig) Build() (*transport.Config, error) {
|
||||
if v.WSConfig != nil {
|
||||
ts, err := v.WSConfig.Build()
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Failed to build WebSocket config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to build WebSocket config.")
|
||||
}
|
||||
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
||||
Protocol: internet.TransportProtocol_WebSocket,
|
||||
|
@ -93,7 +93,7 @@ func (v *HTTPAuthenticatorRequest) Build() (*http.RequestConfig, error) {
|
||||
config.Header = make([]*http.Header, 0, len(v.Headers))
|
||||
for key, value := range v.Headers {
|
||||
if value == nil {
|
||||
return nil, errors.New("Empty HTTP header value: " + key)
|
||||
return nil, errors.New("Config: Empty HTTP header value: " + key)
|
||||
}
|
||||
config.Header = append(config.Header, &http.Header{
|
||||
Name: key,
|
||||
@ -159,7 +159,7 @@ func (v *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
|
||||
config.Header = make([]*http.Header, 0, len(v.Headers))
|
||||
for key, value := range v.Headers {
|
||||
if value == nil {
|
||||
return nil, errors.New("Empty HTTP header value: " + key)
|
||||
return nil, errors.New("Config: Empty HTTP header value: " + key)
|
||||
}
|
||||
config.Header = append(config.Header, &http.Header{
|
||||
Name: key,
|
||||
|
@ -45,14 +45,14 @@ func (v *KCPConfig) Build() (*serial.TypedMessage, error) {
|
||||
if v.Mtu != nil {
|
||||
mtu := *v.Mtu
|
||||
if mtu < 576 || mtu > 1460 {
|
||||
return nil, errors.Format("KCP|Config: Invalid MTU size: %d", mtu)
|
||||
return nil, errors.Format("Config: Invalid mKCP MTU size: %d", mtu)
|
||||
}
|
||||
config.Mtu = &kcp.MTU{Value: mtu}
|
||||
}
|
||||
if v.Tti != nil {
|
||||
tti := *v.Tti
|
||||
if tti < 10 || tti > 100 {
|
||||
return nil, errors.Format("KCP|Config: Invalid TTI: %d", tti)
|
||||
return nil, errors.Format("Config: Invalid mKCP TTI: %d", tti)
|
||||
}
|
||||
config.Tti = &kcp.TTI{Value: tti}
|
||||
}
|
||||
@ -84,11 +84,11 @@ func (v *KCPConfig) Build() (*serial.TypedMessage, error) {
|
||||
if len(v.HeaderConfig) > 0 {
|
||||
headerConfig, _, err := kcpHeaderLoader.Load(v.HeaderConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid mKCP header config.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid mKCP header config.")
|
||||
}
|
||||
ts, err := headerConfig.(Buildable).Build()
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid mKCP header config.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid mKCP header config.")
|
||||
}
|
||||
config.HeaderConfig = ts
|
||||
}
|
||||
@ -111,11 +111,11 @@ func (v *TCPConfig) Build() (*serial.TypedMessage, error) {
|
||||
if len(v.HeaderConfig) > 0 {
|
||||
headerConfig, _, err := tcpHeaderLoader.Load(v.HeaderConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid TCP header config.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid TCP header config.")
|
||||
}
|
||||
ts, err := headerConfig.(Buildable).Build()
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid TCP header config.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid TCP header config.")
|
||||
}
|
||||
config.HeaderSettings = ts
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ func (v *InboundConnectionConfig) Build() (*proxyman.InboundHandlerConfig, error
|
||||
}
|
||||
if v.Listen != nil {
|
||||
if v.Listen.Family().IsDomain() {
|
||||
return nil, errors.New("Point: Unable to listen on domain address: " + v.Listen.Domain())
|
||||
return nil, errors.New("Config: Unable to listen on domain address: " + v.Listen.Domain())
|
||||
}
|
||||
receiverConfig.Listen = v.Listen.Build()
|
||||
}
|
||||
@ -65,7 +65,7 @@ func (v *InboundConnectionConfig) Build() (*proxyman.InboundHandlerConfig, error
|
||||
|
||||
jsonConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Failed to load inbound config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to load inbound config.")
|
||||
}
|
||||
if dokodemoConfig, ok := jsonConfig.(*DokodemoConfig); ok {
|
||||
receiverConfig.ReceiveOriginalDestination = dokodemoConfig.Redirect
|
||||
@ -97,7 +97,7 @@ func (v *OutboundConnectionConfig) Build() (*proxyman.OutboundHandlerConfig, err
|
||||
if v.SendThrough != nil {
|
||||
address := v.SendThrough
|
||||
if address.Family().IsDomain() {
|
||||
return nil, errors.New("Invalid sendThrough address: " + address.String())
|
||||
return nil, errors.New("Config: Invalid sendThrough address: " + address.String())
|
||||
}
|
||||
senderSettings.Via = address.Build()
|
||||
}
|
||||
@ -111,14 +111,14 @@ func (v *OutboundConnectionConfig) Build() (*proxyman.OutboundHandlerConfig, err
|
||||
if v.ProxySettings != nil {
|
||||
ps, err := v.ProxySettings.Build()
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid outbound proxy settings.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid outbound proxy settings.")
|
||||
}
|
||||
senderSettings.ProxySettings = ps
|
||||
}
|
||||
|
||||
rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Failed to parse outbound config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to parse outbound config.")
|
||||
}
|
||||
ts, err := rawConfig.(Buildable).Build()
|
||||
if err != nil {
|
||||
@ -148,7 +148,7 @@ func (v *InboundDetourAllocationConfig) Build() (*proxyman.AllocationStrategy, e
|
||||
case "external":
|
||||
config.Type = proxyman.AllocationStrategy_External
|
||||
default:
|
||||
return nil, errors.New("Unknown allocation strategy: ", v.Strategy)
|
||||
return nil, errors.New("Config: Unknown allocation strategy: ", v.Strategy)
|
||||
}
|
||||
if v.Concurrency != nil {
|
||||
config.Concurrency = &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
|
||||
@ -182,13 +182,13 @@ func (v *InboundDetourConfig) Build() (*proxyman.InboundHandlerConfig, error) {
|
||||
}
|
||||
|
||||
if v.PortRange == nil {
|
||||
return nil, errors.New("Port range not specified in InboundDetour.")
|
||||
return nil, errors.New("Config: Port range not specified in InboundDetour.")
|
||||
}
|
||||
receiverSettings.PortRange = v.PortRange.Build()
|
||||
|
||||
if v.ListenOn != nil {
|
||||
if v.ListenOn.Family().IsDomain() {
|
||||
return nil, errors.New("Unable to listen on domain address: ", v.ListenOn.Domain())
|
||||
return nil, errors.New("Config: Unable to listen on domain address: ", v.ListenOn.Domain())
|
||||
}
|
||||
receiverSettings.Listen = v.ListenOn.Build()
|
||||
}
|
||||
@ -209,7 +209,7 @@ func (v *InboundDetourConfig) Build() (*proxyman.InboundHandlerConfig, error) {
|
||||
|
||||
rawConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Failed to load inbound detour config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to load inbound detour config.")
|
||||
}
|
||||
if dokodemoConfig, ok := rawConfig.(*DokodemoConfig); ok {
|
||||
receiverSettings.ReceiveOriginalDestination = dokodemoConfig.Redirect
|
||||
@ -241,7 +241,7 @@ func (v *OutboundDetourConfig) Build() (*proxyman.OutboundHandlerConfig, error)
|
||||
if v.SendThrough != nil {
|
||||
address := v.SendThrough
|
||||
if address.Family().IsDomain() {
|
||||
return nil, errors.New("Point: Unable to send through: " + address.String())
|
||||
return nil, errors.New("Config: Unable to send through: " + address.String())
|
||||
}
|
||||
senderSettings.Via = address.Build()
|
||||
}
|
||||
@ -257,14 +257,14 @@ func (v *OutboundDetourConfig) Build() (*proxyman.OutboundHandlerConfig, error)
|
||||
if v.ProxySettings != nil {
|
||||
ps, err := v.ProxySettings.Build()
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid outbound detour proxy settings.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid outbound detour proxy settings.")
|
||||
}
|
||||
senderSettings.ProxySettings = ps
|
||||
}
|
||||
|
||||
rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Failed to parse to outbound detour config.")
|
||||
return nil, errors.Base(err).Message("Config: Failed to parse to outbound detour config.")
|
||||
}
|
||||
ts, err := rawConfig.(Buildable).Build()
|
||||
if err != nil {
|
||||
@ -318,7 +318,7 @@ func (v *Config) Build() (*core.Config, error) {
|
||||
}
|
||||
|
||||
if v.InboundConfig == nil {
|
||||
return nil, errors.New("No inbound config specified.")
|
||||
return nil, errors.New("Config: No inbound config specified.")
|
||||
}
|
||||
|
||||
if v.InboundConfig.Port == 0 && v.Port > 0 {
|
||||
@ -367,7 +367,7 @@ func init() {
|
||||
})
|
||||
err := decoder.Decode(jsonConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid V2Ray config.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid V2Ray config.")
|
||||
}
|
||||
|
||||
return jsonConfig.Build()
|
||||
|
@ -95,11 +95,11 @@ func (v *VMessInboundConfig) Build() (*serial.TypedMessage, error) {
|
||||
for idx, rawData := range v.Users {
|
||||
user := new(protocol.User)
|
||||
if err := json.Unmarshal(rawData, user); err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid VMess user.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
||||
}
|
||||
account := new(VMessAccount)
|
||||
if err := json.Unmarshal(rawData, account); err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid VMess user.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
||||
}
|
||||
user.Account = serial.ToTypedMessage(account.Build())
|
||||
config.User[idx] = user
|
||||
@ -121,15 +121,15 @@ func (v *VMessOutboundConfig) Build() (*serial.TypedMessage, error) {
|
||||
config := new(outbound.Config)
|
||||
|
||||
if len(v.Receivers) == 0 {
|
||||
return nil, errors.New("0 VMess receiver configured.")
|
||||
return nil, errors.New("Config: 0 VMess receiver configured.")
|
||||
}
|
||||
serverSpecs := make([]*protocol.ServerEndpoint, len(v.Receivers))
|
||||
for idx, rec := range v.Receivers {
|
||||
if len(rec.Users) == 0 {
|
||||
return nil, errors.New("0 user configured for VMess outbound.")
|
||||
return nil, errors.New("Config: 0 user configured for VMess outbound.")
|
||||
}
|
||||
if rec.Address == nil {
|
||||
return nil, errors.New("Address is not set in VMess outbound config.")
|
||||
return nil, errors.New("Config: Address is not set in VMess outbound config.")
|
||||
}
|
||||
if rec.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
|
||||
rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil))
|
||||
@ -141,11 +141,11 @@ func (v *VMessOutboundConfig) Build() (*serial.TypedMessage, error) {
|
||||
for _, rawUser := range rec.Users {
|
||||
user := new(protocol.User)
|
||||
if err := json.Unmarshal(rawUser, user); err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid VMess user.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
||||
}
|
||||
account := new(VMessAccount)
|
||||
if err := json.Unmarshal(rawUser, account); err != nil {
|
||||
return nil, errors.Base(err).Message("Invalid VMess user.")
|
||||
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
||||
}
|
||||
user.Account = serial.ToTypedMessage(account.Build())
|
||||
spec.User = append(spec.User, user)
|
||||
|
Loading…
Reference in New Issue
Block a user