Simpilify configuration files

This commit is contained in:
V2Ray
2015-10-06 23:11:08 +02:00
parent 78daf8a879
commit 72b4eeba8b
27 changed files with 251 additions and 208 deletions
+10 -11
View File
@@ -1,12 +1,13 @@
package vmess
import (
"encoding/json"
"net"
"strings"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/config"
"github.com/v2ray/v2ray-core/config/json"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
)
@@ -29,12 +30,6 @@ type VMessInboundConfig struct {
UDPEnabled bool `json:"udp"`
}
func loadInboundConfig(rawConfig []byte) (VMessInboundConfig, error) {
config := VMessInboundConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
}
type VNextConfig struct {
Address string `json:"address"`
Port uint16 `json:"port"`
@@ -76,8 +71,12 @@ type VMessOutboundConfig struct {
VNextList []VNextConfig `json:"vnext"`
}
func loadOutboundConfig(rawConfig []byte) (VMessOutboundConfig, error) {
config := VMessOutboundConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
func init() {
json.RegisterConfigType("vmess", config.TypeInbound, func() interface{} {
return new(VMessInboundConfig)
})
json.RegisterConfigType("vmess", config.TypeOutbound, func() interface{} {
return new(VMessOutboundConfig)
})
}
+39 -8
View File
@@ -27,11 +27,22 @@ func TestVMessInAndOut(t *testing.T) {
PortValue: portA,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_ich",
ContentValue: nil,
SettingsValue: nil,
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
ContentValue: []byte("{\"vnext\":[{\"address\": \"127.0.0.1\", \"network\": \"tcp\", \"port\": 13829, \"users\":[{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}]}]}"),
SettingsValue: &VMessOutboundConfig{
[]VNextConfig{
VNextConfig{
Address: "127.0.0.1",
Port: 13829,
Network: "tcp",
Users: []VMessUser{
VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
},
},
},
},
},
}
@@ -54,11 +65,15 @@ func TestVMessInAndOut(t *testing.T) {
PortValue: portB,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
ContentValue: []byte("{\"clients\": [{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}]}"),
SettingsValue: &VMessInboundConfig{
AllowedClients: []VMessUser{
VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
},
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_och",
ContentValue: nil,
SettingsValue: nil,
},
}
@@ -91,11 +106,22 @@ func TestVMessInAndOutUDP(t *testing.T) {
PortValue: portA,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_ich",
ContentValue: nil,
SettingsValue: nil,
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
ContentValue: []byte("{\"vnext\":[{\"address\": \"127.0.0.1\", \"network\": \"udp\", \"port\": 13841, \"users\":[{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}]}]}"),
SettingsValue: &VMessOutboundConfig{
[]VNextConfig{
VNextConfig{
Address: "127.0.0.1",
Port: 13841,
Network: "udp",
Users: []VMessUser{
VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
},
},
},
},
},
}
@@ -118,11 +144,16 @@ func TestVMessInAndOutUDP(t *testing.T) {
PortValue: portB,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
ContentValue: []byte("{\"clients\": [{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}], \"udp\": true}"),
SettingsValue: &VMessInboundConfig{
AllowedClients: []VMessUser{
VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
},
UDPEnabled: true,
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_och",
ContentValue: nil,
SettingsValue: nil,
},
}
+3 -5
View File
@@ -136,11 +136,9 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha
type VMessInboundHandlerFactory struct {
}
func (factory *VMessInboundHandlerFactory) Create(vp *core.Point, rawConfig []byte) (core.InboundConnectionHandler, error) {
config, err := loadInboundConfig(rawConfig)
if err != nil {
panic(log.Error("VMessIn: Failed to load VMess inbound config: %v", err))
}
func (factory *VMessInboundHandlerFactory) Create(vp *core.Point, rawConfig interface{}) (core.InboundConnectionHandler, error) {
config := rawConfig.(*VMessInboundConfig)
allowedClients := user.NewTimedUserSet()
for _, client := range config.AllowedClients {
user, err := client.ToUser()
+3 -15
View File
@@ -193,16 +193,10 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
}
type VMessOutboundHandlerFactory struct {
servers []VNextServer
udpServers []VNextServer
}
func (factory *VMessOutboundHandlerFactory) Initialize(rawConfig []byte) error {
config, err := loadOutboundConfig(rawConfig)
if err != nil {
panic(log.Error("Failed to load VMess outbound config: %v", err))
return err
}
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, rawConfig interface{}, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
config := rawConfig.(*VMessOutboundConfig)
servers := make([]VNextServer, 0, len(config.VNextList))
udpServers := make([]VNextServer, 0, len(config.VNextList))
for _, server := range config.VNextList {
@@ -213,13 +207,7 @@ func (factory *VMessOutboundHandlerFactory) Initialize(rawConfig []byte) error {
udpServers = append(udpServers, server.ToVNextServer("udp"))
}
}
factory.servers = servers
factory.udpServers = udpServers
return nil
}
func (factory *VMessOutboundHandlerFactory) Create(vp *core.Point, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
return NewVMessOutboundHandler(vp, factory.servers, factory.udpServers, firstPacket), nil
return NewVMessOutboundHandler(vp, servers, udpServers, firstPacket), nil
}
func init() {