Can specify custom IP & port access

This commit is contained in:
Pierre HUBERT 2021-10-17 20:01:42 +02:00
parent 71b7f8d71c
commit 6ef144c3b9
5 changed files with 44 additions and 18 deletions

View File

@ -17,6 +17,13 @@ type Config struct {
Path string Path string
Token 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 // Amount of time to wait before automatically
//quitting the application when the websocket //quitting the application when the websocket
// connection was interrupted // connection was interrupted

View File

@ -8,6 +8,13 @@ port: 3000
path: /rtc_proxy/ws path: /rtc_proxy/ws
token: SecretToken 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 # Number of seconds to wait before automatically
# closing the proxy (if WebSocket connection was # closing the proxy (if WebSocket connection was
# interrupted) # interrupted)

View File

@ -19,6 +19,14 @@ func main() {
// First, load the config // First, load the config
conf := loadConfig(os.Args[1]) 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 /// Avoid to quick restart loop
defer timeBeforeQuit(&conf) defer timeBeforeQuit(&conf)

View File

@ -52,7 +52,7 @@ var connections = make(map[string]*activeRelay)
var currID uint = 0 var currID uint = 0
/// Process incoming messages /// 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 // Close all the channels that requested so
processCloseRequests() processCloseRequests()
@ -134,7 +134,7 @@ func onSignal(callHash, peerID string, data map[string]interface{}) {
} }
currID++ currID++
connections[callHash] = &newRelay connections[callHash] = &newRelay
go newCall(newSignal, &newRelay) go newCall(config, newSignal, &newRelay)
} else { } else {
// Forward the message to the channel // 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 /// Request an offer for a client of a broadcast
func onRequestOffer(callHash, peerID string) { func onRequestOffer(c *Config, callHash, peerID string) {
onSignal(callHash, peerID, map[string]interface{}{ onSignal(c, callHash, peerID, map[string]interface{}{
"type": "REQUEST_OFFER", "type": "REQUEST_OFFER",
}) })
} }
/// Request connections to be closed /// Request connections to be closed
func onCloseConnection(callHash, peerID string) { func onCloseConnection(c *Config, callHash, peerID string) {
onSignal(callHash, peerID, map[string]interface{}{ onSignal(c, callHash, peerID, map[string]interface{}{
"type": "CLOSE_CONN", "type": "CLOSE_CONN",
}) })
} }
@ -191,7 +191,7 @@ func processCloseRequests() {
} }
/// Start new call /// 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 // 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... // 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 := webrtc.SettingEngine{}
s.SetTrickle(false) // I did not manage to make the connection trickling, sorry... s.SetTrickle(false) // I did not manage to make the connection trickling, sorry...
// TODO : remove hardcoded ports // Optional : restrict ports
if err := s.SetEphemeralUDPPortRange(5000, 5001); err != nil { 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) log.Println("Error: faild to set ephemeral UDP Port range!", err)
askForClose(r) askForClose(r)
return return
} }
}
// TODO : remove hardcoded ips // Optional : set access IP
ips := []string{"127.0.0.1"} if len(config.AccessIP) > 0 {
ips := []string{config.AccessIP}
s.SetNAT1To1IPs(ips, webrtc.ICECandidateTypeHost) s.SetNAT1To1IPs(ips, webrtc.ICECandidateTypeHost)
}
// Create the API object with the MediaEngine // Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), api := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine),

6
ws.go
View File

@ -82,17 +82,17 @@ func openWs(conf *Config) {
// Remote signal // Remote signal
case "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 break
// Request an offer // Request an offer
case "request_offer": case "request_offer":
onRequestOffer(msg.CallHash, msg.PeerID) onRequestOffer(conf, msg.CallHash, msg.PeerID)
break break
// Close a connection // Close a connection
case "close_conn": case "close_conn":
onCloseConnection(msg.CallHash, msg.PeerID) onCloseConnection(conf, msg.CallHash, msg.PeerID)
break break
default: default: