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

View File

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

View File

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

View File

@ -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 {
// 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"}
// 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),

6
ws.go
View File

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