|
| 1 | +package ctl |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "os/signal" |
| 12 | + "path" |
| 13 | + "sync" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/pkg/sftp" |
| 17 | + "golang.org/x/crypto/ssh" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + Windows = "windows" |
| 22 | + Linux = "linux" |
| 23 | + Macos = "darwin" |
| 24 | +) |
| 25 | + |
| 26 | +type ClientConfig struct { |
| 27 | + Host string |
| 28 | + Port string |
| 29 | + Username string |
| 30 | + Password string |
| 31 | + sshClient *ssh.Client |
| 32 | + sftpClient *sftp.Client |
| 33 | + LastResult string |
| 34 | +} |
| 35 | + |
| 36 | +func CreateClient(host string, port string, username, password string) *ClientConfig { |
| 37 | + cf := &ClientConfig{} |
| 38 | + var ( |
| 39 | + sshClient *ssh.Client |
| 40 | + sftpClient *sftp.Client |
| 41 | + err error |
| 42 | + ) |
| 43 | + |
| 44 | + cf.Host = host |
| 45 | + cf.Port = port |
| 46 | + cf.Username = username |
| 47 | + cf.Password = password |
| 48 | + |
| 49 | + config := ssh.ClientConfig{ |
| 50 | + User: cf.Username, |
| 51 | + Auth: []ssh.AuthMethod{ssh.Password(cf.Password)}, |
| 52 | + HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { |
| 53 | + return nil |
| 54 | + }, |
| 55 | + Timeout: 10 * time.Second, |
| 56 | + } |
| 57 | + |
| 58 | + addr := fmt.Sprintf("%s:%s", cf.Host, cf.Port) |
| 59 | + |
| 60 | + if sshClient, err = ssh.Dial("tcp", addr, &config); err != nil { |
| 61 | + log.Fatalf("connect ssh error: %s", err.Error()) |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + cf.sshClient = sshClient |
| 66 | + |
| 67 | + if sftpClient, err = sftp.NewClient(sshClient); err != nil { |
| 68 | + log.Fatalf("connect sftp error: %s", err.Error()) |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + cf.sftpClient = sftpClient |
| 73 | + return cf |
| 74 | +} |
| 75 | + |
| 76 | +func (cf *ClientConfig) RunShell(shell string) string { |
| 77 | + var ( |
| 78 | + session *ssh.Session |
| 79 | + err error |
| 80 | + ) |
| 81 | + |
| 82 | + if session, err = cf.sshClient.NewSession(); err != nil { |
| 83 | + log.Fatalf("new session error: %s", err.Error()) |
| 84 | + } |
| 85 | + defer session.Close() |
| 86 | + |
| 87 | + var output bytes.Buffer |
| 88 | + session.Stdout = &output |
| 89 | + go func() { |
| 90 | + var tmp string |
| 91 | + for { |
| 92 | + tmp = output.String() |
| 93 | + if cf.LastResult != tmp { |
| 94 | + cmd := exec.Command("cmd", "/C", "cls") |
| 95 | + cmd.Stdout = os.Stdout |
| 96 | + cmd.Run() |
| 97 | + fmt.Println(tmp) |
| 98 | + cf.LastResult = tmp |
| 99 | + } |
| 100 | + } |
| 101 | + }() |
| 102 | + if err := session.Run(shell); err != nil { |
| 103 | + fmt.Println(shell) |
| 104 | + log.Fatalf("run shell error: %s", err.Error()) |
| 105 | + } else { |
| 106 | + cf.LastResult = output.String() |
| 107 | + } |
| 108 | + return cf.LastResult |
| 109 | +} |
| 110 | + |
| 111 | +func (cf *ClientConfig) Upload(srcPath, dstPath string) { |
| 112 | + fmt.Println("searching...") |
| 113 | + s, err := os.Stat(srcPath) |
| 114 | + if err != nil { |
| 115 | + panic(err) |
| 116 | + } |
| 117 | + if s.IsDir() { |
| 118 | + cf.uploadDirectory(srcPath, dstPath) |
| 119 | + } else { |
| 120 | + cf.uploadFile(srcPath, dstPath) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func (cf *ClientConfig) uploadDirectory(srcPath, dstPath string) { |
| 125 | + |
| 126 | + localFiles, err := ioutil.ReadDir(srcPath) |
| 127 | + if err != nil { |
| 128 | + panic(err) |
| 129 | + } |
| 130 | + cf.sftpClient.Mkdir(dstPath) |
| 131 | + |
| 132 | + for _, backupDir := range localFiles { |
| 133 | + localFilePath := path.Join(srcPath, backupDir.Name()) |
| 134 | + remoteFilePath := path.Join(dstPath, backupDir.Name()) |
| 135 | + |
| 136 | + if backupDir.IsDir() { |
| 137 | + cf.sftpClient.Mkdir(remoteFilePath) |
| 138 | + cf.uploadDirectory(localFilePath, remoteFilePath) |
| 139 | + } else { |
| 140 | + cf.uploadFile(localFilePath, remoteFilePath) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func (cf *ClientConfig) uploadFile(srcPath, dstPath string) { |
| 146 | + srcFile, err := os.Open(srcPath) |
| 147 | + if err != nil { |
| 148 | + panic("os.Open error : " + err.Error() + srcPath) |
| 149 | + } |
| 150 | + |
| 151 | + defer srcFile.Close() |
| 152 | + |
| 153 | + dstFile, err := cf.sftpClient.Create(dstPath) |
| 154 | + if err != nil { |
| 155 | + panic("sftpClient.Create error : " + err.Error()) |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + defer dstFile.Close() |
| 160 | + ff, err := ioutil.ReadAll(srcFile) |
| 161 | + if err != nil { |
| 162 | + panic("ReadAll error : " + err.Error()) |
| 163 | + } |
| 164 | + wg := sync.WaitGroup{} |
| 165 | + wg.Add(1) |
| 166 | + go func() { |
| 167 | + stat, _ := dstFile.Stat() |
| 168 | + state2, _ := srcFile.Stat() |
| 169 | + bar := KBar{} |
| 170 | + bar.New(stat.Size(), state2.Size(), dstFile.Name()) |
| 171 | + for { |
| 172 | + stat, _ := dstFile.Stat() |
| 173 | + if stat.Size() < state2.Size() { |
| 174 | + bar.Play(stat.Size()) |
| 175 | + time.Sleep(time.Duration(100 * time.Millisecond)) |
| 176 | + } else { |
| 177 | + bar.Play(stat.Size()) |
| 178 | + bar.Finish() |
| 179 | + break |
| 180 | + } |
| 181 | + } |
| 182 | + wg.Done() |
| 183 | + }() |
| 184 | + dstFile.Write(ff) |
| 185 | + dstFile.Chmod(0777) |
| 186 | + wg.Wait() |
| 187 | + |
| 188 | +} |
| 189 | + |
| 190 | +func (cf *ClientConfig) Download(srcPath, dstPath string) { |
| 191 | + srcFile, _ := cf.sftpClient.Open(srcPath) |
| 192 | + dstFile, _ := os.Create(dstPath) |
| 193 | + |
| 194 | + defer func() { |
| 195 | + _ = srcFile.Close() |
| 196 | + _ = dstFile.Close() |
| 197 | + }() |
| 198 | + |
| 199 | + if _, err := srcFile.WriteTo(dstFile); err != nil { |
| 200 | + log.Fatalf("download erroe: %s", err.Error()) |
| 201 | + } |
| 202 | + |
| 203 | + fmt.Println("download success") |
| 204 | +} |
| 205 | + |
| 206 | +func (cf *ClientConfig) waitForQuit() { |
| 207 | + c := make(chan os.Signal) |
| 208 | + signal.Notify(c, os.Interrupt, os.Kill) |
| 209 | + <-c |
| 210 | +} |
| 211 | + |
| 212 | +func BuildExec(path, system, name string) { |
| 213 | + var cmdStr string |
| 214 | + if system == Windows { |
| 215 | + cmdStr = fmt.Sprintf("cd %s&SET CGO_ENABLED=0&SET GOOS=%s&SET GOARCH=amd64&go build %s", path, system, name) |
| 216 | + } else { |
| 217 | + cmdStr = fmt.Sprintf("cd %s&CGO_ENABLED=0&GOOS=%s&GOARCH=amd64&go build %s", path, system, name) |
| 218 | + } |
| 219 | + cmd := exec.Command("cmd", "/C", cmdStr) |
| 220 | + _ = cmd.Run() |
| 221 | +} |
| 222 | + |
| 223 | +func (cf *ClientConfig) Run(procName string) { |
| 224 | + defer func() { |
| 225 | + pid := cf.RunShell(fmt.Sprintf("pgrep %s", path.Base(procName))) |
| 226 | + cf.RunShell(fmt.Sprintf("kill %s", pid)) |
| 227 | + }() |
| 228 | + go cf.RunShell(procName) |
| 229 | + cf.waitForQuit() |
| 230 | +} |
0 commit comments