update conf, fix tcp count

This commit is contained in:
yuzuki999 2023-05-19 09:49:31 +08:00
parent 97b7322492
commit 179de8ab2a
6 changed files with 57 additions and 43 deletions

View File

@ -10,13 +10,13 @@ import (
) )
type Conf struct { type Conf struct {
LogConfig *LogConfig `yaml:"Log"` LogConfig *LogConfig `yaml:"Log"`
DnsConfigPath string `yaml:"DnsConfigPath"` DnsConfigPath string `yaml:"DnsConfigPath"`
InboundConfigPath string `yaml:"InboundConfigPath"` InboundConfigPath string `yaml:"InboundConfigPath"`
OutboundConfigPath string `yaml:"OutboundConfigPath"` OutboundConfigPath string `yaml:"OutboundConfigPath"`
RouteConfigPath string `yaml:"RouteConfigPath"` RouteConfigPath string `yaml:"RouteConfigPath"`
ConnectionConfig *ConnetionConfig `yaml:"ConnectionConfig"` ConnectionConfig *ConnectionConfig `yaml:"ConnectionConfig"`
NodesConfig []*NodeConfig `yaml:"Nodes"` NodesConfig []*NodeConfig `yaml:"Nodes"`
} }
func New() *Conf { func New() *Conf {
@ -26,7 +26,7 @@ func New() *Conf {
InboundConfigPath: "", InboundConfigPath: "",
OutboundConfigPath: "", OutboundConfigPath: "",
RouteConfigPath: "", RouteConfigPath: "",
ConnectionConfig: NewConnetionConfig(), ConnectionConfig: NewConnectionConfig(),
NodesConfig: []*NodeConfig{}, NodesConfig: []*NodeConfig{},
} }
} }
@ -55,17 +55,15 @@ func (p *Conf) Watch(filePath string, reload func()) error {
defer watcher.Close() defer watcher.Close()
for { for {
select { select {
case event := <-watcher.Events: case <-watcher.Events:
if event.Has(fsnotify.Write) { log.Println("config dir changed, reloading...")
log.Println("config dir changed, reloading...") *p = *New()
*p = *New() err := p.LoadFromPath(filePath)
err := p.LoadFromPath(filePath) if err != nil {
if err != nil { log.Printf("reload config error: %s", err)
log.Printf("reload config error: %s", err)
}
log.Println("reload config success")
reload()
} }
reload()
log.Println("reload config success")
case err := <-watcher.Errors: case err := <-watcher.Errors:
if err != nil { if err != nil {
log.Printf("File watcher error: %s", err) log.Printf("File watcher error: %s", err)

View File

@ -1,6 +1,6 @@
package conf package conf
type ConnetionConfig struct { type ConnectionConfig struct {
Handshake uint32 `yaml:"handshake"` Handshake uint32 `yaml:"handshake"`
ConnIdle uint32 `yaml:"connIdle"` ConnIdle uint32 `yaml:"connIdle"`
UplinkOnly uint32 `yaml:"uplinkOnly"` UplinkOnly uint32 `yaml:"uplinkOnly"`
@ -8,8 +8,8 @@ type ConnetionConfig struct {
BufferSize int32 `yaml:"bufferSize"` BufferSize int32 `yaml:"bufferSize"`
} }
func NewConnetionConfig() *ConnetionConfig { func NewConnectionConfig() *ConnectionConfig {
return &ConnetionConfig{ return &ConnectionConfig{
Handshake: 4, Handshake: 4,
ConnIdle: 30, ConnIdle: 30,
UplinkOnly: 2, UplinkOnly: 2,

View File

@ -15,21 +15,21 @@ type ApiConfig struct {
} }
type ControllerConfig struct { type ControllerConfig struct {
ListenIP string `yaml:"ListenIP"` ListenIP string `yaml:"ListenIP"`
SendIP string `yaml:"SendIP"` SendIP string `yaml:"SendIP"`
EnableDNS bool `yaml:"EnableDNS"` EnableDNS bool `yaml:"EnableDNS"`
DNSType string `yaml:"DNSType"` DNSType string `yaml:"DNSType"`
EnableVless bool `yaml:"EnableVless"` EnableVless bool `yaml:"EnableVless"`
EnableTls bool `yaml:"EnableTls"` EnableTls bool `yaml:"EnableTls"`
LimitConfig LimitConfig `yaml:"LimitConfig"` LimitConfig LimitConfig `yaml:"LimitConfig"`
DisableUploadTraffic bool `yaml:"DisableUploadTraffic"` DisableUploadTraffic bool `yaml:"DisableUploadTraffic"`
DisableGetRule bool `yaml:"DisableGetRule"` DisableGetRule bool `yaml:"DisableGetRule"`
EnableProxyProtocol bool `yaml:"EnableProxyProtocol"` EnableProxyProtocol bool `yaml:"EnableProxyProtocol"`
EnableFallback bool `yaml:"EnableFallback"` EnableFallback bool `yaml:"EnableFallback"`
DisableIVCheck bool `yaml:"DisableIVCheck"` DisableIVCheck bool `yaml:"DisableIVCheck"`
DisableSniffing bool `yaml:"DisableSniffing"` DisableSniffing bool `yaml:"DisableSniffing"`
FallBackConfigs []*FallBackConfig `yaml:"FallBackConfigs"` FallBackConfigs []FallBackConfig `yaml:"FallBackConfigs"`
CertConfig *CertConfig `yaml:"CertConfig"` CertConfig *CertConfig `yaml:"CertConfig"`
} }
type LimitConfig struct { type LimitConfig struct {

View File

@ -33,7 +33,7 @@ func New(c *conf.Conf) *Core {
return &Core{Server: getCore(c)} return &Core{Server: getCore(c)}
} }
func parseConnectionConfig(c *conf.ConnetionConfig) (policy *coreConf.Policy) { func parseConnectionConfig(c *conf.ConnectionConfig) (policy *coreConf.Policy) {
policy = &coreConf.Policy{ policy = &coreConf.Policy{
StatsUserUplink: true, StatsUserUplink: true,
StatsUserDownlink: true, StatsUserDownlink: true,

View File

@ -27,18 +27,23 @@ func (c *ConnLimiter) AddConnCount(user string, ip string, isTcp bool) (limit bo
if c.connLimit != 0 { if c.connLimit != 0 {
if v, ok := c.count.Load(user); ok { if v, ok := c.count.Load(user); ok {
if v.(int) >= c.connLimit { if v.(int) >= c.connLimit {
// over connection limit
return true return true
} else if isTcp { // tcp protocol } else if isTcp {
// tcp protocol
// connection count add
c.count.Store(user, v.(int)+1) 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) c.count.Store(user, 1)
} }
} }
if c.ipLimit == 0 { if c.ipLimit == 0 {
return false return false
} }
// default user map // first user map
ipMap := new(sync.Map) ipMap := new(sync.Map)
if c.realtime { if c.realtime {
if isTcp { 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 { if online, ok := ips.Load(ip); ok {
// online ip // online ip
if c.realtime { if c.realtime {
if online.(int)%2 == 0 && isTcp { if isTcp {
// count add // tcp count add
ips.Store(ip, online.(int)+2) ips.Store(ip, online.(int)+2)
} }
} else { } else {
// update connect time for not realtime
ips.Store(ip, time.Now()) ips.Store(ip, time.Now())
} }
} else { } else {
@ -75,6 +81,7 @@ func (c *ConnLimiter) AddConnCount(user string, ip string, isTcp bool) (limit bo
return true return true
}) })
if limit { if limit {
// over ip limit
return return
} }
if c.realtime { if c.realtime {

View File

@ -3,6 +3,7 @@ package limiter
import ( import (
"sync" "sync"
"testing" "testing"
"time"
) )
var c *ConnLimiter var c *ConnLimiter
@ -23,13 +24,21 @@ func TestConnLimiter_DelConnCount(t *testing.T) {
t.Log(c.AddConnCount("1", "2", true)) 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", "1", false))
t.Log(c.AddConnCount("1", "2", false)) t.Log(c.AddConnCount("1", "2", false))
c.ClearOnlineIP() c.ClearOnlineIP()
t.Log(c.AddConnCount("1", "2", true)) t.Log(c.AddConnCount("1", "2", true))
c.DelConnCount("1", "2") c.DelConnCount("1", "2")
t.Log(c.AddConnCount("1", "1", false)) 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) { func BenchmarkConnLimiter(b *testing.B) {