Skip to content

Commit

Permalink
修复首次启动需要等待加载用户表的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
theresaarcher committed Aug 18, 2024
1 parent eb440e3 commit 4073541
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func (c *serverConfig) fillTrafficLogger(hyConfig *server.Config) error {
}
go runTrafficStatsServer(c.TrafficStats.Listen, tss)
} else {
go auth.UpdateUsers(fmt.Sprintf("%s?token=%s&node_id=%d&node_type=hysteria2&act=user", c.V2RaySocks.ApiHost, c.V2RaySocks.ApiKey, c.V2RaySocks.NodeID), time.Second*5, nil)
go auth.UpdateUsers(fmt.Sprintf("%s?token=%s&node_id=%d&node_type=hysteria2&act=user", c.V2RaySocks.ApiHost, c.V2RaySocks.ApiKey, c.V2RaySocks.NodeID), time.Second*60, nil)
}
return nil
}
Expand Down
31 changes: 28 additions & 3 deletions extras/auth/v2raysocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,47 @@ func getUserList(url string, etag string) ([]User, string, error) {
}

func UpdateUsers(url string, interval time.Duration, trafficlogger server.TrafficLogger) {

fmt.Println("用户列表自动更新服务已激活")
ticker := time.NewTicker(interval)
defer ticker.Stop()

var etag string

// 立即执行一次 getUserList
userList, newEtag, err := getUserList(url, etag)
if err != nil {
fmt.Println("Error:", err)
return // 直接返回,不进入循环
}

// 处理首次获取的用户列表
if newEtag != "" && newEtag != etag {
etag = newEtag

lock.Lock()
newUsersMap := make(map[string]User)
for _, user := range userList {
newUsersMap[user.UUID] = user
}
if trafficlogger != nil {
for uuid := range usersMap {
if _, exists := newUsersMap[uuid]; !exists {
trafficlogger.LogOnlineState(strconv.Itoa(usersMap[uuid].ID), false)
}
}
}

usersMap = newUsersMap
lock.Unlock()
}

for range ticker.C {
userList, newEtag, err := getUserList(url, etag)
if err != nil {
fmt.Println("Error:", err)
continue
}

// 只有在服务器返回的新ETag与当前ETag不同时才更新用户列表
if newEtag != "" && newEtag != etag {
etag = newEtag

Expand All @@ -102,7 +128,6 @@ func UpdateUsers(url string, interval time.Duration, trafficlogger server.Traffi
lock.Unlock()
}
}

}

// 验证代码
Expand Down

0 comments on commit 4073541

Please sign in to comment.