1.optimize Tg bot notify info

2.fixed a bug when ParseIn twith  Tg bot chat id
This commit is contained in:
FranzKafkayu
2022-04-22 23:55:25 +08:00
parent 178716025f
commit b616a5b138
3 changed files with 26 additions and 6 deletions

View File

@@ -58,14 +58,15 @@ func (a *IndexController) login(c *gin.Context) {
return return
} }
user := a.userService.CheckUser(form.Username, form.Password) user := a.userService.CheckUser(form.Username, form.Password)
timeStr := time.Now().Format("2006-01-02 15:04:05")
if user == nil { if user == nil {
job.NewStatsNotifyJob().UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0)
logger.Infof("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password) logger.Infof("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password)
pureJsonMsg(c, false, "用户名或密码错误") pureJsonMsg(c, false, "用户名或密码错误")
return return
} else { } else {
timeStr := time.Now().Format("2006-01-02 15:04:05")
logger.Infof("%s login success,Ip Address:%s\n", form.Username, getRemoteIp(c)) logger.Infof("%s login success,Ip Address:%s\n", form.Username, getRemoteIp(c))
job.NewStatsNotifyJob().UserLoginNotify(form.Username, getRemoteIp(c), timeStr) job.NewStatsNotifyJob().UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 1)
} }
err = session.SetLoginUser(c, user) err = session.SetLoginUser(c, user)

View File

@@ -4,6 +4,9 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"time"
"x-ui/logger" "x-ui/logger"
"x-ui/util/common" "x-ui/util/common"
"x-ui/web/service" "x-ui/web/service"
@@ -11,6 +14,13 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
type LoginStatus byte
const (
LoginSuccess LoginStatus = 1
LoginFail LoginStatus = 0
)
type StatsNotifyJob struct { type StatsNotifyJob struct {
enable bool enable bool
xrayService service.XrayService xrayService service.XrayService
@@ -96,12 +106,17 @@ func (j *StatsNotifyJob) Run() {
//NOTE:If there no any sessions here,need to notify here //NOTE:If there no any sessions here,need to notify here
//TODO:分节点推送,自动转化格式 //TODO:分节点推送,自动转化格式
for _, inbound := range inbouds { for _, inbound := range inbouds {
info += fmt.Sprintf("节点名称:%s\r\n端口:%d\r\n上行流量↑:%s\r\n下行流量↓:%s\r\n总流量:%s\r\n \r\n", inbound.Remark, inbound.Port, common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down), common.FormatTraffic((inbound.Up + inbound.Down))) info += fmt.Sprintf("节点名称:%s\r\n端口:%d\r\n上行流量↑:%s\r\n下行流量↓:%s\r\n总流量:%s\r\n", inbound.Remark, inbound.Port, common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down), common.FormatTraffic((inbound.Up + inbound.Down)))
if inbound.ExpiryTime == 0 {
info += fmt.Sprintf("到期时间:无限期\r\n \r\n")
} else {
info += fmt.Sprintf("到期时间:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
}
} }
j.SendMsgToTgbot(info) j.SendMsgToTgbot(info)
} }
func (j *StatsNotifyJob) UserLoginNotify(username string, ip string, time string) { func (j *StatsNotifyJob) UserLoginNotify(username string, ip string, time string, status LoginStatus) {
if username == "" || ip == "" || time == "" { if username == "" || ip == "" || time == "" {
logger.Warning("UserLoginNotify failed,invalid info") logger.Warning("UserLoginNotify failed,invalid info")
return return
@@ -113,7 +128,11 @@ func (j *StatsNotifyJob) UserLoginNotify(username string, ip string, time string
fmt.Println("get hostname error:", err) fmt.Println("get hostname error:", err)
return return
} }
msg = fmt.Sprintf("面板登录提醒\r\n主机名称:%s\r\n", name) if status == LoginSuccess {
msg = fmt.Sprintf("面板登录成功提醒\r\n主机名称:%s\r\n", name)
} else if status == LoginFail {
msg = fmt.Sprintf("面板登录失败提醒\r\n主机名称:%s\r\n", name)
}
msg += fmt.Sprintf("时间:%s\r\n", time) msg += fmt.Sprintf("时间:%s\r\n", time)
msg += fmt.Sprintf("用户:%s\r\n", username) msg += fmt.Sprintf("用户:%s\r\n", username)
msg += fmt.Sprintf("IP:%s\r\n", ip) msg += fmt.Sprintf("IP:%s\r\n", ip)

View File

@@ -76,7 +76,7 @@ func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
fieldV := v.FieldByName(field.Name) fieldV := v.FieldByName(field.Name)
switch t := fieldV.Interface().(type) { switch t := fieldV.Interface().(type) {
case int: case int:
n, err := strconv.ParseInt(value, 10, 32) n, err := strconv.ParseInt(value, 10, 64)
if err != nil { if err != nil {
return err return err
} }