mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 21:46:11 +05:00
Update msx.go (#400)
This commit is contained in:
@@ -3,7 +3,6 @@ package msx
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -24,109 +23,13 @@ var parameter = param
|
|||||||
|
|
||||||
func SetupRoute(r gin.IRouter) {
|
func SetupRoute(r gin.IRouter) {
|
||||||
authorized := r.Group("/", auth.CheckAuth())
|
authorized := r.Group("/", auth.CheckAuth())
|
||||||
authorized.GET("/msx/stg", func(c *gin.Context) {
|
authorized.Any("/msx", mng)
|
||||||
r := map[string]any{"version": version.Version, "search": settings.BTsets.EnableRutorSearch}
|
authorized.GET("/msx/*pth", func(c *gin.Context) { proxy(c, "https://damiva.github.io/msx"+c.Param("pth")) })
|
||||||
if p, o := c.GetQuery("parameter"); o {
|
|
||||||
if p == "" {
|
authorized.GET("/files", fls)
|
||||||
p = param
|
authorized.StaticFS("/files/", gin.Dir(files, true))
|
||||||
}
|
|
||||||
parameter = p
|
authorized.GET("/imdb/:id", imdb)
|
||||||
}
|
|
||||||
r["parameter"] = parameter
|
|
||||||
if p, o := c.GetQuery(files); o {
|
|
||||||
e := os.Remove(files)
|
|
||||||
if os.IsNotExist(e) {
|
|
||||||
e = nil
|
|
||||||
}
|
|
||||||
if e == nil && p != "" {
|
|
||||||
var f fs.FileInfo
|
|
||||||
if f, e = os.Stat(p); e == nil {
|
|
||||||
if !f.IsDir() {
|
|
||||||
e = errors.New(p + " is not a folder")
|
|
||||||
} else {
|
|
||||||
e = os.Symlink(p, files)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if e == nil {
|
|
||||||
r[files] = p
|
|
||||||
} else {
|
|
||||||
r = map[string]any{"error": e.Error()}
|
|
||||||
}
|
|
||||||
} else if l, e := os.Readlink(files); e == nil {
|
|
||||||
r[files] = l
|
|
||||||
} else if !os.IsNotExist(e) {
|
|
||||||
r["error"] = 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() == h; r {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.JSON(200, r)
|
|
||||||
} 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 {
|
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
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 {
|
|
||||||
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/", func(c *gin.Context) {
|
|
||||||
proxy(c, "https://damiva.github.io"+c.Request.URL.EscapedPath())
|
|
||||||
})
|
|
||||||
authorized.GET("/imdb/:id", func(c *gin.Context) {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
if j {
|
|
||||||
c.JSON(200, l)
|
|
||||||
} else if l == "" {
|
|
||||||
c.Status(http.StatusNotFound)
|
|
||||||
} else {
|
|
||||||
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 == "" {
|
||||||
@@ -147,38 +50,141 @@ func proxy(c *gin.Context, u string, h ...string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func msx(c *gin.Context, d any) {
|
func mng(c *gin.Context) {
|
||||||
var r struct {
|
if p := c.Query("url"); p != "" {
|
||||||
R struct {
|
proxy(c, p, c.QueryArray("header")...)
|
||||||
S int `json:"status"`
|
} else if c.Request.Method == "POST" {
|
||||||
T string `json:"text"`
|
trn(c)
|
||||||
M string `json:"message,omitempty"`
|
} else if p = c.Query("indb"); p != "" {
|
||||||
D any `json:"data,omitempty"`
|
var r bool
|
||||||
} `json:"response"`
|
for _, t := range settings.ListTorrent() {
|
||||||
|
if r = (t != nil && t.InfoHash.HexString() == p); r {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(200, r)
|
||||||
|
} else {
|
||||||
|
if p, o := c.GetQuery("parameter"); o {
|
||||||
|
if p == "" {
|
||||||
|
parameter = param
|
||||||
|
} else {
|
||||||
|
parameter = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(200, map[string]any{"version": version.Version, "search": settings.BTsets.EnableRutorSearch, "parameter": parameter})
|
||||||
}
|
}
|
||||||
if e, o := d.(error); o {
|
}
|
||||||
r.R.S = http.StatusBadRequest
|
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()
|
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 != "" {
|
||||||
|
sc = "{col:" + sc + "}"
|
||||||
|
}
|
||||||
|
r.R.D = map[string]string{"action": "player:label:position:{VALUE}{tb}{tb}" + sc + st}
|
||||||
|
}
|
||||||
|
} else if r.R.M == "" {
|
||||||
|
r.R.M = "wrong data struct"
|
||||||
|
}
|
||||||
|
if r.R.D == nil {
|
||||||
|
r.R.S = http.StatusBadRequest
|
||||||
} else {
|
} else {
|
||||||
r.R.S = http.StatusOK
|
r.R.S = http.StatusOK
|
||||||
r.R.D = d
|
|
||||||
}
|
}
|
||||||
r.R.T = http.StatusText(r.R.S)
|
r.R.T = http.StatusText(r.R.S)
|
||||||
c.JSON(200, &r)
|
c.JSON(200, &r)
|
||||||
}
|
}
|
||||||
func trn(h string) (t, c string) {
|
func fls(c *gin.Context) {
|
||||||
if h := torr.GetTorrent(h); h != nil {
|
var e error
|
||||||
if h := h.Status(); h != nil && h.Stat < 5 {
|
p, o := c.GetQuery("path")
|
||||||
switch h.Stat {
|
if o {
|
||||||
case 4:
|
if e = os.Remove(files); e != nil && os.IsNotExist(e) {
|
||||||
c = "msx-red"
|
e = nil
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
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 {
|
||||||
|
e = errors.New(p + " is not a directory")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if p, e = os.Readlink(files); e != nil && os.IsNotExist(e) {
|
||||||
|
e = nil
|
||||||
|
}
|
||||||
|
if e == nil {
|
||||||
|
c.JSON(200, p)
|
||||||
|
} else {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func imdb(c *gin.Context) {
|
||||||
|
i, l, j := strings.TrimPrefix(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()
|
||||||
|
}
|
||||||
|
if j {
|
||||||
|
c.JSON(200, l)
|
||||||
|
} else if l == "" {
|
||||||
|
c.Status(http.StatusNotFound)
|
||||||
|
} else {
|
||||||
|
c.Redirect(http.StatusMovedPermanently, l)
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user