Skip to content

Commit

Permalink
new func MockVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed Jun 17, 2024
1 parent d7b0aaf commit 455a9b2
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 99 deletions.
9 changes: 5 additions & 4 deletions pkg/base/t_http_an__api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const (
DespSucc = "succ"

ErrorCodePageNotFound = 404
DespPageNotFound = "page not found, check this document out: https://pengrl.com/lal/#/HTTPAPI"

ErrorCodeGroupNotFound = 1001
DespGroupNotFound = "group not found"
Expand All @@ -76,9 +75,11 @@ type ApiRespBasic struct {
Desp string `json:"desp"`
}

var ApiNotFoundResp = ApiRespBasic{
ErrorCode: ErrorCodePageNotFound,
Desp: DespPageNotFound,
func ApiNotFoundRespFn() ApiRespBasic {
return ApiRespBasic{
ErrorCode: ErrorCodePageNotFound,
Desp: DespPageNotFound,
}
}

type ApiStatLalInfoResp struct {
Expand Down
127 changes: 115 additions & 12 deletions pkg/base/t_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,50 @@

package base

import "strings"
import (
"fmt"
"path/filepath"
"strings"
)

// 版本信息相关
// lal的一部分版本信息使用了naza.bininfo
// 另外,我们也在本文件提供另外一些信息
// 并且将这些信息打入可执行文件、日志、各协议中的标准版本字段中

// LalVersion 整个lal工程的版本号。注意,该变量由外部脚本修改维护,不要手动在代码中修改
const LalVersion = "v0.37.4"
var LalVersion = "v0.37.4"

// ConfVersion lalserver的配置文件的版本号
const ConfVersion = "v0.4.1"
var ConfVersion = "v0.4.1"

// HttpApiVersion lalserver的HTTP-API功能的版本号
const HttpApiVersion = "v0.4.8"
var HttpApiVersion = "v0.4.8"

// HttpNotifyVersion lalserver的HTTP-Notify功能的版本号
const HttpNotifyVersion = "v0.2.4"
var HttpNotifyVersion = "v0.2.4"

const HttpWebUiVersion = "v0.0.3"
var HttpWebUiVersion = "v0.0.3"

var (
LalLibraryName = "lal"
LalGithubRepo = "github.com/q191201771/lal"
LalGithubSite = "https://github.com/q191201771/lal"
LalDocSite = "https://pengrl.com/lal"
LalLibraryName = "lal"
LalGithubRepo = "github.com/q191201771/lal"
LalGithubSite = "https://github.com/q191201771/lal"
LalDocSite = "https://pengrl.com/lal"
LalDocSiteHTTPAPI = "https://pengrl.com/lal/#/HTTPAPI"

LalDefaultConfigFilename = "lalserver.conf.json"

LalLogo = `
__ ___ __
/ / / | / /
/ / / /| | / /
/ /___/ ___ |/ /___
/_____/_/ |_/_____/
`

// LalFullInfo e.g. lal v0.12.3 (github.com/q191201771/lal)
LalFullInfo = LalLibraryName + " " + LalVersion + " (" + LalGithubRepo + ")"
LalFullInfo string

// LalVersionDot e.g. 0.12.3
LalVersionDot string
Expand Down Expand Up @@ -90,6 +105,21 @@ var (

// LalRtspRealm e.g. lal
LalRtspRealm string

LalRtmpRandom1528Buf []byte

LalFlvHttpResponseHeader []byte

LalTsHttpResponseHeader []byte

// LalRtspResponseOptionsTmpl CSeq
// rfc2326 10.1 OPTIONS
LalRtspResponseOptionsTmpl string

// LalDefaultConfFilenameList 没有指定配置文件时,按顺序作为优先级,找到第一个存在的并使用
LalDefaultConfFilenameList []string

DespPageNotFound string
)

// - rtmp handshake random buf
Expand Down Expand Up @@ -122,7 +152,30 @@ var (
// - http api
// - `server:`

func init() {
func MockVersion(
lalVersion, confVersion, httpApiVersion, httpNotifyVersion, httpWebUiVersion,
lalLibraryName, lalGithubRepo, lalGithubSite, lalDocSite, lalDocSiteHTTPAPI,
lalDefaultConfigFilename, lalLogo string) {
LalVersion = lalVersion
ConfVersion = confVersion
HttpApiVersion = httpApiVersion
HttpNotifyVersion = httpNotifyVersion
HttpWebUiVersion = httpWebUiVersion

LalLibraryName = lalLibraryName
LalGithubRepo = lalGithubRepo
LalGithubSite = lalGithubSite
LalDocSite = lalDocSite
LalDocSiteHTTPAPI = lalDocSiteHTTPAPI

LalDefaultConfigFilename = lalDefaultConfigFilename
LalLogo = lalLogo

initial()
}

func initial() {
LalFullInfo = LalLibraryName + " " + LalVersion + " (" + LalGithubRepo + ")"
LalVersionDot = strings.TrimPrefix(LalVersion, "v")
LalVersionComma = strings.Replace(LalVersionDot, ".", ",", -1)

Expand All @@ -145,4 +198,54 @@ func init() {
LalPackSdp = LalLibraryName + " " + LalVersionDot

LalRtspRealm = LalLibraryName

DespPageNotFound = "page not found, check this document out: " + LalDocSiteHTTPAPI

LalRtmpRandom1528Buf = make([]byte, 1528)
hack := []byte(fmt.Sprintf("random buf of rtmp handshake gen by %s", LalRtmpHandshakeWaterMark))
for i := 0; i < 1528; i += len(hack) {
copy(LalRtmpRandom1528Buf[i:], hack)
}

LalFlvHttpResponseHeader = []byte("HTTP/1.1 200 OK\r\n" +
"Server: " + LalHttpflvSubSessionServer + "\r\n" +
"Cache-Control: no-cache\r\n" +
"Content-Type: video/x-flv\r\n" +
"Connection: close\r\n" +
"Expires: -1\r\n" +
"Pragma: no-cache\r\n" +
CorsHeaders +
"\r\n")

LalTsHttpResponseHeader = []byte("HTTP/1.1 200 OK\r\n" +
"Server: " + LalHttptsSubSessionServer + "\r\n" +
"Cache-Control: no-cache\r\n" +
"Content-Type: video/mp2t\r\n" +
"Connection: close\r\n" +
"Expires: -1\r\n" +
"Pragma: no-cache\r\n" +
CorsHeaders +
"\r\n")

LalRtspResponseOptionsTmpl = "RTSP/1.0 200 OK\r\n" +
"Server: " + LalRtspOptionsResponseServer + "\r\n" +
"CSeq: %s\r\n" +
"Public: DESCRIBE, ANNOUNCE, SETUP, PLAY, PAUSE, RECORD, TEARDOWN\r\n" +
"\r\n"

LalDefaultConfFilenameList = []string{
filepath.FromSlash(LalDefaultConfigFilename),
filepath.FromSlash("./conf/" + LalDefaultConfigFilename),
filepath.FromSlash("../" + LalDefaultConfigFilename),
filepath.FromSlash("../conf/" + LalDefaultConfigFilename),
filepath.FromSlash("../../" + LalDefaultConfigFilename),
filepath.FromSlash("../../conf/" + LalDefaultConfigFilename),
filepath.FromSlash("../../../" + LalDefaultConfigFilename),
filepath.FromSlash("../../../conf/" + LalDefaultConfigFilename),
filepath.FromSlash("lal/conf/" + LalDefaultConfigFilename),
}
}

func init() {
initial()
}
20 changes: 1 addition & 19 deletions pkg/httpflv/server_sub_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/q191201771/naza/pkg/connection"
)

var flvHttpResponseHeader []byte

type SubSession struct {
core *base.BasicHttpSubSession
IsFresh bool
Expand Down Expand Up @@ -62,7 +60,7 @@ func (session *SubSession) Dispose() error {

func (session *SubSession) WriteHttpResponseHeader() {
Log.Debugf("[%s] > W http response header.", session.core.UniqueKey())
session.core.WriteHttpResponseHeader(flvHttpResponseHeader)
session.core.WriteHttpResponseHeader(base.LalFlvHttpResponseHeader)
}

func (session *SubSession) WriteFlvHeader() {
Expand Down Expand Up @@ -119,19 +117,3 @@ func (session *SubSession) GetStat() base.StatSession {
func (session *SubSession) IsAlive() (readAlive, writeAlive bool) {
return session.core.IsAlive()
}

// ---------------------------------------------------------------------------------------------------------------------

func init() {
flvHttpResponseHeaderStr := "HTTP/1.1 200 OK\r\n" +
"Server: " + base.LalHttpflvSubSessionServer + "\r\n" +
"Cache-Control: no-cache\r\n" +
"Content-Type: video/x-flv\r\n" +
"Connection: close\r\n" +
"Expires: -1\r\n" +
"Pragma: no-cache\r\n" +
base.CorsHeaders +
"\r\n"

flvHttpResponseHeader = []byte(flvHttpResponseHeaderStr)
}
18 changes: 1 addition & 17 deletions pkg/httpts/server_sub_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/q191201771/naza/pkg/connection"
)

var tsHttpResponseHeader []byte

type SubSession struct {
core *base.BasicHttpSubSession
IsFresh bool
Expand Down Expand Up @@ -60,7 +58,7 @@ func (session *SubSession) Dispose() error {

func (session *SubSession) WriteHttpResponseHeader() {
Log.Debugf("[%s] > W http response header.", session.core.UniqueKey())
session.core.WriteHttpResponseHeader(tsHttpResponseHeader)
session.core.WriteHttpResponseHeader(base.LalTsHttpResponseHeader)
}

func (session *SubSession) Write(b []byte) {
Expand Down Expand Up @@ -110,17 +108,3 @@ func (session *SubSession) GetStat() base.StatSession {
func (session *SubSession) IsAlive() (readAlive, writeAlive bool) {
return session.core.IsAlive()
}

func init() {
tsHttpResponseHeaderStr := "HTTP/1.1 200 OK\r\n" +
"Server: " + base.LalHttptsSubSessionServer + "\r\n" +
"Cache-Control: no-cache\r\n" +
"Content-Type: video/mp2t\r\n" +
"Connection: close\r\n" +
"Expires: -1\r\n" +
"Pragma: no-cache\r\n" +
base.CorsHeaders +
"\r\n"

tsHttpResponseHeader = []byte(tsHttpResponseHeaderStr)
}
8 changes: 1 addition & 7 deletions pkg/logic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,7 @@ func LoadConfAndInitLog(rawContent []byte) *Config {
Log.Info("initial log succ.")

// 打印Logo
Log.Info(`
__ ___ __
/ / / | / /
/ / / /| | / /
/ /___/ ___ |/ /___
/_____/_/ |_/_____/
`)
Log.Info(base.LalLogo)

// 检查配置版本号是否匹配
if config.ConfVersion != base.ConfVersion {
Expand Down
2 changes: 1 addition & 1 deletion pkg/logic/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (h *HttpApiServer) webUIHandler(w http.ResponseWriter, req *http.Request) {
func (h *HttpApiServer) notFoundHandler(w http.ResponseWriter, req *http.Request) {
Log.Warnf("invalid http-api request. uri=%s, raddr=%s", req.RequestURI, req.RemoteAddr)
//w.WriteHeader(http.StatusNotFound)
feedback(base.ApiNotFoundResp, w)
feedback(base.ApiNotFoundRespFn(), w)
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down
15 changes: 0 additions & 15 deletions pkg/logic/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
package logic

import (
"path/filepath"

"github.com/q191201771/lal/pkg/base"
)

Expand Down Expand Up @@ -147,19 +145,6 @@ var defaultOption = Option{

type ModOption func(option *Option)

// DefaultConfFilenameList 没有指定配置文件时,按顺序作为优先级,找到第一个存在的并使用
var DefaultConfFilenameList = []string{
filepath.FromSlash("lalserver.conf.json"),
filepath.FromSlash("./conf/lalserver.conf.json"),
filepath.FromSlash("../lalserver.conf.json"),
filepath.FromSlash("../conf/lalserver.conf.json"),
filepath.FromSlash("../../lalserver.conf.json"),
filepath.FromSlash("../../conf/lalserver.conf.json"),
filepath.FromSlash("../../../lalserver.conf.json"),
filepath.FromSlash("../../../conf/lalserver.conf.json"),
filepath.FromSlash("lal/conf/lalserver.conf.json"),
}

// ---------------------------------------------------------------------------------------------------------------------

// TODO(chef): [doc] 整理散乱在源码文件中的以注释形式存在的文档 202302
Expand Down
4 changes: 2 additions & 2 deletions pkg/logic/server_manager__.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func NewServerManager(modOption ...ModOption) *ServerManager {

rawContent := sm.option.ConfRawContent
if len(rawContent) == 0 {
rawContent = base.WrapReadConfigFile(sm.option.ConfFilename, DefaultConfFilenameList, func() {
rawContent = base.WrapReadConfigFile(sm.option.ConfFilename, base.LalDefaultConfFilenameList, func() {
_, _ = fmt.Fprintf(os.Stderr, `
Example:
%s -c %s
Github: %s
Doc: %s
`, os.Args[0], filepath.FromSlash("./conf/lalserver.conf.json"), base.LalGithubSite, base.LalDocSite)
`, os.Args[0], filepath.FromSlash("./conf/"+base.LalDefaultConfigFilename), base.LalGithubSite, base.LalDocSite)
})
}
sm.config = LoadConfAndInitLog(rawContent)
Expand Down
11 changes: 1 addition & 10 deletions pkg/rtmp/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"fmt"
"io"
"time"

Expand Down Expand Up @@ -290,13 +289,5 @@ func makeDigest(b []byte, key []byte) []byte {
}

func random1528(out []byte) {
copy(out, random1528Buf)
}

func init() {
random1528Buf = make([]byte, 1528)
hack := []byte(fmt.Sprintf("random buf of rtmp handshake gen by %s", base.LalRtmpHandshakeWaterMark))
for i := 0; i < 1528; i += len(hack) {
copy(random1528Buf[i:], hack)
}
copy(out, base.LalRtmpRandom1528Buf)
}
11 changes: 1 addition & 10 deletions pkg/rtsp/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ import (
"github.com/q191201771/lal/pkg/base"
)

// rfc2326 10.1 OPTIONS

// ResponseOptionsTmpl CSeq
var ResponseOptionsTmpl = "RTSP/1.0 200 OK\r\n" +
"Server: " + base.LalRtspOptionsResponseServer + "\r\n" +
"CSeq: %s\r\n" +
"Public: DESCRIBE, ANNOUNCE, SETUP, PLAY, PAUSE, RECORD, TEARDOWN\r\n" +
"\r\n"

// rfc2326 10.3 ANNOUNCE
//var RequestAnnounceTmpl = "not impl"

Expand Down Expand Up @@ -84,7 +75,7 @@ var ResponseAuthorizedTmpl = "RTSP/1.0 401 Unauthorized\r\n" +
"\r\n"

func PackResponseOptions(cseq string) string {
return fmt.Sprintf(ResponseOptionsTmpl, cseq)
return fmt.Sprintf(base.LalRtspResponseOptionsTmpl, cseq)
}

func PackResponseAnnounce(cseq string) string {
Expand Down
Loading

0 comments on commit 455a9b2

Please sign in to comment.