Upload logs handler
This commit is contained in:
@@ -91,6 +91,8 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
|
||||
// logs route
|
||||
router.Get("/logs", hs.logsHandler)
|
||||
router.Get("/logs/", hs.logsHandler)
|
||||
router.Get("/logs/upload", hs.uploadLogsHandler)
|
||||
router.Get("/logs/upload/", hs.uploadLogsHandler)
|
||||
|
||||
router.MethodNotAllowed(func(resp http.ResponseWriter, req *http.Request) {
|
||||
hs.log.Debugf("Method not allowed: %s %s %v", req.Method, req.URL, req.Header)
|
||||
@@ -453,3 +455,12 @@ func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
fmt.Fprint(resp, logs)
|
||||
}
|
||||
|
||||
func (hs *Handlers) uploadLogsHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
url, err := hs.log.UploadLogFile()
|
||||
if err != nil {
|
||||
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(resp, req, url, http.StatusFound)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user