Skip to content

Commit

Permalink
fix: add default config to prevent nil problem
Browse files Browse the repository at this point in the history
  • Loading branch information
crackair committed Jun 21, 2021
1 parent d9fe312 commit 51e6d24
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 34 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/go-resty/resty/v2 v2.5.0
github.com/golang/protobuf v1.5.2
github.com/juju/ratelimit v1.0.1
github.com/r3labs/diff/v2 v2.13.1
github.com/shirou/gopsutil v3.21.2+incompatible
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/r3labs/diff/v2 v2.13.1 h1:o0mlcYwUKTeL6jVblu42vCnXS7gfArPKYJCs4qkwOw8=
github.com/r3labs/diff/v2 v2.13.1/go.mod h1:I8noH9Fc2fjSaMxqF3G2lhDdC0b+JXCfyx85tWFM9kc=
github.com/rainycape/memcache v0.0.0-20150622160815-1031fa0ce2f2 h1:dq90+d51/hQRaHEqRAsQ1rE/pC1GUS4sc2rCbbFsAIY=
github.com/rainycape/memcache v0.0.0-20150622160815-1031fa0ce2f2/go.mod h1:7tZKcyumwBO6qip7RNQ5r77yrssm9bfCowcLEBcU5IA=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down Expand Up @@ -586,6 +588,8 @@ github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPU
github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vultr/govultr/v2 v2.4.0 h1:6ySGGAsoOann0lmVNkS8grLvbAT2iYWnO4R1RVYFg0A=
github.com/vultr/govultr/v2 v2.4.0/go.mod h1:U+dZLAmyGD62IGykgC9JYU/zQIOkIhf93nw6dJL/47M=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down Expand Up @@ -853,6 +857,7 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand Down
4 changes: 2 additions & 2 deletions main/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Log:
DnsConfigPath: # ./dns.json Path to dns config
ConnetionConfig:
Handshake: 4 # Handshake time limit, Second
ConnIdle: 5 # Connection idle time limit, Second
ConnIdle: 10 # Connection idle time limit, Second
UplinkOnly: 2 # Time limit when the connection downstream is closed, Second
DownlinkOnly: 4 # Time limit when the connection is closed after the uplink is closed, Second
BufferSize: 0 # The internal cache size of each connection, kB
BufferSize: 64 # The internal cache size of each connection, kB
Nodes:
-
PanelType: "SSpanel" # Panel type: SSpanel, V2board, PMpanel
Expand Down
36 changes: 36 additions & 0 deletions panel/defaultConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package panel

import "github.com/XrayR-project/XrayR/service/controller"

func getDefaultLogConfig() *LogConfig {
return &LogConfig{
Level: "none",
AccessPath: "",
ErrorPath: "",
}
}

func getDefaultConnetionConfig() *ConnetionConfig {
return &ConnetionConfig{
Handshake: 4,
ConnIdle: 10,
UplinkOnly: 2,
DownlinkOnly: 4,
BufferSize: 64,
}
}

