Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions api/panel/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"encoding/json/jsontext"
"encoding/json/v2"

log "github.com/sirupsen/logrus"
)

type OnlineUser struct {
Expand Down Expand Up @@ -84,26 +86,44 @@ func (c *Client) GetUserList() ([]UserInfo, error) {
}

func (c *Client) GetUserAlive() (map[int]int, error) {
c.AliveMap = &AliveMap{}
c.AliveMap.Alive = make(map[int]int)
/*const path = "/v1/server/alivelist"
r, err := c.client.R().
const path = "/v1/server/alivelist"
c.AliveMap = &AliveMap{Alive: make(map[int]int)}

r, err := c.Client.R().
ForceContentType("application/json").
Get(path)
if err != nil || r.StatusCode() >= 399 {
c.AliveMap.Alive = make(map[int]int)
if err != nil || r == nil {
log.WithFields(log.Fields{
"path": path,
"err": err,
}).Warn("alivelist request failed; falling back to empty map")
return c.AliveMap.Alive, nil
}
if r == nil || r.RawResponse == nil {
fmt.Printf("received nil response or raw response")
c.AliveMap.Alive = make(map[int]int)
if r.StatusCode() == 304 {
return c.AliveMap.Alive, nil
}
defer r.RawResponse.Body.Close()
if err := json.Unmarshal(r.Body(), c.AliveMap); err != nil {
//fmt.Printf("unmarshal user alive list error: %s", err)
c.AliveMap.Alive = make(map[int]int)
if r.StatusCode() >= 400 {
log.WithFields(log.Fields{
"path": path,
"status": r.StatusCode(),
"body": string(r.Body()),
}).Warn("alivelist returned non-2xx; falling back to empty map")
return c.AliveMap.Alive, nil
}
body := &AliveMap{}
if err := json.Unmarshal(r.Body(), body); err != nil {
log.WithFields(log.Fields{
"path": path,
"err": err,
}).Warn("alivelist response unmarshal failed; falling back to empty map")
return c.AliveMap.Alive, nil
}
if body.Alive == nil {
log.WithField("path", path).Warn("alivelist response missing 'alive' field; falling back to empty map")
return c.AliveMap.Alive, nil
}
*/
return c.AliveMap.Alive, nil
c.AliveMap = body
return body.Alive, nil
}

type ServerPushUserTrafficRequest struct {
Expand Down
52 changes: 49 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ func (n *Node) StartNodes(apiConfig *conf.ServerApiConfig, core vCore.Core) erro
return err
}
var nodeinfos []*panel.NodeInfo
n.controllers = make([]*Controller, len(nodeinfos))
n.controllers = make([]*Controller, len(protocols))
pushI, pullI := resolveIntervals(basic)
for i, p := range protocols {
node := &panel.NodeInfo{
Id: apiConfig.ServerId,
Type: p.Type,
PushInterval: time.Duration(basic.PushInterval.(int)) * time.Second,
PullInterval: time.Duration(basic.PullInterval.(int)) * time.Second,
PushInterval: pushI,
PullInterval: pullI,
Common: &panel.CommonNode{
Protocol: p.Type,
},
Expand Down Expand Up @@ -233,3 +234,48 @@ func (n *Node) Close() {
}
n.controllers = nil
}

// resolveIntervals returns push/pull intervals from BasicConfig with fallbacks.
// JSON decoders default numeric fields to float64, so the original .(int)
// assertion on basic.PushInterval would panic. Also tolerates a nil basic
// (server without the basic field, or pre-fix server build).
func resolveIntervals(basic *panel.BasicConfig) (push, pull time.Duration) {
const defaultPush = 30 * time.Second
const defaultPull = 60 * time.Second
if basic == nil {
return defaultPush, defaultPull
}
push = intervalSec(basic.PushInterval, defaultPush)
pull = intervalSec(basic.PullInterval, defaultPull)
return
}

func intervalSec(v any, def time.Duration) time.Duration {
if v == nil {
return def
}
switch x := v.(type) {
case int:
if x <= 0 {
return def
}
return time.Duration(x) * time.Second
case int64:
if x <= 0 {
return def
}
return time.Duration(x) * time.Second
case float64:
if x <= 0 {
return def
}
return time.Duration(x) * time.Second
Comment on lines +268 to +272
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clamp float intervals before converting to Duration

When push_interval/pull_interval comes back as a non-integer float (for example 0.5), this cast truncates to 0 and returns a zero interval instead of falling back to defaults. In this codebase, a zero interval feeds time.AfterFunc in the task scheduler, which can create a tight immediate-reschedule loop and high CPU usage rather than a safe polling cadence.

Useful? React with 👍 / 👎.

case string:
n, err := strconv.Atoi(x)
if err != nil || n <= 0 {
return def
}
return time.Duration(n) * time.Second
}
return def
}
Loading