Update msx.go

new MSX
This commit is contained in:
damiva
2024-05-10 17:47:08 +03:00
committed by GitHub
parent 1ce09cd1fa
commit 2595c099a2

View File

@@ -1,96 +1,154 @@
package msx package msx
import ( import (
_ "embed"
"encoding/json" "encoding/json"
"net/http" "net/http"
"sync" "net/url"
"os"
"path"
"path/filepath"
"server/settings"
"server/torr"
"server/version" "server/version"
"server/web/auth"
"strconv"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var ( const base, fls = "https://damiva.github.io/msx", "files"
//go:embed russian.json.gz
rus []byte
//go:embed torrents.js.gz
trs []byte
//go:embed torrent.js.gz
trn []byte
//go:embed ts.js.gz
its []byte
idb = new(sync.Mutex) func SetupRoute(r gin.IRouter) {
ids = make(map[string]string) authorized := r.Group("/", auth.CheckAuth())
) authorized.Any("/msx", func(c *gin.Context) {
if l := c.Query("url"); l != "" {
func asset(c *gin.Context, t string, d []byte) { proxy(c, l, c.QueryArray("header")...)
c.Header("Content-Encoding", "gzip") } else if c.Request.Method == "POST" {
c.Data(200, t+"; charset=UTF-8", d) serve(c)
} } else {
func SetupRoute(r *gin.RouterGroup) { proxy(c, base+"/ts.html")
r.GET("/msx/:pth", func(c *gin.Context) {
js := []string{"http://msx.benzac.de/js/tvx-plugin.min.js"}
switch p := c.Param("pth"); p {
case "start.json":
c.JSON(200, map[string]string{
"name": "TorrServer",
"version": version.Version,
"parameter": "menu:request:interaction:init@{PREFIX}{SERVER}/msx/ts",
})
case "russian.json":
asset(c, "application/json", rus)
case "torrents.js":
asset(c, "text/javascript", trs)
case "torrent.js":
asset(c, "text/javascript", trn)
case "ts.js":
asset(c, "text/javascript", its)
case "torrents":
js = append(js, p+".js")
p = "torrent"
fallthrough
case "torrent":
if c.Query("platform") == "tizen" {
js = append(js, "http://msx.benzac.de/interaction/js/tizen-player.js")
}
fallthrough
case "ts":
b := []byte("<!DOCTYPE html>\n<html>\n<head>\n<title>TorrServer Plugin</title>\n<meta charset='UTF-8'>\n")
for _, j := range append(js, p+".js") {
b = append(b, "<script type='text/javascript' src='"+j+"'></script>\n"...)
}
c.Data(200, "text/html; charset=UTF-8", append(b, "</head>\n<body></body>\n</html>"...))
default:
c.AbortWithStatus(404)
} }
}) })
r.GET("/msx/imdb", func(c *gin.Context) { authorized.GET("/msx/*pth", func(c *gin.Context) {
idb.Lock() p := c.Param("pth")
defer idb.Unlock() if _, n := path.Split(p); n == "" {
l := len(ids) files(c, filepath.Join(fls, filepath.Clean(p)))
ids = make(map[string]string) } else if n = strings.ToLower(path.Ext(n)); n == "" {
c.JSON(200, l) c.AbortWithStatus(http.StatusNotFound)
} else if n == ".html" || n == ".js" || n == ".json" {
proxy(c, base+p)
} else {
c.File(filepath.Join(fls, filepath.Clean(p)))
}
}) })
r.GET("/msx/imdb/:id", func(c *gin.Context) { authorized.GET("/imdb/:id", func(c *gin.Context) {
idb.Lock() const x = ".json"
defer idb.Unlock() i, l := c.Param("id"), ""
p := c.Param("id") j := strings.HasSuffix(i, x)
i, o := ids[p] if i = strings.TrimSuffix(i, x); i != "" {
if !o { 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/" + p + ".json"); e == nil { if r.StatusCode == http.StatusOK {
defer r.Body.Close()
if r.StatusCode == 200 {
var j struct { var j struct {
D []struct{ I struct{ ImageUrl string } } D []struct{ I struct{ ImageUrl string } }
} }
if e = json.NewDecoder(r.Body).Decode(&j); e == nil && len(j.D) > 0 { if e = json.NewDecoder(r.Body).Decode(&j); e == nil && len(j.D) > 0 {
i = j.D[0].I.ImageUrl l = j.D[0].I.ImageUrl
} }
} }
r.Body.Close()
} }
ids[p] = i
} }
c.JSON(200, i) if j {
c.JSON(200, l)
} else if l == "" {
c.Status(http.StatusNotFound)
} else {
c.Redirect(http.StatusMovedPermanently, l)
}
}) })
} }
func proxy(c *gin.Context, u string, h ...string) {
if u == "" {
c.AbortWithStatus(http.StatusBadRequest)
} else if q, e := http.NewRequest(c.Request.Method, u, c.Request.Body); e != nil {
c.AbortWithError(http.StatusInternalServerError, e)
} else {
for _, v := range h {
if v := strings.SplitN(v, ":", 2); len(v) == 2 {
q.Header.Add(v[0], v[1])
}
}
if r, e := http.DefaultClient.Do(q); e != nil {
c.AbortWithError(http.StatusInternalServerError, e)
} else {
c.DataFromReader(r.StatusCode, r.ContentLength, r.Header.Get("Content-Type"), r.Body, nil)
r.Body.Close()
}
}
}
func serve(c *gin.Context) {
var j struct {
Data struct {
Update string
Info struct{ Content struct{ Flag string } }
}
}
if e := c.Bind(&j); e != nil {
c.AbortWithError(http.StatusBadRequest, e)
} 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 {
var r map[string]any
h, sc, st := j.Data.Info.Content.Flag, "", ""
if h == "" {
h = j.Data.Update[strings.LastIndexByte(j.Data.Update, ':')+1:]
}
if t := torr.GetTorrent(h); t != nil {
if t := t.Status(); t != nil && t.Stat < 5 {
switch t.Stat {
case 4:
sc = "msx-red"
case 3:
sc = "msx-green"
default:
sc = "msx-yellow"
}
st = "{ico:north} " + strconv.Itoa(t.ActivePeers) + " / " + strconv.Itoa(t.TotalPeers) + " {ico:south} " + strconv.Itoa(t.ConnectedSeeders)
}
}
if j.Data.Update != "" {
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)
}
}