Skip to content

Commit

Permalink
feat: Add connection setup for memory optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
crackair committed Jun 20, 2021
1 parent e69327a commit d9fe312
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
8 changes: 7 additions & 1 deletion main/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ Log:
Level: none # Log level: none, error, warning, info, debug
AccessPath: # ./access.Log
ErrorPath: # ./error.log
DnsConfigPath: # ./dns.json Path to dns config
DnsConfigPath: # ./dns.json Path to dns config
ConnetionConfig:
Handshake: 4 # Handshake time limit, Second
ConnIdle: 5 # 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
Nodes:
-
PanelType: "SSpanel" # Panel type: SSpanel, V2board, PMpanel
Expand Down
9 changes: 9 additions & 0 deletions panel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Config struct {
LogConfig *LogConfig `mapstructure:"Log"`
DnsConfigPath string `mapstructure:"DnsConfigPath"`
ConnetionConfig *ConnetionConfig `mapstructure:"ConnetionConfig"`
NodesConfig []*NodesConfig `mapstructure:"Nodes"`
}

Expand All @@ -22,3 +23,11 @@ type LogConfig struct {
AccessPath string `mapstructure:"AccessPath"`
ErrorPath string `mapstructure:"ErrorPath"`
}

type ConnetionConfig struct {
Handshake uint32 `mapstructure:"handshake"`
ConnIdle uint32 `mapstructure:"connIdle"`
UplinkOnly uint32 `mapstructure:"uplinkOnly"`
DownlinkOnly uint32 `mapstructure:"downlinkOnly"`
BufferSize int32 `mapstructure:"bufferSize"`
}
37 changes: 32 additions & 5 deletions panel/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ func (p *Panel) loadCore(panelConfig *Config) *core.Instance {
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: &conf.Policy{
StatsUserUplink: true,
StatsUserDownlink: true,
}}
policyConfig.Levels = map[uint32]*conf.Policy{0: policy}
pConfig, _ := policyConfig.Build()
config := &core.Config{
App: []*serial.TypedMessage{
Expand Down Expand Up @@ -102,7 +101,7 @@ func (p *Panel) Start() {
apiClient = sspanel.New(nodeConfig.ApiConfig)
case "V2board":
apiClient = v2board.New(nodeConfig.ApiConfig)
case "PMpanel":
case "PMpanel":
apiClient = pmpanel.New(nodeConfig.ApiConfig)
default:
log.Panicf("Unsupport panel type: %s", nodeConfig.PanelType)
Expand Down Expand Up @@ -139,3 +138,31 @@ func (p *Panel) Close() {
p.Running = false
return
}

func parseConnectionConfig(c *ConnetionConfig) (policy *conf.Policy) {
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
}
}

return
}

0 comments on commit d9fe312

Please sign in to comment.