diff --git a/README.md b/README.md index e8f0df7..8194a6d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,12 @@ HDL ## 配置 ```toml [HDL] -ListenAddr = "2020" +ListenAddr = ":2020" +ListenAddrTLS = ":2021" +CertFile = "file.cert" +KeyFile = "file.key" ``` -用于监听http端口 \ No newline at end of file +用于监听http和https端口 + +- 如果不设置ListenAddr和ListenAddrTLS,将公用网关的端口监听 \ No newline at end of file diff --git a/go.mod b/go.mod index d2c9607..88b274c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,10 @@ module github.com/Monibuca/plugin-hdl go 1.13 require ( - github.com/Monibuca/engine/v2 v2.1.1 - github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 + github.com/Monibuca/engine/v2 v2.2.5 + github.com/logrusorgru/aurora v2.0.3+incompatible + github.com/mattn/go-colorable v0.1.8 // indirect + github.com/shirou/gopsutil v3.20.12+incompatible // indirect github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8 + golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 // indirect ) diff --git a/go.sum b/go.sum index 6a01c01..003d94b 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/Monibuca/engine/v2 v2.0.1 h1:z27YwXVSelYgGfbrx5RmJ+ROveIf5QWGnwZqfOdq github.com/Monibuca/engine/v2 v2.0.1/go.mod h1:34EYjjV15G6myuHOKaJkO7y5tJ1Arq/NfC9Weacr2mc= github.com/Monibuca/engine/v2 v2.1.1 h1:InltEcEh0osSqGBsLaPZlpY1LUwFyI4rc2P4PmW40Ho= github.com/Monibuca/engine/v2 v2.1.1/go.mod h1:34EYjjV15G6myuHOKaJkO7y5tJ1Arq/NfC9Weacr2mc= +github.com/Monibuca/engine/v2 v2.2.5 h1:/w0BrvdTy4cqLD2uaIRaqBwdnu+/VDk+r3sjFbpbc1E= +github.com/Monibuca/engine/v2 v2.2.5/go.mod h1:34EYjjV15G6myuHOKaJkO7y5tJ1Arq/NfC9Weacr2mc= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= @@ -16,8 +18,12 @@ github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI= github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 h1:bqDmpDG49ZRnB5PcgP0RXtQvnMSgIF14M7CBd2shtXs= github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -26,6 +32,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shirou/gopsutil v2.20.1+incompatible h1:oIq9Cq4i84Hk8uQAUOG3eNdI/29hBawGrD5YRl6JRDY= github.com/shirou/gopsutil v2.20.1+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/shirou/gopsutil v3.20.12+incompatible h1:6VEGkOXP/eP4o2Ilk8cSsX0PhOEfX6leqAnD+urrp9M= +github.com/shirou/gopsutil v3.20.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= @@ -34,6 +42,8 @@ github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8/go.mod h1:RZd/Iq golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 h1:wHn06sgWHMO1VsQ8F+KzDJx/JzqfsNLnc+oEi07qD7s= +golang.org/x/sys v0.0.0-20210108172913-0df2131ae363/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/main.go b/main.go index b3617b3..ec54e8c 100644 --- a/main.go +++ b/main.go @@ -3,30 +3,41 @@ package hdl import ( "bytes" "encoding/binary" - "log" "net/http" - "strings" + "regexp" . "github.com/Monibuca/engine/v2" "github.com/Monibuca/engine/v2/avformat" + "github.com/Monibuca/utils" . "github.com/logrusorgru/aurora" - "github.com/zhangpeihao/goamf" + amf "github.com/zhangpeihao/goamf" ) -var config = new(ListenerConfig) +var config struct { + ListenAddr string + ListenAddrTLS string + CertFile string + KeyFile string +} +var streamPathReg = regexp.MustCompile("/(hdl/)?((.+)(\\.flv)|(.+))") func init() { InstallPlugin(&PluginConfig{ Name: "HDL", Type: PLUGIN_SUBSCRIBER, - Config: config, + Config: &config, Run: run, }) } func run() { - Print(Green("HDL start at "), BrightBlue(config.ListenAddr)) - log.Fatal(http.ListenAndServe(config.ListenAddr, http.HandlerFunc(HDLHandler))) + if config.ListenAddr != "" || config.ListenAddrTLS != "" { + Print(Green("HDL start at "), BrightBlue(config.ListenAddr), BrightBlue(config.ListenAddrTLS)) + utils.ListenAddrs(config.ListenAddr, config.ListenAddrTLS, config.CertFile, config.KeyFile, http.HandlerFunc(HDLHandler)) + } else { + Print(Green("HDL start reuse gateway port")) + http.HandleFunc("/hdl/", HDLHandler) + } } func HDLHandler(w http.ResponseWriter, r *http.Request) { @@ -35,9 +46,10 @@ func HDLHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(403) return } - stringPath := strings.TrimLeft(r.RequestURI, "/") - if strings.HasSuffix(stringPath, ".flv") { - stringPath = strings.TrimRight(stringPath, ".flv") + parts := streamPathReg.FindStringSubmatch(r.RequestURI) + stringPath := parts[3] + if stringPath == "" { + stringPath = parts[5] } //atomic.AddInt32(&hdlId, 1) w.Header().Set("Access-Control-Allow-Origin", "*") diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..6f741b9 --- /dev/null +++ b/main_test.go @@ -0,0 +1,23 @@ +package hdl + +import ( + "testing" +) + +func TestHDLHandler(t *testing.T) { + tests := map[string]string{ + "/hdl/abc.flv": "abc", "/hdl/abc": "abc", "/abc": "abc", "/abc.flv": "abc", + } + for name, result := range tests { + t.Run(name, func(t *testing.T) { + parts := streamPathReg.FindStringSubmatch(name) + stringPath := parts[3] + if stringPath == "" { + stringPath = parts[5] + } + if stringPath != result { + t.Fail() + } + }) + } +}