1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-04 00:05:21 -05:00

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

View File

@@ -0,0 +1,15 @@
package json
import (
"github.com/v2ray/v2ray-core/config"
"github.com/v2ray/v2ray-core/config/json"
)
type FreedomConfiguration struct {
}
func init() {
json.RegisterConfigType("freedom", config.TypeOutbound, func() interface{} {
return &FreedomConfiguration{}
})
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/common/net"
_ "github.com/v2ray/v2ray-core/proxy/socks"
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
"github.com/v2ray/v2ray-core/testing/mocks"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
"github.com/v2ray/v2ray-core/testing/servers/udp"
@@ -47,11 +48,11 @@ func TestUDPSend(t *testing.T) {
PortValue: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_ich",
ContentValue: nil,
SettingsValue: nil,
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "freedom",
ContentValue: nil,
SettingsValue: nil,
},
}
@@ -89,11 +90,13 @@ func TestSocksTcpConnect(t *testing.T) {
PortValue: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "socks",
ContentValue: []byte("{\"auth\": \"noauth\"}"),
SettingsValue: &json.SocksConfig{
AuthMethod: "auth",
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "freedom",
ContentValue: nil,
SettingsValue: nil,
},
}

View File

@@ -8,11 +8,7 @@ import (
type FreedomFactory struct {
}
func (factory FreedomFactory) Initialize(config []byte) error {
return nil
}
func (factory FreedomFactory) Create(vp *core.Point, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
func (factory FreedomFactory) Create(vp *core.Point, config interface{}, firstPacket v2net.Packet) (core.OutboundConnectionHandler, error) {
return NewFreedomConnection(firstPacket), nil
}

View File

@@ -1,7 +1,8 @@
package json
import (
"encoding/json"
"github.com/v2ray/v2ray-core/config"
"github.com/v2ray/v2ray-core/config/json"
)
const (
@@ -24,8 +25,8 @@ func (config SocksConfig) IsPassword() bool {
return config.AuthMethod == AuthMethodUserPass
}
func Load(rawConfig []byte) (SocksConfig, error) {
config := SocksConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
func init() {
json.RegisterConfigType("socks", config.TypeInbound, func() interface{} {
return new(SocksConfig)
})
}

View File

@@ -19,15 +19,10 @@ import (
type SocksServer struct {
accepting bool
vPoint *core.Point
config jsonconfig.SocksConfig
config *jsonconfig.SocksConfig
}
func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer {
config, err := jsonconfig.Load(rawConfig)
if err != nil {
log.Error("Unable to load socks config: %v", err)
panic(errors.NewConfigurationError())
}
func NewSocksServer(vp *core.Point, config *jsonconfig.SocksConfig) *SocksServer {
return &SocksServer{
vPoint: vp,
config: config,

View File

@@ -9,6 +9,7 @@ import (
"golang.org/x/net/proxy"
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
"github.com/v2ray/v2ray-core/testing/mocks"
"github.com/v2ray/v2ray-core/testing/unit"
)
@@ -28,11 +29,13 @@ func TestSocksTcpConnect(t *testing.T) {
PortValue: port,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "socks",
ContentValue: []byte("{\"auth\": \"noauth\"}"),
SettingsValue: &json.SocksConfig{
AuthMethod: "noauth",
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_och",
ContentValue: nil,
SettingsValue: nil,
},
}
@@ -79,11 +82,15 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
PortValue: port,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "socks",
ContentValue: []byte("{\"auth\": \"password\",\"user\": \"userx\",\"pass\": \"passy\"}"),
SettingsValue: &json.SocksConfig{
AuthMethod: "noauth",
Username: "userx",
Password: "passy",
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_och",
ContentValue: nil,
SettingsValue: nil,
},
}
@@ -130,11 +137,14 @@ func TestSocksUdpSend(t *testing.T) {
PortValue: port,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "socks",
ContentValue: []byte("{\"auth\": \"noauth\", \"udp\": true}"),
SettingsValue: &json.SocksConfig{
AuthMethod: "noauth",
UDPEnabled: true,
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "mock_och",
ContentValue: nil,
SettingsValue: nil,
},
}

View File

@@ -2,13 +2,14 @@ package socks
import (
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
)
type SocksServerFactory struct {
}
func (factory SocksServerFactory) Create(vp *core.Point, config []byte) (core.InboundConnectionHandler, error) {
return NewSocksServer(vp, config), nil
func (factory SocksServerFactory) Create(vp *core.Point, config interface{}) (core.InboundConnectionHandler, error) {
return NewSocksServer(vp, config.(*json.SocksConfig)), nil
}
func init() {

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)
})
}

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,
},
}

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()

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() {