Create & Init Project...
This commit is contained in:
45
app/interface/main/player/http/BUILD.bazel
Normal file
45
app/interface/main/player/http/BUILD.bazel
Normal file
@ -0,0 +1,45 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"archive.go",
|
||||
"http.go",
|
||||
"player.go",
|
||||
"playurl.go",
|
||||
],
|
||||
importpath = "go-common/app/interface/main/player/http",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//app/interface/main/player/conf:go_default_library",
|
||||
"//app/interface/main/player/model:go_default_library",
|
||||
"//app/interface/main/player/service:go_default_library",
|
||||
"//app/service/main/archive/api:go_default_library",
|
||||
"//library/ecode:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/log/infoc:go_default_library",
|
||||
"//library/net/http/blademaster:go_default_library",
|
||||
"//library/net/http/blademaster/middleware/auth:go_default_library",
|
||||
"//library/net/http/blademaster/middleware/verify:go_default_library",
|
||||
"//library/net/metadata:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
60
app/interface/main/player/http/archive.go
Normal file
60
app/interface/main/player/http/archive.go
Normal file
@ -0,0 +1,60 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"go-common/app/service/main/archive/api"
|
||||
"go-common/library/ecode"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
)
|
||||
|
||||
func pageList(c *bm.Context) {
|
||||
var (
|
||||
aid int64
|
||||
err error
|
||||
pages []*api.Page
|
||||
)
|
||||
aidStr := c.Request.Form.Get("aid")
|
||||
if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
|
||||
c.JSON(nil, ecode.RequestErr)
|
||||
return
|
||||
}
|
||||
if pages, err = playSvr.PageList(c, aid); err != nil {
|
||||
c.JSON(nil, err)
|
||||
return
|
||||
}
|
||||
if len(pages) == 0 {
|
||||
c.JSON(nil, ecode.NothingFound)
|
||||
return
|
||||
}
|
||||
c.JSON(pages, nil)
|
||||
}
|
||||
|
||||
func videoShot(c *bm.Context) {
|
||||
v := new(struct {
|
||||
Aid int64 `form:"aid" validate:"min=1"`
|
||||
Cid int64 `form:"cid"`
|
||||
Index bool `form:"index"`
|
||||
})
|
||||
if err := c.Bind(v); err != nil {
|
||||
return
|
||||
}
|
||||
c.JSON(playSvr.VideoShot(c, v.Aid, v.Cid, v.Index))
|
||||
}
|
||||
|
||||
func playURLToken(c *bm.Context) {
|
||||
var (
|
||||
aid, cid, mid int64
|
||||
err error
|
||||
)
|
||||
params := c.Request.Form
|
||||
aidStr := params.Get("aid")
|
||||
if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil {
|
||||
c.JSON(nil, ecode.RequestErr)
|
||||
return
|
||||
}
|
||||
cid, _ = strconv.ParseInt(params.Get("cid"), 10, 64)
|
||||
midStr, _ := c.Get("mid")
|
||||
mid = midStr.(int64)
|
||||
c.JSON(playSvr.PlayURLToken(c, mid, aid, cid))
|
||||
}
|
69
app/interface/main/player/http/http.go
Normal file
69
app/interface/main/player/http/http.go
Normal file
@ -0,0 +1,69 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go-common/app/interface/main/player/conf"
|
||||
"go-common/app/interface/main/player/service"
|
||||
"go-common/library/log"
|
||||
"go-common/library/log/infoc"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
"go-common/library/net/http/blademaster/middleware/auth"
|
||||
"go-common/library/net/http/blademaster/middleware/verify"
|
||||
)
|
||||
|
||||
var (
|
||||
authSvr *auth.Auth
|
||||
vfySvr *verify.Verify
|
||||
playSvr *service.Service
|
||||
playInfoc *infoc.Infoc
|
||||
)
|
||||
|
||||
// Init init http.
|
||||
func Init(c *conf.Config) error {
|
||||
authSvr = auth.New(c.Auth)
|
||||
vfySvr = verify.New(c.Verify)
|
||||
playSvr = service.New(c)
|
||||
engine := bm.DefaultServer(c.BM.Outer)
|
||||
outerRouter(engine)
|
||||
internalRouter(engine)
|
||||
if err := engine.Start(); err != nil {
|
||||
log.Error("engine.Start error(%v)", err)
|
||||
return err
|
||||
}
|
||||
// init infoc
|
||||
if c.Infoc2 != nil {
|
||||
playInfoc = infoc.New(c.Infoc2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func outerRouter(e *bm.Engine) {
|
||||
e.GET("/monitor/ping", ping)
|
||||
e.GET("/x/player.so", bm.CORS(), authSvr.Guest, player)
|
||||
group := e.Group("/x/player", bm.CORS())
|
||||
{
|
||||
group.GET("/policy", authSvr.Guest, policy)
|
||||
group.GET("/carousel.so", carousel)
|
||||
group.GET("/view", view)
|
||||
group.GET("/matsuri", matPage)
|
||||
group.GET("/pagelist", pageList)
|
||||
group.GET("/videoshot", videoShot)
|
||||
group.GET("/playurl/token", authSvr.User, playURLToken)
|
||||
group.GET("/playurl", authSvr.Guest, playurl)
|
||||
}
|
||||
}
|
||||
|
||||
func internalRouter(e *bm.Engine) {
|
||||
group := e.Group("/x/internal/player")
|
||||
{
|
||||
group.GET("/playurl", vfySvr.Verify, authSvr.Guest, playurl)
|
||||
}
|
||||
}
|
||||
|
||||
func ping(c *bm.Context) {
|
||||
if err := playSvr.Ping(c); err != nil {
|
||||
log.Error("player service ping error(%v)", err)
|
||||
c.AbortWithStatus(http.StatusServiceUnavailable)
|
||||
}
|
||||
}
|
192
app/interface/main/player/http/player.go
Normal file
192
app/interface/main/player/http/player.go
Normal file
@ -0,0 +1,192 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go-common/app/interface/main/player/model"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
"go-common/library/net/metadata"
|
||||
)
|
||||
|
||||
type playerInfoc struct {
|
||||
ip string
|
||||
ctime string
|
||||
api string
|
||||
buvid string
|
||||
mid string
|
||||
client string
|
||||
itemID string
|
||||
displayID string
|
||||
errCode string
|
||||
from string
|
||||
build string
|
||||
trackID string
|
||||
}
|
||||
|
||||
const (
|
||||
_api = "player"
|
||||
_client = "web"
|
||||
)
|
||||
|
||||
var (
|
||||
emptyByte = []byte{}
|
||||
_headerBuvid = "Buvid"
|
||||
_buvid = "buvid3"
|
||||
)
|
||||
|
||||
func player(c *bm.Context) {
|
||||
var (
|
||||
aid, cid, mid int64
|
||||
sid model.Sid
|
||||
sidCookie *http.Cookie
|
||||
err error
|
||||
ip = metadata.String(c, metadata.RemoteIP)
|
||||
)
|
||||
request := c.Request
|
||||
params := request.Form
|
||||
idStr := params.Get("id")
|
||||
aidStr := params.Get("aid")
|
||||
cidStr := params.Get("cid")
|
||||
refer := request.Referer()
|
||||
// response header
|
||||
c.Writer.Header().Set("Cache-Control", "no-cache")
|
||||
c.Writer.Header().Set("X-Remote-IP", ip)
|
||||
// get mid
|
||||
if midInter, ok := c.Get("mid"); ok {
|
||||
mid = midInter.(int64)
|
||||
}
|
||||
if strings.Index(idStr, "cid:") == 0 {
|
||||
cidStr = idStr[4:]
|
||||
}
|
||||
if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// get aid
|
||||
if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
sidCookie, err = request.Cookie("sid")
|
||||
if err != nil || sidCookie.Value == "" {
|
||||
http.SetCookie(c.Writer, &http.Cookie{Name: "sid", Value: string(sid.Create()), Path: "/", Domain: ".bilibili.com"})
|
||||
}
|
||||
if sidCookie != nil {
|
||||
if sidCookie.Value != "" {
|
||||
sidStr := sidCookie.Value
|
||||
if !model.Sid(sidStr).Valid() {
|
||||
http.SetCookie(c.Writer, &http.Cookie{Name: "sid", Value: string(sid.Create()), Path: "/", Domain: ".bilibili.com"})
|
||||
}
|
||||
}
|
||||
}
|
||||
var (
|
||||
ips = strings.Split(c.Request.Header.Get("X-Forwarded-For"), ",")
|
||||
cdnIP string
|
||||
playByte []byte
|
||||
)
|
||||
if len(ips) >= 2 {
|
||||
cdnIP = ips[1]
|
||||
}
|
||||
buvid := request.Header.Get(_headerBuvid)
|
||||
if buvid == "" {
|
||||
cookie, _ := request.Cookie(_buvid)
|
||||
if cookie != nil {
|
||||
buvid = cookie.Value
|
||||
}
|
||||
}
|
||||
now := time.Now()
|
||||
if playInfoc != nil {
|
||||
showInfoc(ip, now, buvid, aid, mid)
|
||||
}
|
||||
if playByte, err = playSvr.Player(c, mid, aid, cid, cdnIP, refer, now); err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
c.Bytes(http.StatusOK, "text/plain; charset=utf-8", playByte)
|
||||
}
|
||||
|
||||
func carousel(c *bm.Context) {
|
||||
// response header
|
||||
c.Writer.Header().Set("Cache-Control", "no-cache")
|
||||
c.Writer.Header().Set("Connection", "keep-alive")
|
||||
type msg struct {
|
||||
Items []*model.Item `xml:"item"`
|
||||
}
|
||||
var (
|
||||
items []*model.Item
|
||||
err error
|
||||
)
|
||||
if items, err = playSvr.Carousel(c); err != nil {
|
||||
c.Bytes(http.StatusOK, "text/xml; charset=utf-8", emptyByte)
|
||||
return
|
||||
}
|
||||
result := msg{Items: items}
|
||||
output, err := xml.MarshalIndent(result, " ", " ")
|
||||
if err != nil {
|
||||
output = emptyByte
|
||||
}
|
||||
c.Bytes(http.StatusOK, "text/xml; charset=utf-8", output)
|
||||
}
|
||||
|
||||
func policy(c *bm.Context) {
|
||||
var (
|
||||
id int64
|
||||
mid int64
|
||||
err error
|
||||
)
|
||||
if midInter, ok := c.Get("mid"); ok {
|
||||
mid = midInter.(int64)
|
||||
}
|
||||
params := c.Request.Form
|
||||
idStr := params.Get("id")
|
||||
if id, err = strconv.ParseInt(idStr, 10, 64); err != nil || id < 0 {
|
||||
c.JSON(nil, ecode.RequestErr)
|
||||
return
|
||||
}
|
||||
c.JSON(playSvr.Policy(c, id, mid))
|
||||
}
|
||||
|
||||
func view(c *bm.Context) {
|
||||
var (
|
||||
aid int64
|
||||
err error
|
||||
)
|
||||
aidStr := c.Request.Form.Get("aid")
|
||||
if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid < 0 {
|
||||
c.JSON(nil, ecode.RequestErr)
|
||||
return
|
||||
}
|
||||
c.JSON(playSvr.View(c, aid))
|
||||
}
|
||||
|
||||
func matPage(c *bm.Context) {
|
||||
c.JSON(playSvr.Matsuri(c, time.Now()), nil)
|
||||
}
|
||||
|
||||
func showInfoc(ip string, now time.Time, buvid string, aid, mid int64) {
|
||||
if playInfoc == nil {
|
||||
return
|
||||
}
|
||||
info := &playerInfoc{
|
||||
ip: ip,
|
||||
ctime: strconv.FormatInt(now.Unix(), 10),
|
||||
api: _api,
|
||||
buvid: buvid,
|
||||
mid: strconv.FormatInt(mid, 10),
|
||||
client: _client,
|
||||
itemID: strconv.FormatInt(aid, 10),
|
||||
displayID: "",
|
||||
errCode: "",
|
||||
from: "",
|
||||
build: "",
|
||||
trackID: "",
|
||||
}
|
||||
err := playInfoc.Info(info.ip, info.ctime, info.api, info.buvid, info.mid, info.client, info.itemID, info.displayID, info.errCode, info.from, info.build, info.trackID)
|
||||
log.Info("playInfoc.Info(%s,%s,%s,%s,%s) error(%v)", info.ip, info.ctime, info.buvid, info.mid, info.itemID, err)
|
||||
}
|
43
app/interface/main/player/http/playurl.go
Normal file
43
app/interface/main/player/http/playurl.go
Normal file
@ -0,0 +1,43 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"go-common/app/interface/main/player/model"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
"go-common/library/net/http/blademaster"
|
||||
)
|
||||
|
||||
func playurl(c *blademaster.Context) {
|
||||
var (
|
||||
mid int64
|
||||
err error
|
||||
)
|
||||
arg := new(model.PlayurlArg)
|
||||
if err = c.Bind(arg); err != nil {
|
||||
return
|
||||
}
|
||||
if midInter, ok := c.Get("mid"); ok {
|
||||
mid = midInter.(int64)
|
||||
}
|
||||
if arg.OType == "" {
|
||||
arg.OType = model.OtypeXML
|
||||
}
|
||||
if arg.OType != model.OtypeXML && arg.OType != model.OtypeJSON {
|
||||
c.JSON(nil, ecode.RequestErr)
|
||||
return
|
||||
}
|
||||
if arg.Player != 0 && arg.Player != 1 {
|
||||
c.JSON(nil, ecode.RequestErr)
|
||||
return
|
||||
}
|
||||
if arg.Player == 1 && arg.OType == model.OtypeXML {
|
||||
log.Warn("playurl warn arg(%+v)", arg)
|
||||
c.JSON(nil, ecode.RequestErr)
|
||||
return
|
||||
}
|
||||
if arg.OType == model.OtypeJSON {
|
||||
c.JSON(playSvr.Playurl(c, mid, arg))
|
||||
} else {
|
||||
c.XML(playSvr.Playurl(c, mid, arg))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user