mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-19 23:03:08 +00:00
Move point to shell
This commit is contained in:
1 parent
cc61e88eca
commit
07582c5b60
21 files changed
+22
-22
No files matched your search
@@ -0,0 +1,36 @@
|
||||
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{} {
|
||||
return loadConnectionConfig(c.SettingsMessage, c.Protocol(), c.Type)
|
||||
}
|
||||
|
||||
func loadConnectionConfig(message json.RawMessage, protocol string, cType proxyconfig.Type) interface{} {
|
||||
configObj := proxyjson.CreateConfig(protocol, cType)
|
||||
if configObj == nil {
|
||||
panic("Unknown protocol " + protocol)
|
||||
}
|
||||
err := json.Unmarshal(message, configObj)
|
||||
if err != nil {
|
||||
log.Error("Unable to parse connection config: %v", err)
|
||||
panic("Failed to parse connection config.")
|
||||
}
|
||||
return configObj
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
)
|
||||
|
||||
type InboundDetourConfig struct {
|
||||
ProtocolValue string `json:"protocol"`
|
||||
PortRangeValue *v2netjson.PortRange `json:"port"`
|
||||
SettingsValue json.RawMessage `json:"settings"`
|
||||
}
|
||||
|
||||
func (this *InboundDetourConfig) Protocol() string {
|
||||
return this.ProtocolValue
|
||||
}
|
||||
|
||||
func (this *InboundDetourConfig) PortRange() v2net.PortRange {
|
||||
return this.PortRangeValue
|
||||
}
|
||||
|
||||
func (this *InboundDetourConfig) Settings() interface{} {
|
||||
return loadConnectionConfig(this.SettingsValue, this.ProtocolValue, proxyconfig.TypeInbound)
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
routerconfig "github.com/v2ray/v2ray-core/app/router/config"
|
||||
routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
"github.com/v2ray/v2ray-core/shell/point/config"
|
||||
)
|
||||
|
||||
// Config is the config for Point server.
|
||||
type Config struct {
|
||||
PortValue uint16 `json:"port"` // Port of this Point server.
|
||||
LogConfigValue *LogConfig `json:"log"`
|
||||
RouterConfigValue *routerconfigjson.RouterConfig `json:"routing"`
|
||||
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
||||
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
||||
InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
|
||||
OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
|
||||
}
|
||||
|
||||
func (config *Config) Port() uint16 {
|
||||
return config.PortValue
|
||||
}
|
||||
|
||||
func (config *Config) LogConfig() config.LogConfig {
|
||||
if config.LogConfigValue == nil {
|
||||
return nil
|
||||
}
|
||||
return config.LogConfigValue
|
||||
}
|
||||
|
||||
func (this *Config) RouterConfig() routerconfig.RouterConfig {
|
||||
if this.RouterConfigValue == nil {
|
||||
return nil
|
||||
}
|
||||
return this.RouterConfigValue
|
||||
}
|
||||
|
||||
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 (this *Config) InboundDetours() []config.InboundDetourConfig {
|
||||
detours := make([]config.InboundDetourConfig, len(this.InboundDetoursValue))
|
||||
for idx, detour := range this.InboundDetoursValue {
|
||||
detours[idx] = detour
|
||||
}
|
||||
return detours
|
||||
}
|
||||
|
||||
func (this *Config) OutboundDetours() []config.OutboundDetourConfig {
|
||||
detours := make([]config.OutboundDetourConfig, len(this.OutboundDetoursValue))
|
||||
for idx, detour := range this.OutboundDetoursValue {
|
||||
detours[idx] = detour
|
||||
}
|
||||
return detours
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package json_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
_ "github.com/v2ray/v2ray-core/proxy/dokodemo/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/shell/point/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()
|
||||
}
|
||||
|
||||
func TestDetourConfig(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_dns_detour.json"))
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
detours := pointConfig.InboundDetours()
|
||||
assert.Int(len(detours)).Equals(1)
|
||||
|
||||
detour := detours[0]
|
||||
assert.String(detour.Protocol()).Equals("dokodemo-door")
|
||||
assert.Uint16(detour.PortRange().From()).Equals(uint16(28394))
|
||||
assert.Uint16(detour.PortRange().To()).Equals(uint16(28394))
|
||||
assert.Pointer(detour.Settings()).IsNotNil()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package json
|
||||
|
||||
type LogConfig struct {
|
||||
AccessLogValue string `json:"access"`
|
||||
}
|
||||
|
||||
func (config *LogConfig) AccessLog() string {
|
||||
return config.AccessLogValue
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
"github.com/v2ray/v2ray-core/shell/point/config"
|
||||
)
|
||||
|
||||
type OutboundDetourConfig struct {
|
||||
ProtocolValue string `json:"protocol"`
|
||||
TagValue string `json:"tag"`
|
||||
SettingsValue json.RawMessage `json:"settings"`
|
||||
}
|
||||
|
||||
func (this *OutboundDetourConfig) Protocol() string {
|
||||
return this.ProtocolValue
|
||||
}
|
||||
|
||||
func (this *OutboundDetourConfig) Tag() config.DetourTag {
|
||||
return config.DetourTag(this.TagValue)
|
||||
}
|
||||
|
||||
func (this *OutboundDetourConfig) Settings() interface{} {
|
||||
return loadConnectionConfig(this.SettingsValue, this.ProtocolValue, proxyconfig.TypeOutbound)
|
||||
}
|
||||
Reference in new issue
Block a user