Skip to content

Commit 868fdd4

Browse files
committed
feat: refactor auth and xpack integration
1 parent 1b6ab99 commit 868fdd4

174 files changed

Lines changed: 5308 additions & 3398 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,17 @@ frontend/components.d.ts
4242
frontend/src/xpack
4343
frontend/src/xpack-ee
4444
agent/xpack
45+
agent/router/entry_xpack.go
46+
agent/server/init_xpack.go
47+
agent/utils/xpack/xpack.go
4548
core/xpack
4649
core/xpack-ee
50+
core/router/entry_xpack.go
51+
core/router/entry_xpackee.go
52+
core/server/init_xpack.go
53+
core/server/init_xpackee.go
54+
core/utils/xpack/xpack.go
55+
core/utils/xpack/xpackee.go
4756

4857
.history/
4958
dist/

agent/app/api/v2/ai.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package v2
33
import (
44
"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
55
"github.com/1Panel-dev/1Panel/agent/app/dto"
6-
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu"
7-
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/common"
8-
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/xpu"
96
"github.com/gin-gonic/gin"
107
)
118

@@ -163,37 +160,6 @@ func (b *BaseApi) DeleteOllamaModel(c *gin.Context) {
163160
helper.Success(c)
164161
}
165162

166-
// @Tags AI
167-
// @Summary Load gpu / xpu info
168-
// @Accept json
169-
// @Success 200
170-
// @Security ApiKeyAuth
171-
// @Security Timestamp
172-
// @Router /ai/gpu/load [get]
173-
func (b *BaseApi) LoadGpuInfo(c *gin.Context) {
174-
ok, client := gpu.New()
175-
if ok {
176-
info, err := client.LoadGpuInfo()
177-
if err != nil {
178-
helper.BadRequest(c, err)
179-
return
180-
}
181-
helper.SuccessWithData(c, info)
182-
return
183-
}
184-
xpuOK, xpuClient := xpu.New()
185-
if xpuOK {
186-
info, err := xpuClient.LoadGpuInfo()
187-
if err != nil {
188-
helper.BadRequest(c, err)
189-
return
190-
}
191-
helper.SuccessWithData(c, info)
192-
return
193-
}
194-
helper.SuccessWithData(c, &common.GpuInfo{})
195-
}
196-
197163
// @Tags AI
198164
// @Summary Bind domain
199165
// @Accept json

agent/app/api/v2/gpu.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package v2
2+
3+
import (
4+
"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
5+
"github.com/1Panel-dev/1Panel/agent/app/dto"
6+
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu"
7+
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/common"
8+
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/xpu"
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
// @Tags AI
13+
// @Summary Load gpu / xpu info
14+
// @Accept json
15+
// @Success 200
16+
// @Security ApiKeyAuth
17+
// @Security Timestamp
18+
// @Router /ai/gpu/load [get]
19+
func (b *BaseApi) LoadGpuInfo(c *gin.Context) {
20+
ok, client := gpu.New()
21+
if ok {
22+
info, err := client.LoadGpuInfo()
23+
if err != nil {
24+
helper.BadRequest(c, err)
25+
return
26+
}
27+
helper.SuccessWithData(c, info)
28+
return
29+
}
30+
xpuOK, xpuClient := xpu.New()
31+
if xpuOK {
32+
info, err := xpuClient.LoadGpuInfo()
33+
if err != nil {
34+
helper.BadRequest(c, err)
35+
return
36+
}
37+
helper.SuccessWithData(c, info)
38+
return
39+
}
40+
helper.SuccessWithData(c, &common.GpuInfo{})
41+
}
42+
43+
func (b *BaseApi) GetCPUOptions(c *gin.Context) {
44+
helper.SuccessWithData(c, monitorService.LoadGPUOptions())
45+
}
46+
47+
// @Tags Monitor
48+
// @Summary Load monitor data
49+
// @Param request body dto.MonitorGPUSearch true "request"
50+
// @Success 200 {object} dto.MonitorGPUData
51+
// @Security ApiKeyAuth
52+
// @Security Timestamp
53+
// @Router /hosts/monitor/gpu/search [post]
54+
func (b *BaseApi) LoadGPUMonitor(c *gin.Context) {
55+
var req dto.MonitorGPUSearch
56+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
57+
return
58+
}
59+
60+
data, err := monitorService.LoadGPUMonitorData(req)
61+
if err != nil {
62+
helper.InternalServer(c, err)
63+
return
64+
}
65+
helper.SuccessWithData(c, data)
66+
}

agent/app/api/v2/monitor.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,6 @@ func (b *BaseApi) LoadMonitor(c *gin.Context) {
3131
helper.SuccessWithDataGzipped(c, data)
3232
}
3333

34-
// @Tags Monitor
35-
// @Summary Load monitor data
36-
// @Param request body dto.MonitorGPUSearch true "request"
37-
// @Success 200 {object} dto.MonitorGPUData
38-
// @Security ApiKeyAuth
39-
// @Security Timestamp
40-
// @Router /hosts/monitor/gpu/search [post]
41-
func (b *BaseApi) LoadGPUMonitor(c *gin.Context) {
42-
var req dto.MonitorGPUSearch
43-
if err := helper.CheckBindAndValidate(&req, c); err != nil {
44-
return
45-
}
46-
47-
data, err := monitorService.LoadGPUMonitorData(req)
48-
if err != nil {
49-
helper.InternalServer(c, err)
50-
return
51-
}
52-
helper.SuccessWithData(c, data)
53-
}
54-
5534
// @Tags Monitor
5635
// @Summary Clean monitor data
5736
// @Success 200
@@ -126,7 +105,3 @@ func (b *BaseApi) GetIOOptions(c *gin.Context) {
126105
sort.Strings(options)
127106
helper.SuccessWithData(c, options)
128107
}
129-
130-
func (b *BaseApi) GetCPUOptions(c *gin.Context) {
131-
helper.SuccessWithData(c, monitorService.LoadGPUOptions())
132-
}

agent/app/api/v2/terminal.go

Lines changed: 88 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/1Panel-dev/1Panel/agent/app/dto"
12+
"github.com/1Panel-dev/1Panel/agent/app/model"
1213
"github.com/1Panel-dev/1Panel/agent/app/service"
1314
"github.com/1Panel-dev/1Panel/agent/global"
1415
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
@@ -19,59 +20,86 @@ import (
1920
"github.com/pkg/errors"
2021
)
2122

