mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
Update msx.go (#411)
This commit is contained in:
@@ -2,93 +2,125 @@ package msx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"server/settings"
|
"server/settings"
|
||||||
"server/torr"
|
"server/torr"
|
||||||
"server/utils"
|
"server/utils"
|
||||||
"server/version"
|
"server/version"
|
||||||
"server/web/auth"
|
"server/web/auth"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const base, files = "tsmsx.yourok.ru", "media"
|
||||||
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 = `"></script>
|
|
||||||
</head>
|
|
||||||
<body></body>
|
|
||||||
</html>`
|
|
||||||
)
|
|
||||||
|
|
||||||
var start = struct {
|
var param = "menu:request:interaction:{SERVER}@{PREFIX}" + base + "/start.html"
|
||||||
N string `json:"name"`
|
|
||||||
V string `json:"version"`
|
func trn(h string) (st, sc string) {
|
||||||
P string `json:"parameter"`
|
if h := torr.GetTorrent(h); h != nil {
|
||||||
}{"TorrServer", version.Version, "menu:request:interaction:init@{PREFIX}{SERVER}/msx/ts"}
|
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 rsp(c *gin.Context, r *http.Response, e error) {
|
||||||
|
if e != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
|
} else {
|
||||||
|
defer r.Body.Close()
|
||||||
|
c.DataFromReader(r.StatusCode, r.ContentLength, r.Header.Get("Content-Type"), r.Body, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func SetupRoute(r gin.IRouter) {
|
func SetupRoute(r gin.IRouter) {
|
||||||
authorized := r.Group("/", auth.CheckAuth())
|
authorized := r.Group("/", auth.CheckAuth())
|
||||||
//MSX:
|
//MSX:
|
||||||
authorized.Any("/msx/*pth", func(c *gin.Context) {
|
authorized.GET("/msx/", func(c *gin.Context) {
|
||||||
switch p := strings.TrimPrefix(c.Param("pth"), "/"); p {
|
r, e := http.Get("http://" + base)
|
||||||
case "start.json":
|
rsp(c, r, e)
|
||||||
if c.Request.Method != "POST" {
|
})
|
||||||
c.JSON(200, &start)
|
authorized.GET("/msx/start.json", func(c *gin.Context) {
|
||||||
} else if e := json.NewDecoder(c.Request.Body).Decode(&start); e != nil {
|
c.JSON(200, map[string]string{"name": "TorrServer", "version": version.Version, "parameter": param})
|
||||||
|
})
|
||||||
|
authorized.POST("/msx/start.json", func(c *gin.Context) {
|
||||||
|
if e := json.NewDecoder(c.Request.Body).Decode(¶m); e != nil {
|
||||||
c.AbortWithError(http.StatusBadRequest, e)
|
c.AbortWithError(http.StatusBadRequest, e)
|
||||||
}
|
}
|
||||||
case "proxy":
|
})
|
||||||
proxy(c, c.Query("url"), c.QueryArray("header")...)
|
authorized.GET("/msx/trn", func(c *gin.Context) {
|
||||||
case "torrent":
|
r := false
|
||||||
torrent(c)
|
if h := c.Query("hash"); h != "" {
|
||||||
default:
|
for _, t := range settings.ListTorrent() {
|
||||||
if p != "" && !strings.HasSuffix(p, "/") && path.Ext(p) == "" {
|
if r = (t != nil && t.InfoHash.HexString() == h); r {
|
||||||
p = base + p + `.js?t=` + strconv.FormatInt(time.Now().Unix(), 16)
|
break
|
||||||
c.Data(200, "text/html;charset=UTF-8", append(append([]byte(htmlB), p...), htmlE...))
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(200, r)
|
||||||
|
})
|
||||||
|
authorized.POST("/msx/trn", func(c *gin.Context) {
|
||||||
|
var r struct {
|
||||||
|
R struct {
|
||||||
|
S int `json:"status"`
|
||||||
|
T string `json:"text"`
|
||||||
|
M string `json:"message,omitempty"`
|
||||||
|
D map[string]any `json:"data,omitempty"`
|
||||||
|
} `json:"response"`
|
||||||
|
}
|
||||||
|
if j := struct{ Data string }{Data: c.Query("hash")}; j.Data != "" {
|
||||||
|
st, sc := trn(j.Data)
|
||||||
|
if sc != "" {
|
||||||
|
sc = "{col:" + sc + "}"
|
||||||
|
}
|
||||||
|
r.R.S, r.R.D = http.StatusOK, map[string]any{"action": "player:label:position:{LABEL}{tb}{tb}" + sc + st}
|
||||||
|
} else if e := json.NewDecoder(c.Request.Body).Decode(&j); e != nil {
|
||||||
|
r.R.S, r.R.M = http.StatusBadRequest, e.Error()
|
||||||
|
} else if j.Data == "" {
|
||||||
|
r.R.S, r.R.M = http.StatusBadRequest, "data is not set"
|
||||||
} else {
|
} else {
|
||||||
proxy(c, base+p)
|
st, sc := trn(j.Data[strings.LastIndexByte(j.Data, ':')+1:])
|
||||||
|
r.R.D = map[string]any{"stamp": st, "stampColor": sc}
|
||||||
|
if sc != "" {
|
||||||
|
r.R.D["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.S, r.R.D = http.StatusOK, map[string]any{"action": j.Data, "data": r.R.D}
|
||||||
|
}
|
||||||
|
r.R.T = http.StatusText(r.R.S)
|
||||||
|
c.JSON(200, &r)
|
||||||
})
|
})
|
||||||
// files:
|
authorized.Any("/msx/proxy", func(c *gin.Context) {
|
||||||
authorized.GET("/files", func(c *gin.Context) {
|
if u := c.Query("url"); u == "" {
|
||||||
if l, e := os.Readlink(files); e == nil || os.IsNotExist(e) {
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
c.JSON(200, l)
|
} else if q, e := http.NewRequest(c.Request.Method, u, c.Request.Body); e != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
} else {
|
} else {
|
||||||
c.AbortWithError(http.StatusInternalServerError, e)
|
for _, v := range c.QueryArray("header") {
|
||||||
|
if v := strings.SplitN(v, ":", 2); len(v) == 2 {
|
||||||
|
q.Header.Add(v[0], v[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r, e := http.DefaultClient.Do(q)
|
||||||
|
rsp(c, r, e)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
authorized.POST("/files", func(c *gin.Context) {
|
authorized.GET("/msx/imdb/:id", 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
authorized.StaticFS("/files/", gin.Dir(files, true))
|
|
||||||
// IMDB:
|
|
||||||
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"
|
||||||
@@ -112,92 +144,25 @@ func SetupRoute(r gin.IRouter) {
|
|||||||
c.Redirect(http.StatusMovedPermanently, l)
|
c.Redirect(http.StatusMovedPermanently, l)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
//Files:
|
||||||
|
authorized.StaticFS("/files/", gin.Dir(filepath.Join(settings.Path, files), true))
|
||||||
func proxy(c *gin.Context, u string, h ...string) {
|
authorized.GET("/files", func(c *gin.Context) {
|
||||||
if u == "" {
|
if l, e := os.Readlink(filepath.Join(settings.Path, files)); e == nil || os.IsNotExist(e) {
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
c.JSON(200, l)
|
||||||
} else if q, e := http.NewRequest(c.Request.Method, u, c.Request.Body); e != nil {
|
} else {
|
||||||
c.AbortWithError(http.StatusInternalServerError, e)
|
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 {
|
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(filepath.Join(settings.Path, files)); e != nil && !os.IsNotExist(e) {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
|
} else if l != "" {
|
||||||
|
if e = os.Symlink(l, filepath.Join(settings.Path, files)); e != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, e)
|
c.AbortWithError(http.StatusInternalServerError, e)
|
||||||
} else {
|
|
||||||
c.DataFromReader(r.StatusCode, r.ContentLength, r.Header.Get("Content-Type"), r.Body, nil)
|
|
||||||
r.Body.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
func trnGet(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 response(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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func torrent(c *gin.Context) {
|
|
||||||
if c.Request.Method != "POST" {
|
|
||||||
r := false
|
|
||||||
if h := c.Query("hash"); h != "" {
|
|
||||||
for _, t := range settings.ListTorrent() {
|
|
||||||
if r = (t != nil && t.InfoHash.HexString() == h); r {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.JSON(200, r)
|
|
||||||
} else if j := struct{ Data string }{Data: c.Query("hash")}; j.Data != "" {
|
|
||||||
st, sc := trnGet(j.Data)
|
|
||||||
if sc != "" {
|
|
||||||
sc = "{col:" + sc + "}"
|
|
||||||
}
|
|
||||||
response(c, map[string]string{"action": "player:label:position:{VALUE}{tb}{tb}" + sc + st})
|
|
||||||
} else if e := json.NewDecoder(c.Request.Body).Decode(&j); e != nil {
|
|
||||||
response(c, e)
|
|
||||||
} else if j.Data == "" {
|
|
||||||
response(c, errors.New("data is not set"))
|
|
||||||
} else {
|
|
||||||
st, sc := trnGet(j.Data[strings.LastIndexByte(j.Data, ':')+1:])
|
|
||||||
response(c, map[string]any{"action": j.Data, "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": j.Data,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user