Commit 0c7c4f48 authored by Eric's avatar Eric

[update]fix selecting asset bugs

parent c32db87d
...@@ -2,6 +2,7 @@ package handler ...@@ -2,6 +2,7 @@ package handler
import ( import (
"fmt" "fmt"
"io"
"strconv" "strconv"
"strings" "strings"
...@@ -176,24 +177,18 @@ func (p *AssetPagination) displayPageAssets() { ...@@ -176,24 +177,18 @@ func (p *AssetPagination) displayPageAssets() {
} }
func (p *AssetPagination) displayTipsInfo() { func (p *AssetPagination) displayTipsInfo() {
tips := []string{ displayAssetPaginationTipsInfo(p.term)
i18n.T("\nTips: Enter the asset ID and log directly into the asset.\n"),
i18n.T("\nPage up: P/p Page down: Enter|N/n BACK: b.\n"),
}
for _, tip := range tips {
_, _ = p.term.Write([]byte(tip))
}
} }
func NewUserPagination(term *utils.Terminal, uid, search string, proxy bool) *UserAssetPagination { func NewUserPagination(term *utils.Terminal, uid, search string, policy bool) *UserAssetPagination {
return &UserAssetPagination{ return &UserAssetPagination{
UserID: uid, UserID: uid,
offset: 0, offset: 0,
limit: 0, limit: 0,
search: search, search: search,
term: term, term: term,
proxy: proxy, displayPolicy: policy,
Data: model.AssetsPaginationResponse{}, Data: model.AssetsPaginationResponse{},
} }
} }
...@@ -204,8 +199,9 @@ type UserAssetPagination struct { ...@@ -204,8 +199,9 @@ type UserAssetPagination struct {
limit int limit int
search string search string
term *utils.Terminal term *utils.Terminal
proxy bool displayPolicy bool
Data model.AssetsPaginationResponse Data model.AssetsPaginationResponse
IsNeedProxy bool
} }
func (p *UserAssetPagination) Start() []model.Asset { func (p *UserAssetPagination) Start() []model.Asset {
...@@ -214,7 +210,8 @@ func (p *UserAssetPagination) Start() []model.Asset { ...@@ -214,7 +210,8 @@ func (p *UserAssetPagination) Start() []model.Asset {
for { for {
p.retrieveData() p.retrieveData()
if p.proxy && p.Data.Total == 1 { if p.displayPolicy && p.Data.Total == 1 {
p.IsNeedProxy = true
return p.Data.Data return p.Data.Data
} }
...@@ -251,6 +248,7 @@ func (p *UserAssetPagination) Start() []model.Asset { ...@@ -251,6 +248,7 @@ func (p *UserAssetPagination) Start() []model.Asset {
default: default:
if indexID, err := strconv.Atoi(line); err == nil { if indexID, err := strconv.Atoi(line); err == nil {
if indexID > 0 && indexID <= len(p.Data.Data) { if indexID > 0 && indexID <= len(p.Data.Data) {
p.IsNeedProxy = true
return []model.Asset{p.Data.Data[indexID-1]} return []model.Asset{p.Data.Data[indexID-1]}
} }
} }
...@@ -259,6 +257,7 @@ func (p *UserAssetPagination) Start() []model.Asset { ...@@ -259,6 +257,7 @@ func (p *UserAssetPagination) Start() []model.Asset {
default: default:
if indexID, err := strconv.Atoi(line); err == nil { if indexID, err := strconv.Atoi(line); err == nil {
if indexID > 0 && indexID <= len(p.Data.Data) { if indexID > 0 && indexID <= len(p.Data.Data) {
p.IsNeedProxy = true
return []model.Asset{p.Data.Data[indexID-1]} return []model.Asset{p.Data.Data[indexID-1]}
} }
} }
...@@ -348,25 +347,18 @@ func (p *UserAssetPagination) displayPageAssets() { ...@@ -348,25 +347,18 @@ func (p *UserAssetPagination) displayPageAssets() {
} }
func (p *UserAssetPagination) displayTipsInfo() { func (p *UserAssetPagination) displayTipsInfo() {
tips := []string{ displayAssetPaginationTipsInfo(p.term)
i18n.T("\nTips: Enter the asset ID and log directly into the asset.\n"),
i18n.T("\nPage up: P/p Page down: Enter|N/n BACK: b.\n"),
}
for _, tip := range tips {
_, _ = p.term.Write([]byte(tip))
}
} }
func (p *UserAssetPagination) retrieveData() { func (p *UserAssetPagination) retrieveData() {
p.limit = GetPageSize(p.term) p.limit = getPageSize(p.term)
if p.limit == 0 || p.offset < 0 || p.limit >= p.Data.Total { if p.limit == 0 || p.offset < 0 || p.limit >= p.Data.Total {
p.offset = 0 p.offset = 0
} }
p.Data = service.GetUserAssets(p.UserID, p.search, p.limit, p.offset) p.Data = service.GetUserAssets(p.UserID, p.search, p.limit, p.offset)
} }
func GetPageSize(term *utils.Terminal) int { func getPageSize(term *utils.Terminal) int {
var ( var (
pageSize int pageSize int
minHeight = 8 // 分页显示的最小高度 minHeight = 8 // 分页显示的最小高度
...@@ -391,3 +383,13 @@ func GetPageSize(term *utils.Terminal) int { ...@@ -391,3 +383,13 @@ func GetPageSize(term *utils.Terminal) int {
} }
return pageSize return pageSize
} }
func displayAssetPaginationTipsInfo(w io.Writer) {
tips := []string{
i18n.T("\nTips: Enter the asset ID and log directly into the asset.\n"),
i18n.T("\nPage up: P/p Page down: Enter|N/n BACK: b.\n"),
}
for _, tip := range tips {
_, _ = w.Write([]byte(tip))
}
}
...@@ -189,9 +189,15 @@ func (h *interactiveHandler) displayAllAssets() { ...@@ -189,9 +189,15 @@ func (h *interactiveHandler) displayAllAssets() {
<-h.loadDataDone <-h.loadDataDone
h.displayAssets(h.allAssets) h.displayAssets(h.allAssets)
default: default:
h.searchAsset("") pag := NewUserPagination(h.term, h.user.ID, "", false)
result := pag.Start()
if pag.IsNeedProxy && len(result) == 1 {
h.searchResult = h.searchResult[:0]
h.ProxyAsset(result[0])
} else {
h.searchResult = result
}
} }
} }
func (h *interactiveHandler) chooseSystemUser(systemUsers []model.SystemUser) model.SystemUser { func (h *interactiveHandler) chooseSystemUser(systemUsers []model.SystemUser) model.SystemUser {
...@@ -304,7 +310,13 @@ func (h *interactiveHandler) searchAsset(key string) { ...@@ -304,7 +310,13 @@ func (h *interactiveHandler) searchAsset(key string) {
h.displayAssets(assets) h.displayAssets(assets)
default: default:
pag := NewUserPagination(h.term, h.user.ID, key, false) pag := NewUserPagination(h.term, h.user.ID, key, false)
h.searchResult = pag.Start() result := pag.Start()
if pag.IsNeedProxy && len(result) == 1 {
h.searchResult = h.searchResult[:0]
h.ProxyAsset(result[0])
} else {
h.searchResult = result
}
} }
} }
...@@ -312,11 +324,7 @@ func (h *interactiveHandler) searchAssetOrProxy(key string) { ...@@ -312,11 +324,7 @@ func (h *interactiveHandler) searchAssetOrProxy(key string) {
if indexNum, err := strconv.Atoi(key); err == nil && len(h.searchResult) > 0 { if indexNum, err := strconv.Atoi(key); err == nil && len(h.searchResult) > 0 {
if indexNum > 0 && indexNum <= len(h.searchResult) { if indexNum > 0 && indexNum <= len(h.searchResult) {
assetSelect := h.searchResult[indexNum-1] assetSelect := h.searchResult[indexNum-1]
systemUsers := service.GetUserAssetSystemUsers(h.user.ID, assetSelect.ID) h.ProxyAsset(assetSelect)
systemUserSelect := h.chooseSystemUser(systemUsers)
h.systemUserSelect = &systemUserSelect
h.assetSelect = &assetSelect
h.Proxy(context.Background())
return return
} }
} }
...@@ -342,14 +350,10 @@ func (h *interactiveHandler) searchAssetOrProxy(key string) { ...@@ -342,14 +350,10 @@ func (h *interactiveHandler) searchAssetOrProxy(key string) {
} }
if len(assets) == 1 { if len(assets) == 1 {
systemUsers := service.GetUserAssetSystemUsers(h.user.ID, assets[0].ID) h.ProxyAsset(assets[0])
systemUserSelect := h.chooseSystemUser(systemUsers) } else {
h.systemUserSelect = &systemUserSelect
h.assetSelect = &assets[0]
h.Proxy(context.Background())
return
}
h.searchResult = assets h.searchResult = assets
}
} }
func (h *interactiveHandler) searchNodeAssets(num int) (assets model.AssetList) { func (h *interactiveHandler) searchNodeAssets(num int) (assets model.AssetList) {
...@@ -361,6 +365,14 @@ func (h *interactiveHandler) searchNodeAssets(num int) (assets model.AssetList) ...@@ -361,6 +365,14 @@ func (h *interactiveHandler) searchNodeAssets(num int) (assets model.AssetList)
return return
} }
func (h *interactiveHandler) ProxyAsset(assetSelect model.Asset) {
systemUsers := service.GetUserAssetSystemUsers(h.user.ID, assetSelect.ID)
systemUserSelect := h.chooseSystemUser(systemUsers)
h.systemUserSelect = &systemUserSelect
h.assetSelect = &assetSelect
h.Proxy(context.Background())
}
func (h *interactiveHandler) Proxy(ctx context.Context) { func (h *interactiveHandler) Proxy(ctx context.Context) {
p := proxy.ProxyServer{ p := proxy.ProxyServer{
UserConn: h.sess, UserConn: h.sess,
......
...@@ -90,7 +90,7 @@ func KeepAlive(c *gossh.Client, closed <-chan struct{}, keepInterval time.Durati ...@@ -90,7 +90,7 @@ func KeepAlive(c *gossh.Client, closed <-chan struct{}, keepInterval time.Durati
case <-t.C: case <-t.C:
_, _, err := c.SendRequest("keepalive@jumpserver.org", true, nil) _, _, err := c.SendRequest("keepalive@jumpserver.org", true, nil)
if err != nil { if err != nil {
logger.Error("SSH client %p keep alive err: ", c, err.Error()) logger.Errorf("SSH client %p keep alive err: ", c, err.Error())
return return
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment