Skip to content

Commit

Permalink
refactor: fallback to gin
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jan 16, 2024
1 parent 55fc516 commit 0ce8b15
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 232 deletions.
108 changes: 54 additions & 54 deletions app/http/controllers/ssh_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package controllers
import (
"bytes"
"context"
nethttp "net/http"
"sync"
"time"

"github.com/fasthttp/websocket"
"github.com/goravel/fiber"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"github.com/gorilla/websocket"
"github.com/spf13/cast"
"github.com/valyala/fasthttp"

"panel/app/models"
"panel/internal"
Expand Down Expand Up @@ -86,75 +85,76 @@ func (r *SshController) UpdateInfo(ctx http.Context) http.Response {

// Session SSH 会话
func (r *SshController) Session(ctx http.Context) http.Response {
upGrader := websocket.FastHTTPUpgrader{
upGrader := websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
CheckOrigin: func(ctx *fasthttp.RequestCtx) bool {
CheckOrigin: func(r *nethttp.Request) bool {
return true
},
Subprotocols: []string{ctx.Request().Header("Sec-WebSocket-Protocol")},
}

ws, err := upGrader.Upgrade(ctx.Response().Writer(), ctx.Request().Origin(), nil)
if err != nil {
facades.Log().Tags("面板", "SSH").With(map[string]any{
"error": err.Error(),
}).Infof("建立连接失败")
return ErrorSystem(ctx)
}
defer ws.Close()

config := ssh.SSHClientConfigPassword(
r.setting.Get(models.SettingKeySshHost)+":"+r.setting.Get(models.SettingKeySshPort),
r.setting.Get(models.SettingKeySshUser),
r.setting.Get(models.SettingKeySshPassword),
)
client, err := ssh.NewSSHClient(config)

err = upGrader.Upgrade(ctx.(*fiber.Context).Instance().Context(), func(conn *websocket.Conn) {
if err != nil {
_ = conn.WriteControl(websocket.CloseMessage,
[]byte(err.Error()), time.Now().Add(time.Second))
return
}

defer client.Close()

turn, err := ssh.NewTurn(conn, client)
if err != nil {
_ = conn.WriteControl(websocket.CloseMessage,
[]byte(err.Error()), time.Now().Add(time.Second))
return
}
defer turn.Close()
if err != nil {
_ = ws.WriteControl(websocket.CloseMessage,
[]byte(err.Error()), time.Now().Add(time.Second))
return ErrorSystem(ctx)
}
defer client.Close()

var bufPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
var logBuff = bufPool.Get().(*bytes.Buffer)
logBuff.Reset()
defer bufPool.Put(logBuff)

ctx2, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
err := turn.LoopRead(logBuff, ctx2)
if err != nil {
facades.Log().Request(ctx.Request()).Tags("面板", "SSH").With(map[string]any{
"error": err.Error(),
}).Info("SSH 会话错误")
}
}()
go func() {
defer wg.Done()
err := turn.SessionWait()
if err != nil {
facades.Log().Request(ctx.Request()).Tags("面板", "SSH").With(map[string]any{
"error": err.Error(),
}).Info("SSH 会话错误")
}
cancel()
}()
wg.Wait()
})
turn, err := ssh.NewTurn(ws, client)
if err != nil {
_ = ws.WriteControl(websocket.CloseMessage,
[]byte(err.Error()), time.Now().Add(time.Second))
return ErrorSystem(ctx)
}
defer turn.Close()

var bufPool = sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
}
var logBuff = bufPool.Get().(*bytes.Buffer)
logBuff.Reset()
defer bufPool.Put(logBuff)

sshCtx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
if err = turn.LoopRead(logBuff, sshCtx); err != nil {
facades.Log().Tags("面板", "SSH").With(map[string]any{
"error": err.Error(),
}).Infof("读取数据失败")
}
}()
go func() {
defer wg.Done()
if err = turn.SessionWait(); err != nil {
facades.Log().Tags("面板", "SSH").With(map[string]any{
"error": err.Error(),
}).Infof("会话错误")
}
cancel()
}()
wg.Wait()

return nil
}
25 changes: 4 additions & 21 deletions app/http/controllers/swagger_controller.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package controllers

import (
"github.com/gofiber/swagger"
"github.com/goravel/fiber"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"github.com/swaggo/http-swagger/v2"

_ "panel/docs"
)
Expand All @@ -13,20 +12,8 @@ type SwaggerController struct {
// Dependent services
}

// Config stores fiberSwagger configuration variables.
type Config struct {
URL string
InstanceName string
DocExpansion string
DomID string
DeepLinking bool
PersistAuthorization bool
}

func NewSwaggerController() *SwaggerController {
return &SwaggerController{
// Inject services
}
return &SwaggerController{}
}

// Index
Expand All @@ -42,12 +29,8 @@ func (r *SwaggerController) Index(ctx http.Context) http.Response {
return Error(ctx, http.StatusNotFound, http.StatusText(http.StatusNotFound))
}

