增加可共用网关端口

This commit is contained in:
langhuihui
2021-01-09 18:26:21 +08:00
parent 84b230a050
commit ad29c0ada4
3 changed files with 21 additions and 10 deletions

View File

@@ -18,3 +18,5 @@ CertFile = "../foo.cert"
KeyFile = "../foo.key" KeyFile = "../foo.key"
ListenAddrTLS = ":8088" ListenAddrTLS = ":8088"
``` ```
- 如果不设置ListenAddr和ListenAddrTLS将共用网关的端口监听

13
main.go
View File

@@ -13,12 +13,12 @@ import (
. "github.com/logrusorgru/aurora" . "github.com/logrusorgru/aurora"
) )
var config = &struct { var config struct {
ListenAddr string ListenAddr string
CertFile string CertFile string
KeyFile string KeyFile string
ListenAddrTLS string ListenAddrTLS string
}{} }
var publicPath string var publicPath string
@@ -26,16 +26,21 @@ func init() {
plugin := &PluginConfig{ plugin := &PluginConfig{
Name: "Jessica", Name: "Jessica",
Type: PLUGIN_SUBSCRIBER, Type: PLUGIN_SUBSCRIBER,
Config: config, Config: &config,
Run: run, Run: run,
} }
InstallPlugin(plugin) InstallPlugin(plugin)
publicPath = filepath.Join(plugin.Dir, "ui", "public") publicPath = filepath.Join(plugin.Dir, "ui", "public")
} }
func run() { func run() {
Print(Green("server Jessica start at"), BrightBlue(config.ListenAddr))
http.HandleFunc("/jessibuca/", jessibuca) http.HandleFunc("/jessibuca/", jessibuca)
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)) 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) { func jessibuca(w http.ResponseWriter, r *http.Request) {
filePath := strings.TrimPrefix(r.URL.Path, "/jessibuca") filePath := strings.TrimPrefix(r.URL.Path, "/jessibuca")

View File

@@ -3,7 +3,7 @@ package jessica
import ( import (
"encoding/binary" "encoding/binary"
"net/http" "net/http"
"strings" "regexp"
. "github.com/Monibuca/engine/v2" . "github.com/Monibuca/engine/v2"
"github.com/Monibuca/engine/v2/avformat" "github.com/Monibuca/engine/v2/avformat"
@@ -11,6 +11,8 @@ import (
"github.com/gobwas/ws" "github.com/gobwas/ws"
) )
var streamPathReg = regexp.MustCompile("/(jessica/)?((.+)(\\.flv)|(.+))")
func WsHandler(w http.ResponseWriter, r *http.Request) { func WsHandler(w http.ResponseWriter, r *http.Request) {
sign := r.URL.Query().Get("sign") sign := r.URL.Query().Get("sign")
isFlv := false isFlv := false
@@ -18,9 +20,11 @@ func WsHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(403) w.WriteHeader(403)
return return
} }
streamPath := strings.TrimLeft(r.RequestURI, "/") parts := streamPathReg.FindStringSubmatch(r.RequestURI)
if strings.HasSuffix(streamPath, ".flv") { stringPath := parts[3]
streamPath = strings.TrimRight(streamPath, ".flv") if stringPath == "" {
stringPath = parts[5]
} else {
isFlv = true isFlv = true
} }
conn, _, _, err := ws.UpgradeHTTP(r, w) conn, _, _, err := ws.UpgradeHTTP(r, w)