mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-19 23:03:08 +00:00
unified config loader
This commit is contained in:
1 parent
3f7b70a465
commit
e732ba62a6
18 files changed
+257
-215
No files matched your search
+40
-41
@@ -8,7 +8,6 @@ import (
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/proxy/internal"
|
||||
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -16,48 +15,48 @@ const (
|
||||
AuthMethodUserPass = "password"
|
||||
)
|
||||
|
||||
func init() {
|
||||
config.RegisterInboundConfig("socks",
|
||||
func(data []byte) (interface{}, error) {
|
||||
type SocksAccount struct {
|
||||
Username string `json:"user"`
|
||||
Password string `json:"pass"`
|
||||
}
|
||||
func (this *Config) UnmarshalJSON(data []byte) error {
|
||||
type SocksAccount struct {
|
||||
Username string `json:"user"`
|
||||
Password string `json:"pass"`
|
||||
}
|
||||
|
||||
type SocksConfig struct {
|
||||
AuthMethod string `json:"auth"`
|
||||
Accounts []*SocksAccount `json:"accounts"`
|
||||
UDP bool `json:"udp"`
|
||||
Host *v2net.AddressJson `json:"ip"`
|
||||
}
|
||||
type SocksConfig struct {
|
||||
AuthMethod string `json:"auth"`
|
||||
Accounts []*SocksAccount `json:"accounts"`
|
||||
UDP bool `json:"udp"`
|
||||
Host *v2net.AddressJson `json:"ip"`
|
||||
}
|
||||
|
||||
rawConfig := new(SocksConfig)
|
||||
if err := json.Unmarshal(data, rawConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
socksConfig := new(Config)
|
||||
if rawConfig.AuthMethod == AuthMethodNoAuth {
|
||||
socksConfig.AuthType = AuthTypeNoAuth
|
||||
} else if rawConfig.AuthMethod == AuthMethodUserPass {
|
||||
socksConfig.AuthType = AuthTypePassword
|
||||
} else {
|
||||
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
|
||||
return nil, internal.ErrorBadConfiguration
|
||||
}
|
||||
rawConfig := new(SocksConfig)
|
||||
if err := json.Unmarshal(data, rawConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
if rawConfig.AuthMethod == AuthMethodNoAuth {
|
||||
this.AuthType = AuthTypeNoAuth
|
||||
} else if rawConfig.AuthMethod == AuthMethodUserPass {
|
||||
this.AuthType = AuthTypePassword
|
||||
} else {
|
||||
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
|
||||
return internal.ErrorBadConfiguration
|
||||
}
|
||||
|
||||
if len(rawConfig.Accounts) > 0 {
|
||||
socksConfig.Accounts = make(map[string]string, len(rawConfig.Accounts))
|
||||
for _, account := range rawConfig.Accounts {
|
||||
socksConfig.Accounts[account.Username] = account.Password
|
||||
}
|
||||
}
|
||||
if len(rawConfig.Accounts) > 0 {
|
||||
this.Accounts = make(map[string]string, len(rawConfig.Accounts))
|
||||
for _, account := range rawConfig.Accounts {
|
||||
this.Accounts[account.Username] = account.Password
|
||||
}
|
||||
}
|
||||
|
||||
socksConfig.UDPEnabled = rawConfig.UDP
|
||||
if rawConfig.Host != nil {
|
||||
socksConfig.Address = rawConfig.Host.Address
|
||||
} else {
|
||||
socksConfig.Address = v2net.LocalHostIP
|
||||
}
|
||||
return socksConfig, nil
|
||||
})
|
||||
this.UDPEnabled = rawConfig.UDP
|
||||
if rawConfig.Host != nil {
|
||||
this.Address = rawConfig.Host.Address
|
||||
} else {
|
||||
this.Address = v2net.LocalHostIP
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
internal.RegisterInboundConfig("socks", func() interface{} { return new(Config) })
|
||||
}
|
||||
Reference in new issue
Block a user