diff --git a/common/stats.go b/common/stats.go new file mode 100644 index 0000000..23ecfb9 --- /dev/null +++ b/common/stats.go @@ -0,0 +1,12 @@ +package common + +var OnlineUsers int + +func init(){ + OnlineUsers = 0 +} + +func OnlineStatUpdate(t int) { + OnlineUsers += t +} + diff --git a/gpcm/main.go b/gpcm/main.go index 9c12f5e..6508e60 100644 --- a/gpcm/main.go +++ b/gpcm/main.go @@ -122,6 +122,7 @@ func handleRequest(conn net.Conn) { conn.Write([]byte(payload)) logging.Notice(session.ModuleName, "Connection established from", conn.RemoteAddr()) + common.OnlineStatUpdate(1) // Here we go into the listening loop for { @@ -132,10 +133,12 @@ func handleRequest(conn net.Conn) { if errors.Is(err, io.EOF) { // Client closed connection, terminate. logging.Notice(session.ModuleName, "Client closed connection") + common.OnlineStatUpdate(-1) return } logging.Error(session.ModuleName, "Connection lost") + common.OnlineStatUpdate(-1) return } diff --git a/nas/online.go b/nas/online.go new file mode 100644 index 0000000..0560aa4 --- /dev/null +++ b/nas/online.go @@ -0,0 +1,13 @@ +package nas + +import ( + "strconv" + "wwfc/common" + "net/http" +) + +func returnOnlineStats(w http.ResponseWriter) { + w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Length", strconv.Itoa(len(strconv.Itoa(common.OnlineUsers)))) + w.Write([]byte(strconv.Itoa(common.OnlineUsers))) +} diff --git a/nas/route.go b/nas/route.go index 1a3a3bf..b693278 100644 --- a/nas/route.go +++ b/nas/route.go @@ -73,6 +73,12 @@ func (route *Route) Handle() http.Handler { return } + // Check for /online + if strings.HasPrefix(r.URL.String(), "/online") { + returnOnlineStats(w) + return + } + err := r.ParseForm() if err != nil { logging.Error(moduleName, "Failed to parse form")