-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf:兼容ssh登录时的密码修改提示&修复把用户名或密码输入提示误判为命令结束提示符的问题
- Loading branch information
Showing
13 changed files
with
284 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/3th1nk/easyshell/pkg/interceptor" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
DefaultPromptTailChars = `$#%>\]:` | ||
DefaultPromptSuffixPattern = `.*[` + DefaultPromptTailChars + `]\s*$` | ||
) | ||
|
||
var ( | ||
DefaultPromptRegex = regexp.MustCompile(`\S+` + DefaultPromptSuffixPattern) | ||
FlexibleOptionPromptRegex = regexp.MustCompile(interceptor.FlexibleOptionPromptPattern) | ||
UsernameRegex = regexp.MustCompile(`(?i).*(login|user(name)?):\s*$`) | ||
PasswordRegex = regexp.MustCompile(`(?i).*pass(word)?:\s*$`) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,10 @@ import ( | |
) | ||
|
||
func TestDefaultPromptRegex(t *testing.T) { | ||
var rw ReadWriter | ||
for _, obj := range []struct { | ||
Prompt string | ||
Matched bool | ||
Prompt string | ||
Expect bool | ||
}{ | ||
{"root@test-01 $", true}, | ||
{"root@test-01 #", true}, | ||
|
@@ -33,8 +34,13 @@ func TestDefaultPromptRegex(t *testing.T) { | |
{"$", false}, | ||
{" # ", false}, | ||
{"[[email protected] /home/mon]", true}, | ||
{"[testuser@localhost ~]$ Login:", false}, | ||
{"[testuser@localhost ~]$ Username:", false}, | ||
{"[testuser@localhost ~]$ Password:", false}, | ||
} { | ||
assert.Equal(t, obj.Matched, defaultPromptRegex.MatchString(obj.Prompt)) | ||
if obj.Expect != rw.IsEndLine(obj.Prompt) { | ||
t.Error(obj.Prompt) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package interceptor | ||
|
||
import ( | ||
"github.com/3th1nk/easygo/util/strUtil" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// DefaultOptionPromptPattern 默认选项的匹配规则 | ||
DefaultOptionPromptPattern = `(?i)[\[(]y(es)?[/|]no?[\])][?:]\s*$` | ||
// FlexibleOptionPromptPattern 灵活选项的匹配规则 | ||
FlexibleOptionPromptPattern = `(?i)[\[(][a-z]+([/|][a-z\[\]]+)+[\])][?:]\s*$` | ||
) | ||
|
||
func AlwaysYes(showOut ...bool) Interceptor { | ||
showOut = append(showOut, false) | ||
return Regexp(regexp.MustCompile(DefaultOptionPromptPattern), AppendLF("y"), LastLine, showOut...) | ||
} | ||
|
||
func AlwaysNo(showOut ...bool) Interceptor { | ||
showOut = append(showOut, false) | ||
return Regexp(regexp.MustCompile(DefaultOptionPromptPattern), AppendLF("n"), LastLine, showOut...) | ||
} | ||
|
||
func AlwaysOption(optionIndex int, showOut ...bool) Interceptor { | ||
showOut = append(showOut, false) | ||
return func(str string) (bool, bool, string) { | ||
str = LastLine(str) | ||
if re := regexp.MustCompile(FlexibleOptionPromptPattern); re.MatchString(str) { | ||
// 截取选项部分,并去掉前后的括号,例如 [yes/no] -> yes/no | ||
if idx := strings.IndexAny(str, "[("); idx != -1 { | ||
str = str[idx+1:] | ||
} | ||
if idx := strings.IndexAny(str, ")]"); idx != -1 { | ||
str = str[:idx] | ||
} | ||
// 获取所有选项,这里options一定不为空(由匹配正则保证) | ||
options := strUtil.Split(str, "/|", false, func(s string) string { | ||
// 有的选项是可选的,需要去掉括号,例如 yes/no/[fingerprint] | ||
s = strings.TrimPrefix(s, "[") | ||
s = strings.TrimSuffix(s, "]") | ||
return s | ||
}) | ||
|
||
// 选择的选项不能超出范围 | ||
var input string | ||
if optionIndex >= 0 && optionIndex < len(options) { | ||
input = strings.TrimSpace(options[optionIndex]) | ||
} | ||
return true, showOut[0], AppendLF(input) | ||
} | ||
|
||
return false, false, "" | ||
} | ||
} |
Oops, something went wrong.