From 6ef144c3b97e197283a0b5f9019393cbfe93c4cc Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sun, 17 Oct 2021 20:01:42 +0200 Subject: [PATCH] Can specify custom IP & port access --- config.go | 7 +++++++ config.yaml | 7 +++++++ main.go | 8 ++++++++ relay.go | 34 +++++++++++++++++++--------------- ws.go | 6 +++--- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/config.go b/config.go index 32602f0..706dd38 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,13 @@ type Config struct { Path string Token string + // Optional information to inject proxy reachability (can be left empty) + AccessIP string + + // Optionaly restrict Ports range (can be left to 0) + PortStart int + PortEnd int + // Amount of time to wait before automatically //quitting the application when the websocket // connection was interrupted diff --git a/config.yaml b/config.yaml index fab1894..ed073b2 100644 --- a/config.yaml +++ b/config.yaml @@ -8,6 +8,13 @@ port: 3000 path: /rtc_proxy/ws token: SecretToken +# Set the IP this relay can be reached (optional) +# accessip: + +# Limit UDP ports this server will listen to +portstart: 0 +portend: 0 + # Number of seconds to wait before automatically # closing the proxy (if WebSocket connection was # interrupted) diff --git a/main.go b/main.go index b0faf52..b711415 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,14 @@ func main() { // First, load the config conf := loadConfig(os.Args[1]) + if len(conf.AccessIP) > 0 { + fmt.Printf("RTC reachable at %s\n", conf.AccessIP) + } + + if conf.PortStart > 0 && conf.PortEnd > conf.PortStart { + fmt.Printf("Will listen only on these ports: %d to %d\n", conf.PortStart, conf.PortEnd) + } + /// Avoid to quick restart loop defer timeBeforeQuit(&conf) diff --git a/relay.go b/relay.go index a530b23..0587ec5 100644 --- a/relay.go +++ b/relay.go @@ -52,7 +52,7 @@ var connections = make(map[string]*activeRelay) var currID uint = 0 /// Process incoming messages -func onSignal(callHash, peerID string, data map[string]interface{}) { +func onSignal(config *Config, callHash, peerID string, data map[string]interface{}) { // Close all the channels that requested so processCloseRequests() @@ -134,7 +134,7 @@ func onSignal(callHash, peerID string, data map[string]interface{}) { } currID++ connections[callHash] = &newRelay - go newCall(newSignal, &newRelay) + go newCall(config, newSignal, &newRelay) } else { // Forward the message to the channel @@ -143,15 +143,15 @@ func onSignal(callHash, peerID string, data map[string]interface{}) { } /// Request an offer for a client of a broadcast -func onRequestOffer(callHash, peerID string) { - onSignal(callHash, peerID, map[string]interface{}{ +func onRequestOffer(c *Config, callHash, peerID string) { + onSignal(c, callHash, peerID, map[string]interface{}{ "type": "REQUEST_OFFER", }) } /// Request connections to be closed -func onCloseConnection(callHash, peerID string) { - onSignal(callHash, peerID, map[string]interface{}{ +func onCloseConnection(c *Config, callHash, peerID string) { + onSignal(c, callHash, peerID, map[string]interface{}{ "type": "CLOSE_CONN", }) } @@ -191,7 +191,7 @@ func processCloseRequests() { } /// Start new call -func newCall(mainOffer receivedSignal, r *activeRelay) { +func newCall(config *Config, mainOffer receivedSignal, r *activeRelay) { // I am not sure this is a strong way to determine whether we are having // a video call or not, but I have not found any better way yet... @@ -220,16 +220,20 @@ func newCall(mainOffer receivedSignal, r *activeRelay) { s := webrtc.SettingEngine{} s.SetTrickle(false) // I did not manage to make the connection trickling, sorry... - // TODO : remove hardcoded ports - if err := s.SetEphemeralUDPPortRange(5000, 5001); err != nil { - log.Println("Error: faild to set ephemeral UDP Port range!", err) - askForClose(r) - return + // Optional : restrict ports + if config.PortStart > 0 && config.PortEnd > config.PortStart { + if err := s.SetEphemeralUDPPortRange(uint16(config.PortStart), uint16(config.PortEnd)); err != nil { + log.Println("Error: faild to set ephemeral UDP Port range!", err) + askForClose(r) + return + } } - // TODO : remove hardcoded ips - ips := []string{"127.0.0.1"} - s.SetNAT1To1IPs(ips, webrtc.ICECandidateTypeHost) + // Optional : set access IP + if len(config.AccessIP) > 0 { + ips := []string{config.AccessIP} + s.SetNAT1To1IPs(ips, webrtc.ICECandidateTypeHost) + } // Create the API object with the MediaEngine api := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), diff --git a/ws.go b/ws.go index fdc694f..8421a66 100644 --- a/ws.go +++ b/ws.go @@ -82,17 +82,17 @@ func openWs(conf *Config) { // Remote signal case "signal": - onSignal(msg.CallHash, msg.PeerID, msg.Data.(map[string]interface{})) + onSignal(conf, msg.CallHash, msg.PeerID, msg.Data.(map[string]interface{})) break // Request an offer case "request_offer": - onRequestOffer(msg.CallHash, msg.PeerID) + onRequestOffer(conf, msg.CallHash, msg.PeerID) break // Close a connection case "close_conn": - onCloseConnection(msg.CallHash, msg.PeerID) + onCloseConnection(conf, msg.CallHash, msg.PeerID) break default: