mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-21 06:09:39 +08:00
feat(webconsole): support sftp for ssh (#18915)
This commit is contained in:
@@ -39,6 +39,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/webconsole/command"
|
||||
"yunion.io/x/onecloud/pkg/webconsole/models"
|
||||
o "yunion.io/x/onecloud/pkg/webconsole/options"
|
||||
"yunion.io/x/onecloud/pkg/webconsole/server"
|
||||
"yunion.io/x/onecloud/pkg/webconsole/session"
|
||||
)
|
||||
|
||||
@@ -57,6 +58,9 @@ func InitHandlers(app *appsrv.Application) {
|
||||
app.AddHandler("POST", ApiPathPrefix+"ssh/<ip>", auth.Authenticate(handleSshShell))
|
||||
app.AddHandler("POST", ApiPathPrefix+"server/<id>", auth.Authenticate(handleServerRemoteConsole))
|
||||
app.AddHandler("POST", ApiPathPrefix+"server-rdp/<id>", auth.Authenticate(handleServerRemoteRDPConsole))
|
||||
app.AddHandler("GET", ApiPathPrefix+"sftp/<session-id>/list", server.HandleSftpList)
|
||||
app.AddHandler("GET", ApiPathPrefix+"sftp/<session-id>/download", server.HandleSftpDownload)
|
||||
app.AddHandler("POST", ApiPathPrefix+"sftp/<session-id>/upload", server.HandleSftpUpload)
|
||||
|
||||
for _, man := range []db.IModelManager{
|
||||
models.GetCommandLogManager(),
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/sftp"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/appsrv"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
)
|
||||
|
||||
const (
|
||||
SESSION_ID = "<session-id>"
|
||||
)
|
||||
|
||||
var (
|
||||
sftpMux = sync.Mutex{}
|
||||
sftpClients = make(map[string]*sftp.Client)
|
||||
)
|
||||
|
||||
func addSftpClient(sId string, client *sftp.Client) {
|
||||
sftpMux.Lock()
|
||||
defer sftpMux.Unlock()
|
||||
sftpClients[sId] = client
|
||||
}
|
||||
|
||||
func delSftpClient(sId string) {
|
||||
sftpMux.Lock()
|
||||
defer sftpMux.Unlock()
|
||||
delete(sftpClients, sId)
|
||||
}
|
||||
|
||||
func getSftpClient(sId string) (*sftp.Client, error) {
|
||||
sftpMux.Lock()
|
||||
defer sftpMux.Unlock()
|
||||
client, ok := sftpClients[sId]
|
||||
if !ok {
|
||||
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", sId)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
type sFileList struct {
|
||||
Name string
|
||||
Path string
|
||||
Mode fs.FileMode
|
||||
}
|
||||
|
||||
func HandleSftpList(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
params, query, _ := appsrv.FetchEnv(ctx, w, r)
|
||||
dir := "/"
|
||||
if query.Contains("path") {
|
||||
dir, _ = query.GetString("path")
|
||||
}
|
||||
sId := params[SESSION_ID]
|
||||
files, err := func() ([]sFileList, error) {
|
||||
sftp, err := getSftpClient(sId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "getSftpClient")
|
||||
}
|
||||
files, err := sftp.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "ReadDir %s", dir)
|
||||
}
|
||||
ret := []sFileList{}
|
||||
for _, f := range files {
|
||||
ret = append(ret, sFileList{
|
||||
Name: f.Name(),
|
||||
Mode: f.Mode(),
|
||||
Path: path.Join(dir, f.Name()),
|
||||
})
|
||||
}
|
||||
return ret, nil
|
||||
}()
|
||||
if err != nil {
|
||||
httperrors.GeneralServerError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
appsrv.SendJSON(w, jsonutils.Marshal(files))
|
||||
}
|
||||
|
||||
func HandleSftpUpload(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
params, query, _ := appsrv.FetchEnv(ctx, w, r)
|
||||
dir := "/"
|
||||
if query.Contains("path") {
|
||||
dir, _ = query.GetString("path")
|
||||
}
|
||||
sId := params[SESSION_ID]
|
||||
|
||||
err := func() error {
|
||||
sftp, err := getSftpClient(sId)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "getSftpClient")
|
||||
}
|
||||
|
||||
r.ParseMultipartForm(32 << 20)
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "FormFile")
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
_, err = sftp.Stat(dir)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "stat %s", dir)
|
||||
}
|
||||
|
||||
newFile, err := sftp.Create(path.Join(dir, header.Filename))
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "create file")
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
defer newFile.Close()
|
||||
|
||||
_, err = newFile.ReadFrom(file)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "ReadFrom")
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
httperrors.GeneralServerError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
appsrv.SendJSON(w, jsonutils.Marshal(map[string]string{"status": "success"}))
|
||||
}
|
||||
|
||||
func HandleSftpDownload(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
params, query, _ := appsrv.FetchEnv(ctx, w, r)
|
||||
if !query.Contains("path") {
|
||||
httperrors.GeneralServerError(ctx, w, httperrors.NewMissingParameterError("path"))
|
||||
return
|
||||
}
|
||||
dir, _ := query.GetString("path")
|
||||
sId := params[SESSION_ID]
|
||||
|
||||
err := func() error {
|
||||
sftp, err := getSftpClient(sId)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "getSftpClient")
|
||||
}
|
||||
file, err := sftp.Stat(dir)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "stat %s", dir)
|
||||
}
|
||||
if file.IsDir() {
|
||||
return fmt.Errorf("dir %s can not download", dir)
|
||||
}
|
||||
|
||||
reader, err := sftp.Open(dir)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "open file")
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
w.Header().Add("Content-Disposition", "attachment;filename="+file.Name())
|
||||
w.Header().Add("Content-Type", "application/octet-stream")
|
||||
_, err = io.Copy(w, reader)
|
||||
return err
|
||||
}()
|
||||
if err != nil {
|
||||
httperrors.GeneralServerError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/anacrolix/sync"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pkg/sftp"
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -45,6 +46,7 @@ type WebsocketServer struct {
|
||||
StdinPipe io.WriteCloser
|
||||
ws *websocket.Conn
|
||||
conn *ssh.Client
|
||||
sftp *sftp.Client
|
||||
timer *time.Timer
|
||||
}
|
||||
|
||||
@@ -104,6 +106,12 @@ func (s *WebsocketServer) initWs(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.Wrapf(err, "dial %s", addr)
|
||||
}
|
||||
|
||||
s.sftp, err = sftp.NewClient(s.conn)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "new sftp client")
|
||||
}
|
||||
addSftpClient(s.Session.Id, s.sftp)
|
||||
|
||||
s.session, err = s.conn.NewSession()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "NewSession")
|
||||
@@ -225,6 +233,8 @@ func (s *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s.ws.Close()
|
||||
s.StdinPipe.Close()
|
||||
s.session.Close()
|
||||
delSftpClient(s.Session.Id)
|
||||
s.sftp.Close()
|
||||
s.conn.Close()
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user