This commit is contained in:
damiva
2024-05-17 17:39:43 +03:00
committed by GitHub
parent 92e8063d39
commit fb22cfa6c9

View File

@@ -3,60 +3,88 @@ package msx
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"net/url"
"os" "os"
"path"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"server/settings" "server/settings"
"server/torr" "server/torr"
"server/utils"
"server/version" "server/version"
"server/web/auth" "server/web/auth"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
const base, fls = "https://damiva.github.io/msx", "files" const files, param = "files", "menu:request:interaction:init@{PREFIX}{SERVER}/msx/plugin.html"
var parameter = param
func SetupRoute(r gin.IRouter) { func SetupRoute(r gin.IRouter) {
authorized := r.Group("/", auth.CheckAuth()) authorized := r.Group("/", auth.CheckAuth())
authorized.Any("/msx", func(c *gin.Context) { authorized.GET("/msx/inf", func(c *gin.Context) {
if l := c.Query("url"); l != "" { if p, o := c.GetQuery("parameter"); o {
proxy(c, l, c.QueryArray("header")...) if p == "" {
} else if l = c.Query("indb"); l != "" { p = param
}
parameter = p
}
r := map[string]any{"version": version.Version, "search": settings.BTsets.EnableRutorSearch}
if f, e := os.Stat(files); e == nil {
r[files] = !f.IsDir()
} else if !os.IsNotExist(e) {
r[files] = e.Error()
}
c.JSON(200, r)
})
authorized.GET("/msx/trn", func(c *gin.Context) {
if h := c.Query("indb"); h != "" {
var r bool var r bool
for _, t := range settings.ListTorrent() { for _, t := range settings.ListTorrent() {
if r = t.InfoHash.HexString() == l; r { if r = t.InfoHash.HexString() == h; r {
break break
} }
} }
c.JSON(200, r) c.JSON(200, r)
} else if c.Request.Method == "POST" { } else if h = c.Query("hash"); h != "" {
serve(c) st, sc := trn(h)
if sc != "" {
sc = "{col:" + sc + "}"
}
msx(c, map[string]string{"action": "player:label:position:{VALUE}{tb}{tb}" + sc + st})
} else { } else {
proxy(c, base+"/ts.html") c.AbortWithStatus(http.StatusBadRequest)
} }
}) })
authorized.GET("/msx/*pth", func(c *gin.Context) { authorized.POST("/msx/trn", func(c *gin.Context) {
p := c.Param("pth") var j struct{ Data string }
if _, n := path.Split(p); n == "" { if e := json.NewDecoder(c.Request.Body).Decode(&j); e != nil {
files(c, filepath.Join(fls, filepath.Clean(p))) msx(c, e)
} else if n = strings.ToLower(path.Ext(n)); n == "" {
c.AbortWithStatus(http.StatusNotFound)
} else if n == ".html" || n == ".js" || n == ".json" {
proxy(c, base+p)
} else { } else {
c.File(filepath.Join(fls, filepath.Clean(p))) st, sc := trn(j.Data[strings.LastIndexByte(j.Data, ':')+1:])
msx(c, map[string]any{"stamp": st, "stampColor": sc, "live": map[string]any{
"type": "airtime", "duration": 1000, "over": map[string]any{
"action": "execute:" + utils.GetScheme(c) + "://" + c.Request.Host + c.Request.URL.Path,
"data": j.Data,
},
}})
} }
}) })
authorized.Any("/msx/proxy", func(c *gin.Context) {
proxy(c, c.Query("url"), c.QueryArray("header")...)
})
authorized.GET("/msx/start.json", func(c *gin.Context) {
c.JSON(200, map[string]any{"name": "TorrServer", "version": version.Version, "parameter": parameter})
})
authorized.GET("/msx/:pth", func(c *gin.Context) {
proxy(c, "https://damiva.github.io"+c.Request.URL.Path)
})
authorized.GET("/imdb/:id", func(c *gin.Context) { authorized.GET("/imdb/:id", func(c *gin.Context) {
const x = ".json" i, l, j := c.Param("id"), "", false
i, l := c.Param("id"), "" if j = strings.HasSuffix(i, ".json"); !j {
j := strings.HasSuffix(i, x) i += ".json"
if i = strings.TrimSuffix(i, x); i != "" { }
if r, e := http.Get("https://v2.sg.media-imdb.com/suggestion/h/" + i + x); e == nil { if r, e := http.Get("https://v2.sg.media-imdb.com/suggestion/h/" + i); e == nil {
if r.StatusCode == http.StatusOK { if r.StatusCode == http.StatusOK {
var j struct { var j struct {
D []struct{ I struct{ ImageUrl string } } D []struct{ I struct{ ImageUrl string } }
@@ -67,7 +95,6 @@ func SetupRoute(r gin.IRouter) {
} }
r.Body.Close() r.Body.Close()
} }
}
if j { if j {
c.JSON(200, l) c.JSON(200, l)
} else if l == "" { } else if l == "" {
@@ -76,8 +103,8 @@ func SetupRoute(r gin.IRouter) {
c.Redirect(http.StatusMovedPermanently, l) c.Redirect(http.StatusMovedPermanently, l)
} }
}) })
authorized.Static("/files", files)
} }
func proxy(c *gin.Context, u string, h ...string) { func proxy(c *gin.Context, u string, h ...string) {
if u == "" { if u == "" {
c.AbortWithStatus(http.StatusBadRequest) c.AbortWithStatus(http.StatusBadRequest)
@@ -97,70 +124,38 @@ func proxy(c *gin.Context, u string, h ...string) {
} }
} }
} }
func msx(c *gin.Context, d any) {
func serve(c *gin.Context) { var r struct {
var j struct { R struct {
Data struct { S int `json:"status"`
Update string T string `json:"text"`
Info struct{ Content struct{ Flag string } } M string `json:"message,omitempty"`
D any `json:"data,omitempty"`
} `json:"response"`
} }
} if e, o := d.(error); o {
if e := c.Bind(&j); e != nil { r.R.S = http.StatusBadRequest
c.AbortWithError(http.StatusBadRequest, e) r.R.M = e.Error()
} else if j.Data.Update == "" && j.Data.Info.Content.Flag == "" {
r := map[string]any{"version": version.Version, "search": settings.BTsets.EnableRutorSearch}
if l, e := os.Readlink(fls); e == nil {
r["files"] = l
} else if !os.IsNotExist(e) {
r["error"] = e.Error()
}
c.JSON(200, r)
} else { } else {
var r map[string]any r.R.S = http.StatusOK
h, sc, st := j.Data.Info.Content.Flag, "", "" r.R.D = d
if h == "" {
h = j.Data.Update[strings.LastIndexByte(j.Data.Update, ':')+1:]
} }
if t := torr.GetTorrent(h); t != nil { r.R.T = http.StatusText(r.R.S)
if t := t.Status(); t != nil && t.Stat < 5 { c.JSON(200, &r)
switch t.Stat { }
func trn(h string) (t, c string) {
if h := torr.GetTorrent(h); h != nil {
if h := h.Status(); h != nil && h.Stat < 5 {
switch h.Stat {
case 4: case 4:
sc = "msx-red" c = "msx-red"
case 3: case 3:
sc = "msx-green" c = "msx-green"
default: default:
sc = "msx-yellow" c = "msx-yellow"
} }
st = "{ico:north} " + strconv.Itoa(t.ActivePeers) + " / " + strconv.Itoa(t.TotalPeers) + " {ico:south} " + strconv.Itoa(t.ConnectedSeeders) t = "{ico:north} " + strconv.Itoa(h.ActivePeers) + " / " + strconv.Itoa(h.TotalPeers) + " {ico:south} " + strconv.Itoa(h.ConnectedSeeders)
} }
} }
if j.Data.Update != "" { return
r = map[string]any{"action": "update:" + j.Data.Update, "data": map[string]string{"stamp": st, "stampColor": sc}}
} else {
if sc != "" {
sc = "{tb}{tb}{col:" + sc + "}"
}
r = map[string]any{"action": "player:label:position:{LABEL}" + sc + st}
}
c.JSON(200, map[string]any{"response": map[string]any{"status": http.StatusOK, "data": r}})
}
}
func files(c *gin.Context, p string) {
if d, e := os.ReadDir(p); e == nil {
var ds, fs []map[string]any
u := c.Request.URL.EscapedPath()
for _, f := range d {
if n := f.Name(); f.IsDir() {
ds = append(ds, map[string]any{"id": u + url.PathEscape(n) + "/", "path": n})
} else if f, e := f.Info(); e == nil {
fs = append(fs, map[string]any{"id": u + url.PathEscape(n), "path": n, "length": f.Size()})
}
}
c.JSON(200, map[string]any{"title": filepath.Base(strings.TrimSuffix(p, "/")), "path": u, "files": append(ds, fs...)})
} else if os.IsNotExist(e) {
c.AbortWithError(http.StatusNotFound, e)
} else {
c.AbortWithError(http.StatusInternalServerError, e)
}
} }