From e85a01b8093c34b5d7943ff7b827919e3a1aaf48 Mon Sep 17 00:00:00 2001 From: benyamin Date: Thu, 21 Sep 2023 18:07:05 +0330 Subject: [PATCH] added argument to set custom ICE Host Candidate IP --- app.go | 13 +++++++------ go.mod | 2 +- main.go | 3 ++- models/config.go | 11 ++++++----- repositories/room.go | 5 +++-- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app.go b/app.go index af4e73a..8c29605 100644 --- a/app.go +++ b/app.go @@ -23,7 +23,7 @@ type App struct { src string } -func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string, iceTCPMUXListenPort uint) { +func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, targetRoom string, iceTCPMUXListenPort uint, customICEHostCandidateIP string) { println("initializing ..") a.src = srcListenAddr var iceServers []webrtc.ICEServer @@ -37,11 +37,12 @@ func (a *App) Init(srcListenAddr string, svcAddr string, logjamBaseUrl string, t } } a.conf = &models.ConfigModel{ - LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node", - TargetRoom: targetRoom, - ServiceAddress: svcAddr, - ICEServers: iceServers, - ICETCPMUXListenPort: iceTCPMUXListenPort, + LogjamBaseUrl: logjamBaseUrl + "/auxiliary-node", + TargetRoom: targetRoom, + ServiceAddress: svcAddr, + ICEServers: iceServers, + ICETCPMUXListenPort: iceTCPMUXListenPort, + CustomICEHostCandidateIP: customICEHostCandidateIP, } roomRepo := repositories.NewRoomRepository(a.conf) a.router = &routers.Router{} diff --git a/go.mod b/go.mod index c4ac04a..68a1253 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/gin-gonic/gin v1.9.1 + github.com/pion/interceptor v0.1.17 github.com/pion/rtcp v1.2.10 github.com/pion/webrtc/v3 v3.2.12 ) @@ -29,7 +30,6 @@ require ( github.com/pion/datachannel v1.5.5 // indirect github.com/pion/dtls/v2 v2.2.7 // indirect github.com/pion/ice/v2 v2.3.9 // indirect - github.com/pion/interceptor v0.1.17 // indirect github.com/pion/logging v0.2.2 // indirect github.com/pion/mdns v0.0.7 // indirect github.com/pion/randutil v0.1.0 // indirect diff --git a/main.go b/main.go index f96db8b..a5ff3cf 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ func main() { logjamBaseUrl := flag.String("logjam-base-url", "http://localhost:8090", "logjam base url( shouldn't end with / )") targetRoom := flag.String("targetRoom", "test", "target room") icetcpmuxListenPort := flag.Uint("ice-tcp-mux-listen-port", 4444, "listen port to use for tcp ice candidates") + customICEHostCandidateIP := flag.String("custom-ice-host-candidate-ip", "", "set to override host ice candidates address") flag.Parse() if strings.HasSuffix(*logjamBaseUrl, "/") { @@ -20,6 +21,6 @@ func main() { panic("service address shouldn't end with /") } app := App{} - app.Init(*src, *svcAddr, *logjamBaseUrl, *targetRoom, *icetcpmuxListenPort) + app.Init(*src, *svcAddr, *logjamBaseUrl, *targetRoom, *icetcpmuxListenPort, *customICEHostCandidateIP) app.Run() } diff --git a/models/config.go b/models/config.go index ac16148..9343de6 100644 --- a/models/config.go +++ b/models/config.go @@ -3,9 +3,10 @@ package models import "github.com/pion/webrtc/v3" type ConfigModel struct { - ServiceAddress string `json:"serviceAddress"` - LogjamBaseUrl string `json:"logjamBaseUrl"` - TargetRoom string `json:"targetRoom"` - ICETCPMUXListenPort uint `json:"ice_tcpmux_listenPort"` - ICEServers []webrtc.ICEServer `json:"iceServers"` + ServiceAddress string `json:"serviceAddress"` + LogjamBaseUrl string `json:"logjamBaseUrl"` + TargetRoom string `json:"targetRoom"` + ICETCPMUXListenPort uint `json:"ice_tcpmux_listenPort"` + CustomICEHostCandidateIP string `json:"customICEHostCandidateIP"` + ICEServers []webrtc.ICEServer `json:"iceServers"` } diff --git a/repositories/room.go b/repositories/room.go index 425e7d5..a865252 100644 --- a/repositories/room.go +++ b/repositories/room.go @@ -49,7 +49,9 @@ type RoomRepository struct { func NewRoomRepository(conf *models.ConfigModel) *RoomRepository { settingEngine := webrtc.SettingEngine{} - + if len(conf.CustomICEHostCandidateIP) > 0 { + settingEngine.SetNAT1To1IPs([]string{conf.CustomICEHostCandidateIP}, webrtc.ICECandidateTypeHost) + } settingEngine.SetNetworkTypes([]webrtc.NetworkType{ webrtc.NetworkTypeTCP6, webrtc.NetworkTypeUDP6, @@ -277,7 +279,6 @@ func (r *RoomRepository) onPeerConnectionStateChange(room *Room, peer *Peer, new func (r *RoomRepository) onPeerTrack(roomId string, id uint64, remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { fmt.Println("got a track!", remote.ID(), remote.StreamID(), remote.Kind().String()) - println("pc", id, "streamid", remote.StreamID()) r.Lock() if !r.doesRoomExists(roomId) { r.Unlock()