From b8102550042582d0db39be13e3b171e69532c43f Mon Sep 17 00:00:00 2001 From: M09ic Date: Thu, 17 Feb 2022 14:08:16 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E4=BC=98=E5=8C=96=E8=BE=93=E5=87=BA=20*=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=8A=E4=BC=A0=E7=BB=93=E6=9E=9C=E7=9A=84?= =?UTF-8?q?hash=E6=A0=A1=E9=AA=8C=20*=20=E8=87=AA=E5=8A=A8=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=BB=98=E8=AE=A4=E7=9A=84ssh=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v1/rscp.go | 2 +- v1/ssh.go | 123 +++++++++++++++++++++++++++++------------------ v1/utils.go | 16 ++++++ v1/utils_test.go | 7 +++ 4 files changed, 99 insertions(+), 49 deletions(-) create mode 100644 v1/utils_test.go diff --git a/v1/rscp.go b/v1/rscp.go index 2232d1f..6e061b8 100644 --- a/v1/rscp.go +++ b/v1/rscp.go @@ -37,6 +37,7 @@ func CMD() { if err != nil { return } + fmt.Println("ssh connect successfully") if opt.upload { sshs.Upload(opt.localfile, opt.remotefile, opt.blockoffset) @@ -75,5 +76,4 @@ func (opt *options) initOptions() { os.Exit(0) } } - } diff --git a/v1/ssh.go b/v1/ssh.go index 6c7de9e..8fe19dc 100644 --- a/v1/ssh.go +++ b/v1/ssh.go @@ -25,6 +25,9 @@ func NewSSH(connectStr, pkfile string) *Rscp { return nil } + if pair := strings.Split(url.Host, ":"); len(pair) == 1 { + url.Host += ":22" + } var SSH *Rscp = &Rscp{ target: url.Host, } @@ -88,19 +91,77 @@ func (s *Rscp) Run(command string, logHistory bool) (string, error) { if err != nil { return "", err } - if bytes.Contains(output, []byte("finish")) { - return string(output), nil + if !bytes.Contains(output, []byte("sangfor")) { + return "", errors.New("command exec failed") + } + if bytes.Equal(output, []byte("\nsangfor\nfinish\n")) { + return "", io.EOF + } + return strings.Split(string(output), "\n")[0], nil +} + +func (s *Rscp) md5(filename string) string { + md5, err := s.Run("md5sum "+filename, false) + if err != nil { + return "" + } + if strings.Contains(md5, filename) { + return strings.Split(md5, " ")[0] } else { - return "", errors.New("no success") + return "" } + } var filemask = "1632002513" +func (s *Rscp) echo(content, tmpfile string) (string, error) { + cmd := fmt.Sprintf("echo %s | base64 -d > %s && md5sum %s", content, tmpfile, tmpfile) + output, err := s.Run(cmd, false) + if err != nil { + return "", nil + } + //if !strings.Contains(output, "sangfor") { + // return "", errors.New("write fail") + //} + outs := strings.Split(output, " ") + md5sum := outs[0] + return md5sum, nil +} + +func (s *Rscp) read(remotefile string, off int) ([]byte, error) { + cmd := fmt.Sprintf("dd if=%s bs=%d count=1 skip=%d 2>/dev/null | base64 -w 0 && echo", remotefile, blockSize, off) + output, err := s.Run(cmd, false) + if err != nil { + return []byte{}, err + } + //if !strings.Contains(output, "sangfor") { + // return []byte{}, errors.New("read fail") + //} + //if output == "\nsangfor\nfinish\n" { + // return []byte{}, io.EOF + //} + //outs := strings.Split(output, "\n") + return Base64Decode(output), nil +} + +func pkAuth(kPath string) ssh.AuthMethod { + + key, err := ioutil.ReadFile(kPath) + if err != nil { + log.Fatal("ssh key file read failed", err) + } + // Create the Signer for this private key. + signer, err := ssh.ParsePrivateKey(key) + if err != nil { + log.Fatal("ssh key signer failed", err) + } + return ssh.PublicKeys(signer) +} func (s *Rscp) Upload(filename string, path string, offset int) { var err error + localfilemd5 := filename ch := splitFile(filename, blockSize) - if path[len(path)-1] != '/' { path += "/" } @@ -150,23 +211,15 @@ func (s *Rscp) Upload(filename string, path string, offset int) { return } fmt.Println("rm all blocks successfully") -} -func (s *Rscp) echo(content, tmpfile string) (string, error) { - cmd := fmt.Sprintf("echo %s | base64 -d > %s && md5sum %s", content, tmpfile, tmpfile) - output, err := s.Run(cmd, false) - if err != nil { - return "", nil - } - if !strings.Contains(output, "sangfor") { - return "", errors.New("write fail") - } - outs := strings.Split(output, " ") - md5sum := outs[0] - return md5sum, nil + remotefilemd5 := s.md5(filepath.Join(tmpbase, filename)) + fmt.Printf("local file md5: %s, remote file md5:%s, check status: %t\n", localfilemd5, remotefilemd5, localfilemd5 == remotefilemd5) + } func (s Rscp) Download(remoteFile, localFile string, offset int) { + remotefilemd5 := s.md5(remoteFile) + fmt.Println("remote file md5: " + remotefilemd5) if localFile == "" { _, localFile = filepath.Split(remoteFile) } else { @@ -188,8 +241,8 @@ func (s Rscp) Download(remoteFile, localFile string, offset int) { break } _ = s.Connect() - fmt.Printf("%s\n retry %d times\n", err.Error(), retry) retry++ + fmt.Printf("%s, retry %d times\n", err.Error(), retry) continue } _, err = writer.Write(content) @@ -201,38 +254,12 @@ func (s Rscp) Download(remoteFile, localFile string, offset int) { fmt.Printf("read %d block %d bytes successfully, next block id: %d \n", offset, len(content), offset+1) offset++ retry = 0 + time.Sleep(1000) } - fmt.Printf("download %s successfully, write it to %s \n", remoteFile, localFile) -} - -func (s *Rscp) read(remotefile string, off int) ([]byte, error) { - cmd := fmt.Sprintf("dd if=%s bs=%d count=1 skip=%d 2>/dev/null | base64 -w 0 && echo", remotefile, blockSize, off) - output, err := s.Run(cmd, false) - if err != nil { - return []byte{}, err - } - if !strings.Contains(output, "sangfor") { - return []byte{}, errors.New("read fail") - } - if output == "\nsangfor\nfinish\n" { - return []byte{}, io.EOF - } - outs := strings.Split(output, "\n") - return Base64Decode(outs[0]), nil -} + localfilemd5 := fileMd5(localFile) -func pkAuth(kPath string) ssh.AuthMethod { - - key, err := ioutil.ReadFile(kPath) - if err != nil { - log.Fatal("ssh key file read failed", err) - } - // Create the Signer for this private key. - signer, err := ssh.ParsePrivateKey(key) - if err != nil { - log.Fatal("ssh key signer failed", err) - } - return ssh.PublicKeys(signer) + fmt.Printf("download %s successfully, save to %s \n", remoteFile, localFile) + fmt.Printf("local file md5: %s, remote file md5: %s, check status: %t\n", localfilemd5, remotefilemd5, localfilemd5 == remotefilemd5) } type block struct { diff --git a/v1/utils.go b/v1/utils.go index 3f55ba3..accb6eb 100644 --- a/v1/utils.go +++ b/v1/utils.go @@ -5,6 +5,8 @@ import ( "encoding/base64" "encoding/hex" "errors" + "fmt" + "io/ioutil" "os" ) @@ -57,3 +59,17 @@ func fileSize(filename string) int { } return 0 } + +func fileMd5(filename string) string { + file, err := os.Open(filename) + defer file.Close() + if err != nil { + fmt.Println(err.Error()) + } + content, err := ioutil.ReadAll(file) + if err != nil { + fmt.Println(err.Error()) + } + + return Md5Hash(content) +} diff --git a/v1/utils_test.go b/v1/utils_test.go new file mode 100644 index 0000000..82b8b62 --- /dev/null +++ b/v1/utils_test.go @@ -0,0 +1,7 @@ +package v1 + +import "testing" + +func TestFileMd5(t *testing.T) { + println(fileMd5("OPatch")) +}