Upload logs handler

This commit is contained in:
Ben Sarmiento
2024-01-26 09:52:37 +01:00
parent 96ec870bb2
commit 4852b93c67
2 changed files with 67 additions and 0 deletions

View File

@@ -4,7 +4,10 @@ import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@@ -111,3 +114,56 @@ func (l *Logger) GetLogsFromFile() (string, error) {
return buffer.String(), nil
}
func (l *Logger) UploadLogFile() (string, error) {
file, err := os.Open(l.logPath)
if err != nil {
return "", err
}
defer file.Close()
// Prepare a form that you will submit to that URL.
var b bytes.Buffer
w := multipart.NewWriter(&b)
fw, err := w.CreateFormFile("file", filepath.Base(l.logPath))
if err != nil {
return "", err
}
// Copy the file into the form file
if _, err = io.Copy(fw, file); err != nil {
return "", err
}
// Close the multipart writer to set the terminating boundary
if err = w.Close(); err != nil {
return "", err
}
// Create a request and add the proper headers.
req, err := http.NewRequest("POST", "https://0x0.st/", &b)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", w.FormDataContentType())
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to upload log file, server responded with status code: %d", resp.StatusCode)
}
// Read the response body
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(responseBody), nil
}