mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
new MSX (#394)
This commit is contained in:
@@ -3,70 +3,97 @@ package msx
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"server/settings"
|
||||
"server/torr"
|
||||
"server/utils"
|
||||
"server/version"
|
||||
"server/web/auth"
|
||||
|
||||
"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) {
|
||||
authorized := r.Group("/", auth.CheckAuth())
|
||||
authorized.Any("/msx", func(c *gin.Context) {
|
||||
if l := c.Query("url"); l != "" {
|
||||
proxy(c, l, c.QueryArray("header")...)
|
||||
} else if l = c.Query("indb"); l != "" {
|
||||
authorized.GET("/msx/inf", func(c *gin.Context) {
|
||||
if p, o := c.GetQuery("parameter"); o {
|
||||
if p == "" {
|
||||
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
|
||||
for _, t := range settings.ListTorrent() {
|
||||
if r = t.InfoHash.HexString() == l; r {
|
||||
if r = t.InfoHash.HexString() == h; r {
|
||||
break
|
||||
}
|
||||
}
|
||||
c.JSON(200, r)
|
||||
} else if c.Request.Method == "POST" {
|
||||
serve(c)
|
||||
} else if h = c.Query("hash"); h != "" {
|
||||
st, sc := trn(h)
|
||||
if sc != "" {
|
||||
sc = "{col:" + sc + "}"
|
||||
}
|
||||
msx(c, map[string]string{"action": "player:label:position:{VALUE}{tb}{tb}" + sc + st})
|
||||
} else {
|
||||
proxy(c, base+"/ts.html")
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
})
|
||||
authorized.GET("/msx/*pth", func(c *gin.Context) {
|
||||
p := c.Param("pth")
|
||||
if _, n := path.Split(p); n == "" {
|
||||
files(c, filepath.Join(fls, filepath.Clean(p)))
|
||||
} 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)
|
||||
authorized.POST("/msx/trn", func(c *gin.Context) {
|
||||
var j struct{ Data string }
|
||||
if e := json.NewDecoder(c.Request.Body).Decode(&j); e != nil {
|
||||
msx(c, e)
|
||||
} 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) {
|
||||
const x = ".json"
|
||||
i, l := c.Param("id"), ""
|
||||
j := strings.HasSuffix(i, x)
|
||||
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.StatusCode == http.StatusOK {
|
||||
var j struct {
|
||||
D []struct{ I struct{ ImageUrl string } }
|
||||
}
|
||||
if e = json.NewDecoder(r.Body).Decode(&j); e == nil && len(j.D) > 0 {
|
||||
l = j.D[0].I.ImageUrl
|
||||
}
|
||||
i, l, j := c.Param("id"), "", false
|
||||
if j = strings.HasSuffix(i, ".json"); !j {
|
||||
i += ".json"
|
||||
}
|
||||
if r, e := http.Get("https://v2.sg.media-imdb.com/suggestion/h/" + i); e == nil {
|
||||
if r.StatusCode == http.StatusOK {
|
||||
var j struct {
|
||||
D []struct{ I struct{ ImageUrl string } }
|
||||
}
|
||||
if e = json.NewDecoder(r.Body).Decode(&j); e == nil && len(j.D) > 0 {
|
||||
l = j.D[0].I.ImageUrl
|
||||
}
|
||||
r.Body.Close()
|
||||
}
|
||||
r.Body.Close()
|
||||
}
|
||||
if j {
|
||||
c.JSON(200, l)
|
||||
@@ -76,8 +103,8 @@ func SetupRoute(r gin.IRouter) {
|
||||
c.Redirect(http.StatusMovedPermanently, l)
|
||||
}
|
||||
})
|
||||
authorized.Static("/files", files)
|
||||
}
|
||||
|
||||
func proxy(c *gin.Context, u string, h ...string) {
|
||||
if u == "" {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
@@ -97,70 +124,38 @@ func proxy(c *gin.Context, u string, h ...string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func serve(c *gin.Context) {
|
||||
var j struct {
|
||||
Data struct {
|
||||
Update string
|
||||
Info struct{ Content struct{ Flag string } }
|
||||
}
|
||||
func msx(c *gin.Context, d any) {
|
||||
var r struct {
|
||||
R struct {
|
||||
S int `json:"status"`
|
||||
T string `json:"text"`
|
||||
M string `json:"message,omitempty"`
|
||||
D any `json:"data,omitempty"`
|
||||
} `json:"response"`
|
||||
}
|
||||
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)
|
||||
if e, o := d.(error); o {
|
||||
r.R.S = http.StatusBadRequest
|
||||
r.R.M = e.Error()
|
||||
} 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}})
|
||||
r.R.S = http.StatusOK
|
||||
r.R.D = d
|
||||
}
|
||||
r.R.T = http.StatusText(r.R.S)
|
||||
c.JSON(200, &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()})
|
||||
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:
|
||||
c = "msx-red"
|
||||
case 3:
|
||||
c = "msx-green"
|
||||
default:
|
||||
c = "msx-yellow"
|
||||
}
|
||||
t = "{ico:north} " + strconv.Itoa(h.ActivePeers) + " / " + strconv.Itoa(h.TotalPeers) + " {ico:south} " + strconv.Itoa(h.ConnectedSeeders)
|
||||
}
|
||||
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)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user