add session get api (#566)

This commit is contained in:
yubo 2021-01-31 10:48:37 +08:00 committed by GitHub
parent 55718a09e0
commit 7fa84af66a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 5 deletions

1
.gitignore vendored
View File

@ -58,3 +58,4 @@ _test
tmp/ tmp/
main main
Makefile Makefile
src/modules/monapi/plugins/snmp/

3
go.mod
View File

@ -20,6 +20,8 @@ require (
github.com/hpcloud/tail v1.0.0 github.com/hpcloud/tail v1.0.0
github.com/influxdata/influxdb v1.8.0 github.com/influxdata/influxdb v1.8.0
github.com/influxdata/telegraf v1.16.2 github.com/influxdata/telegraf v1.16.2
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65
github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8
github.com/m3db/m3 v0.15.17 github.com/m3db/m3 v0.15.17
github.com/mattn/go-isatty v0.0.12 github.com/mattn/go-isatty v0.0.12
github.com/mattn/go-sqlite3 v1.14.0 // indirect github.com/mattn/go-sqlite3 v1.14.0 // indirect
@ -31,6 +33,7 @@ require (
github.com/prometheus/common v0.9.1 github.com/prometheus/common v0.9.1
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62 // indirect github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62 // indirect
github.com/shirou/gopsutil v3.20.11+incompatible // indirect github.com/shirou/gopsutil v3.20.11+incompatible // indirect
github.com/soniah/gosnmp v1.25.0
github.com/spaolacci/murmur3 v1.1.0 github.com/spaolacci/murmur3 v1.1.0
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1
github.com/streadway/amqp v1.0.0 github.com/streadway/amqp v1.0.0

4
go.sum
View File

@ -550,8 +550,10 @@ github.com/influxdata/tail v1.0.1-0.20200707181643-03a791b270e4/go.mod h1:VeiWgI
github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0= github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0=
github.com/influxdata/telegraf v1.16.2 h1:G988b0+CL2IVDft9V2ZUteKgnbp+eI7vtQ0liPhQKxw= github.com/influxdata/telegraf v1.16.2 h1:G988b0+CL2IVDft9V2ZUteKgnbp+eI7vtQ0liPhQKxw=
github.com/influxdata/telegraf v1.16.2/go.mod h1:LZ/6hlf60cwqGr8phfbRKf8x1HoAoqxoMpTp/iqcNXk= github.com/influxdata/telegraf v1.16.2/go.mod h1:LZ/6hlf60cwqGr8phfbRKf8x1HoAoqxoMpTp/iqcNXk=
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65 h1:vvyMtD5LTJc1W9sQKjDkAWdcg0478CszSdzlHtiAXCY=
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65/go.mod h1:zApaNFpP/bTpQItGZNNUMISDMDAnTXu9UqJ4yT3ocz8= github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65/go.mod h1:zApaNFpP/bTpQItGZNNUMISDMDAnTXu9UqJ4yT3ocz8=
github.com/influxdata/usage-client v0.0.0-20160829180054-6d3895376368/go.mod h1:Wbbw6tYNvwa5dlB6304Sd+82Z3f7PmVZHVKU637d4po= github.com/influxdata/usage-client v0.0.0-20160829180054-6d3895376368/go.mod h1:Wbbw6tYNvwa5dlB6304Sd+82Z3f7PmVZHVKU637d4po=
github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8 h1:W2IgzRCb0L9VzMujq/QuTaZUKcH8096jWwP519mHN6Q=
github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8/go.mod h1:/2NMgWB1DHM1ti/gqhOlg+LJeBVk6FqR5aVGYY0hlwI= github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8/go.mod h1:/2NMgWB1DHM1ti/gqhOlg+LJeBVk6FqR5aVGYY0hlwI=
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ= github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=
github.com/jackc/pgx v3.6.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I= github.com/jackc/pgx v3.6.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
@ -740,6 +742,7 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg= github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU= github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
@ -918,6 +921,7 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9
github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs= github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs=
github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E= github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/soniah/gosnmp v1.25.0 h1:0y8vpjD07NPmnT+wojnUrKkYLX9Fxw1jI4cGTumWugQ=
github.com/soniah/gosnmp v1.25.0/go.mod h1:8YvfZxH388NIIw2A+X5z2Oh97VcNhtmxDLt5QeUzVuQ= github.com/soniah/gosnmp v1.25.0/go.mod h1:8YvfZxH388NIIw2A+X5z2Oh97VcNhtmxDLt5QeUzVuQ=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

View File

@ -58,6 +58,11 @@ func SessionCleanupByCreatedAt(ts int64) error {
logger.Debugf("delete before created_at %d session %d", ts, n) logger.Debugf("delete before created_at %d session %d", ts, n)
return err return err
} }
func SessionCleanupByUpdatedAt(ts int64) error {
n, err := DB["rdb"].Where("updated_at<?", ts).Delete(new(Session))
logger.Debugf("delete before updated_at %d session %d", ts, n)
return err
}
func (s *Session) Update(cols ...string) error { func (s *Session) Update(cols ...string) error {
_, err := DB["rdb"].Where("id=?", s.Sid).Cols(cols...).Update(s) _, err := DB["rdb"].Where("id=?", s.Sid).Cols(cols...).Update(s)
return err return err
@ -115,3 +120,46 @@ func SessionGetUserWithCache(sid string) (*User, error) {
return UserMustGet("username=?", s.Username) return UserMustGet("username=?", s.Username)
} }
func SessionTotal(where string, args ...interface{}) (int64, error) {
session := DB["rdb"].NewSession()
defer session.Close()
if where != "" {
session = session.Where(where, args...)
}
return session.Count(new(Session))
}
func SessionGets(limit, offset int, where string, args ...interface{}) ([]Session, error) {
session := DB["rdb"].Limit(limit, offset).Desc("updated_at")
if where != "" {
session = session.Where(where, args...)
}
var sessions []Session
err := session.Find(&sessions)
return sessions, err
}
func SessionAndTotalGets(query string, limit, offset int) ([]Session, int64, error) {
where := "username != ''"
param := []interface{}{}
if query != "" {
q := "%" + query + "%"
where += " and (username like ? or remote_addr like ?)"
param = append(param, q, q)
}
total, err := SessionTotal(where, param...)
if err != nil {
return []Session{}, total, err
}
list, err := SessionGets(limit, offset, where, param...)
return list, total, err
}

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"net/url" "net/url"
"time" "time"
@ -425,7 +426,7 @@ func lockedUserAccess(cf *models.AuthConfig, user *models.User, loginErr error)
user.UpdatedAt = now user.UpdatedAt = now
return nil return nil
} }
return _e("User is locked") return _e("User is locked, unlock at %dm later", math.Ceil(float64(user.LockedAt+cf.LockTime-now))/60.0)
} }
func frozenUserAccess(cf *models.AuthConfig, user *models.User, loginErr error) error { func frozenUserAccess(cf *models.AuthConfig, user *models.User, loginErr error) error {

View File

@ -102,11 +102,11 @@ var (
"The limited sessions %d": "会话数量限制,最多%d个会话", "The limited sessions %d": "会话数量限制,最多%d个会话",
"Password has been expired": "密码已过期,请重置密码", "Password has been expired": "密码已过期,请重置密码",
"User is inactive": "用户已禁用", "User is inactive": "用户已禁用",
"User is locked": "用户已锁定", "User is locked, unlock at %dm later": "用户已锁定, %d分钟后解锁",
"User is frozen": "用户已休眠", "User is frozen": "用户已休眠",
"User is writen off": "用户已注销", "User is writen off": "用户已注销",
"Minimum password length %d": "密码最小长度 %d", "Minimum password length %d": "密码最小长度%d",
"Password too short (min:%d)": "密码太短 (最小 %d)", "Password too short (min:%d)": "密码太短(最小%d)",
"%s format error": "%s 所填内容不符合规范", "%s format error": "%s 所填内容不符合规范",
"%s %s format error": "%s %s 所填内容不符合规范", "%s %s format error": "%s %s 所填内容不符合规范",
"username too long (max:%d)": "用户名太长 (最长:%d)", "username too long (max:%d)": "用户名太长 (最长:%d)",

View File

@ -205,6 +205,7 @@ func Config(r *gin.Engine) {
// session // session
v1.GET("/sessions/:sid", v1SessionGet) v1.GET("/sessions/:sid", v1SessionGet)
v1.GET("/sessions/:sid/user", v1SessionGetUser) v1.GET("/sessions/:sid/user", v1SessionGetUser)
v1.GET("/sessions", v1SessionListGet)
v1.DELETE("/sessions/:sid", v1SessionDelete) v1.DELETE("/sessions/:sid", v1SessionDelete)
// token // token

View File

@ -689,6 +689,18 @@ func v1SessionGetUser(c *gin.Context) {
renderData(c, user, err) renderData(c, user, err)
} }
func v1SessionListGet(c *gin.Context) {
limit := queryInt(c, "limit", 20)
query := queryStr(c, "query", "")
list, total, err := models.SessionAndTotalGets(query, limit, offset(c, limit))
renderData(c, gin.H{
"list": list,
"total": total,
}, err)
}
func v1SessionDelete(c *gin.Context) { func v1SessionDelete(c *gin.Context) {
sid := urlParamStr(c, "sid") sid := urlParamStr(c, "sid")
logger.Debugf("session del sid %s", sid) logger.Debugf("session del sid %s", sid)

View File

@ -29,7 +29,7 @@ func newDbStorage(cf *config.SessionSection, opts *options) (storage, error) {
case <-opts.ctx.Done(): case <-opts.ctx.Done():
return return
case <-t.C: case <-t.C:
err := models.SessionCleanupByCreatedAt(time.Now().Unix() - lifeTime) err := models.SessionCleanupByUpdatedAt(time.Now().Unix() - lifeTime)
if err != nil { if err != nil {
logger.Errorf("session gc err %s", err) logger.Errorf("session gc err %s", err)
} }