From cf8625d8e963d0b50ecba0f688ffd8cbea695d8a Mon Sep 17 00:00:00 2001 From: LiYuqing Date: Sat, 29 Jan 2022 20:15:02 +0800 Subject: [PATCH 1/4] Better get outbond ip --- engine/engine.go | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index d832aa2..4d60e09 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -87,29 +87,13 @@ func getIPFromInterface(iname string, addr string) (string, error) { return addr, nil } - ifaces, err := net.Interfaces() + conn, err := net.Dial("udp", "8.8.8.8:80") if err != nil { return "", err } - for _, i := range ifaces { - if i.Name != iname { - continue - } - addrs, err := i.Addrs() - if err != nil { - return "", err - } - for _, addr := range addrs { - switch v := addr.(type) { - case *net.IPNet: - return v.IP.String(), nil - case *net.IPAddr: - return v.IP.String(), nil - } - } - } - - return "", fmt.Errorf("no address for interface %s", iname) + defer conn.Close() + localAddr := conn.LocalAddr().(*net.UDPAddr) + return localAddr.IP.String(), nil } type pmap struct { From 15b32b893e57cfd24548c7d592d41d4dd45ec216 Mon Sep 17 00:00:00 2001 From: LiYuqing Date: Tue, 22 Feb 2022 13:47:27 +0800 Subject: [PATCH 2/4] Slight fixes --- engine/peer.go | 5 +---- engine/router.go | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/engine/peer.go b/engine/peer.go index 5134b05..cb21f32 100644 --- a/engine/peer.go +++ b/engine/peer.go @@ -49,10 +49,7 @@ type Peer struct { } func BuildPeer(rid, uid string, pc *webrtc.PeerConnection, callback string) *Peer { - cid, err := uuid.NewV4() - if err != nil { - panic(err) - } + cid := uuid.Must(uuid.NewV4()) peer := &Peer{rid: rid, uid: uid, cid: cid.String(), pc: pc} peer.callback = callback peer.connected = make(chan bool, 1) diff --git a/engine/router.go b/engine/router.go index 24f3786..8b1790e 100644 --- a/engine/router.go +++ b/engine/router.go @@ -324,8 +324,7 @@ func (r *Router) subscribe(rid, uid, cid string) (*webrtc.SessionDescription, er ec <- buildError(ErrorServerSetLocalOffer, err) return } - c := <-gatherComplete - gc <- c + gc <- (<-gatherComplete) }() select { From dc778c97dfa7efbe34c7e3f2e6e989736558c082 Mon Sep 17 00:00:00 2001 From: LiYuqing Date: Tue, 22 Feb 2022 17:01:15 +0800 Subject: [PATCH 3/4] Should return user specified interface ip first --- engine/engine.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/engine/engine.go b/engine/engine.go index 4d60e09..4d4a830 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -82,10 +82,39 @@ func (engine *Engine) Loop() { } } +func getSpecificIPFromInterface(iname string) (string, error) { + ifaces, err := net.Interfaces() + if err != nil { + return "", err + } + for _, i := range ifaces { + if i.Name != iname { + continue + } + addrs, err := i.Addrs() + if err != nil { + return "", err + } + for _, addr := range addrs { + switch v := addr.(type) { + case *net.IPNet: + return v.IP.String(), nil + case *net.IPAddr: + return v.IP.String(), nil + } + } + } + + return "", fmt.Errorf("no address for interface %s", iname) +} + func getIPFromInterface(iname string, addr string) (string, error) { if addr != "" { return addr, nil } + if iname != "" { + return getSpecificIPFromInterface(iname) + } conn, err := net.Dial("udp", "8.8.8.8:80") if err != nil { From 01503efa3b153b02eb18e0537ef03ae6aa554bb2 Mon Sep 17 00:00:00 2001 From: LiYuqing Date: Tue, 22 Feb 2022 17:04:08 +0800 Subject: [PATCH 4/4] Fix engine config --- config/engine.example.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/engine.example.toml b/config/engine.example.toml index 3c4e30c..41ede8d 100644 --- a/config/engine.example.toml +++ b/config/engine.example.toml @@ -1,6 +1,6 @@ [engine] -# the network interface to bind -interface = "eth0" +# the network interface to bind, e.g.: 'eth0' +interface = "" # the IP address to bind, empty allows the engine to get it from interface address = "" log-level = 10