修复sftp上传下载数据丢失问题
This commit is contained in:
		
							parent
							
								
									2c1ec13a0c
								
							
						
					
					
						commit
						9bff38bd4b
					
				
							
								
								
									
										61
									
								
								ssh.go
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								ssh.go
									
									
									
									
									
								
							| @ -53,6 +53,7 @@ type StarShell struct { | ||||
| 	isprint bool | ||||
| 	isfuncs bool | ||||
| 	iscolor bool | ||||
| 	isecho  bool | ||||
| 	funcs   func(string) | ||||
| } | ||||
| 
 | ||||
| @ -101,6 +102,10 @@ func (this *StarShell) SwitchNoColor(is bool) { | ||||
| 	this.iscolor = is | ||||
| } | ||||
| 
 | ||||
| func (this *StarShell) SwitchEcho(is bool) { | ||||
| 	this.isecho = is | ||||
| } | ||||
| 
 | ||||
| func (this *StarShell) TrimColor(str string) string { | ||||
| 	if this.iscolor { | ||||
| 		return SedColor(str) | ||||
| @ -129,6 +134,7 @@ func (this *StarShell) SetFunc(funcs func(string)) { | ||||
| func (this *StarShell) Clear() { | ||||
| 	this.outbyte = []byte{} | ||||
| 	this.errbyte = []byte{} | ||||
| 	time.Sleep(time.Millisecond * 5) | ||||
| } | ||||
| 
 | ||||
| func (this *StarShell) ShellClear(cmd string, sleep int) (string, string, error) { | ||||
| @ -142,7 +148,35 @@ func (this *StarShell) Shell(cmd string, sleep int) (string, string, error) { | ||||
| 		return "", "", err | ||||
| 	} | ||||
| 	tmp1, tmp2, err := this.GetResult(sleep) | ||||
| 	return this.TrimColor(strings.TrimSpace(string(tmp1))), this.TrimColor(strings.TrimSpace(string(tmp2))), err | ||||
| 	tmps := this.TrimColor(strings.TrimSpace(string(tmp1))) | ||||
| 	if this.isecho { | ||||
| 		n := len(strings.Split(cmd, "\n")) | ||||
| 		if n == 1 { | ||||
| 			list := strings.SplitN(tmps, "\n", 2) | ||||
| 			if len(list) == 2 { | ||||
| 				tmps = list[1] | ||||
| 			} | ||||
| 		} else { | ||||
| 			list := strings.Split(tmps, "\n") | ||||
| 			cmds := strings.Split(cmd, "\n") | ||||
| 			for _, v := range cmds { | ||||
| 				for k, v2 := range list { | ||||
| 					if strings.TrimSpace(v2) == strings.TrimSpace(v) { | ||||
| 						list[k] = "" | ||||
| 						break | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 			tmps = "" | ||||
| 			for _, v := range list { | ||||
| 				if v != "" { | ||||
| 					tmps += v + "\n" | ||||
| 				} | ||||
| 			} | ||||
| 			tmps = tmps[0 : len(tmps)-1] | ||||
| 		} | ||||
| 	} | ||||
| 	return tmps, this.TrimColor(strings.TrimSpace(string(tmp2))), err | ||||
| } | ||||
| 
 | ||||
| func (this *StarShell) GetResult(sleep int) ([]byte, []byte, error) { | ||||
| @ -224,12 +258,15 @@ func (this *StarSSH) NewShell() (shell *StarShell, err error) { | ||||
| 	tmp, _ = shell.Session.StderrPipe() | ||||
| 	shell.er = bufio.NewReader(tmp) | ||||
| 	err = shell.Session.Shell() | ||||
| 	shell.isecho = true | ||||
| 	go shell.Session.Wait() | ||||
| 	shell.WriteCommand("bash") | ||||
| 	shell.WriteCommand("export PS1= ") | ||||
| 	shell.WriteCommand("export PS2= ") | ||||
| 	go shell.gohub() | ||||
| 	time.Sleep(1 * time.Second) | ||||
| 	shell.Clear() | ||||
| 
 | ||||
| 	shell.Clear() | ||||
| 	return | ||||
| } | ||||
| 
 | ||||
| @ -354,14 +391,14 @@ func NewSession(client *ssh.Client) (*ssh.Session, error) { | ||||
| 	if session, err = client.NewSession(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 
 | ||||
| 	modes := ssh.TerminalModes{ | ||||
| 		ssh.ECHO:          0,     // disable echoing | ||||
| 		ssh.ECHO: 1, // 还是要强制开启 | ||||
| 		//ssh.IGNCR:         0, | ||||
| 		ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud | ||||
| 		ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud | ||||
| 	} | ||||
| 
 | ||||
| 	if err := session.RequestPty("xterm", 80, 40, modes); err != nil { | ||||
| 	if err := session.RequestPty("vt100", 80, 40, modes); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return session, nil | ||||
| @ -412,7 +449,7 @@ func Connect(user, password, host, key string, port int, cipherList []string) (* | ||||
| 
 | ||||
| 	if len(cipherList) == 0 { | ||||
| 		config = ssh.Config{ | ||||
| 			Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"}, | ||||
| 			Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc", "chacha20-poly1305@openssh.com"}, | ||||
| 		} | ||||
| 	} else { | ||||
| 		config = ssh.Config{ | ||||
| @ -508,15 +545,15 @@ func FtpTransferOutFunc(localFilePath, remotePath string, bufcap int, rtefunc fu | ||||
| 	for { | ||||
| 		buf := make([]byte, bufcap) | ||||
| 		n, err := srcFile.Read(buf) | ||||
| 		num += n | ||||
| 		go rtefunc(float64(num) / filebig * 100) | ||||
| 		dstFile.Write(buf[:n]) | ||||
| 		if err == io.EOF { | ||||
| 			break | ||||
| 		} | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		num += n | ||||
| 		go rtefunc(float64(num) / filebig * 100) | ||||
| 		dstFile.Write(buf[:n]) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| @ -559,15 +596,15 @@ func FtpTransferInFunc(src, dst string, bufcap int, rtefunc func(float64), sftpC | ||||
| 	for { | ||||
| 		buf := make([]byte, bufcap) | ||||
| 		n, err := srcFile.Read(buf) | ||||
| 		num += n | ||||
| 		go rtefunc(float64(num) / filebig * 100) | ||||
| 		dstFile.Write(buf[:n]) | ||||
| 		if err == io.EOF { | ||||
| 			break | ||||
| 		} | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		num += n | ||||
| 		go rtefunc(float64(num) / filebig * 100) | ||||
| 		dstFile.Write(buf[:n]) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
							
								
								
									
										13
									
								
								ssh_test.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								ssh_test.go
									
									
									
									
									
								
							| @ -3,21 +3,24 @@ package sshd | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"testing" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| func TestSSH(t *testing.T) { | ||||
| 	myssh := new(StarSSH) | ||||
| 	err := myssh.Connect("root", "", "9sday.me", "c:\\id_rsa", 22) | ||||
| 	err := myssh.Connect("root", "sakua", "9sday.me", "C:\\id_rsa", 22) | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("%e", err) | ||||
| 	} | ||||
| 	shell, err := myssh.NewShell() | ||||
| 	if err != nil { | ||||
| 		fmt.Println(err) | ||||
| 		t.Fatalf("%e", err) | ||||
| 
 | ||||
| 	} | ||||
| 	shell.isprint = true | ||||
| 	time.Sleep(5 * time.Second) | ||||
| 	a, b, err := shell.ShellClear("apt update\n", 20000) | ||||
| 	shell.isprint = false | ||||
| 	fmt.Println(myssh.Exists("/root")) | ||||
| 	fmt.Println(myssh.IsFile("/root")) | ||||
| 	fmt.Println(myssh.IsFolder("/root")) | ||||
| 	a, b, err := shell.ShellClear("whoami", 2000) | ||||
| 	fmt.Println(a, b, err) | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user