Skip to content

Commit 669575d

Browse files
committed
feature: Added missing endpoints
1 parent 8d438f9 commit 669575d

File tree

9 files changed

+189
-384
lines changed

9 files changed

+189
-384
lines changed

app/handlers/extension.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,8 @@ func ExtensionRunner(c *fiber.Ctx) error {
7373

7474
return c.SendString(output)
7575
}
76+
77+
func ExtensionLogger(c *fiber.Ctx) error {
78+
// deprecated, middleware logging everything
79+
return c.SendString("")
80+
}

app/handlers/external.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package handlers
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/limanmys/render-engine/app/models"
6+
"github.com/limanmys/render-engine/internal/liman"
7+
"github.com/limanmys/render-engine/internal/sandbox"
8+
"github.com/limanmys/render-engine/pkg/helpers"
9+
"github.com/limanmys/render-engine/pkg/linux"
10+
"github.com/limanmys/render-engine/pkg/logger"
11+
)
12+
13+
func ExternalAPI(c *fiber.Ctx) error {
14+
extension := &models.Extension{}
15+
var err error
16+
if len(c.FormValue("extension_id")) > 0 {
17+
if !helpers.CheckUUID(c.FormValue("extension_id")) {
18+
extension, err = liman.GetExtension(&models.Extension{Name: c.FormValue("extension_id")})
19+
if err != nil {
20+
return err
21+
}
22+
} else {
23+
extension, err = liman.GetExtension(&models.Extension{ID: c.FormValue("extension_id")})
24+
if err != nil {
25+
return err
26+
}
27+
}
28+
} else {
29+
return logger.FiberError(fiber.StatusBadRequest, "extension not found")
30+
}
31+
32+
if extension.Status == "0" {
33+
return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable")
34+
}
35+
36+
credentials := &models.Credentials{}
37+
if extension.RequireKey == "true" {
38+
credentials, err = liman.GetCredentials(
39+
&models.User{
40+
ID: c.Locals("user_id").(string),
41+
},
42+
&models.Server{
43+
ID: c.FormValue("server_id"),
44+
},
45+
)
46+
47+
if err != nil || len(credentials.Username) < 1 {
48+
return logger.FiberError(fiber.StatusForbidden, "you need a key to use this extension")
49+
}
50+
}
51+
52+
formValues := helpers.GetFormData(c)
53+
54+
token := c.FormValue("token")
55+
if len(c.FormValue("liman-token")) > 0 {
56+
token = c.FormValue("liman-token")
57+
}
58+
59+
command, err := sandbox.GenerateCommand(
60+
extension,
61+
credentials,
62+
&models.CommandParams{
63+
TargetFunction: c.FormValue("lmntargetFunction"),
64+
User: c.Locals("user_id").(string),
65+
Extension: c.FormValue("extension_id"),
66+
Server: c.FormValue("server_id"),
67+
RequestData: formValues,
68+
Token: token,
69+
BaseURL: c.FormValue("lmnbaseurl", c.Get("origin")),
70+
Locale: c.FormValue("locale", helpers.Env("APP_LANG", "tr")),
71+
},
72+
)
73+
if err != nil {
74+
return err
75+
}
76+
77+
output := linux.Execute(command)
78+
79+
if helpers.IsJSON(output) {
80+
return c.JSON(output)
81+
} else {
82+
return c.SendString(output)
83+
}
84+
}

app/handlers/job.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package handlers
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/limanmys/render-engine/app/models"
6+
"github.com/limanmys/render-engine/internal/liman"
7+
"github.com/limanmys/render-engine/internal/sandbox"
8+
"github.com/limanmys/render-engine/pkg/helpers"
9+
"github.com/limanmys/render-engine/pkg/linux"
10+
"github.com/limanmys/render-engine/pkg/logger"
11+
)
12+
13+
func BackgroundJob(c *fiber.Ctx) error {
14+
if len(c.FormValue("extension_id")) < 1 {
15+
return logger.FiberError(fiber.StatusBadRequest, "extension not found")
16+
}
17+
18+
extension, err := liman.GetExtension(&models.Extension{
19+
ID: c.FormValue("extension_id"),
20+
})
21+
if err != nil {
22+
return err
23+
}
24+
25+
if extension.Status == "0" {
26+
return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable")
27+
}
28+
29+
credentials := &models.Credentials{}
30+
if extension.RequireKey == "true" {
31+
credentials, err = liman.GetCredentials(
32+
&models.User{
33+
ID: c.Locals("user_id").(string),
34+
},
35+
&models.Server{
36+
ID: c.FormValue("server_id"),
37+
},
38+
)
39+
40+
if err != nil || len(credentials.Username) < 1 {
41+
return logger.FiberError(fiber.StatusForbidden, "you need a key to use this extension")
42+
}
43+
}
44+
45+
formValues := helpers.GetFormData(c)
46+
47+
token := c.FormValue("token")
48+
if len(c.FormValue("liman-token")) > 0 {
49+
token = c.FormValue("liman-token")
50+
}
51+
52+
command, err := sandbox.GenerateCommand(
53+
extension,
54+
credentials,
55+
&models.CommandParams{
56+
TargetFunction: c.FormValue("lmntargetFunction"),
57+
User: c.Locals("user_id").(string),
58+
Extension: c.FormValue("extension_id"),
59+
Server: c.FormValue("server_id"),
60+
RequestData: formValues,
61+
Token: token,
62+
BaseURL: c.FormValue("lmnbaseurl", c.Get("origin")),
63+
Locale: c.FormValue("locale", helpers.Env("APP_LANG", "tr")),
64+
},
65+
)
66+
if err != nil {
67+
return err
68+
}
69+
70+
go linux.Execute(command)
71+
72+
return c.JSON(&fiber.Map{
73+
"status": "ok",
74+
"message": "job dispatched successfully",
75+
})
76+
}

