mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-06-07 11:39:11 -04:00
Move config to app/point
This commit is contained in:
24
app/point/config/config.go
Normal file
24
app/point/config/config.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package config
|
||||
|
||||
type RouterConfig interface {
|
||||
Strategy() string
|
||||
Settings() interface{}
|
||||
}
|
||||
|
||||
type ConnectionTag string
|
||||
|
||||
type ConnectionConfig interface {
|
||||
Protocol() string
|
||||
Settings() interface{}
|
||||
}
|
||||
|
||||
type LogConfig interface {
|
||||
AccessLog() string
|
||||
}
|
||||
|
||||
type PointConfig interface {
|
||||
Port() uint16
|
||||
LogConfig() LogConfig
|
||||
InboundConfig() ConnectionConfig
|
||||
OutboundConfig() ConnectionConfig
|
||||
}
|
||||
9
app/point/config/errors.go
Normal file
9
app/point/config/errors.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
BadConfiguration = errors.New("Bad configuration.")
|
||||
)
|
||||
32
app/point/config/json/connection.go
Normal file
32
app/point/config/json/connection.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
proxyjson "github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
type ConnectionConfig struct {
|
||||
ProtocolString string `json:"protocol"`
|
||||
SettingsMessage json.RawMessage `json:"settings"`
|
||||
Type proxyconfig.Type `json:"-"`
|
||||
}
|
||||
|
||||
func (c *ConnectionConfig) Protocol() string {
|
||||
return c.ProtocolString
|
||||
}
|
||||
|
||||
func (c *ConnectionConfig) Settings() interface{} {
|
||||
configObj := proxyjson.CreateConfig(c.Protocol(), c.Type)
|
||||
if configObj == nil {
|
||||
panic("Unknown protocol " + c.Protocol())
|
||||
}
|
||||
err := json.Unmarshal(c.SettingsMessage, configObj)
|
||||
if err != nil {
|
||||
log.Error("Unable to parse connection config: %v", err)
|
||||
panic("Failed to parse connection config.")
|
||||
}
|
||||
return configObj
|
||||
}
|
||||
65
app/point/config/json/json.go
Normal file
65
app/point/config/json/json.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/v2ray/v2ray-core/app/point/config"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
)
|
||||
|
||||
// Config is the config for Point server.
|
||||
type Config struct {
|
||||
PortValue uint16 `json:"port"` // Port of this Point server.
|
||||
LogConfigValue *LogConfig `json:"log"`
|
||||
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
||||
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
||||
}
|
||||
|
||||
func (config *Config) Port() uint16 {
|
||||
return config.PortValue
|
||||
}
|
||||
|
||||
func (config *Config) LogConfig() config.LogConfig {
|
||||
if config.LogConfigValue == nil {
|
||||
return nil
|
||||
}
|
||||
return config.LogConfigValue
|
||||
}
|
||||
|
||||
func (config *Config) InboundConfig() config.ConnectionConfig {
|
||||
if config.InboundConfigValue == nil {
|
||||
return nil
|
||||
}
|
||||
return config.InboundConfigValue
|
||||
}
|
||||
|
||||
func (config *Config) OutboundConfig() config.ConnectionConfig {
|
||||
if config.OutboundConfigValue == nil {
|
||||
return nil
|
||||
}
|
||||
return config.OutboundConfigValue
|
||||
}
|
||||
|
||||
func LoadConfig(file string) (*Config, error) {
|
||||
fixedFile := os.ExpandEnv(file)
|
||||
rawConfig, err := ioutil.ReadFile(fixedFile)
|
||||
if err != nil {
|
||||
log.Error("Failed to read server config file (%s): %v", file, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jsonConfig := &Config{}
|
||||
err = json.Unmarshal(rawConfig, jsonConfig)
|
||||
if err != nil {
|
||||
log.Error("Failed to load server config: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jsonConfig.InboundConfigValue.Type = proxyconfig.TypeInbound
|
||||
jsonConfig.OutboundConfigValue.Type = proxyconfig.TypeOutbound
|
||||
|
||||
return jsonConfig, err
|
||||
}
|
||||
53
app/point/config/json/json_test.go
Normal file
53
app/point/config/json/json_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package json_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/app/point/config/json"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
|
||||
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
)
|
||||
|
||||
func TestClientSampleConfig(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
// TODO: fix for Windows
|
||||
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
|
||||
|
||||
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Uint16(pointConfig.Port()).Positive()
|
||||
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
|
||||
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
|
||||
|
||||
assert.String(pointConfig.InboundConfig().Protocol()).Equals("socks")
|
||||
assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil()
|
||||
|
||||
assert.String(pointConfig.OutboundConfig().Protocol()).Equals("vmess")
|
||||
assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil()
|
||||
}
|
||||
|
||||
func TestServerSampleConfig(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
// TODO: fix for Windows
|
||||
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
|
||||
|
||||
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Uint16(pointConfig.Port()).Positive()
|
||||
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
|
||||
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
|
||||
|
||||
assert.String(pointConfig.InboundConfig().Protocol()).Equals("vmess")
|
||||
assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil()
|
||||
|
||||
assert.String(pointConfig.OutboundConfig().Protocol()).Equals("freedom")
|
||||
assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil()
|
||||
}
|
||||
9
app/point/config/json/log.go
Normal file
9
app/point/config/json/log.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package json
|
||||
|
||||
type LogConfig struct {
|
||||
AccessLogValue string `json:"access"`
|
||||
}
|
||||
|
||||
func (config *LogConfig) AccessLog() string {
|
||||
return config.AccessLogValue
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package point
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/app/point/config"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/common/retry"
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
|
||||
"github.com/v2ray/v2ray-core/transport/ray"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user