package goscp import ( "encoding/json" "errors" "fmt" "net/http" "regexp" "strings" "time" "github.com/ScriptRock/crypto/ssh" "github.com/gofrs/uuid" ) var ( pwRe = regexp.MustCompile(`(?i)password: $`) refusedRe = regexp.MustCompile(`(?i)refused`) saveRe = regexp.MustCompile(`(?i)[\ !$%^?]+`) errRefuse = errors.New("Conneciton Refused") errWrite = errors.New("Write Error") ciphers = []string{"aes256-ctr", "aes192-ctr", "aes128-ctr", "aes256-cbc", "aes192-cbc", "aes128-cb", "3des-cbc"} ) //SCPFile starts the system command and excutes UploadFile to perform the upload func SCPFile(remote, user, host, password, content string, port int) error { auth := sshKeyboardConfig(user, password) client, err := openClient(auth, host) if err != nil { return err } defer client.Close() if err = scp(client, ".", remote, content); err != nil { return err } return nil } func openClient(sshConfig *ssh.ClientConfig, host string) (*ssh.Client, error) { client := new(ssh.Client) errc := make(chan error) go func() { var err error client, err = ssh.Dial("tcp", host+":22", sshConfig) errc <- err }() select { case err := <-errc: if err != nil { return nil, err } return client, nil case <-time.After(time.Second * 10): return nil, fmt.Errorf("Connection Timeout: %s", host) } } func sshKeyboardConfig(u, p string) *ssh.ClientConfig { var authMethods []ssh.AuthMethod keyboardInteractiveChallenge := func( user, instruction string, questions []string, echos []bool, ) (answers []string, err error) { if len(questions) == 0 { return []string{}, nil } return []string{p}, nil } authMethods = append(authMethods, ssh.KeyboardInteractive(keyboardInteractiveChallenge)) authMethods = append(authMethods, ssh.Password(p)) return &ssh.ClientConfig{ User: u, Auth: authMethods, Config: ssh.Config{ Ciphers: ciphers, }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } } func scp(client *ssh.Client, path, file, content string) error { session, err := client.NewSession() if err != nil { return err } defer session.Close() go func() { w, _ := session.StdinPipe() defer w.Close() fmt.Fprintln(w, "C0644", len(content), file) fmt.Fprint(w, content) fmt.Fprint(w, "\x00") }() if err := session.Run("scp -v -tr " + path); err != nil { return err } return nil } func addReturn(s string) string { return fmt.Sprintf("%s\n", strings.TrimRight(s, "\n")) } type ( //SCPRequest is used to request a config upload SCPRequest struct { UserInfo UserInfo `json:"user_info"` SaveAs string `json:"save_as"` Content string `json:"content"` Host string `json:"destination"` Port int `json:"port"` Username string `json:"username"` Password string `json:"password"` } //UserInfo contains the requester and uuid for process tracking UserInfo struct { Username string `json:"username"` UUID string `json:"uuid"` } ) //Decode will unmarhal the http reqeust and return an error if an option is missing func Decode(r *http.Request) (s SCPRequest, err error) { d := json.NewDecoder(r.Body) if err = d.Decode(&s); err != nil { return } switch { case s.UserInfo.Username == "": return s, errors.New("username in userinfo cannot be empty") case s.Username == "": return s, errors.New("authentication username cannot be empty") case s.Host == "": return s, errors.New("destination cannot be blank") case s.Password == "": return s, errors.New("password cannot be blank") case s.Content == "": return s, errors.New("no content") case saveRe.MatchString(s.SaveAs): return s, errors.New("save string can only contain numbers, letters, -, and _") } return } //ScpPush is used to uplaod the SCPRequest data or return an error if there is a failure, if a name is given the file //will be saved with this name or a uuid will be generated and it will be used as the new filename. func (s SCPRequest) ScpPush() (u string, err error) { saveas := s.SaveAs if s.SaveAs == "" { as, _ := uuid.NewV4() saveas = as.String() } if err = SCPFile(saveas, s.Username, s.Host, s.Password, s.Content, s.Port); err != nil { return } return saveas, nil }