更换日志组件为logrus,修改资产的账号和密码不再为必填选项

This commit is contained in:
dushixiang
2020-12-27 21:44:40 +08:00
parent 20030dac7d
commit bbeed5344a
23 changed files with 183 additions and 90 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"next-terminal/pkg/global"
"strings"
"time"
@ -35,7 +36,7 @@ func Auth(next echo.HandlerFunc) echo.HandlerFunc {
token := GetToken(c)
user, found := global.Cache.Get(token)
if !found {
c.Logger().Error("您的登录信息已失效,请重新登录后再试。")
logrus.Debugf("您的登录信息已失效,请重新登录后再试。")
return Fail(c, 403, "您的登录信息已失效,请重新登录后再试。")
}
global.Cache.Set(token, user, time.Minute*time.Duration(30))

View File

@ -19,9 +19,6 @@ func SetupRoutes() *echo.Echo {
e.File("/favicon.ico", "web/build/favicon.ico")
e.Static("/static", "web/build/static")
// Middleware
e.Use(middleware.Logger())
//fd, _ := os.OpenFile(
// "next-terminal.log",
// os.O_RDWR|os.O_APPEND,

View File

@ -4,8 +4,9 @@ import (
"bytes"
"errors"
"fmt"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
@ -16,6 +17,7 @@ import (
"path"
"strconv"
"strings"
"time"
)
func SessionPagingEndpoint(c echo.Context) error {
@ -38,10 +40,10 @@ func SessionPagingEndpoint(c echo.Context) error {
recording := items[i].Recording + "/recording"
if utils.FileExists(recording) {
log.Infof("检测到录屏文件[%v]存在", recording)
logrus.Debugf("检测到录屏文件[%v]存在", recording)
items[i].Recording = "1"
} else {
log.Warnf("检测到录屏文件[%v]不存在", recording)
logrus.Warnf("检测到录屏文件[%v]不存在", recording)
items[i].Recording = "0"
}
} else {
@ -89,16 +91,18 @@ func SessionDiscontentEndpoint(c echo.Context) error {
split := strings.Split(sessionIds, ",")
for i := range split {
tun, ok := global.Store.Get(split[i])
if ok {
CloseSession(split[i], tun)
}
CloseSessionById(split[i], 2001, "管理员强制关闭了此次接入。")
}
return Success(c, nil)
}
func CloseSession(sessionId string, tun global.Tun) {
_ = tun.Tun.Close()
func CloseSessionById(sessionId string, code int, reason string) {
tun, _ := global.Store.Get(sessionId)
if tun != nil {
_ = tun.Tun.Close()
CloseSessionByWebSocket(tun.WebSocket, code, reason)
}
global.Store.Del(sessionId)
session := model.Session{}
@ -109,6 +113,21 @@ func CloseSession(sessionId string, tun global.Tun) {
model.UpdateSessionById(&session, sessionId)
}
func CloseSessionByWebSocket(ws *websocket.Conn, c int, t string) {
if ws == nil {
return
}
ws.SetCloseHandler(func(code int, text string) error {
var message []byte
if code != websocket.CloseNoStatusReceived {
message = websocket.FormatCloseMessage(c, t)
}
_ = ws.WriteControl(websocket.CloseMessage, message, time.Now().Add(time.Second))
return nil
})
defer ws.Close()
}
func SessionResizeEndpoint(c echo.Context) error {
width := c.QueryParam("width")
height := c.QueryParam("height")
@ -457,6 +476,6 @@ func SessionRecordingEndpoint(c echo.Context) error {
return err
}
recording := path.Join(session.Recording, "recording")
log.Printf("读取录屏文件:%s", recording)
logrus.Debugf("读取录屏文件:%s", recording)
return c.File(recording)
}

View File

@ -6,8 +6,8 @@ import (
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/pkg/sftp"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"log"
"net/http"
"next-terminal/pkg/model"
"strconv"
@ -111,7 +111,7 @@ func SSHEndpoint(c echo.Context) error {
}
_, err = stdinPipe.Write(message)
if err != nil {
log.Println("Tunnel write:", err)
logrus.Debugf("Tunnel write: %v", err)
}
}
return err
@ -173,7 +173,7 @@ func WriteMessage(ws *websocket.Conn, message string) {
func WriteByteMessage(ws *websocket.Conn, p []byte) {
err := ws.WriteMessage(websocket.TextMessage, p)
if err != nil {
log.Println("write:", err)
logrus.Debugf("write: %v", err)
}
}

View File

@ -1,11 +1,9 @@
package api
import (
"fmt"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/pkg/sftp"
"log"
"github.com/sirupsen/logrus"
"next-terminal/pkg/global"
"next-terminal/pkg/guacd"
"next-terminal/pkg/model"
@ -17,6 +15,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())
return err
}
@ -35,7 +34,6 @@ func TunEndpoint(c echo.Context) error {
propertyMap := model.FindAllPropertiesMap()
var session model.Session
var sftpClient *sftp.Client
if len(connectionId) > 0 {
session, err = model.FindSessionByConnectionId(connectionId)
@ -95,11 +93,6 @@ func TunEndpoint(c echo.Context) error {
configuration.SetParameter(guacd.FontSize, strconv.Itoa(fontSize))
configuration.SetParameter(guacd.FontName, propertyMap[guacd.FontName])
configuration.SetParameter(guacd.ColorScheme, propertyMap[guacd.ColorScheme])
sftpClient, err = CreateSftpClient(session.AssetId)
if err != nil {
return err
}
break
case "vnc":
configuration.SetParameter("password", session.Password)
@ -117,21 +110,20 @@ func TunEndpoint(c echo.Context) error {
}
addr := propertyMap[guacd.Host] + ":" + propertyMap[guacd.Port]
logrus.Infof("connect to %v with global: %+v", addr, configuration)
tunnel, err := guacd.NewTunnel(addr, configuration)
if err != nil {
return err
}
fmt.Printf("=====================================================\n")
fmt.Printf("connect to %v with global: %+v\n", addr, configuration)
fmt.Printf("=====================================================\n")
tun := global.Tun{
Tun: tunnel,
SftpClient: sftpClient,
Tun: tunnel,
WebSocket: ws,
}
global.Store.Set(sessionId, tun)
global.Store.Set(sessionId, &tun)
if len(session.ConnectionId) == 0 {
session.ConnectionId = tunnel.UUID
@ -142,19 +134,30 @@ func TunEndpoint(c echo.Context) error {
model.UpdateSessionById(&session, sessionId)
}
go func() {
sftpClient, err := CreateSftpClient(session.AssetId)
if err != nil {
CloseSessionById(sessionId, 2002, err.Error())
logrus.Errorf("创建sftp客户端失败%v", err.Error())
}
item, ok := global.Store.Get(sessionId)
if ok {
item.SftpClient = sftpClient
}
}()
go func() {
for true {
instruction, err := tunnel.Read()
if err != nil {
CloseSession(sessionId, tun)
log.Printf("WS读取异常: %v", err)
CloseSessionById(sessionId, 523, err.Error())
logrus.Printf("WebSocket读取错误: %v", err)
break
}
//fmt.Printf("<= %v \n", string(instruction))
err = ws.WriteMessage(websocket.TextMessage, instruction)
if err != nil {
CloseSession(sessionId, tun)
log.Printf("WS写入异常: %v", err)
CloseSessionById(sessionId, 523, err.Error())
logrus.Printf("WebSocket写入错误: %v", err)
break
}
}
@ -163,14 +166,14 @@ func TunEndpoint(c echo.Context) error {
for true {
_, message, err := ws.ReadMessage()
if err != nil {
CloseSession(sessionId, tun)
log.Printf("Tunnel读取异常: %v", err)
CloseSessionById(sessionId, 523, err.Error())
logrus.Printf("隧道读取错误: %v", err)
break
}
_, err = tunnel.WriteAndFlush(message)
if err != nil {
CloseSession(sessionId, tun)
log.Printf("Tunnel写入异常: %v", err)
CloseSessionById(sessionId, 523, err.Error())
logrus.Printf("隧道写入错误: %v", err)
break
}
}