fix(be):使用next-terminal 自定义的日志
This commit is contained in:
@ -4,10 +4,10 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/global"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func LoginLogPagingEndpoint(c echo.Context) error {
|
||||
@ -35,7 +35,7 @@ func LoginLogDeleteEndpoint(c echo.Context) error {
|
||||
token := split[i]
|
||||
global.Cache.Delete(token)
|
||||
if err := userService.Logout(token); err != nil {
|
||||
logrus.WithError(err).Error("Cache Delete Failed")
|
||||
log.WithError(err).Error("Cache Delete Failed")
|
||||
}
|
||||
}
|
||||
if err := loginLogRepository.DeleteByIdIn(split); err != nil {
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/global"
|
||||
"next-terminal/server/log"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/service"
|
||||
@ -17,7 +17,6 @@ import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
@ -54,13 +53,13 @@ func SetupRoutes(db *gorm.DB) *echo.Echo {
|
||||
InitService()
|
||||
|
||||
if err := InitDBData(); err != nil {
|
||||
logrus.WithError(err).Error("初始化数据异常")
|
||||
log.WithError(err).Error("初始化数据异常")
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
e.Logger = log.GetEchoLogger()
|
||||
|
||||
//e.Logger = log.GetEchoLogger()
|
||||
e.Use(log.Hook())
|
||||
e.File("/", "web/build/index.html")
|
||||
e.File("/asciinema.html", "web/build/asciinema.html")
|
||||
e.File("/asciinema-player.js", "web/build/asciinema-player.js")
|
||||
@ -284,7 +283,7 @@ func ResetPassword() error {
|
||||
if err := userRepository.Update(u); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("用户「%v」密码初始化为: %v", user.Username, password)
|
||||
log.Debugf("用户「%v」密码初始化为: %v", user.Username, password)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -294,10 +293,10 @@ func SetupCache() *cache.Cache {
|
||||
mCache.OnEvicted(func(key string, value interface{}) {
|
||||
if strings.HasPrefix(key, Token) {
|
||||
token := GetTokenFormCacheKey(key)
|
||||
logrus.Debugf("用户Token「%v」过期", token)
|
||||
log.Debugf("用户Token「%v」过期", token)
|
||||
err := userService.Logout(token)
|
||||
if err != nil {
|
||||
logrus.Errorf("退出登录失败 %v", err)
|
||||
log.Errorf("退出登录失败 %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -334,13 +333,13 @@ func SetupDB() *gorm.DB {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logrus.WithError(err).Panic("连接数据库异常")
|
||||
log.WithError(err).Panic("连接数据库异常")
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(&model.User{}, &model.Asset{}, &model.AssetAttribute{}, &model.Session{}, &model.Command{},
|
||||
&model.Credential{}, &model.Property{}, &model.ResourceSharer{}, &model.UserGroup{}, &model.UserGroupMember{},
|
||||
&model.LoginLog{}, &model.Num{}, &model.Job{}, &model.JobLog{}, &model.AccessSecurity{}); err != nil {
|
||||
logrus.WithError(err).Panic("初始化数据库表结构异常")
|
||||
log.WithError(err).Panic("初始化数据库表结构异常")
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/global"
|
||||
"next-terminal/server/model"
|
||||
@ -20,7 +21,6 @@ import (
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/pkg/sftp"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func SessionPagingEndpoint(c echo.Context) error {
|
||||
@ -106,12 +106,12 @@ func CloseSessionById(sessionId string, code int, reason string) {
|
||||
defer mutex.Unlock()
|
||||
observable, _ := global.Store.Get(sessionId)
|
||||
if observable != nil {
|
||||
logrus.Debugf("会话%v创建者退出,原因:%v", sessionId, reason)
|
||||
log.Debugf("会话%v创建者退出,原因:%v", sessionId, reason)
|
||||
observable.Subject.Close(code, reason)
|
||||
|
||||
for i := 0; i < len(observable.Observers); i++ {
|
||||
observable.Observers[i].Close(code, reason)
|
||||
logrus.Debugf("强制踢出会话%v的观察者", sessionId)
|
||||
log.Debugf("强制踢出会话%v的观察者", sessionId)
|
||||
}
|
||||
}
|
||||
global.Store.Del(sessionId)
|
||||
@ -265,7 +265,7 @@ func SessionUploadEndpoint(c echo.Context) error {
|
||||
n, err := src.Read(buf)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
logrus.Warnf("文件上传错误 %v", err)
|
||||
log.Warnf("文件上传错误 %v", err)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
@ -381,7 +381,7 @@ func SessionLsEndpoint(c echo.Context) error {
|
||||
if tun.Subject.NextTerminal.SftpClient == nil {
|
||||
sftpClient, err := sftp.NewClient(tun.Subject.NextTerminal.SshClient)
|
||||
if err != nil {
|
||||
logrus.Errorf("创建sftp客户端失败:%v", err.Error())
|
||||
log.Errorf("创建sftp客户端失败:%v", err.Error())
|
||||
return err
|
||||
}
|
||||
tun.Subject.NextTerminal.SftpClient = sftpClient
|
||||
@ -450,7 +450,7 @@ func SessionLsEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
func SafetyRuleTrigger(c echo.Context) {
|
||||
logrus.Warnf("IP %v 尝试进行攻击,请ban掉此IP", c.RealIP())
|
||||
log.Warnf("IP %v 尝试进行攻击,请ban掉此IP", c.RealIP())
|
||||
security := model.AccessSecurity{
|
||||
ID: utils.UUID(),
|
||||
Source: "安全规则触发",
|
||||
@ -612,6 +612,6 @@ func SessionRecordingEndpoint(c echo.Context) error {
|
||||
recording = session.Recording + "/recording"
|
||||
}
|
||||
|
||||
logrus.Debugf("读取录屏文件:%v,是否存在: %v, 是否为文件: %v", recording, utils.FileExists(recording), utils.IsFile(recording))
|
||||
log.Debugf("读取录屏文件:%v,是否存在: %v, 是否为文件: %v", recording, utils.FileExists(recording), utils.IsFile(recording))
|
||||
return c.File(recording)
|
||||
}
|
||||
|
@ -7,16 +7,16 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"next-terminal/pkg/guacd"
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/global"
|
||||
"next-terminal/server/guacd"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/term"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var UpGrader = websocket.Upgrader{
|
||||
@ -46,7 +46,7 @@ type WindowSize struct {
|
||||
func SSHEndpoint(c echo.Context) (err error) {
|
||||
ws, err := UpGrader.Upgrade(c.Response().Writer, c.Request(), nil)
|
||||
if err != nil {
|
||||
logrus.Errorf("升级为WebSocket协议失败:%v", err.Error())
|
||||
log.Errorf("升级为WebSocket协议失败:%v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
||||
observers := append(observable.Observers, tun)
|
||||
observable.Observers = observers
|
||||
global.Store.Set(sessionId, observable)
|
||||
logrus.Debugf("加入会话%v,当前观察者数量为:%v", session.ConnectionId, len(observers))
|
||||
log.Debugf("加入会话%v,当前观察者数量为:%v", session.ConnectionId, len(observers))
|
||||
}
|
||||
|
||||
return err
|
||||
@ -118,7 +118,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
||||
nextTerminal, err := term.NewNextTerminal(ip, port, username, password, privateKey, passphrase, rows, cols, recording)
|
||||
|
||||
if err != nil {
|
||||
logrus.Errorf("创建SSH客户端失败:%v", err.Error())
|
||||
log.Errorf("创建SSH客户端失败:%v", err.Error())
|
||||
msg := Message{
|
||||
Type: Closed,
|
||||
Content: err.Error(),
|
||||
@ -144,7 +144,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
||||
Recording: recording,
|
||||
}
|
||||
// 创建新会话
|
||||
logrus.Debugf("创建新会话 %v", sess.ConnectionId)
|
||||
log.Debugf("创建新会话 %v", sess.ConnectionId)
|
||||
if err := sessionRepository.UpdateById(&sess, sessionId); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -172,7 +172,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
||||
var msg Message
|
||||
err = json.Unmarshal(message, &msg)
|
||||
if err != nil {
|
||||
logrus.Warnf("解析Json失败: %v, 原始字符串:%v", err, string(message))
|
||||
log.Warnf("解析Json失败: %v, 原始字符串:%v", err, string(message))
|
||||
continue
|
||||
}
|
||||
|
||||
@ -181,17 +181,17 @@ func SSHEndpoint(c echo.Context) (err error) {
|
||||
var winSize WindowSize
|
||||
err = json.Unmarshal([]byte(msg.Content), &winSize)
|
||||
if err != nil {
|
||||
logrus.Warnf("解析SSH会话窗口大小失败: %v", err)
|
||||
log.Warnf("解析SSH会话窗口大小失败: %v", err)
|
||||
continue
|
||||
}
|
||||
if err := nextTerminal.WindowChange(winSize.Rows, winSize.Cols); err != nil {
|
||||
logrus.Warnf("更改SSH会话窗口大小失败: %v", err)
|
||||
log.Warnf("更改SSH会话窗口大小失败: %v", err)
|
||||
continue
|
||||
}
|
||||
case Data:
|
||||
_, err = nextTerminal.Write([]byte(msg.Content))
|
||||
if err != nil {
|
||||
logrus.Debugf("SSH会话写入失败: %v", err)
|
||||
log.Debugf("SSH会话写入失败: %v", err)
|
||||
msg := Message{
|
||||
Type: Closed,
|
||||
Content: "the remote connection is closed.",
|
||||
@ -247,7 +247,7 @@ func WriteMessage(ws *websocket.Conn, msg Message) error {
|
||||
func WriteByteMessage(ws *websocket.Conn, p []byte) {
|
||||
err := ws.WriteMessage(websocket.TextMessage, p)
|
||||
if err != nil {
|
||||
logrus.Debugf("write: %v", err)
|
||||
log.Debugf("write: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,8 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/constant"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func SetupTicker() {
|
||||
@ -22,7 +21,7 @@ func SetupTicker() {
|
||||
if now.Sub(sessions[i].ConnectedTime.Time) > time.Hour*1 {
|
||||
_ = sessionRepository.DeleteById(sessions[i].ID)
|
||||
s := sessions[i].Username + "@" + sessions[i].IP + ":" + strconv.Itoa(sessions[i].Port)
|
||||
logrus.Infof("会话「%v」ID「%v」超过1小时未打开,已删除。", s, sessions[i].ID)
|
||||
log.Infof("会话「%v」ID「%v」超过1小时未打开,已删除。", s, sessions[i].ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -56,7 +55,7 @@ func SetupTicker() {
|
||||
}
|
||||
err := sessionRepository.DeleteByIds(sessionIds)
|
||||
if err != nil {
|
||||
logrus.Errorf("删除离线会话失败 %v", err)
|
||||
log.Errorf("删除离线会话失败 %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,14 +5,14 @@ import (
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"next-terminal/pkg/guacd"
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/global"
|
||||
"next-terminal/server/guacd"
|
||||
"next-terminal/server/model"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -27,7 +27,7 @@ func TunEndpoint(c echo.Context) error {
|
||||
|
||||
ws, err := UpGrader.Upgrade(c.Response().Writer, c.Request(), nil)
|
||||
if err != nil {
|
||||
logrus.Errorf("升级为WebSocket协议失败:%v", err.Error())
|
||||
log.Errorf("升级为WebSocket协议失败:%v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
@ -49,11 +49,11 @@ func TunEndpoint(c echo.Context) error {
|
||||
if len(connectionId) > 0 {
|
||||
session, err = sessionRepository.FindByConnectionId(connectionId)
|
||||
if err != nil {
|
||||
logrus.Warnf("会话不存在")
|
||||
log.Warnf("会话不存在")
|
||||
return err
|
||||
}
|
||||
if session.Status != constant.Connected {
|
||||
logrus.Warnf("会话未在线")
|
||||
log.Warnf("会话未在线")
|
||||
return errors.New("会话未在线")
|
||||
}
|
||||
configuration.ConnectionID = connectionId
|
||||
@ -135,7 +135,7 @@ func TunEndpoint(c echo.Context) error {
|
||||
configuration.SetParameter(guacd.Backspace, propertyMap[guacd.Backspace])
|
||||
configuration.SetParameter(guacd.TerminalType, propertyMap[guacd.TerminalType])
|
||||
default:
|
||||
logrus.WithField("configuration.Protocol", configuration.Protocol).Error("UnSupport Protocol")
|
||||
log.WithField("configuration.Protocol", configuration.Protocol).Error("UnSupport Protocol")
|
||||
return Fail(c, 400, "不支持的协议")
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ func TunEndpoint(c echo.Context) error {
|
||||
if connectionId == "" {
|
||||
CloseSessionById(sessionId, NewTunnelError, err.Error())
|
||||
}
|
||||
logrus.Printf("建立连接失败: %v", err.Error())
|
||||
log.Printf("建立连接失败: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
@ -194,7 +194,7 @@ func TunEndpoint(c echo.Context) error {
|
||||
Recording: configuration.GetParameter(guacd.RecordingPath),
|
||||
}
|
||||
// 创建新会话
|
||||
logrus.Debugf("创建新会话 %v", sess.ConnectionId)
|
||||
log.Debugf("创建新会话 %v", sess.ConnectionId)
|
||||
if err := sessionRepository.UpdateById(&sess, sessionId); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -205,7 +205,7 @@ func TunEndpoint(c echo.Context) error {
|
||||
observers := append(observable.Observers, tun)
|
||||
observable.Observers = observers
|
||||
global.Store.Set(sessionId, observable)
|
||||
logrus.Debugf("加入会话%v,当前观察者数量为:%v", session.ConnectionId, len(observers))
|
||||
log.Debugf("加入会话%v,当前观察者数量为:%v", session.ConnectionId, len(observers))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,12 +4,12 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/global"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func UserCreateEndpoint(c echo.Context) error {
|
||||
@ -97,7 +97,7 @@ func UserDeleteEndpoint(c echo.Context) error {
|
||||
for j := range loginLogs {
|
||||
global.Cache.Delete(loginLogs[j].ID)
|
||||
if err := userService.Logout(loginLogs[j].ID); err != nil {
|
||||
logrus.WithError(err).WithField("id:", loginLogs[j].ID).Error("Cache Deleted Error")
|
||||
log.WithError(err).WithField("id:", loginLogs[j].ID).Error("Cache Deleted Error")
|
||||
return Fail(c, 500, "强制下线错误")
|
||||
}
|
||||
}
|
||||
@ -173,7 +173,7 @@ func ReloadToken() error {
|
||||
token := loginLog.ID
|
||||
user, err := userRepository.FindById(loginLog.UserId)
|
||||
if err != nil {
|
||||
logrus.Debugf("用户「%v」获取失败,忽略", loginLog.UserId)
|
||||
log.Debugf("用户「%v」获取失败,忽略", loginLog.UserId)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -191,7 +191,7 @@ func ReloadToken() error {
|
||||
} else {
|
||||
global.Cache.Set(cacheKey, authorization, NotRememberEffectiveTime)
|
||||
}
|
||||
logrus.Debugf("重新加载用户「%v」授权Token「%v」到缓存", user.Nickname, token)
|
||||
log.Debugf("重新加载用户「%v」授权Token「%v」到缓存", user.Nickname, token)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package constant
|
||||
|
||||
import "next-terminal/server/guacd"
|
||||
import "next-terminal/pkg/guacd"
|
||||
|
||||
const (
|
||||
AccessRuleAllow = "allow" // 允许访问
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"next-terminal/server/guacd"
|
||||
"next-terminal/pkg/guacd"
|
||||
"next-terminal/server/term"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
@ -1,292 +0,0 @@
|
||||
package guacd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Host = "host"
|
||||
Port = "port"
|
||||
EnableRecording = "enable-recording"
|
||||
RecordingPath = "recording-path"
|
||||
CreateRecordingPath = "create-recording-path"
|
||||
|
||||
FontName = "font-name"
|
||||
FontSize = "font-size"
|
||||
ColorScheme = "color-scheme"
|
||||
Backspace = "backspace"
|
||||
TerminalType = "terminal-type"
|
||||
|
||||
EnableDrive = "enable-drive"
|
||||
DriveName = "drive-name"
|
||||
DrivePath = "drive-path"
|
||||
EnableWallpaper = "enable-wallpaper"
|
||||
EnableTheming = "enable-theming"
|
||||
EnableFontSmoothing = "enable-font-smoothing"
|
||||
EnableFullWindowDrag = "enable-full-window-drag"
|
||||
EnableDesktopComposition = "enable-desktop-composition"
|
||||
EnableMenuAnimations = "enable-menu-animations"
|
||||
DisableBitmapCaching = "disable-bitmap-caching"
|
||||
DisableOffscreenCaching = "disable-offscreen-caching"
|
||||
DisableGlyphCaching = "disable-glyph-caching"
|
||||
|
||||
Domain = "domain"
|
||||
RemoteApp = "remote-app"
|
||||
RemoteAppDir = "remote-app-dir"
|
||||
RemoteAppArgs = "remote-app-args"
|
||||
|
||||
ColorDepth = "color-depth"
|
||||
Cursor = "cursor"
|
||||
SwapRedBlue = "swap-red-blue"
|
||||
DestHost = "dest-host"
|
||||
DestPort = "dest-port"
|
||||
|
||||
UsernameRegex = "username-regex"
|
||||
PasswordRegex = "password-regex"
|
||||
LoginSuccessRegex = "login-success-regex"
|
||||
LoginFailureRegex = "login-failure-regex"
|
||||
|
||||
Namespace = "namespace"
|
||||
Pod = "pod"
|
||||
Container = "container"
|
||||
UesSSL = "use-ssl"
|
||||
ClientCert = "client-cert"
|
||||
ClientKey = "client-key"
|
||||
CaCert = "ca-cert"
|
||||
IgnoreCert = "ignore-cert"
|
||||
)
|
||||
|
||||
const Delimiter = ';'
|
||||
const Version = "VERSION_1_3_0"
|
||||
|
||||
type Configuration struct {
|
||||
ConnectionID string
|
||||
Protocol string
|
||||
Parameters map[string]string
|
||||
}
|
||||
|
||||
func NewConfiguration() (ret Configuration) {
|
||||
ret.Parameters = make(map[string]string)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (opt *Configuration) SetParameter(name, value string) {
|
||||
opt.Parameters[name] = value
|
||||
}
|
||||
|
||||
func (opt *Configuration) UnSetParameter(name string) {
|
||||
delete(opt.Parameters, name)
|
||||
}
|
||||
|
||||
func (opt *Configuration) GetParameter(name string) string {
|
||||
return opt.Parameters[name]
|
||||
}
|
||||
|
||||
type Instruction struct {
|
||||
Opcode string
|
||||
Args []string
|
||||
ProtocolForm string
|
||||
}
|
||||
|
||||
func NewInstruction(opcode string, args ...string) (ret Instruction) {
|
||||
ret.Opcode = opcode
|
||||
ret.Args = args
|
||||
return ret
|
||||
}
|
||||
|
||||
func (opt *Instruction) String() string {
|
||||
if len(opt.ProtocolForm) > 0 {
|
||||
return opt.ProtocolForm
|
||||
}
|
||||
|
||||
opt.ProtocolForm = fmt.Sprintf("%d.%s", len(opt.Opcode), opt.Opcode)
|
||||
for _, value := range opt.Args {
|
||||
opt.ProtocolForm += fmt.Sprintf(",%d.%s", len(value), value)
|
||||
}
|
||||
opt.ProtocolForm += string(Delimiter)
|
||||
return opt.ProtocolForm
|
||||
}
|
||||
|
||||
func (opt *Instruction) Parse(content string) Instruction {
|
||||
if strings.LastIndex(content, ";") > 0 {
|
||||
content = strings.TrimRight(content, ";")
|
||||
}
|
||||
messages := strings.Split(content, ",")
|
||||
|
||||
var args = make([]string, len(messages))
|
||||
for i := range messages {
|
||||
lm := strings.Split(messages[i], ".")
|
||||
args[i] = lm[1]
|
||||
}
|
||||
return NewInstruction(args[0], args[1:]...)
|
||||
}
|
||||
|
||||
type Tunnel struct {
|
||||
rw *bufio.ReadWriter
|
||||
conn net.Conn
|
||||
UUID string
|
||||
Config Configuration
|
||||
IsOpen bool
|
||||
}
|
||||
|
||||
func NewTunnel(address string, config Configuration) (ret *Tunnel, err error) {
|
||||
|
||||
conn, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &Tunnel{}
|
||||
ret.conn = conn
|
||||
ret.rw = bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
|
||||
ret.Config = config
|
||||
|
||||
selectArg := config.ConnectionID
|
||||
if selectArg == "" {
|
||||
selectArg = config.Protocol
|
||||
}
|
||||
|
||||
if err := ret.WriteInstructionAndFlush(NewInstruction("select", selectArg)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
args, err := ret.expect("args")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
width := config.GetParameter("width")
|
||||
height := config.GetParameter("height")
|
||||
dpi := config.GetParameter("dpi")
|
||||
|
||||
// send size
|
||||
if err := ret.WriteInstructionAndFlush(NewInstruction("size", width, height, dpi)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ret.WriteInstructionAndFlush(NewInstruction("audio", "audio/L8", "audio/L16")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ret.WriteInstructionAndFlush(NewInstruction("video")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ret.WriteInstructionAndFlush(NewInstruction("image", "image/jpeg", "image/png", "image/webp")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ret.WriteInstructionAndFlush(NewInstruction("timezone", "Asia/Shanghai")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parameters := make([]string, len(args.Args))
|
||||
for i := range args.Args {
|
||||
argName := args.Args[i]
|
||||
if strings.Contains(argName, "VERSION") {
|
||||
parameters[i] = Version
|
||||
continue
|
||||
}
|
||||
parameters[i] = config.GetParameter(argName)
|
||||
}
|
||||
// send connect
|
||||
if err := ret.WriteInstructionAndFlush(NewInstruction("connect", parameters...)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ready, err := ret.expect("ready")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(ready.Args) == 0 {
|
||||
return nil, errors.New("no connection id received")
|
||||
}
|
||||
|
||||
ret.UUID = ready.Args[0]
|
||||
ret.IsOpen = true
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (opt *Tunnel) WriteInstructionAndFlush(instruction Instruction) error {
|
||||
if _, err := opt.WriteAndFlush([]byte(instruction.String())); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (opt *Tunnel) WriteInstruction(instruction Instruction) error {
|
||||
if _, err := opt.Write([]byte(instruction.String())); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (opt *Tunnel) WriteAndFlush(p []byte) (int, error) {
|
||||
//fmt.Printf("-> %v\n", string(p))
|
||||
nn, err := opt.rw.Write(p)
|
||||
if err != nil {
|
||||
return nn, err
|
||||
}
|
||||
err = opt.rw.Flush()
|
||||
if err != nil {
|
||||
return nn, err
|
||||
}
|
||||
return nn, nil
|
||||
}
|
||||
|
||||
func (opt *Tunnel) Write(p []byte) (int, error) {
|
||||
//fmt.Printf("-> %v \n", string(p))
|
||||
nn, err := opt.rw.Write(p)
|
||||
if err != nil {
|
||||
return nn, err
|
||||
}
|
||||
return nn, nil
|
||||
}
|
||||
|
||||
func (opt *Tunnel) Flush() error {
|
||||
return opt.rw.Flush()
|
||||
}
|
||||
|
||||
func (opt *Tunnel) ReadInstruction() (instruction Instruction, err error) {
|
||||
msg, err := opt.rw.ReadString(Delimiter)
|
||||
//fmt.Printf("<- %v \n", msg)
|
||||
if err != nil {
|
||||
return instruction, err
|
||||
}
|
||||
return instruction.Parse(msg), err
|
||||
}
|
||||
|
||||
func (opt *Tunnel) Read() (p []byte, err error) {
|
||||
p, err = opt.rw.ReadBytes(Delimiter)
|
||||
//fmt.Printf("<- %v \n", string(p))
|
||||
s := string(p)
|
||||
if s == "rate=44100,channels=2;" {
|
||||
return make([]byte, 0), nil
|
||||
}
|
||||
if s == "rate=22050,channels=2;" {
|
||||
return make([]byte, 0), nil
|
||||
}
|
||||
if s == "5.audio,1.1,31.audio/L16;" {
|
||||
s += "rate=44100,channels=2;"
|
||||
}
|
||||
return []byte(s), err
|
||||
}
|
||||
|
||||
func (opt *Tunnel) expect(opcode string) (instruction Instruction, err error) {
|
||||
instruction, err = opt.ReadInstruction()
|
||||
if err != nil {
|
||||
return instruction, err
|
||||
}
|
||||
|
||||
if opcode != instruction.Opcode {
|
||||
msg := fmt.Sprintf(`expected "%s" instruction but instead received "%s"`, opcode, instruction.Opcode)
|
||||
return instruction, errors.New(msg)
|
||||
}
|
||||
return instruction, nil
|
||||
}
|
||||
|
||||
func (opt *Tunnel) Close() error {
|
||||
opt.IsOpen = false
|
||||
return opt.conn.Close()
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"next-terminal/server/guacd"
|
||||
"next-terminal/pkg/guacd"
|
||||
"next-terminal/server/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/global"
|
||||
"next-terminal/server/model"
|
||||
@ -15,7 +16,6 @@ import (
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type JobService struct {
|
||||
@ -43,14 +43,14 @@ func (r JobService) ChangeStatusById(id, status string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("开启计划任务「%v」,运行中计划任务数量「%v」", job.Name, len(global.Cron.Entries()))
|
||||
log.Debugf("开启计划任务「%v」,运行中计划任务数量「%v」", job.Name, len(global.Cron.Entries()))
|
||||
|
||||
jobForUpdate := model.Job{ID: id, Status: constant.JobStatusRunning, CronJobId: int(entryID)}
|
||||
|
||||
return r.jobRepository.UpdateById(&jobForUpdate)
|
||||
} else {
|
||||
global.Cron.Remove(cron.EntryID(job.CronJobId))
|
||||
logrus.Debugf("关闭计划任务「%v」,运行中计划任务数量「%v」", job.Name, len(global.Cron.Entries()))
|
||||
log.Debugf("关闭计划任务「%v」,运行中计划任务数量「%v」", job.Name, len(global.Cron.Entries()))
|
||||
jobForUpdate := model.Job{ID: id, Status: constant.JobStatusNotRunning}
|
||||
return r.jobRepository.UpdateById(&jobForUpdate)
|
||||
}
|
||||
@ -102,7 +102,7 @@ func (r CheckAssetStatusJob) Run() {
|
||||
msg := fmt.Sprintf("资产「%v」存活状态检测完成,存活「%v」,耗时「%v」", asset.Name, active, elapsed)
|
||||
|
||||
_ = r.jobService.assetRepository.UpdateActiveById(active, asset.ID)
|
||||
logrus.Infof(msg)
|
||||
log.Infof(msg)
|
||||
msgChan <- msg
|
||||
}()
|
||||
}
|
||||
@ -154,7 +154,7 @@ func (r ShellJob) Run() {
|
||||
var metadataShell MetadataShell
|
||||
err := json.Unmarshal([]byte(r.Metadata), &metadataShell)
|
||||
if err != nil {
|
||||
logrus.Errorf("JSON数据解析失败 %v", err)
|
||||
log.Errorf("JSON数据解析失败 %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -200,10 +200,10 @@ func (r ShellJob) Run() {
|
||||
var msg string
|
||||
if err != nil {
|
||||
msg = fmt.Sprintf("资产「%v」Shell执行失败,返回值「%v」,耗时「%v」", asset.Name, err.Error(), elapsed)
|
||||
logrus.Infof(msg)
|
||||
log.Infof(msg)
|
||||
} else {
|
||||
msg = fmt.Sprintf("资产「%v」Shell执行成功,返回值「%v」,耗时「%v」", asset.Name, result, elapsed)
|
||||
logrus.Infof(msg)
|
||||
log.Infof(msg)
|
||||
}
|
||||
|
||||
msgChan <- msg
|
||||
@ -274,7 +274,7 @@ func (r JobService) InitJob() error {
|
||||
if err := r.jobRepository.Create(&job); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("创建计划任务「%v」cron「%v」", job.Name, job.Cron)
|
||||
log.Debugf("创建计划任务「%v」cron「%v」", job.Name, job.Cron)
|
||||
} else {
|
||||
for i := range jobs {
|
||||
if jobs[i].Status == constant.JobStatusRunning {
|
||||
@ -282,7 +282,7 @@ func (r JobService) InitJob() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debugf("启动计划任务「%v」cron「%v」", jobs[i].Name, jobs[i].Cron)
|
||||
log.Debugf("启动计划任务「%v」cron「%v」", jobs[i].Name, jobs[i].Cron)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,11 @@ package service
|
||||
import (
|
||||
"net/smtp"
|
||||
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/repository"
|
||||
|
||||
"github.com/jordan-wright/email"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type MailService struct {
|
||||
@ -26,7 +26,7 @@ func (r MailService) SendMail(to, subject, text string) {
|
||||
password := propertiesMap[constant.MailPassword]
|
||||
|
||||
if host == "" || port == "" || username == "" || password == "" {
|
||||
logrus.Debugf("邮箱信息不完整,跳过发送邮件。")
|
||||
log.Debugf("邮箱信息不完整,跳过发送邮件。")
|
||||
return
|
||||
}
|
||||
|
||||
@ -37,6 +37,6 @@ func (r MailService) SendMail(to, subject, text string) {
|
||||
e.Text = []byte(text)
|
||||
err := e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
|
||||
if err != nil {
|
||||
logrus.Errorf("邮件发送失败: %v", err.Error())
|
||||
log.Errorf("邮件发送失败: %v", err.Error())
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package service
|
||||
import (
|
||||
"os"
|
||||
|
||||
"next-terminal/server/guacd"
|
||||
"next-terminal/pkg/guacd"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/utils"
|
||||
|
@ -1,12 +1,11 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"next-terminal/pkg/log"
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
@ -40,7 +39,7 @@ func (r UserService) InitUser() (err error) {
|
||||
if err := r.userRepository.Create(&user); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("初始用户创建成功,账号:「%v」密码:「%v」", user.Username, initPassword)
|
||||
log.Infof("初始用户创建成功,账号:「%v」密码:「%v」", user.Username, initPassword)
|
||||
} else {
|
||||
for i := range users {
|
||||
// 修正默认用户类型为管理员
|
||||
@ -52,7 +51,7 @@ func (r UserService) InitUser() (err error) {
|
||||
if err := r.userRepository.Update(&user); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("自动修正用户「%v」ID「%v」类型为管理员", users[i].Nickname, users[i].ID)
|
||||
log.Infof("自动修正用户「%v」ID「%v」类型为管理员", users[i].Nickname, users[i].ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -83,7 +82,7 @@ func (r UserService) Logout(token string) (err error) {
|
||||
|
||||
loginLog, err := r.loginLogRepository.FindById(token)
|
||||
if err != nil {
|
||||
logrus.Warnf("登录日志「%v」获取失败", token)
|
||||
log.Warnf("登录日志「%v」获取失败", token)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,8 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"next-terminal/pkg/log"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
@ -30,7 +31,7 @@ func main() {
|
||||
|
||||
client, err := ssh.Dial("tcp", "172.16.101.32:22", sshConfig)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
log.Error(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
@ -87,9 +88,9 @@ func (t *SSHTerminal) interactiveSession() error {
|
||||
|
||||
defer func() {
|
||||
if t.exitMsg == "" {
|
||||
logrus.Info(os.Stdout, "the connection was closed on the remote side on ", time.Now().Format(time.RFC822))
|
||||
log.Info(os.Stdout, "the connection was closed on the remote side on ", time.Now().Format(time.RFC822))
|
||||
} else {
|
||||
logrus.Info(os.Stdout, t.exitMsg)
|
||||
log.Info(os.Stdout, t.exitMsg)
|
||||
}
|
||||
}()
|
||||
|
||||
|
Reference in New Issue
Block a user