diff --git a/README.md b/README.md index c959ef4..525a00b 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,6 @@ ListenAddr = ":8080" CertFile = "../foo.cert" KeyFile = "../foo.key" ListenAddrTLS = ":8088" -``` \ No newline at end of file +``` + +- 如果不设置ListenAddr和ListenAddrTLS,将共用网关的端口监听 \ No newline at end of file diff --git a/main.go b/main.go index a4f7878..a0a6d4c 100644 --- a/main.go +++ b/main.go @@ -13,12 +13,12 @@ import ( . "github.com/logrusorgru/aurora" ) -var config = &struct { +var config struct { ListenAddr string CertFile string KeyFile string ListenAddrTLS string -}{} +} var publicPath string @@ -26,16 +26,21 @@ func init() { plugin := &PluginConfig{ Name: "Jessica", Type: PLUGIN_SUBSCRIBER, - Config: config, + Config: &config, Run: run, } InstallPlugin(plugin) publicPath = filepath.Join(plugin.Dir, "ui", "public") } func run() { - Print(Green("server Jessica start at"), BrightBlue(config.ListenAddr)) http.HandleFunc("/jessibuca/", jessibuca) - utils.ListenAddrs(config.ListenAddr, config.ListenAddrTLS, config.CertFile, config.KeyFile, http.HandlerFunc(WsHandler)) + if config.ListenAddr != "" || config.ListenAddrTLS != "" { + Print(Green("Jessica start at"), BrightBlue(config.ListenAddr), BrightBlue(config.ListenAddrTLS)) + utils.ListenAddrs(config.ListenAddr, config.ListenAddrTLS, config.CertFile, config.KeyFile, http.HandlerFunc(WsHandler)) + } else { + Print(Green("Jessica start reuse gateway port")) + http.HandleFunc("/jessica/", WsHandler) + } } func jessibuca(w http.ResponseWriter, r *http.Request) { filePath := strings.TrimPrefix(r.URL.Path, "/jessibuca") diff --git a/subscriber.go b/subscriber.go index 7c19b96..bde86d5 100644 --- a/subscriber.go +++ b/subscriber.go @@ -3,7 +3,7 @@ package jessica import ( "encoding/binary" "net/http" - "strings" + "regexp" . "github.com/Monibuca/engine/v2" "github.com/Monibuca/engine/v2/avformat" @@ -11,6 +11,8 @@ import ( "github.com/gobwas/ws" ) +var streamPathReg = regexp.MustCompile("/(jessica/)?((.+)(\\.flv)|(.+))") + func WsHandler(w http.ResponseWriter, r *http.Request) { sign := r.URL.Query().Get("sign") isFlv := false @@ -18,9 +20,11 @@ func WsHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(403) return } - streamPath := strings.TrimLeft(r.RequestURI, "/") - if strings.HasSuffix(streamPath, ".flv") { - streamPath = strings.TrimRight(streamPath, ".flv") + parts := streamPathReg.FindStringSubmatch(r.RequestURI) + stringPath := parts[3] + if stringPath == "" { + stringPath = parts[5] + } else { isFlv = true } conn, _, _, err := ws.UpgradeHTTP(r, w)