增加连接信息
This commit is contained in:
parent
7ac73b9489
commit
a5be141bac
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/bolkedebruin/gokrb5/v8/keytab"
|
||||
"github.com/bolkedebruin/gokrb5/v8/service"
|
||||
@ -23,6 +24,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -65,6 +67,27 @@ func initOIDC(callbackUrl *url.URL) *web.OIDC {
|
||||
return o.New()
|
||||
}
|
||||
|
||||
// 定时记录连接用户信息
|
||||
func startConnectionLogger(interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
connections := protocol.GetActiveConnections()
|
||||
if len(connections) > 0 {
|
||||
connData, err := json.Marshal(connections)
|
||||
if err != nil {
|
||||
log.Printf("连接信息序列化失败: %v", err)
|
||||
continue
|
||||
}
|
||||
log.Printf("当前活跃连接数: %d, 连接详情: %s", len(connections), string(connData))
|
||||
} else {
|
||||
log.Printf("当前无活跃连接")
|
||||
}
|
||||
}
|
||||
}()
|
||||
log.Printf("启动连接信息记录器,间隔时间: %v", interval)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// load config
|
||||
_, err := flags.Parse(&opts)
|
||||
@ -125,6 +148,10 @@ func main() {
|
||||
h := w.NewHandler()
|
||||
|
||||
log.Printf("Starting remote desktop gateway server")
|
||||
|
||||
// 启动连接信息记录器
|
||||
startConnectionLogger(10 * time.Second)
|
||||
|
||||
cfg := &tls.Config{}
|
||||
|
||||
// configure tls security
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package protocol
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Connections map[string]*Monitor
|
||||
|
||||
@ -41,6 +44,40 @@ func Disconnect(id string) error {
|
||||
return fmt.Errorf("%s connection does not exist", id)
|
||||
}
|
||||
|
||||
// GetActiveConnections 返回所有当前活跃的连接信息
|
||||
func GetActiveConnections() []map[string]interface{} {
|
||||
connections := []map[string]interface{}{}
|
||||
|
||||
for id, monitor := range Connections {
|
||||
tunnel := monitor.Tunnel
|
||||
if tunnel == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 计算连接持续时间
|
||||
duration := time.Since(tunnel.ConnectedOn)
|
||||
|
||||
// 收集每个连接的关键信息
|
||||
connInfo := map[string]interface{}{
|
||||
"id": id,
|
||||
"rdgId": tunnel.RDGId,
|
||||
"targetServer": tunnel.TargetServer,
|
||||
"remoteAddr": tunnel.RemoteAddr,
|
||||
"userName": tunnel.User.UserName(),
|
||||
"domain": tunnel.User.Domain(),
|
||||
"connectedOn": tunnel.ConnectedOn,
|
||||
"lastSeen": tunnel.LastSeen,
|
||||
"bytesSent": tunnel.BytesSent,
|
||||
"bytesReceived": tunnel.BytesReceived,
|
||||
"durationSecs": int(duration.Seconds()),
|
||||
}
|
||||
|
||||
connections = append(connections, connInfo)
|
||||
}
|
||||
|
||||
return connections
|
||||
}
|
||||
|
||||
// CalculateSpeedPerSecond calculate moving average.
|
||||
/*
|
||||
func CalculateSpeedPerSecond(connId string) (in int, out int) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user