1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-05-10 14:39:08 -04:00

remove use of any

This commit is contained in:
Darien Raymond
2016-10-16 14:22:21 +02:00
parent 5d9e6b0799
commit e33b7df34c
72 changed files with 728 additions and 792 deletions

View File

@@ -3,6 +3,7 @@ package blackhole
import (
"v2ray.com/core/app"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/loader"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/registry"
@@ -16,7 +17,7 @@ type BlackHole struct {
}
func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) (*BlackHole, error) {
response, err := config.Response.GetInternalResponse()
response, err := config.GetInternalResponse()
if err != nil {
return nil, err
}
@@ -50,5 +51,5 @@ func (this *Factory) Create(space app.Space, config interface{}, meta *proxy.Out
}
func init() {
registry.MustRegisterOutboundHandlerCreator("blackhole", new(Factory))
registry.MustRegisterOutboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
}

View File

@@ -4,11 +4,8 @@ import (
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"strings"
"v2ray.com/core/common/loader"
)
const (
@@ -42,30 +39,14 @@ func (this *HTTPResponse) AsAny() *any.Any {
return r
}
func (this *Response) GetInternalResponse() (ResponseConfig, error) {
if this == nil {
func (this *Config) GetInternalResponse() (ResponseConfig, error) {
if this.GetResponse() == nil {
return new(NoneResponse), nil
}
var r ResponseConfig
switch this.Type {
case Response_None:
r = new(NoneResponse)
case Response_HTTP:
r = new(HTTPResponse)
}
err := ptypes.UnmarshalAny(this.Settings, r.(proto.Message))
config, err := this.GetResponse().GetInstance()
if err != nil {
return nil, err
}
return r, nil
}
var (
cache = loader.ConfigCreatorCache{}
)
func init() {
cache.RegisterCreator(strings.ToLower(Response_Type_name[int32(Response_None)]), func() interface{} { return new(NoneResponse) })
cache.RegisterCreator(strings.ToLower(Response_Type_name[int32(Response_HTTP)]), func() interface{} { return new(HTTPResponse) })
return config.(ResponseConfig), nil
}

View File

@@ -6,9 +6,8 @@ import (
"encoding/json"
"errors"
"strings"
"github.com/golang/protobuf/proto"
"v2ray.com/core/common/loader"
"v2ray.com/core/proxy/registry"
)
func (this *Config) UnmarshalJSON(data []byte) error {
@@ -21,27 +20,22 @@ func (this *Config) UnmarshalJSON(data []byte) error {
}
if jsonConfig.Response != nil {
response, rType, err := configLoader.Load(jsonConfig.Response)
response, _, err := configLoader.Load(jsonConfig.Response)
if err != nil {
return errors.New("Blackhole: Failed to parse response config: " + err.Error())
}
this.Response = new(Response)
switch rType {
case strings.ToLower(Response_Type_name[int32(Response_None)]):
this.Response.Type = Response_None
case strings.ToLower(Response_Type_name[int32(Response_HTTP)]):
this.Response.Type = Response_HTTP
}
this.Response.Settings = response.(ResponseConfig).AsAny()
this.Response = loader.NewTypedSettings(response.(proto.Message))
}
return nil
}
var (
configLoader = loader.NewJSONConfigLoader(cache, "type", "")
configLoader = loader.NewJSONConfigLoader(
loader.NamedTypeMap{
"none": loader.GetType(new(NoneResponse)),
"http": loader.GetType(new(HTTPResponse)),
},
"type",
"")
)
func init() {
registry.RegisterOutboundConfig("blackhole", func() interface{} { return new(Config) })
}

View File

@@ -7,7 +7,6 @@ import (
"errors"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy/registry"
)
func (this *Config) UnmarshalJSON(data []byte) error {
@@ -31,7 +30,3 @@ func (this *Config) UnmarshalJSON(data []byte) error {
this.FollowRedirect = rawConfig.Redirect
return nil
}
func init() {
registry.RegisterInboundConfig("dokodemo-door", func() interface{} { return new(Config) })
}

View File

@@ -7,6 +7,7 @@ import (
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
@@ -205,5 +206,5 @@ func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.
}
func init() {
registry.MustRegisterInboundHandlerCreator("dokodemo-door", new(Factory))
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
}

View File

@@ -6,8 +6,6 @@ import (
"encoding/json"
"errors"
"strings"
"v2ray.com/core/proxy/registry"
)
func (this *Config) UnmarshalJSON(data []byte) error {
@@ -27,7 +25,3 @@ func (this *Config) UnmarshalJSON(data []byte) error {
this.Timeout = jsonConfig.Timeout
return nil
}
func init() {
registry.RegisterOutboundConfig("freedom", func() interface{} { return new(Config) })
}

View File

@@ -8,6 +8,7 @@ import (
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/dice"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
@@ -140,5 +141,5 @@ func (this *FreedomFactory) Create(space app.Space, config interface{}, meta *pr
}
func init() {
registry.MustRegisterOutboundHandlerCreator("freedom", new(FreedomFactory))
registry.MustRegisterOutboundHandlerCreator(loader.GetType(new(Config)), new(FreedomFactory))
}

View File

@@ -5,8 +5,6 @@ package http
import (
"encoding/json"
"errors"
"v2ray.com/core/proxy/registry"
)
// UnmarshalJSON implements json.Unmarshaler
@@ -22,7 +20,3 @@ func (this *ServerConfig) UnmarshalJSON(data []byte) error {
return nil
}
func init() {
registry.RegisterInboundConfig("http", func() interface{} { return new(ServerConfig) })
}

View File

@@ -13,6 +13,7 @@ import (
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
@@ -281,5 +282,5 @@ func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *
}
func init() {
registry.MustRegisterInboundHandlerCreator("http", new(ServerFactory))
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(ServerConfig)), new(ServerFactory))
}

View File

@@ -1,50 +0,0 @@
package registry
import (
"v2ray.com/core/common/loader"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
)
var (
inboundConfigCreatorCache = loader.ConfigCreatorCache{}
outboundConfigCreatorCache = loader.ConfigCreatorCache{}
)
func RegisterInboundConfig(protocol string, creator loader.ConfigCreator) error {
return inboundConfigCreatorCache.RegisterCreator(protocol, creator)
}
func RegisterOutboundConfig(protocol string, creator loader.ConfigCreator) error {
return outboundConfigCreatorCache.RegisterCreator(protocol, creator)
}
func MarshalInboundConfig(protocol string, settings *any.Any) (interface{}, error) {
config, err := inboundConfigCreatorCache.CreateConfig(protocol)
if err != nil {
return nil, err
}
if settings == nil {
return config, nil
}
if err := ptypes.UnmarshalAny(settings, config.(proto.Message)); err != nil {
return nil, err
}
return config, nil
}
func MarshalOutboundConfig(protocol string, settings *any.Any) (interface{}, error) {
config, err := outboundConfigCreatorCache.CreateConfig(protocol)
if err != nil {
return nil, err
}
if settings == nil {
return config, nil
}
if err := ptypes.UnmarshalAny(settings, config.(proto.Message)); err != nil {
return nil, err
}
return config, nil
}

View File

@@ -1,25 +0,0 @@
// +build json
package registry
import (
"v2ray.com/core/common/loader"
)
var (
inboundConfigCache loader.ConfigLoader
outboundConfigCache loader.ConfigLoader
)
func CreateInboundConfig(protocol string, data []byte) (interface{}, error) {
return inboundConfigCache.LoadWithID(data, protocol)
}
func CreateOutboundConfig(protocol string, data []byte) (interface{}, error) {
return outboundConfigCache.LoadWithID(data, protocol)
}
func init() {
inboundConfigCache = loader.NewJSONConfigLoader(inboundConfigCreatorCache, "protocol", "settings")
outboundConfigCache = loader.NewJSONConfigLoader(outboundConfigCreatorCache, "protocol", "settings")
}

View File

@@ -8,11 +8,9 @@ import (
"strings"
"v2ray.com/core/common"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/registry"
"github.com/golang/protobuf/ptypes"
)
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
@@ -52,20 +50,11 @@ func (this *ServerConfig) UnmarshalJSON(data []byte) error {
return common.ErrBadConfiguration
}
anyAccount, err := ptypes.MarshalAny(account)
if err != nil {
log.Error("Shadowsocks: Failed to create account: ", err)
return common.ErrBadConfiguration
}
this.User = &protocol.User{
Email: jsonConfig.Email,
Level: uint32(jsonConfig.Level),
Account: anyAccount,
Account: loader.NewTypedSettings(account),
}
return nil
}
func init() {
registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(ServerConfig) })
}

View File

@@ -21,10 +21,12 @@ func TestConfigParsing(t *testing.T) {
err := json.Unmarshal([]byte(rawJson), config)
assert.Error(err).IsNil()
account := new(Account)
_, err = config.User.GetTypedAccount(account)
rawAccount, err = config.User.GetTypedAccount()
assert.Error(err).IsNil()
account, ok := rawAccount.(*Account)
assert.Bool(ok).IsTrue()
cipher, err := account.GetCipher()
assert.Error(err).IsNil()
assert.Int(cipher.KeySize()).Equals(16)

View File

@@ -12,6 +12,7 @@ import (
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/crypto"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
@@ -37,10 +38,14 @@ func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandler
if config.GetUser() == nil {
return nil, protocol.ErrUserMissing
}
account := new(Account)
if _, err := config.GetUser().GetTypedAccount(account); err != nil {
rawAccount, err := config.GetUser().GetTypedAccount()
if err != nil {
return nil, err
}
account, ok := rawAccount.(*Account)
if !ok {
return nil, protocol.ErrUnknownAccountType
}
cipher, err := account.GetCipher()
if err != nil {
return nil, err
@@ -292,5 +297,5 @@ func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *
}
func init() {
registry.MustRegisterInboundHandlerCreator("shadowsocks", new(ServerFactory))
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(ServerConfig)), new(ServerFactory))
}

View File

@@ -8,7 +8,6 @@ import (
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/registry"
"github.com/golang/protobuf/ptypes"
google_protobuf "github.com/golang/protobuf/ptypes/any"
@@ -58,11 +57,11 @@ const (
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
type SocksConfig struct {
AuthMethod string `json:"auth"`
Accounts []*Account `json:"accounts"`
UDP bool `json:"udp"`
AuthMethod string `json:"auth"`
Accounts []*Account `json:"accounts"`
UDP bool `json:"udp"`
Host *v2net.IPOrDomain `json:"ip"`
Timeout uint32 `json:"timeout"`
Timeout uint32 `json:"timeout"`
}
rawConfig := new(SocksConfig)
@@ -95,7 +94,3 @@ func (this *ServerConfig) UnmarshalJSON(data []byte) error {
}
return nil
}
func init() {
registry.RegisterInboundConfig("socks", func() interface{} { return new(ServerConfig) })
}

View File

@@ -6,9 +6,9 @@ import (
"encoding/json"
"errors"
"v2ray.com/core/common/loader"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/registry"
)
func (this *Account) UnmarshalJSON(data []byte) error {
@@ -27,7 +27,7 @@ func (this *Account) UnmarshalJSON(data []byte) error {
func (this *ClientConfig) UnmarshalJSON(data []byte) error {
type ServerConfig struct {
Address *v2net.IPOrDomain `json:"address"`
Address *v2net.IPOrDomain `json:"address"`
Port v2net.Port `json:"port"`
Users []json.RawMessage `json:"users"`
}
@@ -53,18 +53,10 @@ func (this *ClientConfig) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(rawUser, account); err != nil {
return errors.New("Socks|Client: Failed to parse socks account: " + err.Error())
}
anyAccount, err := account.AsAny()
if err != nil {
return err
}
user.Account = anyAccount
user.Account = loader.NewTypedSettings(account)
server.User = append(server.User, user)
}
this.Server[idx] = server
}
return nil
}
func init() {
registry.RegisterOutboundConfig("socks", func() interface{} { return new(ClientConfig) })
}

View File

@@ -9,6 +9,7 @@ import (
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
@@ -326,5 +327,5 @@ func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *
}
func init() {
registry.MustRegisterInboundHandlerCreator("socks", new(ServerFactory))
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(ServerConfig)), new(ServerFactory))
}

View File

@@ -52,7 +52,7 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession {
func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) {
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
account, err := header.User.GetTypedAccount(&vmess.Account{})
account, err := header.User.GetTypedAccount()
if err != nil {
log.Error("VMess: Failed to get user account: ", err)
return

View File

@@ -59,11 +59,12 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
timestampHash := md5.New()
timestampHash.Write(hashTimestamp(timestamp))
iv := timestampHash.Sum(nil)
account, err := user.GetTypedAccount(&vmess.Account{})
account, err := user.GetTypedAccount()
if err != nil {
log.Error("Vmess: Failed to get user account: ", err)
return nil, err
}
aesStream := crypto.NewAesDecryptionStream(account.(*vmess.InternalAccount).ID.CmdKey(), iv)
decryptor := crypto.NewCryptionReader(aesStream, reader)

View File

@@ -22,7 +22,7 @@ func (this *VMessInboundHandler) generateCommand(request *protocol.RequestHeader
if user == nil {
return nil
}
account, _ := user.GetTypedAccount(&vmess.Account{})
account, _ := user.GetTypedAccount()
return &protocol.CommandSwitchAccount{
Port: inboundHandler.Port(),
ID: account.(*vmess.InternalAccount).ID.UUID(),

View File

@@ -6,13 +6,9 @@ import (
"encoding/json"
"errors"
"v2ray.com/core/common"
"v2ray.com/core/common/log"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/registry"
"v2ray.com/core/proxy/vmess"
"github.com/golang/protobuf/ptypes"
)
func (this *DetourConfig) UnmarshalJSON(data []byte) error {
@@ -81,18 +77,9 @@ func (this *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(rawData, account); err != nil {
return errors.New("VMess|Inbound: Invalid user: " + err.Error())
}
anyAccount, err := ptypes.MarshalAny(account)
if err != nil {
log.Error("VMess|Inbound: Failed to create account: ", err)
return common.ErrBadConfiguration
}
user.Account = anyAccount
user.Account = loader.NewTypedSettings(account)
this.User[idx] = user
}
return nil
}
func init() {
registry.RegisterInboundConfig("vmess", func() interface{} { return new(Config) })
}

View File

@@ -10,6 +10,7 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
@@ -20,8 +21,6 @@ import (
"v2ray.com/core/proxy/vmess/encoding"
vmessio "v2ray.com/core/proxy/vmess/io"
"v2ray.com/core/transport/internet"
"github.com/golang/protobuf/ptypes"
)
type userByEmail struct {
@@ -57,11 +56,10 @@ func (this *userByEmail) Get(email string) (*protocol.User, bool) {
Id: uuid.New().String(),
AlterId: uint32(this.defaultAlterIDs),
}
anyAccount, _ := ptypes.MarshalAny(account)
user = &protocol.User{
Level: this.defaultLevel,
Email: email,
Account: anyAccount,
Account: loader.NewTypedSettings(account),
}
this.cache[email] = user
}
@@ -282,5 +280,5 @@ func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.
}
func init() {
registry.MustRegisterInboundHandlerCreator("vmess", new(Factory))
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
}

View File

@@ -3,11 +3,10 @@ package outbound
import (
"time"
"v2ray.com/core/common/loader"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/vmess"
"github.com/golang/protobuf/ptypes"
)
func (this *VMessOutboundHandler) handleSwitchAccount(cmd *protocol.CommandSwitchAccount) {
@@ -15,11 +14,11 @@ func (this *VMessOutboundHandler) handleSwitchAccount(cmd *protocol.CommandSwitc
Id: cmd.ID.String(),
AlterId: uint32(cmd.AlterIds),
}
anyAccount, _ := ptypes.MarshalAny(account)
user := &protocol.User{
Email: "",
Level: cmd.Level,
Account: anyAccount,
Account: loader.NewTypedSettings(account),
}
dest := v2net.TCPDestination(cmd.Host, cmd.Port)
until := time.Now().Add(time.Duration(cmd.ValidMin) * time.Minute)

View File

@@ -7,14 +7,12 @@ import (
"errors"
"v2ray.com/core/common"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy/registry"
"v2ray.com/core/proxy/vmess"
"github.com/golang/protobuf/ptypes"
)
func (this *Config) UnmarshalJSON(data []byte) error {
@@ -65,12 +63,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
log.Error("VMess|Outbound: Invalid user: ", err)
return err
}
anyAccount, err := ptypes.MarshalAny(account)
if err != nil {
log.Error("VMess|Outbound: Failed to create account: ", err)
return common.ErrBadConfiguration
}
user.Account = anyAccount
user.Account = loader.NewTypedSettings(account)
spec.User = append(spec.User, user)
}
serverSpecs[idx] = spec
@@ -78,7 +71,3 @@ func (this *Config) UnmarshalJSON(data []byte) error {
this.Receiver = serverSpecs
return nil
}
func init() {
registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
}

View File

@@ -7,6 +7,7 @@ import (
"v2ray.com/core/app"
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
@@ -183,5 +184,5 @@ func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.
}
func init() {
registry.MustRegisterOutboundHandlerCreator("vmess", new(Factory))
registry.MustRegisterOutboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
}

View File

@@ -118,7 +118,7 @@ L:
func (this *TimedUserValidator) Add(user *protocol.User) error {
idx := len(this.validUsers)
this.validUsers = append(this.validUsers, user)
rawAccount, err := user.GetTypedAccount(&Account{})
rawAccount, err := user.GetTypedAccount()
if err != nil {
return err
}