@@ -3,70 +3,66 @@ package controllers
33import (
44 "fmt"
55 "path/filepath"
6- "reflect"
7- "time"
86
97 "github.com/beego/beego/v2/core/logs"
8+ "github.com/beego/beego/v2/core/utils"
109 beego "github.com/beego/beego/v2/server/web"
1110 "github.com/toolkits/file"
1211
1312 "nginx-http-auth/g"
14- "nginx-http-auth/utils"
1513)
1614
1715type ControlController struct {
1816 beego.Controller
1917}
2018
2119func (this * ControlController ) Get () {
20+ // 获取客户端IP
2221 clientIP := this .Ctx .Input .IP ()
23- logtime := time .Now ().Format ("02/Jan/2006 03:04:05" )
2422
25- uname := this .GetSession ("uname" )
26- if uname == nil {
27- this .Ctx .Redirect (302 , "/passport/login" )
23+ // 获取用户Session
24+ username := this .GetSession ("uname" )
25+ if username == nil {
26+ this .Redirect ("/passport/login" , 302 )
2827 return
2928 }
3029
31- controlUsers , err := beego .AppConfig .Strings ("controlUsers" )
30+ // 获取管理用户配置
31+ manageUsers , err := beego .AppConfig .Strings ("manageUsers" )
3232 if err != nil {
33- logs .Error (err .Error ())
34- this .Ctx .Output .SetStatus (500 )
35- this .Ctx .WriteString ("Internal Server Error" )
36- return
33+ logs .Warn (fmt .Sprintf ("%s - get manage users failed: %s" , clientIP , err .Error ()))
34+ manageUsers = []string {"admin" }
3735 }
38- if ! utils .InSlice (uname .(string ), controlUsers ) {
39- logs .Debug (uname .(string ), controlUsers , controlUsers [0 ], reflect .TypeOf (controlUsers [0 ]))
40- this .Ctx .Output .SetStatus (401 )
41- this .Ctx .Output .Body ([]byte ("Not Allowed" ))
42- return
36+ // 管理用户校验
37+ if ! utils .InSlice (username .(string ), manageUsers ) {
38+ logs .Warn (fmt .Sprintf ("%s - %s - access to control API was denied" , clientIP , username ))
39+ this .Abort ("403" )
4340 }
4441
42+ // 获取管理类型
4543 control := this .Ctx .Input .Param (":control" )
4644 switch control {
47- case "version" :
48- this .Ctx .Output .Body ([]byte (g .VERSION ))
49- case "health" :
50- this .Ctx .Output .Body ([]byte ("ok" ))
51- case "config" :
45+ case "version" : // 获取版本
46+ _ = this .Ctx .Output .Body ([]byte (g .VERSION ))
47+ case "health" : // 探活
48+ _ = this .Ctx .Output .Body ([]byte ("ok" ))
49+ case "config" : // 获取配置信息
5250 var json map [string ]interface {}
5351 err = beego .AppConfig .Unmarshaler ("" , & json )
5452 if err != nil {
55- logs .Error (err .Error ())
56- this .Ctx .Output .SetStatus (500 )
57- this .Ctx .WriteString ("Internal Server Error" )
58- return
53+ logs .Error (fmt .Sprintf ("%s - get config info failed: %s" , clientIP , err .Error ()))
54+ this .Abort ("550" )
5955 }
6056 this .Data ["json" ] = json
61- this .ServeJSON ()
62- case "reload" :
57+ _ = this .ServeJSON ()
58+ case "reload" : // 重新加载配置
6359 err := beego .LoadAppConfig ("ini" , filepath .Join (file .SelfDir (), "conf/app.conf" ))
6460 if err != nil {
65- logs .Error (fmt .Sprintf ("%s - - [%s] Config reload failed: %s" , clientIP , logtime , err .Error ()))
66- this .Ctx .Output .Body ([]byte ("config reload failed" ))
61+ logs .Error (fmt .Sprintf ("%s - config reload failed: %s" , clientIP , err .Error ()))
62+ _ = this .Ctx .Output .Body ([]byte ("config reload failed" ))
6763 } else {
68- logs .Notice (fmt .Sprintf ("%s - - [%s] Config Reloaded" , clientIP , logtime ))
69- this .Ctx .Output .Body ([]byte ("config reloaded" ))
64+ logs .Info (fmt .Sprintf ("%s - config Reloaded" , clientIP ))
65+ _ = this .Ctx .Output .Body ([]byte ("config reloaded" ))
7066 }
7167 }
7268}
0 commit comments