diff --git a/pkg/base/t_http_an__api.go b/pkg/base/t_http_an__api.go index 741c19d..f70a1ec 100644 --- a/pkg/base/t_http_an__api.go +++ b/pkg/base/t_http_an__api.go @@ -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" @@ -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 { diff --git a/pkg/base/t_version.go b/pkg/base/t_version.go index bffba43..b27b631 100644 --- a/pkg/base/t_version.go +++ b/pkg/base/t_version.go @@ -8,7 +8,11 @@ package base -import "strings" +import ( + "fmt" + "path/filepath" + "strings" +) // 版本信息相关 // lal的一部分版本信息使用了naza.bininfo @@ -16,27 +20,38 @@ import "strings" // 并且将这些信息打入可执行文件、日志、各协议中的标准版本字段中 // 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 @@ -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 @@ -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) @@ -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() } diff --git a/pkg/httpflv/server_sub_session.go b/pkg/httpflv/server_sub_session.go index 5ddf44e..ec98772 100644 --- a/pkg/httpflv/server_sub_session.go +++ b/pkg/httpflv/server_sub_session.go @@ -16,8 +16,6 @@ import ( "github.com/q191201771/naza/pkg/connection" ) -var flvHttpResponseHeader []byte - type SubSession struct { core *base.BasicHttpSubSession IsFresh bool @@ -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() { @@ -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) -} diff --git a/pkg/httpts/server_sub_session.go b/pkg/httpts/server_sub_session.go index 672ca21..526741a 100644 --- a/pkg/httpts/server_sub_session.go +++ b/pkg/httpts/server_sub_session.go @@ -15,8 +15,6 @@ import ( "github.com/q191201771/naza/pkg/connection" ) -var tsHttpResponseHeader []byte - type SubSession struct { core *base.BasicHttpSubSession IsFresh bool @@ -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) { @@ -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) -} diff --git a/pkg/logic/config.go b/pkg/logic/config.go index 67b5f09..cffbfaf 100644 --- a/pkg/logic/config.go +++ b/pkg/logic/config.go @@ -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 { diff --git a/pkg/logic/http_api.go b/pkg/logic/http_api.go index 87dccba..0f4fad3 100644 --- a/pkg/logic/http_api.go +++ b/pkg/logic/http_api.go @@ -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) } // --------------------------------------------------------------------------------------------------------------------- diff --git a/pkg/logic/logic.go b/pkg/logic/logic.go index c0dfc93..cfd1b7e 100644 --- a/pkg/logic/logic.go +++ b/pkg/logic/logic.go @@ -9,8 +9,6 @@ package logic import ( - "path/filepath" - "github.com/q191201771/lal/pkg/base" ) @@ -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 diff --git a/pkg/logic/server_manager__.go b/pkg/logic/server_manager__.go index 4783f17..bdb2764 100644 --- a/pkg/logic/server_manager__.go +++ b/pkg/logic/server_manager__.go @@ -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) diff --git a/pkg/rtmp/handshake.go b/pkg/rtmp/handshake.go index 8835d93..d505878 100644 --- a/pkg/rtmp/handshake.go +++ b/pkg/rtmp/handshake.go @@ -12,7 +12,6 @@ import ( "bytes" "crypto/hmac" "crypto/sha256" - "fmt" "io" "time" @@ -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) } diff --git a/pkg/rtsp/pack.go b/pkg/rtsp/pack.go index f66cd42..fa1f835 100644 --- a/pkg/rtsp/pack.go +++ b/pkg/rtsp/pack.go @@ -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" @@ -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 { diff --git a/script/gen_tag.sh b/script/gen_tag.sh index 0c48144..eb30134 100755 --- a/script/gen_tag.sh +++ b/script/gen_tag.sh @@ -36,7 +36,7 @@ GitTag=`git tag --sort=version:refname | tail -n 1` echo "newest version in git tag: " $GitTag # 源码中的版本号 -FileVersion=`cat ${ROOT_DIR}/pkg/base/t_version.go | grep 'const LalVersion' | awk -F\" '{print $2}'` +FileVersion=`cat ${ROOT_DIR}/pkg/base/t_version.go | grep 'var LalVersion' | awk -F\" '{print $2}'` echo "newest version in t_version.go: " $FileVersion # CHANGELOG.md和源码中的不一致,更新源码,并提交修改 @@ -44,7 +44,7 @@ if [ "$NewVersion" == "$FileVersion" ];then echo 'same tag, noop.' else echo 'update t_version.go' - gsed -i "/^const LalVersion/cconst LalVersion = \"${NewVersion}\"" ${ROOT_DIR}/pkg/base/t_version.go + gsed -i "/^var LalVersion/cvar LalVersion = \"${NewVersion}\"" ${ROOT_DIR}/pkg/base/t_version.go git add ${ROOT_DIR}/pkg/base/t_version.go git commit -m "${NewVersion} -> t_version.go" git push