func getDefaultControllerConfig() *controller.Config {
return &controller.Config{
ListenIP: "0.0.0.0",
SendIP: "0.0.0.0",
UpdatePeriodic: 60,
DNSType: "AsIs",
CertConfig: &controller.CertConfig{
CertMode: "none",
},
FallBackConfigs: []*controller.FallBackConfig{
&controller.FallBackConfig{},
},
}
}
69 changes: 37 additions & 32 deletions panel/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
_ "github.com/XrayR-project/XrayR/main/distro/all"
"github.com/XrayR-project/XrayR/service"
"github.com/XrayR-project/XrayR/service/controller"
"github.com/r3labs/diff/v2"
"github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/app/stats"
"github.com/xtls/xray-core/common/serial"
Expand All @@ -37,34 +38,40 @@ func New(panelConfig *Config) *Panel {

func (p *Panel) loadCore(panelConfig *Config) *core.Instance {
// Log Config
logConfig := &conf.LogConfig{
LogLevel: panelConfig.LogConfig.Level,
AccessLog: panelConfig.LogConfig.AccessPath,
ErrorLog: panelConfig.LogConfig.ErrorPath,
coreLogConfig := &conf.LogConfig{}
defaultLogConfig := getDefaultLogConfig()
if panelConfig.LogConfig != nil {
if _, err := diff.Merge(defaultLogConfig, panelConfig.LogConfig, defaultLogConfig); err != nil {
log.Panicf("Read Log config failed: %s", err)
}
}
coreLogConfig.LogLevel = defaultLogConfig.Level
coreLogConfig.AccessLog = defaultLogConfig.AccessPath
coreLogConfig.ErrorLog = defaultLogConfig.ErrorPath

// DNS config
dnsConfig := &conf.DNSConfig{}
coreDnsConfig := &conf.DNSConfig{}
if panelConfig.DnsConfigPath != "" {
if data, err := io.ReadFile(panelConfig.DnsConfigPath); err != nil {
log.Panicf("Failed to read dns.json at: %s", panelConfig.DnsConfigPath)
} else {
if err = json.Unmarshal(data, dnsConfig); err != nil {
if err = json.Unmarshal(data, coreDnsConfig); err != nil {
log.Panicf("Failed to unmarshal dns.json")
}
}
}
dConfig, err := dnsConfig.Build()
dConfig, err := coreDnsConfig.Build()
if err != nil {
log.Panicf("Failed to understand dns.json, Please check: https://xtls.github.io/config/base/dns/ for help: %s", err)
}
// Policy config
policy := parseConnectionConfig(panelConfig.ConnetionConfig)
policyConfig := &conf.PolicyConfig{}
policyConfig.Levels = map[uint32]*conf.Policy{0: policy}
pConfig, _ := policyConfig.Build()
levelPolicyConfig := parseConnectionConfig(panelConfig.ConnetionConfig)
corePolicyConfig := &conf.PolicyConfig{}
corePolicyConfig.Levels = map[uint32]*conf.Policy{0: levelPolicyConfig}
pConfig, _ := corePolicyConfig.Build()
config := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(logConfig.Build()),
serial.ToTypedMessage(coreLogConfig.Build()),
serial.ToTypedMessage(&mydispatcher.Config{}),
serial.ToTypedMessage(&stats.Config{}),
serial.ToTypedMessage(&proxyman.InboundConfig{}),
Expand Down Expand Up @@ -108,7 +115,13 @@ func (p *Panel) Start() {
}
var controllerService service.Service
// Regist controller service
controllerService = controller.New(server, apiClient, nodeConfig.ControllerConfig)
defaultControllerConfig := getDefaultControllerConfig()
if nodeConfig.ControllerConfig != nil {
if _, err := diff.Merge(defaultControllerConfig, nodeConfig.ControllerConfig, defaultControllerConfig); err != nil {
log.Panicf("Read Controller Config Failed")
}
}
controllerService = controller.New(server, apiClient, defaultControllerConfig)
p.Service = append(p.Service, controllerService)

}
Expand Down Expand Up @@ -140,28 +153,20 @@ func (p *Panel) Close() {
}

func parseConnectionConfig(c *ConnetionConfig) (policy *conf.Policy) {
defaultConnetionConfig := getDefaultConnetionConfig()
if c != nil {
if _, err := diff.Merge(defaultConnetionConfig, c, defaultConnetionConfig); err != nil {
log.Panicf("Read ConnetionConfig failed: %s", err)
}
}
policy = &conf.Policy{
StatsUserUplink: true,
StatsUserDownlink: true,
}
if c != nil {
if c.ConnIdle > 0 {
policy.ConnectionIdle = &c.ConnIdle
} else {
c.ConnIdle = 30
}
if c.Handshake > 0 {
policy.Handshake = &c.Handshake
}
if c.UplinkOnly > 0 {
policy.UplinkOnly = &c.UplinkOnly
}
if c.DownlinkOnly > 0 {
policy.DownlinkOnly = &c.DownlinkOnly
}
if c.BufferSize > 0 {
policy.BufferSize = &c.BufferSize
}
Handshake: &defaultConnetionConfig.Handshake,
ConnectionIdle: &defaultConnetionConfig.ConnIdle,
UplinkOnly: &defaultConnetionConfig.UplinkOnly,
DownlinkOnly: &defaultConnetionConfig.DownlinkOnly,
BufferSize: &defaultConnetionConfig.BufferSize,
}

return
Expand Down

0 comments on commit 51e6d24

Please sign in to comment.