From 179de8ab2a645d9a6645cdcdc7b0fcc545b44ffd Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Fri, 19 May 2023 09:49:31 +0800 Subject: [PATCH] update conf, fix tcp count --- conf/conf.go | 34 ++++++++++++++++------------------ conf/connetion.go | 6 +++--- conf/node.go | 30 +++++++++++++++--------------- core/core.go | 2 +- limiter/conn.go | 17 ++++++++++++----- limiter/conn_test.go | 11 ++++++++++- 6 files changed, 57 insertions(+), 43 deletions(-) diff --git a/conf/conf.go b/conf/conf.go index 37def33..03885d9 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -10,13 +10,13 @@ import ( ) type Conf struct { - LogConfig *LogConfig `yaml:"Log"` - DnsConfigPath string `yaml:"DnsConfigPath"` - InboundConfigPath string `yaml:"InboundConfigPath"` - OutboundConfigPath string `yaml:"OutboundConfigPath"` - RouteConfigPath string `yaml:"RouteConfigPath"` - ConnectionConfig *ConnetionConfig `yaml:"ConnectionConfig"` - NodesConfig []*NodeConfig `yaml:"Nodes"` + LogConfig *LogConfig `yaml:"Log"` + DnsConfigPath string `yaml:"DnsConfigPath"` + InboundConfigPath string `yaml:"InboundConfigPath"` + OutboundConfigPath string `yaml:"OutboundConfigPath"` + RouteConfigPath string `yaml:"RouteConfigPath"` + ConnectionConfig *ConnectionConfig `yaml:"ConnectionConfig"` + NodesConfig []*NodeConfig `yaml:"Nodes"` } func New() *Conf { @@ -26,7 +26,7 @@ func New() *Conf { InboundConfigPath: "", OutboundConfigPath: "", RouteConfigPath: "", - ConnectionConfig: NewConnetionConfig(), + ConnectionConfig: NewConnectionConfig(), NodesConfig: []*NodeConfig{}, } } @@ -55,17 +55,15 @@ func (p *Conf) Watch(filePath string, reload func()) error { defer watcher.Close() for { select { - case event := <-watcher.Events: - if event.Has(fsnotify.Write) { - log.Println("config dir changed, reloading...") - *p = *New() - err := p.LoadFromPath(filePath) - if err != nil { - log.Printf("reload config error: %s", err) - } - log.Println("reload config success") - reload() + case <-watcher.Events: + log.Println("config dir changed, reloading...") + *p = *New() + err := p.LoadFromPath(filePath) + if err != nil { + log.Printf("reload config error: %s", err) } + reload() + log.Println("reload config success") case err := <-watcher.Errors: if err != nil { log.Printf("File watcher error: %s", err) diff --git a/conf/connetion.go b/conf/connetion.go index 228b800..a34c49f 100644 --- a/conf/connetion.go +++ b/conf/connetion.go @@ -1,6 +1,6 @@ package conf -type ConnetionConfig struct { +type ConnectionConfig struct { Handshake uint32 `yaml:"handshake"` ConnIdle uint32 `yaml:"connIdle"` UplinkOnly uint32 `yaml:"uplinkOnly"` @@ -8,8 +8,8 @@ type ConnetionConfig struct { BufferSize int32 `yaml:"bufferSize"` } -func NewConnetionConfig() *ConnetionConfig { - return &ConnetionConfig{ +func NewConnectionConfig() *ConnectionConfig { + return &ConnectionConfig{ Handshake: 4, ConnIdle: 30, UplinkOnly: 2, diff --git a/conf/node.go b/conf/node.go index 276b297..ccde4cf 100644 --- a/conf/node.go +++ b/conf/node.go @@ -15,21 +15,21 @@ type ApiConfig struct { } type ControllerConfig struct { - ListenIP string `yaml:"ListenIP"` - SendIP string `yaml:"SendIP"` - EnableDNS bool `yaml:"EnableDNS"` - DNSType string `yaml:"DNSType"` - EnableVless bool `yaml:"EnableVless"` - EnableTls bool `yaml:"EnableTls"` - LimitConfig LimitConfig `yaml:"LimitConfig"` - DisableUploadTraffic bool `yaml:"DisableUploadTraffic"` - DisableGetRule bool `yaml:"DisableGetRule"` - EnableProxyProtocol bool `yaml:"EnableProxyProtocol"` - EnableFallback bool `yaml:"EnableFallback"` - DisableIVCheck bool `yaml:"DisableIVCheck"` - DisableSniffing bool `yaml:"DisableSniffing"` - FallBackConfigs []*FallBackConfig `yaml:"FallBackConfigs"` - CertConfig *CertConfig `yaml:"CertConfig"` + ListenIP string `yaml:"ListenIP"` + SendIP string `yaml:"SendIP"` + EnableDNS bool `yaml:"EnableDNS"` + DNSType string `yaml:"DNSType"` + EnableVless bool `yaml:"EnableVless"` + EnableTls bool `yaml:"EnableTls"` + LimitConfig LimitConfig `yaml:"LimitConfig"` + DisableUploadTraffic bool `yaml:"DisableUploadTraffic"` + DisableGetRule bool `yaml:"DisableGetRule"` + EnableProxyProtocol bool `yaml:"EnableProxyProtocol"` + EnableFallback bool `yaml:"EnableFallback"` + DisableIVCheck bool `yaml:"DisableIVCheck"` + DisableSniffing bool `yaml:"DisableSniffing"` + FallBackConfigs []FallBackConfig `yaml:"FallBackConfigs"` + CertConfig *CertConfig `yaml:"CertConfig"` } type LimitConfig struct { diff --git a/core/core.go b/core/core.go index 6ab555d..57fcb1b 100644 --- a/core/core.go +++ b/core/core.go @@ -33,7 +33,7 @@ func New(c *conf.Conf) *Core { return &Core{Server: getCore(c)} } -func parseConnectionConfig(c *conf.ConnetionConfig) (policy *coreConf.Policy) { +func parseConnectionConfig(c *conf.ConnectionConfig) (policy *coreConf.Policy) { policy = &coreConf.Policy{ StatsUserUplink: true, StatsUserDownlink: true, diff --git a/limiter/conn.go b/limiter/conn.go index 8ce88ae..d157ef4 100644 --- a/limiter/conn.go +++ b/limiter/conn.go @@ -27,18 +27,23 @@ func (c *ConnLimiter) AddConnCount(user string, ip string, isTcp bool) (limit bo if c.connLimit != 0 { if v, ok := c.count.Load(user); ok { if v.(int) >= c.connLimit { + // over connection limit return true - } else if isTcp { // tcp protocol + } else if isTcp { + // tcp protocol + // connection count add c.count.Store(user, v.(int)+1) } - } else if isTcp { // tcp protocol + } else if isTcp { + // tcp protocol + // store connection count c.count.Store(user, 1) } } if c.ipLimit == 0 { return false } - // default user map + // first user map ipMap := new(sync.Map) if c.realtime { if isTcp { @@ -57,11 +62,12 @@ func (c *ConnLimiter) AddConnCount(user string, ip string, isTcp bool) (limit bo if online, ok := ips.Load(ip); ok { // online ip if c.realtime { - if online.(int)%2 == 0 && isTcp { - // count add + if isTcp { + // tcp count add ips.Store(ip, online.(int)+2) } } else { + // update connect time for not realtime ips.Store(ip, time.Now()) } } else { @@ -75,6 +81,7 @@ func (c *ConnLimiter) AddConnCount(user string, ip string, isTcp bool) (limit bo return true }) if limit { + // over ip limit return } if c.realtime { diff --git a/limiter/conn_test.go b/limiter/conn_test.go index 7342df0..2ca55f4 100644 --- a/limiter/conn_test.go +++ b/limiter/conn_test.go @@ -3,6 +3,7 @@ package limiter import ( "sync" "testing" + "time" ) var c *ConnLimiter @@ -23,13 +24,21 @@ func TestConnLimiter_DelConnCount(t *testing.T) { t.Log(c.AddConnCount("1", "2", true)) } -func TestConnLimiter_ClearPacketOnlineIP(t *testing.T) { +func TestConnLimiter_ClearOnlineIP(t *testing.T) { t.Log(c.AddConnCount("1", "1", false)) t.Log(c.AddConnCount("1", "2", false)) c.ClearOnlineIP() t.Log(c.AddConnCount("1", "2", true)) c.DelConnCount("1", "2") t.Log(c.AddConnCount("1", "1", false)) + // not realtime + c.realtime = false + t.Log(c.AddConnCount("3", "2", true)) + c.ClearOnlineIP() + t.Log(c.ip.Load("3")) + time.Sleep(time.Minute) + c.ClearOnlineIP() + t.Log(c.ip.Load("3")) } func BenchmarkConnLimiter(b *testing.B) {