mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
MSX bug fixed (#405)
* MSX bug fixed * Update msx.go one more bug fixec
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -17,154 +18,97 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const files, param = "files", "menu:request:interaction:init@{PREFIX}{SERVER}/msx/plugin.html"
|
const (
|
||||||
|
files = `files`
|
||||||
|
base = `https://damiva.github.io/msx`
|
||||||
|
htmlB = `<!DOCTYPE html><html><head><title>TorrServer MSX Plugin</title><meta charset="UTF-8" /><meta name="author" content="damiva" /><script type="text/javascript" src="http://msx.benzac.de/js/tvx-plugin.min.js"></script><script type="text/javascript" src="`
|
||||||
|
htmlE = `.js"></script></head><body></body></html>`
|
||||||
|
)
|
||||||
|
|
||||||
var parameter = param
|
var start = struct {
|
||||||
|
N string `json:"name"`
|
||||||
|
V string `json:"version"`
|
||||||
|
P string `json:"parameter"`
|
||||||
|
}{"TorrServer", version.Version, "menu:request:interaction:init@{PREFIX}{SERVER}/msx/ts"}
|
||||||
|
|
||||||
func SetupRoute(r gin.IRouter) {
|
func SetupRoute(r gin.IRouter) {
|
||||||
authorized := r.Group("/", auth.CheckAuth())
|
authorized := r.Group("/", auth.CheckAuth())
|
||||||
authorized.Any("/msx", mng)
|
authorized.GET("/msx/start.json", func(c *gin.Context) {
|
||||||
authorized.GET("/msx/*pth", func(c *gin.Context) { proxy(c, "https://damiva.github.io/msx"+c.Param("pth")) })
|
c.JSON(200, &start)
|
||||||
|
})
|
||||||
authorized.GET("/files", fls)
|
authorized.POST("/msx/start,json", func(c *gin.Context) {
|
||||||
authorized.StaticFS("/files/", gin.Dir(files, true))
|
if e := c.Bind(&start); e != nil {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, e)
|
||||||
authorized.GET("/imdb/:id", imdb)
|
|
||||||
}
|
}
|
||||||
func proxy(c *gin.Context, u string, h ...string) {
|
})
|
||||||
if u == "" {
|
authorized.Any("/msx/proxy", func(c *gin.Context) {
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
proxy(c, c.Query("url"), c.QueryArray("header")...)
|
||||||
} else if q, e := http.NewRequest(c.Request.Method, u, c.Request.Body); e != nil {
|
})
|
||||||
c.AbortWithError(http.StatusInternalServerError, e)
|
authorized.GET("/msx/trn", func(c *gin.Context) {
|
||||||
} else {
|
if h := c.Query("hash"); h != "" {
|
||||||
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 mng(c *gin.Context) {
|
|
||||||
if p := c.Query("url"); p != "" {
|
|
||||||
proxy(c, p, c.QueryArray("header")...)
|
|
||||||
} else if c.Request.Method == "POST" {
|
|
||||||
trn(c)
|
|
||||||
} else if p = c.Query("indb"); p != "" {
|
|
||||||
var r bool
|
var r bool
|
||||||
for _, t := range settings.ListTorrent() {
|
for _, t := range settings.ListTorrent() {
|
||||||
if r = (t != nil && t.InfoHash.HexString() == p); r {
|
if r = (t != nil && t.InfoHash.HexString() == h); r {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.JSON(200, r)
|
c.JSON(200, r)
|
||||||
} else {
|
} else {
|
||||||
if p, o := c.GetQuery("parameter"); o {
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
if p == "" {
|
|
||||||
parameter = param
|
|
||||||
} else {
|
|
||||||
parameter = p
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
c.JSON(200, map[string]any{"version": version.Version, "search": settings.BTsets.EnableRutorSearch, "parameter": parameter})
|
authorized.POST("/msx/trn", func(c *gin.Context) {
|
||||||
}
|
if j := struct{ Data string }{Data: c.Query("hash")}; j.Data != "" {
|
||||||
}
|
st, sc := trn(j.Data)
|
||||||
func trn(c *gin.Context) {
|
|
||||||
var (
|
|
||||||
h, a string
|
|
||||||
q struct{ Data any }
|
|
||||||
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 := json.NewDecoder(c.Request.Body).Decode(&q); e != nil {
|
|
||||||
r.R.M = e.Error()
|
|
||||||
} else if s, o := q.Data.(string); o {
|
|
||||||
a, h = s, a[strings.LastIndexByte(s, ':')+1:]
|
|
||||||
} else if s, o := q.Data.(map[string]any); o {
|
|
||||||
if s, o := s["info"].(map[string]any); o {
|
|
||||||
if s, o := s["content"].(map[string]any); o {
|
|
||||||
h, _ = s["flag"].(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if h != "" {
|
|
||||||
var st, sc string
|
|
||||||
if h := torr.GetTorrent(h); h != nil {
|
|
||||||
if h := h.Status(); h != nil && h.Stat < 5 {
|
|
||||||
switch h.Stat {
|
|
||||||
case 4:
|
|
||||||
sc = "msx-red"
|
|
||||||
case 3:
|
|
||||||
sc = "msx-green"
|
|
||||||
default:
|
|
||||||
sc = "msx-yellow"
|
|
||||||
}
|
|
||||||
st = "{ico:north} " + strconv.Itoa(h.ActivePeers) + " / " + strconv.Itoa(h.TotalPeers) + " {ico:south} " + strconv.Itoa(h.ConnectedSeeders)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if a != "" {
|
|
||||||
r.R.D = map[string]any{"action": a, "data": 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": a,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
} else {
|
|
||||||
if sc != "" {
|
if sc != "" {
|
||||||
sc = "{col:" + sc + "}"
|
sc = "{col:" + sc + "}"
|
||||||
}
|
}
|
||||||
r.R.D = map[string]string{"action": "player:label:position:{VALUE}{tb}{tb}" + sc + st}
|
msx(c, map[string]string{"action": "player:label:position:{VALUE}{tb}{tb}" + sc + st})
|
||||||
}
|
} else if e := c.Bind(&j); e != nil {
|
||||||
} else if r.R.M == "" {
|
msx(c, e)
|
||||||
r.R.M = "wrong data struct"
|
} else if j.Data == "" {
|
||||||
}
|
msx(c, errors.New("data is not set"))
|
||||||
if r.R.D == nil {
|
|
||||||
r.R.S = http.StatusBadRequest
|
|
||||||
} else {
|
} else {
|
||||||
r.R.S = http.StatusOK
|
st, sc := trn(j.Data[strings.LastIndexByte(j.Data, ':')+1:])
|
||||||
|
msx(c, map[string]any{"stamp": st, "stampColor": sc, "live": 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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}})
|
||||||
}
|
}
|
||||||
r.R.T = http.StatusText(r.R.S)
|
})
|
||||||
c.JSON(200, &r)
|
authorized.GET("/msx/*pth", func(c *gin.Context) {
|
||||||
}
|
if p := c.Param("pth"); !strings.HasSuffix(p, "/") && path.Ext(p) == "" {
|
||||||
func fls(c *gin.Context) {
|
c.Data(200, "text/html;charset=UTF-8", append(append(append([]byte(htmlB), base...), p...), htmlE...))
|
||||||
var e error
|
|
||||||
p, o := c.GetQuery("path")
|
|
||||||
if o {
|
|
||||||
if e = os.Remove(files); e != nil && os.IsNotExist(e) {
|
|
||||||
e = nil
|
|
||||||
}
|
|
||||||
if e == nil && p != "" {
|
|
||||||
var f os.FileInfo
|
|
||||||
if f, e = os.Stat(p); e == nil {
|
|
||||||
if f.IsDir() {
|
|
||||||
e = os.Symlink(p, files)
|
|
||||||
} else {
|
} else {
|
||||||
e = errors.New(p + " is not a directory")
|
proxy(c, base+p)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
|
||||||
} else if p, e = os.Readlink(files); e != nil && os.IsNotExist(e) {
|
authorized.GET("/files", func(c *gin.Context) {
|
||||||
e = nil
|
if l, e := os.Readlink(files); e == nil || os.IsNotExist(e) {
|
||||||
}
|
c.JSON(200, l)
|
||||||
if e == nil {
|
|
||||||
c.JSON(200, p)
|
|
||||||
} else {
|
} else {
|
||||||
c.AbortWithError(http.StatusInternalServerError, e)
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
authorized.POST("/files", func(c *gin.Context) {
|
||||||
|
var l string
|
||||||
|
if e := c.Bind(&l); e != nil {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, e)
|
||||||
|
} else if e = os.Remove(files); e != nil && !os.IsNotExist(e) {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
|
} else if l != "" {
|
||||||
|
if e = os.Symlink(l, files); e != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
}
|
}
|
||||||
func imdb(c *gin.Context) {
|
}
|
||||||
|
})
|
||||||
|
authorized.StaticFS("/files/", gin.Dir(files, true))
|
||||||
|
|
||||||
|
authorized.GET("/imdb/:id", func(c *gin.Context) {
|
||||||
i, l, j := strings.TrimPrefix(c.Param("id"), "/"), "", false
|
i, l, j := strings.TrimPrefix(c.Param("id"), "/"), "", false
|
||||||
if j = strings.HasSuffix(i, ".json"); !j {
|
if j = strings.HasSuffix(i, ".json"); !j {
|
||||||
i += ".json"
|
i += ".json"
|
||||||
@@ -187,4 +131,57 @@ func imdb(c *gin.Context) {
|
|||||||
} else {
|
} else {
|
||||||
c.Redirect(http.StatusMovedPermanently, l)
|
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 trn(h string) (st, sc string) {
|
||||||
|
if h := torr.GetTorrent(h); h != nil {
|
||||||
|
if h := h.Status(); h != nil && h.Stat < 5 {
|
||||||
|
switch h.Stat {
|
||||||
|
case 4:
|
||||||
|
sc = "msx-red"
|
||||||
|
case 3:
|
||||||
|
sc = "msx-green"
|
||||||
|
default:
|
||||||
|
sc = "msx-yellow"
|
||||||
|
}
|
||||||
|
st = "{ico:north} " + strconv.Itoa(h.ActivePeers) + " / " + strconv.Itoa(h.TotalPeers) + " {ico:south} " + strconv.Itoa(h.ConnectedSeeders)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func msx(c *gin.Context, a 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, o := a.(error); o {
|
||||||
|
r.R.S, r.R.M = http.StatusBadRequest, e.Error()
|
||||||
|
} else {
|
||||||
|
r.R.S, r.R.D = http.StatusOK, a
|
||||||
|
}
|
||||||
|
r.R.T = http.StatusText(r.R.S)
|
||||||
|
c.JSON(200, &r)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user