增加可共用网关端口

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"
ListenAddrTLS = ":8088"
```
- 如果不设置ListenAddr和ListenAddrTLS将共用网关的端口监听

13
main.go
View File

@@ -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)
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")

View File

@@ -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)