From a106abc8ba9d1655a3b97733b86e4bc0742b81d2 Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Mon, 15 May 2023 11:40:52 +0100 Subject: [PATCH 1/3] Fixed compression error --- pkg/utils/file/file.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/utils/file/file.go b/pkg/utils/file/file.go index 7a1e83c67..e67a8e4f9 100644 --- a/pkg/utils/file/file.go +++ b/pkg/utils/file/file.go @@ -431,7 +431,9 @@ func AddFile(ar archiver.Writer, path, commonPath string) error { defer file.Close() if path != commonPath { - filename := info.Name() + //filename := info.Name() + filename := strings.TrimPrefix(path, commonPath) + filename = strings.TrimPrefix(filename, string(filepath.Separator)) err = ar.Write(archiver.File{ FileInfo: archiver.FileInfo{ FileInfo: info, From 93262dc549742e86a36e42688223e795887d65df Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Fri, 16 Jun 2023 07:24:35 +0100 Subject: [PATCH 2/3] Update logs --- api/casaos/openapi.yaml | 16 ++++++++++++ codegen/casaos_api.go | 52 ++++++++++++++++++++++++------------- route/v2/health.go | 57 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 18 deletions(-) diff --git a/api/casaos/openapi.yaml b/api/casaos/openapi.yaml index 1da3ba10c..eafaff9db 100644 --- a/api/casaos/openapi.yaml +++ b/api/casaos/openapi.yaml @@ -60,6 +60,22 @@ paths: "500": $ref: "#/components/responses/ResponseInternalServerError" + /health/logs: + get: + tags: + - Health methods + summary: Get log + operationId: getHealthlogs + responses: + "200": + description: OK + content: + application/octet-stream: + schema: + type: string + format: binary + "500": + $ref: "#/components/responses/ResponseInternalServerError" /file/test: get: tags: diff --git a/codegen/casaos_api.go b/codegen/casaos_api.go index 751a6101e..fc7e87461 100644 --- a/codegen/casaos_api.go +++ b/codegen/casaos_api.go @@ -65,6 +65,9 @@ type ServerInterface interface { // Test file methods // (GET /file/test) GetFileTest(ctx echo.Context) error + // Get log + // (GET /health/logs) + GetHealthlogs(ctx echo.Context) error // Get port in use // (GET /health/ports) GetHealthPorts(ctx echo.Context) error @@ -89,6 +92,17 @@ func (w *ServerInterfaceWrapper) GetFileTest(ctx echo.Context) error { return err } +// GetHealthlogs converts echo context to params. +func (w *ServerInterfaceWrapper) GetHealthlogs(ctx echo.Context) error { + var err error + + ctx.Set(Access_tokenScopes, []string{""}) + + // Invoke the callback with all the unmarshalled arguments + err = w.Handler.GetHealthlogs(ctx) + return err +} + // GetHealthPorts converts echo context to params. func (w *ServerInterfaceWrapper) GetHealthPorts(ctx echo.Context) error { var err error @@ -140,6 +154,7 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL } router.GET(baseURL+"/file/test", wrapper.GetFileTest) + router.GET(baseURL+"/health/logs", wrapper.GetHealthlogs) router.GET(baseURL+"/health/ports", wrapper.GetHealthPorts) router.GET(baseURL+"/health/services", wrapper.GetHealthServices) @@ -148,24 +163,25 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/8xW4W/bthP9Vwj+fh+aQbaMBAUKAf2QNksaBIODJcMGxIZLU2eJjURyd6QTL9D/PpCS", - "Y8V2g6Rdh31yRB7fvfdyx+MDl6a2RoN2xLMHjkDWaIL4cQbuE4jKlZcGHY0vwpo02oF24U9hbaWkcMro", - "9AsZHdZIllCLuFtV4wXPbh74/xEWPOP/Szep0jaO0g+C4NcuJ2+SB27RWECnWga5cBHsOYgeRd40zbRp", - "moTnQBKVDdx4xscXvEk2cq4Al0rCf1zRmuXzotapzrUD1KIKpwB/RjT4KnEvl7TLZJ2btclZm71H7pVG", - "fw+X4EqTdGDR8Scnsu3/Rw1EoogbT4G6DYbgPGrI2XzFqNVHKgemFsyVgMAUMaFXPOFwL2pbAc84TziC", - "yMe6WvHMoYeEu5UNO+RQ6aIl3i/cHV5O2vCjHNTx+xH83egRTGkHBUSnuxWBKAKV+0FhBlrUY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hy9fhXlrGdzlkJg0s2dPbPd/x1GHFX2nYTjPwiBhB1fcb99JhrVhm", + "cmFsYH6w7R2aYv5hv86AReWKgUud1sM++U0HPm5U16i/JTf25ugfozjoEMaKhuCBymnQug8WToDtO64g", + "VtyQcEsBSpfiY99rf/ooepiDRbUzI3yXfYO5/cc42AvppR41cTAvxJ8E9yeFq+v2OhwIwSjuN1jJTKar", + "p31/kOFAD7/r+pPL6ZvpyXbA2IkeflQcv3DvTQiBbkasijN0je/i9ed+2XtN9oRet38HAAD///s5WXsj", + "DgAA", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/route/v2/health.go b/route/v2/health.go index 37eee19b9..33a5ddb85 100644 --- a/route/v2/health.go +++ b/route/v2/health.go @@ -2,10 +2,15 @@ package v2 import ( "net/http" + "net/url" + "os" + "path/filepath" "github.com/IceWhaleTech/CasaOS/codegen" + "github.com/IceWhaleTech/CasaOS/pkg/utils/file" "github.com/IceWhaleTech/CasaOS/service" "github.com/labstack/echo/v4" + "github.com/mholt/archiver/v3" ) func (s *CasaOS) GetHealthServices(ctx echo.Context) error { @@ -41,3 +46,55 @@ func (s *CasaOS) GetHealthPorts(ctx echo.Context) error { }, }) } +func (c *CasaOS) GetHealthlogs(ctx echo.Context) error { + var name, currentPath, commonDir, extension string + var err error + var ar archiver.Writer + fileList, err := os.ReadDir("/var/log/casaos") + if err != nil { + message := err.Error() + return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{ + Message: &message, + }) + } + extension, ar, err = file.GetCompressionAlgorithm("zip") + if err != nil { + ctx.Response().Header().Set("Content-Type", "application/json") + message := err.Error() + return ctx.JSON(http.StatusNotFound, codegen.ResponseInternalServerError{ + Message: &message, + }) + } + err = ar.Create(ctx.Response().Writer) + if err != nil { + ctx.Response().Header().Set("Content-Type", "application/json") + message := err.Error() + return ctx.JSON(http.StatusNotFound, codegen.ResponseInternalServerError{ + Message: &message, + }) + } + defer ar.Close() + + commonDir = "/var/log/casaos" + + currentPath = filepath.Base(commonDir) + + name = currentPath + name += extension + ctx.Response().Header().Add("Content-Type", "application/octet-stream") + ctx.Response().Header().Add("Content-Transfer-Encoding", "binary") + ctx.Response().Header().Add("Cache-Control", "no-cache") + ctx.Response().Header().Add("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name)) + + for _, fname := range fileList { + err := file.AddFile(ar, filepath.Join("/var/log/casaos", fname.Name()), commonDir) + if err != nil { + message := err.Error() + return ctx.JSON(http.StatusInternalServerError, codegen.ResponseInternalServerError{ + Message: &message, + }) + } + + } + return nil +} From c649b67c2058e7f6ba40650edf34eb9947a581d0 Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Mon, 3 Jul 2023 09:34:09 +0100 Subject: [PATCH 3/3] update 401 --- route/v2.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/route/v2.go b/route/v2.go index d07e06292..3741ec87d 100644 --- a/route/v2.go +++ b/route/v2.go @@ -90,7 +90,10 @@ func InitV2Router() http.Handler { }, TokenLookupFuncs: []echo_middleware.ValuesExtractor{ func(c echo.Context) ([]string, error) { - return []string{c.Request().Header.Get(echo.HeaderAuthorization)}, nil + if len(c.Request().Header.Get(echo.HeaderAuthorization)) > 0 { + return []string{c.Request().Header.Get(echo.HeaderAuthorization)}, nil + } + return []string{c.QueryParam("token")}, nil }, }, }))