mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-12 22:42:18 +00:00
remove use of any
This commit is contained in:
72 files changed
+728
-792
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"strings"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
v2net.Address
|
||||
}
|
||||
|
||||
func (this *Address) UnmarshalJSON(data []byte) error {
|
||||
var rawStr string
|
||||
if err := json.Unmarshal(data, &rawStr); err != nil {
|
||||
return err
|
||||
}
|
||||
this.Address = v2net.ParseAddress(rawStr)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Address) Build() *v2net.IPOrDomain {
|
||||
if this.Family().IsDomain() {
|
||||
return &v2net.IPOrDomain{
|
||||
Address: &v2net.IPOrDomain_Domain{
|
||||
Domain: this.Domain(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return &v2net.IPOrDomain{
|
||||
Address: &v2net.IPOrDomain_Ip{
|
||||
Ip: []byte(this.IP()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Network string
|
||||
|
||||
func (this Network) Build() v2net.Network {
|
||||
return v2net.ParseNetwork(string(this))
|
||||
}
|
||||
|
||||
type NetworkList []Network
|
||||
|
||||
func (this *NetworkList) UnmarshalJSON(data []byte) error {
|
||||
var strarray []Network
|
||||
if err := json.Unmarshal(data, &strarray); err == nil {
|
||||
nl := NetworkList(strarray)
|
||||
*this = nl
|
||||
return nil
|
||||
}
|
||||
|
||||
var rawstr Network
|
||||
if err := json.Unmarshal(data, &rawstr); err == nil {
|
||||
strlist := strings.Split(string(rawstr), ",")
|
||||
nl := make([]Network, len(strlist))
|
||||
for idx, network := range strlist {
|
||||
nl[idx] = Network(network)
|
||||
}
|
||||
*this = nl
|
||||
return nil
|
||||
}
|
||||
return errors.New("Unknown format of a string list: " + string(data))
|
||||
}
|
||||
|
||||
func (this *NetworkList) Build() *v2net.NetworkList {
|
||||
list := new(v2net.NetworkList)
|
||||
for _, network := range *this {
|
||||
list.Network = append(list.Network, network.Build())
|
||||
}
|
||||
return list
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package conf_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/testing/assert"
|
||||
. "v2ray.com/core/tools/conf"
|
||||
)
|
||||
|
||||
func TestIPParsing(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rawJson := "\"8.8.8.8\""
|
||||
var address Address
|
||||
err := json.Unmarshal([]byte(rawJson), &address)
|
||||
assert.Error(err).IsNil()
|
||||
assert.Bytes([]byte(address.IP())).Equals([]byte{8, 8, 8, 8})
|
||||
}
|
||||
|
||||
func TestDomainParsing(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rawJson := "\"v2ray.com\""
|
||||
var address Address
|
||||
err := json.Unmarshal([]byte(rawJson), &address)
|
||||
assert.Error(err).IsNil()
|
||||
assert.String(address.Domain()).Equals("v2ray.com")
|
||||
}
|
||||
|
||||
func TestInvalidAddressJson(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rawJson := "1234"
|
||||
var address Address
|
||||
err := json.Unmarshal([]byte(rawJson), &address)
|
||||
assert.Error(err).IsNotNil()
|
||||
}
|
||||
|
||||
func TestStringNetwork(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
var network Network
|
||||
err := json.Unmarshal([]byte(`"tcp"`), &network)
|
||||
assert.Error(err).IsNil()
|
||||
assert.Bool(network.Build() == v2net.Network_TCP).IsTrue()
|
||||
}
|
||||
|
||||
func TestArrayNetworkList(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
var list NetworkList
|
||||
err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
nlist := list.Build()
|
||||
assert.Bool(nlist.HasNetwork(v2net.ParseNetwork("tcp"))).IsTrue()
|
||||
assert.Bool(nlist.HasNetwork(v2net.ParseNetwork("udp"))).IsFalse()
|
||||
}
|
||||
|
||||
func TestStringNetworkList(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
var list NetworkList
|
||||
err := json.Unmarshal([]byte("\"TCP, ip\""), &list)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
nlist := list.Build()
|
||||
assert.Bool(nlist.HasNetwork(v2net.ParseNetwork("tcp"))).IsTrue()
|
||||
assert.Bool(nlist.HasNetwork(v2net.ParseNetwork("udp"))).IsFalse()
|
||||
}
|
||||
|
||||
func TestInvalidNetworkJson(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
var list NetworkList
|
||||
err := json.Unmarshal([]byte("0"), &list)
|
||||
assert.Error(err).IsNotNil()
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"v2ray.com/core/common/log"
|
||||
)
|
||||
|
||||
type LogConfig struct {
|
||||
AccessLog string `json:"access"`
|
||||
ErrorLog string `json:"error"`
|
||||
LogLevel string `json:"loglevel"`
|
||||
}
|
||||
|
||||
func (this *LogConfig) Build() *log.Config {
|
||||
if this == nil {
|
||||
return nil
|
||||
}
|
||||
config := new(log.Config)
|
||||
if len(this.AccessLog) > 0 {
|
||||
config.AccessLogPath = this.AccessLog
|
||||
config.AccessLogType = log.LogType_File
|
||||
}
|
||||
if len(this.ErrorLog) > 0 {
|
||||
config.ErrorLogPath = this.ErrorLog
|
||||
config.ErrorLogType = log.LogType_File
|
||||
}
|
||||
|
||||
level := strings.ToLower(this.LogLevel)
|
||||
switch level {
|
||||
case "debug":
|
||||
config.ErrorLogLevel = log.LogLevel_Debug
|
||||
case "info":
|
||||
config.ErrorLogLevel = log.LogLevel_Info
|
||||
case "error":
|
||||
config.ErrorLogLevel = log.LogLevel_Error
|
||||
case "none":
|
||||
config.ErrorLogType = log.LogType_None
|
||||
default:
|
||||
config.ErrorLogLevel = log.LogLevel_Warning
|
||||
}
|
||||
return config
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package conf
|
||||
|
||||
/*
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"v2ray.com/core/app/dns"
|
||||
"v2ray.com/core/app/router"
|
||||
"v2ray.com/core/common"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/transport"
|
||||
"v2ray.com/core/transport/internet"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port uint16 `json:"port"` // Port of this Point server.
|
||||
LogConfig *LogConfig `json:"log"`
|
||||
RouterConfig *router.Config `json:"routing"`
|
||||
DNSConfig *dns.Config `json:"dns"`
|
||||
InboundConfig *InboundConnectionConfig `json:"inbound"`
|
||||
OutboundConfig *OutboundConnectionConfig `json:"outbound"`
|
||||
InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
|
||||
OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
|
||||
Transport *transport.Config `json:"transport"`
|
||||
}
|
||||
*/
|
||||
Reference in new issue
Block a user