mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -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
|
package core
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
// User is the user account that is used for connection to a Point
|
// User is the user account that is used for connection to a Point
|
||||||
type User struct {
|
type User struct {
|
||||||
Id ID `json:"id"` // The ID of this User.
|
Id ID `json:"id"` // The ID of this User.
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionConfig struct {
|
type ConnectionConfig interface {
|
||||||
Protocol string `json:"protocol"`
|
Protocol() string
|
||||||
File string `json:"file"`
|
Content() []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config is the config for Point server.
|
type PointConfig interface {
|
||||||
type Config struct {
|
Port() uint16
|
||||||
Port uint16 `json:"port"` // Port of this Point server.
|
InboundConfig() ConnectionConfig
|
||||||
InboundConfig ConnectionConfig `json:"inbound"`
|
OutboundConfig() ConnectionConfig
|
||||||
OutboundConfig ConnectionConfig `json:"outbound"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadConfig(rawConfig []byte) (Config, error) {
|
|
||||||
config := Config{}
|
|
||||||
err := json.Unmarshal(rawConfig, &config)
|
|
||||||
return config, err
|
|
||||||
}
|
}
|
||||||
|
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
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/net"
|
v2net "github.com/v2ray/v2ray-core/net"
|
||||||
)
|
)
|
||||||
@ -35,36 +33,24 @@ type Point struct {
|
|||||||
|
|
||||||
// NewPoint returns a new Point server based on given configuration.
|
// NewPoint returns a new Point server based on given configuration.
|
||||||
// The server is not started at this point.
|
// The server is not started at this point.
|
||||||
func NewPoint(config Config) (*Point, error) {
|
func NewPoint(config PointConfig) (*Point, error) {
|
||||||
var vpoint = new(Point)
|
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 {
|
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
|
vpoint.ichFactory = ichFactory
|
||||||
if len(config.InboundConfig.File) > 0 {
|
vpoint.ichConfig = config.InboundConfig().Content()
|
||||||
ichConfig, err := ioutil.ReadFile(config.InboundConfig.File)
|
|
||||||
if err != nil {
|
|
||||||
panic(log.Error("Unable to read config file %v", err))
|
|
||||||
}
|
|
||||||
vpoint.ichConfig = ichConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
ochFactory, ok := outboundFactories[config.OutboundConfig.Protocol]
|
ochFactory, ok := outboundFactories[config.OutboundConfig().Protocol()]
|
||||||
if !ok {
|
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
|
vpoint.ochFactory = ochFactory
|
||||||
if len(config.OutboundConfig.File) > 0 {
|
vpoint.ochConfig = config.OutboundConfig().Content()
|
||||||
ochConfig, err := ioutil.ReadFile(config.OutboundConfig.File)
|
|
||||||
if err != nil {
|
|
||||||
panic(log.Error("Unable to read config file %v", err))
|
|
||||||
}
|
|
||||||
vpoint.ochConfig = ochConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
return vpoint, nil
|
return vpoint, nil
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
|
jsonconf "github.com/v2ray/v2ray-core/io/config/json"
|
||||||
"github.com/v2ray/v2ray-core/log"
|
"github.com/v2ray/v2ray-core/log"
|
||||||
|
|
||||||
// The following are neccesary as they register handlers in their init functions.
|
// The following are neccesary as they register handlers in their init functions.
|
||||||
@ -44,24 +43,12 @@ func main() {
|
|||||||
if configFile == nil || len(*configFile) == 0 {
|
if configFile == nil || len(*configFile) == 0 {
|
||||||
panic(log.Error("Config file is not set."))
|
panic(log.Error("Config file is not set."))
|
||||||
}
|
}
|
||||||
rawVConfig, err := ioutil.ReadFile(*configFile)
|
config, err := jsonconf.LoadConfig(*configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(log.Error("Failed to read config file (%s): %v", *configFile, err))
|
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 {
|
vPoint, err := core.NewPoint(config)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(log.Error("Failed to create Point server: %v", err))
|
panic(log.Error("Failed to create Point server: %v", err))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user