massive refactoring for configuration

This commit is contained in:
V2Ray committed 2015-09-12 11:51:42 +02:00
1 parent 791ac780f0
commit 0ca7dab49b
15 files changed
+212 -147

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
package vmess
import (
"encoding/json"
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/net"
)
type VMessInboundConfig struct {
AllowedClients []core.VUser `json:"clients"`
}
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"`
Users []core.VUser `json:"users"`
}
func (config VNextConfig) ToVNextServer() VNextServer {
return VNextServer{
v2net.DomainAddress(config.Address, config.Port),
config.Users}
}
type VMessOutboundConfig struct {
VNextList []VNextConfig
}
func loadOutboundConfig(rawConfig []byte) (VMessOutboundConfig, error) {
config := VMessOutboundConfig{}
err := json.Unmarshal(rawConfig, &config)
return config, err
}
+11 -11
View File
@@ -8,6 +8,7 @@ import (
"github.com/v2ray/v2ray-core"
v2io "github.com/v2ray/v2ray-core/io"
vmessio "github.com/v2ray/v2ray-core/io/vmess"
"github.com/v2ray/v2ray-core/log"
)
type VMessInboundHandler struct {
@@ -89,6 +90,7 @@ func (handler *VMessInboundHandler) dumpInput(reader io.Reader, input chan<- []b
buffer := make([]byte, BufferSize)
nBytes, err := reader.Read(buffer)
if err == io.EOF {
close(input)
finish <- true
break
}
@@ -114,18 +116,16 @@ func (handler *VMessInboundHandler) waitForFinish(finish <-chan bool) {
}
type VMessInboundHandlerFactory struct {
allowedClients *core.VUserSet
}
func NewVMessInboundHandlerFactory(clients []core.VUser) *VMessInboundHandlerFactory {
factory := new(VMessInboundHandlerFactory)
factory.allowedClients = core.NewVUserSet()
for _, user := range clients {
factory.allowedClients.AddUser(user)
func (factory *VMessInboundHandlerFactory) Create(vp *core.VPoint, rawConfig []byte) *VMessInboundHandler {
config, err := loadInboundConfig(rawConfig)
if err != nil {
panic(log.Error("Failed to load VMess inbound config: %v", err))
}
return factory
}
func (factory *VMessInboundHandlerFactory) Create(vp *core.VPoint) *VMessInboundHandler {
return NewVMessInboundHandler(vp, factory.allowedClients)
allowedClients := core.NewVUserSet()
for _, user := range config.AllowedClients {
allowedClients.AddUser(user)
}
return NewVMessInboundHandler(vp, allowedClients)
}
+26 -8
View File
@@ -10,28 +10,37 @@ import (
"github.com/v2ray/v2ray-core"
v2io "github.com/v2ray/v2ray-core/io"
vmessio "github.com/v2ray/v2ray-core/io/vmess"
"github.com/v2ray/v2ray-core/log"
v2net "github.com/v2ray/v2ray-core/net"
)
type VMessOutboundHandler struct {
vPoint *core.VPoint
dest v2net.VAddress
// VNext is the next VPoint server in the connection chain.
type VNextServer struct {
Address v2net.VAddress // Address of VNext server
Users []core.VUser // User accounts for accessing VNext.
}
func NewVMessOutboundHandler(vp *core.VPoint, dest v2net.VAddress) *VMessOutboundHandler {
type VMessOutboundHandler struct {
vPoint *core.VPoint
dest v2net.VAddress
vNextList []VNextServer
}
func NewVMessOutboundHandler(vp *core.VPoint, vNextList []VNextServer, dest v2net.VAddress) *VMessOutboundHandler {
handler := new(VMessOutboundHandler)
handler.vPoint = vp
handler.dest = dest
handler.vNextList = vNextList
return handler
}
func (handler *VMessOutboundHandler) pickVNext() (v2net.VAddress, core.VUser) {
vNextLen := len(handler.vPoint.Config.VNextList)
vNextLen := len(handler.vNextList)
if vNextLen == 0 {
panic("Zero vNext is configured.")
}
vNextIndex := mrand.Intn(vNextLen)
vNext := handler.vPoint.Config.VNextList[vNextIndex]
vNext := handler.vNextList[vNextIndex]
vNextUserLen := len(vNext.Users)
if vNextUserLen == 0 {
panic("Zero User account.")
@@ -91,6 +100,7 @@ func (handler *VMessOutboundHandler) dumpOutput(reader io.Reader, output chan<-
buffer := make([]byte, BufferSize)
nBytes, err := reader.Read(buffer)
if err == io.EOF {
close(output)
finish <- true
break
}
@@ -118,6 +128,14 @@ func (handler *VMessOutboundHandler) waitForFinish(finish <-chan bool) {
type VMessOutboundHandlerFactory struct {
}
func (factory *VMessOutboundHandlerFactory) Create(vp *core.VPoint, destination v2net.VAddress) *VMessOutboundHandler {
return NewVMessOutboundHandler(vp, destination)
func (factory *VMessOutboundHandlerFactory) Create(vp *core.VPoint, rawConfig []byte, destination v2net.VAddress) *VMessOutboundHandler {
config, err := loadOutboundConfig(rawConfig)
if err != nil {
panic(log.Error("Failed to load VMess outbound config: %v", err))
}
servers := make([]VNextServer, 0, len(config.VNextList))
for _, server := range config.VNextList {
servers = append(servers, server.ToVNextServer())
}
return NewVMessOutboundHandler(vp, servers, destination)
}