Skip to content

Commit

Permalink
perf: 优化 sftp 直连格式连接资产
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Jul 9, 2024
1 parent 8e9f1b6 commit 8c69b8d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
23 changes: 23 additions & 0 deletions pkg/handler/direct_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ func (d *DirectHandler) NewSFTPHandler() *SftpHandler {
opts = append(opts, srvconn.WithLoginFrom(model.LoginFromSSH))
if !d.opts.IsTokenConnection() {
opts = append(opts, srvconn.WithAssets(d.opts.assets))
if len(d.opts.assets) == 1 {
asset := d.opts.assets[0]
if permAssetDetail, err := d.jmsService.GetUserPermAssetDetailById(d.opts.User.ID, asset.ID); err == nil {
matchedAccount := GetMatchedAccounts(permAssetDetail.PermedAccounts, d.opts.targetAccount)
if len(matchedAccount) == 1 {
selectAccount := &matchedAccount[0]
req := service.SuperConnectTokenReq{
UserId: d.opts.User.ID,
AssetId: asset.ID,
Account: selectAccount.Alias,
Protocol: model.ProtocolSFTP,
ConnectMethod: model.ProtocolSSH,
RemoteAddr: addr,
}
if tokenInfo, err1 := d.jmsService.CreateSuperConnectToken(&req); err1 == nil {
if connectToken, err2 := d.jmsService.GetConnectTokenInfo(tokenInfo.ID); err2 == nil {
opts = append(opts, srvconn.WithConnectToken(&connectToken))
opts = append(opts, srvconn.WithAssets(nil))
}
}
}
}
}
} else {
opts = append(opts, srvconn.WithConnectToken(d.opts.tokenInfo))
}
Expand Down
13 changes: 0 additions & 13 deletions pkg/httpd/sftpvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,6 @@ func NewUserVolume(jmsService *service.JMService, opts ...VolumeOption) *UserVol
homeName := "Home"
basePath := "/"
asset := volOpts.asset
if volOpts.connectToken != nil {
assetDetail := volOpts.connectToken.Asset
asset = &model.PermAsset{
ID: assetDetail.ID,
Name: assetDetail.Name,
Address: assetDetail.Address,
Comment: assetDetail.Comment,
Platform: assetDetail.Platform,
OrgID: assetDetail.OrgID,
OrgName: assetDetail.OrgName,
IsActive: assetDetail.IsActive,
}
}
if asset != nil {
folderName := asset.Name
if strings.Contains(folderName, "/") {
Expand Down
1 change: 1 addition & 0 deletions pkg/httpd/webfolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (h *webFolder) CheckValidation() error {
if h.ws.ConnectToken != nil {
connectToken := h.ws.ConnectToken
volOpts = append(volOpts, WithConnectToken(connectToken))
volOpts = append(volOpts, WithAsset(nil))
}
h.volume = NewUserVolume(apiClient, volOpts...)
return nil
Expand Down
37 changes: 37 additions & 0 deletions pkg/srvconn/sftpconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ type UserSftpConn struct {
searchDir *SearchResultDir

jmsService *service.JMService

assetDir *AssetDir
}

func (u *UserSftpConn) ReadDir(path string) (res []os.FileInfo, err error) {
if u.assetDir != nil {
return u.assetDir.ReadDir(path)
}
fi, restPath := u.ParsePath(path)
if rootDir, ok := fi.(*UserSftpConn); ok {
return rootDir.List()
Expand All @@ -49,6 +54,9 @@ func (u *UserSftpConn) ReadDir(path string) (res []os.FileInfo, err error) {
}

func (u *UserSftpConn) Stat(path string) (res os.FileInfo, err error) {
if u.assetDir != nil {
return u.assetDir.Stat(path)
}
fi, restPath := u.ParsePath(path)
if rootDir, ok := fi.(*UserSftpConn); ok {
return rootDir, nil
Expand All @@ -65,6 +73,9 @@ func (u *UserSftpConn) Stat(path string) (res os.FileInfo, err error) {
}

func (u *UserSftpConn) ReadLink(path string) (name string, err error) {
if u.assetDir != nil {
return u.assetDir.ReadLink(path)
}
fi, restPath := u.ParsePath(path)
if _, ok := fi.(*UserSftpConn); ok && restPath == "" {
return "", sftp.ErrSshFxOpUnsupported
Expand All @@ -81,6 +92,9 @@ func (u *UserSftpConn) ReadLink(path string) (name string, err error) {
}

func (u *UserSftpConn) Rename(oldNamePath, newNamePath string) (err error) {
if u.assetDir != nil {
return u.assetDir.Rename(oldNamePath, newNamePath)
}
oldFi, oldRestPath := u.ParsePath(oldNamePath)
newFi, newRestPath := u.ParsePath(newNamePath)
if oldAssetDir, ok := oldFi.(*AssetDir); ok {
Expand All @@ -95,6 +109,9 @@ func (u *UserSftpConn) Rename(oldNamePath, newNamePath string) (err error) {
}

func (u *UserSftpConn) RemoveDirectory(path string) (err error) {
if u.assetDir != nil {
return u.assetDir.RemoveDirectory(path)
}
fi, restPath := u.ParsePath(path)
if _, ok := fi.(*UserSftpConn); ok && restPath == "" {
return sftp.ErrSshFxPermissionDenied
Expand All @@ -110,6 +127,9 @@ func (u *UserSftpConn) RemoveDirectory(path string) (err error) {
}

func (u *UserSftpConn) Remove(path string) (err error) {
if u.assetDir != nil {
return u.assetDir.Remove(path)
}
fi, restPath := u.ParsePath(path)
if _, ok := fi.(*UserSftpConn); ok && restPath == "" {
return sftp.ErrSshFxPermissionDenied
Expand All @@ -125,6 +145,9 @@ func (u *UserSftpConn) Remove(path string) (err error) {
}

func (u *UserSftpConn) MkdirAll(path string) (err error) {
if u.assetDir != nil {
return u.assetDir.MkdirAll(path)
}
fi, restPath := u.ParsePath(path)
if _, ok := fi.(*UserSftpConn); ok && restPath == "" {
return sftp.ErrSshFxPermissionDenied
Expand All @@ -140,6 +163,9 @@ func (u *UserSftpConn) MkdirAll(path string) (err error) {
}

func (u *UserSftpConn) Symlink(oldNamePath, newNamePath string) (err error) {
if u.assetDir != nil {
return u.assetDir.Symlink(oldNamePath, newNamePath)
}
oldFi, oldRestPath := u.ParsePath(oldNamePath)
newFi, newRestPath := u.ParsePath(newNamePath)
if oldAssetDir, ok := oldFi.(*AssetDir); ok {
Expand All @@ -153,6 +179,9 @@ func (u *UserSftpConn) Symlink(oldNamePath, newNamePath string) (err error) {
}

func (u *UserSftpConn) Create(path string) (*SftpFile, error) {
if u.assetDir != nil {
return u.assetDir.Create(path)
}
fi, restPath := u.ParsePath(path)
if _, ok := fi.(*UserSftpConn); ok {
return nil, sftp.ErrSshFxPermissionDenied
Expand All @@ -169,6 +198,9 @@ func (u *UserSftpConn) Create(path string) (*SftpFile, error) {
}

func (u *UserSftpConn) Open(path string) (*SftpFile, error) {
if u.assetDir != nil {
return u.assetDir.Open(path)
}
fi, restPath := u.ParsePath(path)
if _, ok := fi.(*UserSftpConn); ok {
return nil, sftp.ErrSshFxPermissionDenied
Expand Down Expand Up @@ -220,6 +252,9 @@ func (u *UserSftpConn) Sys() interface{} {
}

func (u *UserSftpConn) List() (res []os.FileInfo, err error) {
if u.assetDir != nil {
return u.assetDir.ReadDir("/")
}
for _, item := range u.Dirs {
res = append(res, item)
}
Expand Down Expand Up @@ -345,6 +380,8 @@ func (u *UserSftpConn) generateSubFoldersFromToken(token *model.ConnectToken) ma
opts = append(opts, WithFromType(u.loginFrom))
assetDir := NewAssetDir(u.jmsService, u.User, opts...)
dirs[folderName] = &assetDir
assetDir.loadSystemUsers()
u.assetDir = &assetDir
return dirs
}

Expand Down

0 comments on commit 8c69b8d

Please sign in to comment.