app/middleware/app_logger/new.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ func New() fiber.Handler {
2424
"lmn_level", "request",
2525
"log_id", uuid.NewString(),
2626
"user_id", user_id,
27-
"server_id", formData["server_id"],
28-
"extension_id", formData["extension_id"],
2927
"route", c.Path(),
3028
"ip_address", c.IP(),
31-
"display", true,
32-
"view", formData["lmntargetFunction"],
3329
"request_details", formData,
3430
)
3531

app/routes/index.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ func Install(app *fiber.App) {
2929

3030
// extensionDb
3131
app.Post("/setExtensionDb", handlers.SetExtensionDb)
32+
33+
// logger: deprecate on liman v2
34+
app.Post("/sendLog", handlers.ExtensionLogger)
35+
36+
// background job
37+
app.Post("/backgroundJob", handlers.BackgroundJob)
38+
39+
// external api proxy
40+
app.Post("/externalAPI", handlers.ExternalAPI)
3241
}

go.mod

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,11 @@ require (
1717
require (
1818
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e // indirect
1919
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
20-
github.com/atotto/clipboard v0.1.4 // indirect
2120
github.com/benbjohnson/clock v1.3.0 // indirect
22-
github.com/charmbracelet/bubbles v0.14.0 // indirect
23-
github.com/charmbracelet/bubbletea v0.22.1 // indirect
24-
github.com/charmbracelet/lipgloss v0.6.0 // indirect
2521
github.com/chenzhuoyu/base64x v0.0.0-20220526154910-8bf9453eb81a // indirect
26-
github.com/containerd/console v1.0.3 // indirect
27-
github.com/fsnotify/fsnotify v1.5.4 // indirect
2822
github.com/geoffgarside/ber v1.1.0 // indirect
29-
github.com/gofiber/cli v0.0.9 // indirect
30-
github.com/gofiber/fiber v1.13.3 // indirect
31-
github.com/gofiber/utils v0.0.9 // indirect
3223
github.com/gofrs/uuid v4.2.0+incompatible // indirect
33-
github.com/gorilla/schema v1.1.0 // indirect
3424
github.com/hashicorp/go-uuid v1.0.3 // indirect
35-
github.com/inconshreveable/mousetrap v1.0.1 // indirect
3625
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
3726
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
3827
github.com/jcmturner/gofork v1.7.6 // indirect
@@ -42,21 +31,10 @@ require (
4231
github.com/klauspost/cpuid/v2 v2.1.1 // indirect
4332
github.com/kr/fs v0.1.0 // indirect
4433
github.com/kr/pretty v0.3.0 // indirect
45-
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
4634
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786 // indirect
47-
github.com/mattn/go-colorable v0.1.7 // indirect
48-
github.com/mattn/go-isatty v0.0.16 // indirect
49-
github.com/mattn/go-localereader v0.0.1 // indirect
50-
github.com/mattn/go-runewidth v0.0.13 // indirect
51-
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
52-
github.com/muesli/cancelreader v0.2.2 // indirect
53-
github.com/muesli/reflow v0.3.0 // indirect
54-
github.com/muesli/termenv v0.12.0 // indirect
35+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5536
github.com/pkg/errors v0.9.1 // indirect
56-
github.com/rivo/uniseg v0.3.4 // indirect
5737
github.com/rogpeppe/go-internal v1.8.1 // indirect
58-
github.com/spf13/cobra v1.5.0 // indirect
59-
github.com/spf13/pflag v1.0.5 // indirect
6038
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
6139
go.uber.org/atomic v1.10.0 // indirect
6240
go.uber.org/goleak v1.1.12 // indirect
@@ -72,7 +50,6 @@ require (
7250
github.com/alessio/shellescape v1.4.1
7351
github.com/andybalholm/brotli v1.0.4 // indirect
7452
github.com/go-sql-driver/mysql v1.6.0 // indirect
75-
github.com/gofiber/helmet v0.1.2
7653
github.com/hirochachacha/go-smb2 v1.1.0
7754
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
7855
github.com/jackc/pgconn v1.13.0 // indirect

0 commit comments

Comments
 (0)