mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-01 08:17:25 +00:00
json config parser
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
package socks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/log"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
|
||||
@@ -49,48 +44,3 @@ func (this *ServerConfig) GetNetAddress() v2net.Address {
|
||||
}
|
||||
return this.Address.AsAddress()
|
||||
}
|
||||
|
||||
const (
|
||||
AuthMethodNoAuth = "noauth"
|
||||
AuthMethodUserPass = "password"
|
||||
)
|
||||
|
||||
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
|
||||
type SocksConfig struct {
|
||||
AuthMethod string `json:"auth"`
|
||||
Accounts []*Account `json:"accounts"`
|
||||
UDP bool `json:"udp"`
|
||||
Host *v2net.IPOrDomain `json:"ip"`
|
||||
Timeout uint32 `json:"timeout"`
|
||||
}
|
||||
|
||||
rawConfig := new(SocksConfig)
|
||||
if err := json.Unmarshal(data, rawConfig); err != nil {
|
||||
return errors.New("Socks: Failed to parse config: " + err.Error())
|
||||
}
|
||||
if rawConfig.AuthMethod == AuthMethodNoAuth {
|
||||
this.AuthType = AuthType_NO_AUTH
|
||||
} else if rawConfig.AuthMethod == AuthMethodUserPass {
|
||||
this.AuthType = AuthType_PASSWORD
|
||||
} else {
|
||||
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
|
||||
return common.ErrBadConfiguration
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
this.UdpEnabled = rawConfig.UDP
|
||||
if rawConfig.Host != nil {
|
||||
this.Address = rawConfig.Host
|
||||
}
|
||||
|
||||
if rawConfig.Timeout >= 0 {
|
||||
this.Timeout = rawConfig.Timeout
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
// +build json
|
||||
|
||||
package socks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"v2ray.com/core/common/loader"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
)
|
||||
|
||||
func (this *Account) UnmarshalJSON(data []byte) error {
|
||||
type JsonConfig struct {
|
||||
Username string `json:"user"`
|
||||
Password string `json:"pass"`
|
||||
}
|
||||
jsonConfig := new(JsonConfig)
|
||||
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
||||
return errors.New("Socks: Failed to parse account: " + err.Error())
|
||||
}
|
||||
this.Username = jsonConfig.Username
|
||||
this.Password = jsonConfig.Password
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *ClientConfig) UnmarshalJSON(data []byte) error {
|
||||
type ServerConfig struct {
|
||||
Address *v2net.IPOrDomain `json:"address"`
|
||||
Port v2net.Port `json:"port"`
|
||||
Users []json.RawMessage `json:"users"`
|
||||
}
|
||||
type JsonConfig struct {
|
||||
Servers []*ServerConfig `json:"servers"`
|
||||
}
|
||||
jsonConfig := new(JsonConfig)
|
||||
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
||||
return errors.New("Socks|Client: Failed to parse config: " + err.Error())
|
||||
}
|
||||
this.Server = make([]*protocol.ServerEndpoint, len(jsonConfig.Servers))
|
||||
for idx, serverConfig := range jsonConfig.Servers {
|
||||
server := &protocol.ServerEndpoint{
|
||||
Address: serverConfig.Address,
|
||||
Port: uint32(serverConfig.Port),
|
||||
}
|
||||
for _, rawUser := range serverConfig.Users {
|
||||
user := new(protocol.User)
|
||||
if err := json.Unmarshal(rawUser, user); err != nil {
|
||||
return errors.New("Socks|Client: Failed to parse user: " + err.Error())
|
||||
}
|
||||
account := new(Account)
|
||||
if err := json.Unmarshal(rawUser, account); err != nil {
|
||||
return errors.New("Socks|Client: Failed to parse socks account: " + err.Error())
|
||||
}
|
||||
user.Account = loader.NewTypedSettings(account)
|
||||
server.User = append(server.User, user)
|
||||
}
|
||||
this.Server[idx] = server
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
// +build json
|
||||
|
||||
package socks_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"v2ray.com/core/proxy/registry"
|
||||
. "v2ray.com/core/proxy/socks"
|
||||
"v2ray.com/core/testing/assert"
|
||||
)
|
||||
|
||||
func TestDefaultIPAddress(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
socksConfig, err := registry.CreateInboundConfig("socks", []byte(`{
|
||||
"auth": "noauth"
|
||||
}`))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Address(socksConfig.(*ServerConfig).GetNetAddress()).EqualsString("127.0.0.1")
|
||||
}
|
||||
Reference in New Issue
Block a user