dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 9b1dd9a8a17d7340459bcb9e8e1e6e45a1838896
parent 34caa6914a83e1f6192f35b37fa6202a22ec8002
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Fri,  3 Feb 2023 21:08:10 -0800

requires api key to download files

Diffstat:
Mcmd/dkfdownload/main.go | 21++++++++++++++++-----
Mpkg/web/handlers/handlers.go | 2+-
Mpkg/web/web.go | 2+-
3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/cmd/dkfdownload/main.go b/cmd/dkfdownload/main.go @@ -43,13 +43,17 @@ func main() { customFormatter.FullTimestamp = true logrus.SetFormatter(customFormatter) + var apiKey string var nbThreads int var filedropUUID string var isLocal bool var httpTimeout time.Duration + apiKeyUsage := "api key" filedropUUIDUsage := "dkf filedrop uuid" nbThreadsUsage := "nb threads" nbThreadsDefaultValue := 20 + flag.StringVar(&apiKey, "api-key", "", apiKeyUsage) + flag.StringVar(&apiKey, "a", "", apiKeyUsage) flag.DurationVar(&httpTimeout, "http-timeout", 2*time.Minute, "http timeout") flag.StringVar(&filedropUUID, "uuid", "", filedropUUIDUsage) flag.StringVar(&filedropUUID, "u", "", filedropUUIDUsage) @@ -67,14 +71,18 @@ func main() { if err != nil { body := url.Values{} body.Set("init", "1") - req, _ := http.NewRequest(http.MethodPost, baseUrl+"/file-drop/"+filedropUUID+"/dkfdownload", strings.NewReader(body.Encode())) + req, _ := http.NewRequest(http.MethodPost, baseUrl+"/api/v1/file-drop/"+filedropUUID+"/dkfdownload", strings.NewReader(body.Encode())) req.Header.Set("User-Agent", userAgent) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("DKF_API_KEY", apiKey) resp, err := client.Do(req) if err != nil { logrus.Fatalln(err) } defer resp.Body.Close() + if resp.StatusCode == http.StatusUnauthorized { + logrus.Fatalln(resp.Status) + } by, _ = io.ReadAll(resp.Body) _ = os.Mkdir(filedropUUID, 0755) @@ -119,7 +127,7 @@ func main() { wg := &sync.WaitGroup{} wg.Add(nbThreads) for i := 0; i < nbThreads; i++ { - go work(i, wg, baseUrl, filedropUUID, chunksCh, isLocal, httpTimeout, nbChunks) + go work(i, wg, baseUrl, filedropUUID, apiKey, chunksCh, isLocal, httpTimeout, nbChunks) time.Sleep(25 * time.Millisecond) } wg.Wait() @@ -191,7 +199,7 @@ func main() { logrus.Infof("all done in %s", ShortDur(time.Since(start))) } -func work(i int, wg *sync.WaitGroup, baseUrl, filedropUUID string, chunksCh chan int64, isLocal bool, httpTimeout time.Duration, nbChunks int64) { +func work(i int, wg *sync.WaitGroup, baseUrl, filedropUUID, apiKey string, chunksCh chan int64, isLocal bool, httpTimeout time.Duration, nbChunks int64) { defer wg.Done() client := doGetClient(isLocal, httpTimeout) for chunkNum := range chunksCh { @@ -201,12 +209,13 @@ func work(i int, wg *sync.WaitGroup, baseUrl, filedropUUID string, chunksCh chan start = time.Now() body := url.Values{} body.Set("chunk", strconv.FormatInt(chunkNum, 10)) - req, err := http.NewRequest(http.MethodPost, baseUrl+"/file-drop/"+filedropUUID+"/dkfdownload", strings.NewReader(body.Encode())) + req, err := http.NewRequest(http.MethodPost, baseUrl+"/api/v1/file-drop/"+filedropUUID+"/dkfdownload", strings.NewReader(body.Encode())) if err != nil { return err } req.Header.Set("User-Agent", userAgent) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("DKF_API_KEY", apiKey) resp, err := client.Do(req) if err != nil { if os.IsTimeout(err) { @@ -216,7 +225,9 @@ func work(i int, wg *sync.WaitGroup, baseUrl, filedropUUID string, chunksCh chan return err } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { + if resp.StatusCode == http.StatusUnauthorized { + logrus.Fatalln(resp.Status) + } else if resp.StatusCode != http.StatusOK { return fmt.Errorf("thread #%03d | chunk #%03d | invalid status code %s", i, chunkNum, resp.Status) } f, err := os.OpenFile(filepath.Join(filedropUUID, "part_"+strconv.FormatInt(chunkNum, 10)), os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644) diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -4484,7 +4484,7 @@ func FileDropDkfDownloadHandler(c echo.Context) error { filedropUUID := c.Param("uuid") filedrop, err := database.GetFiledropByUUID(filedropUUID) if err != nil { - return c.Redirect(http.StatusFound, "/") + return c.NoContent(http.StatusNotFound) } maxChunkSize := int64(2 << 20) // 2MB diff --git a/pkg/web/web.go b/pkg/web/web.go @@ -302,7 +302,7 @@ func getBaseServer() *echo.Echo { e.GET("/file-drop/:uuid", handlers.FileDropHandler) e.POST("/file-drop/:uuid", handlers.FileDropHandler) e.POST("/file-drop/:uuid/dkfupload", handlers.FileDropDkfUploadHandler) - e.POST("/file-drop/:uuid/dkfdownload", handlers.FileDropDkfDownloadHandler) + e.POST("/api/v1/file-drop/:uuid/dkfdownload", handlers.FileDropDkfDownloadHandler, middlewares.SetUserMiddleware, middlewares.IsAuthMiddleware) e.GET("/downloads/:fileName", handlers.FileDropDownloadHandler) e.POST("/downloads/:fileName", handlers.FileDropDownloadHandler) e.Any("*", getMainServer(i18nBundle, renderer))