mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 01:27:03 -05:00
Convert point server config to interface for testibility
This commit is contained in:
parent
76cb92f918
commit
729312f3b5
25
config.go
25
config.go
@ -1,28 +1,17 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// User is the user account that is used for connection to a Point
|
||||
type User struct {
|
||||
Id ID `json:"id"` // The ID of this User.
|
||||
}
|
||||
|
||||
type ConnectionConfig struct {
|
||||
Protocol string `json:"protocol"`
|
||||
File string `json:"file"`
|
||||
type ConnectionConfig interface {
|
||||
Protocol() string
|
||||
Content() []byte
|
||||
}
|
||||
|
||||
// Config is the config for Point server.
|
||||
type Config struct {
|
||||
Port uint16 `json:"port"` // Port of this Point server.
|
||||
InboundConfig ConnectionConfig `json:"inbound"`
|
||||
OutboundConfig ConnectionConfig `json:"outbound"`
|
||||
}
|
||||
|
||||
func LoadConfig(rawConfig []byte) (Config, error) {
|
||||
config := Config{}
|
||||
err := json.Unmarshal(rawConfig, &config)
|
||||
return config, err
|
||||
type PointConfig interface {
|
||||
Port() uint16
|
||||
InboundConfig() ConnectionConfig
|
||||
OutboundConfig() ConnectionConfig
|
||||
}
|
||||
|
70
io/config/json/json.go
Normal file
70
io/config/json/json.go
Normal file
@ -0,0 +1,70 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/log"
|
||||
)
|
||||
|
||||
type ConnectionConfig struct {
|
||||
ProtocolString string `json:"protocol"`
|
||||
File string `json:"file"`
|
||||
}
|
||||
|
||||
func (config *ConnectionConfig) Protocol() string {
|
||||
return config.ProtocolString
|
||||
}
|
||||
|
||||
func (config *ConnectionConfig) Content() []byte {
|
||||
if len(config.File) == 0 {
|
||||
return nil
|
||||
}
|
||||
content, err := ioutil.ReadFile(config.File)
|
||||
if err != nil {
|
||||
panic(log.Error("Failed to read config file (%s): %v", config.File, err))
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
// Config is the config for Point server.
|
||||
type Config struct {
|
||||
PortValue uint16 `json:"port"` // Port of this Point server.
|
||||
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
||||
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
||||
}
|
||||
|
||||
func (config *Config) Port() uint16 {
|
||||
return config.PortValue
|
||||
}
|
||||
|
||||
func (config *Config) InboundConfig() core.ConnectionConfig {
|
||||
return config.InboundConfigValue
|
||||
}
|
||||
|
||||
func (config *Config) OutboundConfig() core.ConnectionConfig {
|
||||
return config.OutboundConfigValue
|
||||
}
|
||||
|
||||
func LoadConfig(file string) (*Config, error) {
|
||||
rawConfig, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Error("Failed to read point config file (%s): %v", file, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config := &Config{}
|
||||
err = json.Unmarshal(rawConfig, config)
|
||||
|
||||
if !filepath.IsAbs(config.InboundConfigValue.File) && len(config.InboundConfigValue.File) > 0 {
|
||||
config.InboundConfigValue.File = filepath.Join(filepath.Dir(file), config.InboundConfigValue.File)
|
||||
}
|
||||
|
||||
if !filepath.IsAbs(config.OutboundConfigValue.File) && len(config.OutboundConfigValue.File) > 0 {
|
||||
config.OutboundConfigValue.File = filepath.Join(filepath.Dir(file), config.OutboundConfigValue.File)
|
||||
}
|
||||
|
||||
return config, err
|
||||
}
|
30
point.go
30
point.go
@ -1,8 +1,6 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/v2ray/v2ray-core/log"
|
||||
v2net "github.com/v2ray/v2ray-core/net"
|
||||
)
|
||||
@ -35,36 +33,24 @@ type Point struct {
|
||||
|
||||
// NewPoint returns a new Point server based on given configuration.
|
||||
// The server is not started at this point.
|
||||
func NewPoint(config Config) (*Point, error) {
|
||||
func NewPoint(config PointConfig) (*Point, error) {
|
||||
var vpoint = new(Point)
|
||||
vpoint.port = config.Port
|
||||
vpoint.port = config.Port()
|
||||
|
||||
ichFactory, ok := inboundFactories[config.InboundConfig.Protocol]
|
||||
ichFactory, ok := inboundFactories[config.InboundConfig().Protocol()]
|
||||
if !ok {
|
||||
panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig.Protocol))
|
||||
panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig().Protocol()))
|
||||
}
|
||||
vpoint.ichFactory = ichFactory
|
||||
if len(config.InboundConfig.File) > 0 {
|
||||
ichConfig, err := ioutil.ReadFile(config.InboundConfig.File)
|
||||
if err != nil {
|
||||
panic(log.Error("Unable to read config file %v", err))
|
||||
}
|
||||
vpoint.ichConfig = ichConfig
|
||||
}
|
||||
vpoint.ichConfig = config.InboundConfig().Content()
|
||||
|
||||
ochFactory, ok := outboundFactories[config.OutboundConfig.Protocol]
|
||||
ochFactory, ok := outboundFactories[config.OutboundConfig().Protocol()]
|
||||
if !ok {
|
||||
panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig.Protocol))
|
||||
panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig().Protocol))
|
||||
}
|
||||
|
||||
vpoint.ochFactory = ochFactory
|
||||
if len(config.OutboundConfig.File) > 0 {
|
||||
ochConfig, err := ioutil.ReadFile(config.OutboundConfig.File)
|
||||
if err != nil {
|
||||
panic(log.Error("Unable to read config file %v", err))
|
||||
}
|
||||
vpoint.ochConfig = ochConfig
|
||||
}
|
||||
vpoint.ochConfig = config.OutboundConfig().Content()
|
||||
|
||||
return vpoint, nil
|
||||
}
|
||||
|
@ -3,10 +3,9 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
jsonconf "github.com/v2ray/v2ray-core/io/config/json"
|
||||
"github.com/v2ray/v2ray-core/log"
|
||||
|
||||
// The following are neccesary as they register handlers in their init functions.
|
||||
@ -44,24 +43,12 @@ func main() {
|
||||
if configFile == nil || len(*configFile) == 0 {
|
||||
panic(log.Error("Config file is not set."))
|
||||
}
|
||||
rawVConfig, err := ioutil.ReadFile(*configFile)
|
||||
config, err := jsonconf.LoadConfig(*configFile)
|
||||
if err != nil {
|
||||
panic(log.Error("Failed to read config file (%s): %v", *configFile, err))
|
||||
}
|
||||
vconfig, err := core.LoadConfig(rawVConfig)
|
||||
if err != nil {
|
||||
panic(log.Error("Failed to parse Config: %v", err))
|
||||
}
|
||||
|
||||
if !filepath.IsAbs(vconfig.InboundConfig.File) && len(vconfig.InboundConfig.File) > 0 {
|
||||
vconfig.InboundConfig.File = filepath.Join(filepath.Dir(*configFile), vconfig.InboundConfig.File)
|
||||
}
|
||||
|
||||
if !filepath.IsAbs(vconfig.OutboundConfig.File) && len(vconfig.OutboundConfig.File) > 0 {
|
||||
vconfig.OutboundConfig.File = filepath.Join(filepath.Dir(*configFile), vconfig.OutboundConfig.File)
|
||||
}
|
||||
|
||||
vPoint, err := core.NewPoint(vconfig)
|
||||
vPoint, err := core.NewPoint(config)
|
||||
if err != nil {
|
||||
panic(log.Error("Failed to create Point server: %v", err))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user