Fix #981 Use -webserverip to set http listen address (#1032)

* Fix #981 Use -webserverip to set http listen address

* use 0.0.0.0 as default http listen address

* add Admin REST API for setting http listen address

* full input validation of port and IP
This commit is contained in:
leuc 2021-05-25 01:13:49 +02:00 committed by GitHub
parent dd8bf54f66
commit 5ab901bb36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 6 deletions

View file

@ -21,6 +21,9 @@ var VersionNumber = StaticVersionNumber
// WebServerPort is the port for Owncast's webserver that is used for this execution of the service.
var WebServerPort = 8080
// Bind WebServer to this IP address. Be secure by default.
var WebServerIP = "0.0.0.0"
// InternalHLSListenerPort is the port for HLS writes that is used for this execution of the service.
var InternalHLSListenerPort = "8927"

View file

@ -14,6 +14,7 @@ type Defaults struct {
DatabaseFilePath string
WebServerPort int
WebServerIP string
RTMPServerPort int
StreamKey string
@ -46,6 +47,7 @@ func GetDefaults() Defaults {
YPServer: "https://directory.owncast.online",
WebServerPort: 8080,
WebServerIP: "0.0.0.0",
RTMPServerPort: 1935,
StreamKey: "abc123",

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"path/filepath"
"reflect"
@ -293,12 +294,48 @@ func SetWebServerPort(w http.ResponseWriter, r *http.Request) {
return
}
if err := data.SetHTTPPortNumber(configValue.Value.(float64)); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
if port, ok := configValue.Value.(float64); ok {
if (port < 1) || (port > 65535) {
controllers.WriteSimpleResponse(w, false, "Port number must be between 1 and 65535")
return
}
if err := data.SetHTTPPortNumber(port); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
} else {
controllers.WriteSimpleResponse(w, true, "HTTP port set")
return
}
}
controllers.WriteSimpleResponse(w, false, "Invalid type or value, port must be a number")
}
// SetWebServerIP will handle the web config request to set the server's HTTP listen address.
func SetWebServerIP(w http.ResponseWriter, r *http.Request) {
if !requirePOST(w, r) {
return
}
controllers.WriteSimpleResponse(w, true, "http port set")
configValue, success := getValueFromRequest(w, r)
if !success {
return
}
if input, ok := configValue.Value.(string); ok {
if ip := net.ParseIP(input); ip != nil {
if err := data.SetHTTPListenAddress(ip.String()); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error())
return
} else {
controllers.WriteSimpleResponse(w, true, "HTTP listen address set")
return
}
} else {
controllers.WriteSimpleResponse(w, false, "Invalid IP address")
return
}
}
controllers.WriteSimpleResponse(w, false, "Invalid type or value, IP address must be a string")
}
// SetRTMPServerPort will handle the web config request to set the inbound RTMP port.

View file

@ -46,6 +46,7 @@ func GetServerConfig(w http.ResponseWriter, r *http.Request) {
FFmpegPath: ffmpeg,
StreamKey: data.GetStreamKey(),
WebServerPort: config.WebServerPort,
WebServerIP: config.WebServerIP,
RTMPServerPort: data.GetRTMPPortNumber(),
ChatDisabled: data.GetChatDisabled(),
VideoSettings: videoSettings{
@ -75,6 +76,7 @@ type serverConfigAdminResponse struct {
FFmpegPath string `json:"ffmpegPath"`
StreamKey string `json:"streamKey"`
WebServerPort int `json:"webServerPort"`
WebServerIP string `json:"webServerIP"`
RTMPServerPort int `json:"rtmpServerPort"`
S3 models.S3 `json:"s3"`
VideoSettings videoSettings `json:"videoSettings"`

View file

@ -22,6 +22,7 @@ const serverWelcomeMessageKey = "server_welcome_message"
const serverNameKey = "server_name"
const serverURLKey = "server_url"
const httpPortNumberKey = "http_port_number"
const httpListenAddressKey = "http_listen_address"
const rtmpPortNumberKey = "rtmp_port_number"
const serverMetadataTagsKey = "server_metadata_tags"
const directoryEnabledKey = "directory_enabled"
@ -191,6 +192,21 @@ func SetHTTPPortNumber(port float64) error {
return _datastore.SetNumber(httpPortNumberKey, port)
}
// GetHTTPListenAddress will return the HTTP listen address.
func GetHTTPListenAddress() string {
address, err := _datastore.GetString(httpListenAddressKey)
if err != nil {
log.Traceln(httpListenAddressKey, err)
return config.GetDefaults().WebServerIP
}
return string(address)
}
// SetHTTPListenAddress will set the server HTTP listen address.
func SetHTTPListenAddress(address string) error {
return _datastore.SetString(httpListenAddressKey, address)
}
// GetRTMPPortNumber will return the server RTMP port.
func GetRTMPPortNumber() int {
port, err := _datastore.GetNumber(rtmpPortNumberKey)

View file

@ -41,6 +41,7 @@ func main() {
restoreDatabaseFile := flag.String("restoreDatabase", "", "Restore an Owncast database backup")
newStreamKey := flag.String("streamkey", "", "Set your stream key/admin password")
webServerPortOverride := flag.String("webserverport", "", "Force the web server to listen on a specific port")
webServerIPOverride := flag.String("webserverip", "", "Force web server to listen on this IP address")
rtmpPortOverride := flag.Int("rtmpport", 0, "Set listen port for the RTMP server")
flag.Parse()
@ -117,9 +118,15 @@ func main() {
log.Println("Saving new web server port number to", portNumber)
data.SetHTTPPortNumber(float64(portNumber))
}
config.WebServerPort = data.GetHTTPPortNumber()
// Set the web server ip
if *webServerIPOverride != "" {
log.Println("Saving new web server listen IP address to", *webServerIPOverride)
data.SetHTTPListenAddress(string(*webServerIPOverride))
}
config.WebServerIP = data.GetHTTPListenAddress()
// Set the rtmp server port
if *rtmpPortOverride > 0 {
log.Println("Saving new RTMP server port number to", *rtmpPortOverride)

View file

@ -2,6 +2,7 @@ package router
import (
"fmt"
"net"
"net/http"
log "github.com/sirupsen/logrus"
@ -172,6 +173,9 @@ func Start() error {
// Server http port
http.HandleFunc("/api/admin/config/webserverport", middleware.RequireAdminAuth(admin.SetWebServerPort))
// Server http listen address
http.HandleFunc("/api/admin/config/webserverip", middleware.RequireAdminAuth(admin.SetWebServerIP))
// Server rtmp port
http.HandleFunc("/api/admin/config/rtmpserverport", middleware.RequireAdminAuth(admin.SetRTMPServerPort))
@ -206,9 +210,14 @@ func Start() error {
http.HandleFunc("/api/admin/config/customstyles", middleware.RequireAdminAuth(admin.SetCustomStyles))
port := config.WebServerPort
ip := config.WebServerIP
log.Infof("Web server is listening on port %d.", port)
ip_addr := net.ParseIP(ip)
if ip_addr == nil {
log.Fatalln("Invalid IP address", ip)
}
log.Infof("Web server is listening on IP %s port %d.", ip_addr.String(), port)
log.Infoln("The web admin interface is available at /admin.")
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
return http.ListenAndServe(fmt.Sprintf("%s:%d", ip_addr.String(), port), nil)
}