22-
func (b *BaseApi) WsSSH(c *gin.Context) {
23+
func (b *BaseApi) WsLocalTerminal(c *gin.Context) {
24+
client, err := loadLocalConn()
25+
b.runSSHSession(c, client, err, c.DefaultQuery("command", ""))
26+
}
27+
28+
func (b *BaseApi) WsHostSSH(c *gin.Context) {
29+
hostID, _ := strconv.Atoi(c.DefaultQuery("id", "0"))
30+
if hostID <= 0 {
31+
b.runSSHSession(c, nil, errors.New("missing host id"), c.DefaultQuery("command", ""))
32+
return
33+
}
34+
host, err := service.GetHostInfo(uint(hostID))
35+
client, err := newHostSSHClient(host, err)
36+
b.runSSHSession(c, client, err, c.DefaultQuery("command", ""))
37+
}
38+
39+
func (b *BaseApi) WsContainerTerminal(c *gin.Context) {
40+
wsConn, cols, rows, ok := prepareTerminalSession(c)
41+
if !ok {
42+
return
43+
}
44+
defer wsConn.Close()
45+
46+
slave, err := loadContainerTerminalCommand(c)
47+
if wshandleError(wsConn, err) {
48+
return
49+
}
50+
defer slave.Close()
51+
52+
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, false)
53+
if wshandleError(wsConn, err) {
54+
return
55+
}
56+
57+
quitChan := make(chan bool, 3)
58+
tty.Start(quitChan)
59+
go slave.Wait(quitChan)
60+
61+
<-quitChan
62+
63+
global.LOG.Info("websocket finished")
64+
closeTerminalConn(wsConn)
65+
}
66+
67+
func prepareTerminalSession(c *gin.Context) (*websocket.Conn, int, int, bool) {
2368
wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
2469
if err != nil {
2570
global.LOG.Errorf("gin context http handler failed, err: %v", err)
26-
return
71+
return nil, 0, 0, false
2772
}
28-
defer wsConn.Close()
2973

3074
if global.CONF.Base.IsDemo {
3175
if wshandleError(wsConn, errors.New(" demo server, prohibit this operation!")) {
32-
return
76+
return nil, 0, 0, false
3377
}
3478
}
3579

3680
cols, err := strconv.Atoi(c.DefaultQuery("cols", "80"))
3781
if wshandleError(wsConn, errors.WithMessage(err, "invalid param cols in request")) {
38-
return
82+
return nil, 0, 0, false
3983
}
4084
rows, err := strconv.Atoi(c.DefaultQuery("rows", "40"))
4185
if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) {
86+
return nil, 0, 0, false
87+
}
88+
return wsConn, cols, rows, true
89+
}
90+
91+
func (b *BaseApi) runSSHSession(c *gin.Context, client *ssh.SSHClient, clientErr error, command string) {
92+
wsConn, cols, rows, ok := prepareTerminalSession(c)
93+
if !ok {
4294
return
4395
}
96+
defer wsConn.Close()
4497

45-
hostID, _ := strconv.Atoi(c.DefaultQuery("id", "0"))
46-
var client *ssh.SSHClient
47-
if hostID > 0 {
48-
host, err := service.GetHostInfo(uint(hostID))
49-
if wshandleError(wsConn, errors.WithMessage(err, "load host info by id failed")) {
50-
return
51-
}
52-
connInfo := ssh.ConnInfo{
53-
Addr: host.Addr,
54-
Port: int(host.Port),
55-
User: host.User,
56-
AuthMode: host.AuthMode,
57-
Password: host.Password,
58-
PrivateKey: []byte(host.PrivateKey),
59-
}
60-
if len(host.PassPhrase) != 0 {
61-
connInfo.PassPhrase = []byte(host.PassPhrase)
62-
}
63-
client, err = ssh.NewClient(connInfo)
64-
if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection. Please check the host information")) {
65-
return
66-
}
67-
} else {
68-
client, err = loadLocalConn()
69-
if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection. Please check the host information")) {
70-
return
71-
}
98+
if wshandleError(wsConn, errors.WithMessage(clientErr, "failed to set up the connection. Please check the host information")) {
99+
return
72100
}
73101
defer client.Close()
74-
command := c.DefaultQuery("command", "")
102+
75103
sws, err := terminal.NewLogicSshWsSession(cols, rows, client.Client, wsConn, command)
76104
if wshandleError(wsConn, err) {
77105
return
@@ -84,34 +112,38 @@ func (b *BaseApi) WsSSH(c *gin.Context) {
84112

85113
<-quitChan
86114

115+
closeTerminalConn(wsConn)
116+
}
117+
118+
func closeTerminalConn(wsConn *websocket.Conn) {
87119
dt := time.Now().Add(time.Second)
88120
_ = wsConn.WriteControl(websocket.CloseMessage, nil, dt)
89121
}
90122

91-
func (b *BaseApi) ContainerWsSSH(c *gin.Context) {
92-
wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
123+
func newHostSSHClient(host *model.Host, err error) (*ssh.SSHClient, error) {
93124
if err != nil {
94-
global.LOG.Errorf("gin context http handler failed, err: %v", err)
95-
return
96-
}
97-
defer wsConn.Close()
98-
99-
if global.CONF.Base.IsDemo {
100-
if wshandleError(wsConn, errors.New(" demo server, prohibit this operation!")) {
101-
return
102-
}
125+
return nil, errors.WithMessage(err, "load host info by id failed")
103126
}
104-
105-
cols, err := strconv.Atoi(c.DefaultQuery("cols", "80"))
106-
if wshandleError(wsConn, errors.WithMessage(err, "invalid param cols in request")) {
107-
return
127+
connInfo := ssh.ConnInfo{
128+
Addr: host.Addr,
129+
Port: int(host.Port),
130+
User: host.User,
131+
AuthMode: host.AuthMode,
132+
Password: host.Password,
133+
PrivateKey: []byte(host.PrivateKey),
108134
}
109-
rows, err := strconv.Atoi(c.DefaultQuery("rows", "40"))
110-
if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) {
111-
return
135+
if len(host.PassPhrase) != 0 {
136+
connInfo.PassPhrase = []byte(host.PassPhrase)
112137
}
138+
return ssh.NewClient(connInfo)
139+
}
140+
141+
func loadContainerTerminalCommand(c *gin.Context) (*terminal.LocalCommand, error) {
113142
source := c.Query("source")
114-
var initCmd []string
143+
var (
144+
initCmd []string
145+
err error
146+
)
115147
switch source {
116148
case "redis", "redis-cluster":
117149
initCmd, err = loadRedisInitCmd(c, source)
@@ -122,33 +154,12 @@ func (b *BaseApi) ContainerWsSSH(c *gin.Context) {
122154
case "database":
123155
initCmd, err = loadDatabaseInitCmd(c)
124156
default:
125-
if wshandleError(wsConn, fmt.Errorf("not support such source %s", source)) {
126-
return
127-
}
157+
return nil, fmt.Errorf("not support such source %s", source)
128158
}
129-
if wshandleError(wsConn, err) {
130-
return
131-
}
132-
slave, err := terminal.NewCommand("docker", initCmd...)
133-
if wshandleError(wsConn, err) {
134-
return
135-
}
136-
defer slave.Close()
137-
138-
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, false)
139-
if wshandleError(wsConn, err) {
140-
return
159+
if err != nil {
160+
return nil, err
141161
}
142-
143-
quitChan := make(chan bool, 3)
144-
tty.Start(quitChan)
145-
go slave.Wait(quitChan)
146-
147-
<-quitChan
148-
149-
global.LOG.Info("websocket finished")
150-
dt := time.Now().Add(time.Second)
151-
_ = wsConn.WriteControl(websocket.CloseMessage, nil, dt)
162+
return terminal.NewCommand("docker", initCmd...)
152163
}
153164

154165
func loadRedisInitCmd(c *gin.Context, redisType string) ([]string, error) {

0 commit comments

Comments
 (0)