err := swagger.New(swagger.Config{
Title: "耗子面板 Swagger",
})(ctx.(*fiber.Context).Instance())
if err != nil {
return Error(ctx, http.StatusNotFound, err.Error())
}
handler := httpSwagger.Handler()
handler(ctx.Response().Writer(), ctx.Request().Origin())

return nil
}
4 changes: 2 additions & 2 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"github.com/goravel/fiber"
"github.com/goravel/framework/auth"
"github.com/goravel/framework/cache"
"github.com/goravel/framework/console"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/goravel/framework/support/carbon"
"github.com/goravel/framework/testing"
"github.com/goravel/framework/validation"
"github.com/goravel/gin"

"panel/app/providers"
)
Expand Down Expand Up @@ -91,7 +91,7 @@ func init() {
&providers.EventServiceProvider{},
&providers.ValidationServiceProvider{},
&providers.DatabaseServiceProvider{},
&fiber.ServiceProvider{},
&gin.ServiceProvider{},
},
})
}
10 changes: 4 additions & 6 deletions config/http.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package config

import (
fiberfacades "github.com/goravel/fiber/facades"
"github.com/goravel/framework/contracts/route"
"github.com/goravel/framework/facades"
ginfacades "github.com/goravel/gin/facades"
)

func init() {
config := facades.Config()
config.Add("http", map[string]any{
// HTTP Driver
"default": "fiber",
"default": "gin",
// HTTP Drivers
"drivers": map[string]any{
"fiber": map[string]any{
// prefork mode, see https://docs.gofiber.io/api/fiber/#config
"prefork": false,
"gin": map[string]any{
// Optional, default is 4096 KB
"body_limit": 1024 * 1024 * 4,
"route": func() (route.Route, error) {
return fiberfacades.Route("fiber"), nil
return ginfacades.Route("gin"), nil
},
},
},
Expand Down
63 changes: 31 additions & 32 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ module panel
go 1.20

require (
github.com/fasthttp/websocket v1.5.6
github.com/gertd/go-pluralize v0.2.1
github.com/go-acme/lego/v4 v4.14.2
github.com/gofiber/swagger v0.1.14
github.com/gookit/color v1.5.4
github.com/gookit/validate v1.5.1
github.com/goravel/fiber v1.1.11-0.20231222082732-ab5faaaee0d2
github.com/goravel/framework v1.13.1-0.20231222074402-8955968c9670
github.com/goravel/framework v1.13.1-0.20240113045353-f52459bcef59
github.com/goravel/gin v1.1.6-0.20240116040838-cf1f7d78b1f5
github.com/gorilla/websocket v1.5.1
github.com/iancoleman/strcase v0.3.0
github.com/imroc/req/v3 v3.42.1
github.com/mholt/archiver/v3 v3.5.1
github.com/mojocn/base64Captcha v1.3.6
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.8.4
github.com/swaggo/http-swagger/v2 v2.0.2
github.com/swaggo/swag v1.16.2
github.com/valyala/fasthttp v1.51.0
golang.org/x/crypto v0.17.0
golang.org/x/crypto v0.18.0
)

require (
Expand Down Expand Up @@ -55,26 +54,28 @@ require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gaukas/godicttls v0.0.4 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/glebarez/go-sqlite v1.22.0 // indirect
github.com/glebarez/sqlite v1.10.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-redsync/redsync/v4 v4.8.1 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gofiber/fiber/v2 v2.51.0 // indirect
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/template/html/v2 v2.0.5 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang-migrate/migrate/v4 v4.17.0 // indirect
github.com/golang-module/carbon/v2 v2.2.14 // indirect
github.com/golang-module/carbon/v2 v2.3.5 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
Expand Down Expand Up @@ -111,12 +112,11 @@ require (
github.com/klauspost/compress v1.17.3 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
github.com/miekg/dns v1.1.55 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -134,18 +134,17 @@ require (
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.4 // indirect
github.com/quic-go/quic-go v0.38.1 // indirect
github.com/redis/go-redis/v9 v9.3.0 // indirect
github.com/redis/go-redis/v9 v9.4.0 // indirect
github.com/refraction-networking/utls v1.5.3 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rotisserie/eris v0.5.4 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/savioxavier/termlink v1.3.0 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
Expand All @@ -158,10 +157,10 @@ require (
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/urfave/cli/v2 v2.26.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/unrolled/secure v1.14.0 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
Expand All @@ -178,13 +177,13 @@ require (
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/image v0.13.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.15.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/api v0.153.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
Expand All @@ -200,8 +199,8 @@ require (
gorm.io/driver/sqlserver v1.5.2 // indirect
gorm.io/gorm v1.25.5 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.23.1 // indirect
modernc.org/libc v1.37.6 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/sqlite v1.28.0 // indirect
)
Loading

0 comments on commit 0ce8b15

Please sign in to comment.