Skip to content

Commit eafe89a

Browse files
authored
Merge pull request #8 from TNK-Studio/dev
[FIX] fix allow users input error
2 parents 4e3ceee + 91902b3 commit eafe89a

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

core/pui/baseui.go

+2
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,7 @@ var allowUsersPrompt = func(defaultShow string, stdio io.ReadWriteCloser) prompt
110110
},
111111
},
112112
),
113+
Stdin: stdio,
114+
Stdout: stdio,
113115
}
114116
}

core/pui/server.go

+56-8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func EditServer(server *config.Server, sess *ssh.Session) (*config.Server, error
8585
// AddServerSSHUser add server ssh user
8686
func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSHUser, error) {
8787
logger.Logger.Info("Add a server ssh user.")
88+
var runningErr *error
89+
8890
stdio := utils.SessIO(sess)
8991
usernamePui := sshUserNamePrompt("", stdio)
9092
server := (*config.Conf.Servers)[serverKey]
@@ -124,9 +126,10 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
124126
return nil, nil, err
125127
}
126128
} else {
129+
defaultPath := fmt.Sprintf("~/.ssh/id_%s", strings.ReplaceAll(server.Host, ".", "_"))
127130
identityFilePui = identityFilePrompt(
128131
"Enter new identity file path",
129-
"",
132+
defaultPath,
130133
stdio,
131134
FileNotExited("Identity File"),
132135
IsNotDir(),
@@ -158,14 +161,27 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
158161
if err != nil {
159162
return nil, nil, err
160163
}
164+
165+
runningErr = &err
166+
defer func() {
167+
if *runningErr != nil && identityFile != "" {
168+
if utils.FileExited(identityFile) {
169+
os.Remove(utils.FilePath(identityFile))
170+
}
171+
}
172+
}()
173+
161174
_, pubKeyFile, err := sshd.GenKey(identityFile)
162175
if err != nil {
163176
return nil, nil, err
164177
}
178+
165179
times := 0
166180
for {
167181
if times > 2 {
168-
return nil, nil, errors.New("Failed to copy public key to the server. ")
182+
err = errors.New("Failed to copy public key to the server. ")
183+
runningErr = &err
184+
return nil, nil, err
169185
}
170186
serverPasswdPui := promptui.Prompt{
171187
Label: "Server password (use to send your public key to the server). ",
@@ -181,7 +197,7 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
181197
continue
182198
}
183199

184-
err = sshd.CopyID(
200+
_, err = sshd.CopyID(
185201
username,
186202
server.Host,
187203
server.Port,
@@ -201,12 +217,18 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
201217
allowAllUserPui := allowAllUserPrompt("", stdio)
202218

203219
allAllUser, err := allowAllUserPui.Run()
220+
if err != nil {
221+
runningErr = &err
222+
return nil, nil, err
223+
}
224+
204225
allowUsersStr := ""
205226
if allAllUser == "no" {
206227
allowUsersPui := allowUsersPrompt("", stdio)
207228

208229
allowUsersStr, err = allowUsersPui.Run()
209230
if err != nil {
231+
runningErr = &err
210232
return nil, nil, err
211233
}
212234
}
@@ -317,6 +339,8 @@ func GetEditedServersMenu(
317339
// EditSSHUser EditSSHUser
318340
func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Session) (*config.SSHUser, error) {
319341
logger.Logger.Info("Delete ssh user")
342+
var runningErr *error
343+
320344
stdio := utils.SessIO(sess)
321345
usernamePui := sshUserNamePrompt(sshUser.SSHUsername, stdio)
322346

@@ -353,9 +377,10 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
353377
return nil, err
354378
}
355379
} else {
380+
defaultPath := fmt.Sprintf("~/.ssh/id_%s", strings.ReplaceAll(server.Host, ".", "_"))
356381
identityFilePui = identityFilePrompt(
357382
"Enter new identity file path",
358-
"",
383+
defaultPath,
359384
stdio,
360385
FileNotExited("Identity File"),
361386
IsNotDir(),
@@ -387,14 +412,27 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
387412
if err != nil {
388413
return nil, err
389414
}
415+
416+
runningErr = &err
417+
defer func() {
418+
if *runningErr != nil && identityFile != "" {
419+
if utils.FileExited(identityFile) {
420+
os.Remove(utils.FilePath(identityFile))
421+
}
422+
}
423+
}()
424+
390425
_, pubKeyFile, err := sshd.GenKey(identityFile)
391426
if err != nil {
392427
return nil, err
393428
}
429+
394430
times := 0
395431
for {
396432
if times > 2 {
397-
return nil, errors.New("Failed to copy public key to the server. ")
433+
err = errors.New("Failed to copy public key to the server. ")
434+
runningErr = &err
435+
return nil, err
398436
}
399437
serverPasswdPui := promptui.Prompt{
400438
Label: "Server password (use to send your public key to the server). ",
@@ -410,7 +448,7 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
410448
continue
411449
}
412450

413-
err = sshd.CopyID(
451+
_, err = sshd.CopyID(
414452
username,
415453
server.Host,
416454
server.Port,
@@ -433,11 +471,21 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
433471
)
434472

435473
allAllUser, err := allowAllUserPui.Run()
474+
if err != nil {
475+
runningErr = &err
476+
return nil, err
477+
}
478+
436479
allowUsersStr := ""
437480
if allAllUser == "no" {
438-
allowUsersPui := allowUsersPrompt(strings.Join(*sshUser.AllowUsers, ","), stdio)
481+
defaultUsers := ""
482+
if sshUser.AllowUsers != nil {
483+
defaultUsers = strings.Join(*sshUser.AllowUsers, ",")
484+
}
485+
allowUsersPui := allowUsersPrompt(defaultUsers, stdio)
439486
allowUsersStr, err = allowUsersPui.Run()
440487
if err != nil {
488+
runningErr = &err
441489
return nil, err
442490
}
443491
}
@@ -455,7 +503,7 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
455503
AllowUsers: allowUsers,
456504
}
457505

458-
return newSSHUser, nil
506+
return newSSHUser, err
459507
}
460508

461509
// DelSSHUser DelSSHUser

core/sshd/copyid.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
)
1212

1313
// CopyID CopyID
14-
func CopyID(username, host string, port int, passwd, pubKeyFile string) error {
14+
func CopyID(username, host string, port int, passwd, pubKeyFile string) ([]byte, error) {
1515
client, err := GetClientByPasswd(username, host, port, passwd)
1616
if err != nil {
17-
return err
17+
return []byte(""), err
1818
}
1919

2020
file, err := os.Open(utils.FilePath(pubKeyFile))
2121
if err != nil {
22-
return err
22+
return []byte(""), err
2323
}
2424
defer file.Close()
2525

@@ -30,12 +30,10 @@ func CopyID(username, host string, port int, passwd, pubKeyFile string) error {
3030
copyIDCmd = strings.ReplaceAll(copyIDCmd, "\n", "")
3131
logger.Logger.Debugf("CopyID run command:\n%s", copyIDCmd)
3232

33-
out, err := client.Cmd(copyIDCmd).Output()
33+
output, err := client.Cmd(copyIDCmd).Output()
3434
if err != nil {
35-
return err
35+
return []byte(""), err
3636
}
3737

38-
logger.Logger.Debugf("%s", string(out))
39-
40-
return nil
38+
return output, nil
4139
}

main.go

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ func main() {
3030
}
3131

3232
ssh.Handle(func(s ssh.Session) {
33+
defer func() {
34+
if e, ok := recover().(error); ok {
35+
logger.Logger.Error(e)
36+
}
37+
s.Close()
38+
}()
3339
jps := jump.JumpService{}
3440
jps.Run(&s)
3541
})

0 commit comments

Comments
 (0)