Adds canditates from domain resolver

This commit is contained in:
Alexey Khit
2022-09-15 09:07:53 +03:00
parent 3a10cb25bb
commit 800ebb39be
2 changed files with 41 additions and 10 deletions
+3 -8
View File
@@ -5,7 +5,6 @@ import (
"github.com/AlexxIT/go2rtc/pkg/streamer" "github.com/AlexxIT/go2rtc/pkg/streamer"
"github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/AlexxIT/go2rtc/pkg/webrtc"
"github.com/pion/sdp/v3" "github.com/pion/sdp/v3"
"strings"
) )
var candidates []string var candidates []string
@@ -32,16 +31,12 @@ func addCanditates(answer string) (string, error) {
} }
for _, address := range candidates { for _, address := range candidates {
if strings.HasPrefix(address, "stun:") { var err error
ip, err := webrtc.GetPublicIP() address, err = webrtc.LookupIP(address)
if err != nil { if err != nil {
log.Warn().Err(err).Msg("[webrtc] public IP") log.Warn().Err(err).Msg("[webrtc] candidate")
continue continue
} }
address = ip.String() + address[4:]
log.Debug().Str("addr", address).Msg("[webrtc] stun public address")
}
cand, err := webrtc.NewCandidate(address) cand, err := webrtc.NewCandidate(address)
if err != nil { if err != nil {
+36
View File
@@ -1,12 +1,14 @@
package webrtc package webrtc
import ( import (
"fmt"
"github.com/AlexxIT/go2rtc/pkg/streamer" "github.com/AlexxIT/go2rtc/pkg/streamer"
"github.com/pion/ice/v2" "github.com/pion/ice/v2"
"github.com/pion/stun" "github.com/pion/stun"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"net" "net"
"strconv" "strconv"
"strings"
) )
func NewCandidate(address string) (string, error) { func NewCandidate(address string) (string, error) {
@@ -34,6 +36,31 @@ func NewCandidate(address string) (string, error) {
return "candidate:" + cand.Marshal(), nil return "candidate:" + cand.Marshal(), nil
} }
func LookupIP(address string) (string, error) {
if strings.HasPrefix(address, "stun:") {
ip, err := GetPublicIP()
if err != nil {
return "", err
}
return ip.String() + address[4:], nil
}
if IsIP(address) {
return address, nil
}
i := strings.IndexByte(address, ':')
ips, err := net.LookupIP(address[:i])
if err != nil {
return "", err
}
if len(ips) == 0 {
return "", fmt.Errorf("can't resolve: %s", address)
}
return ips[0].String() + address[i:], nil
}
// GetPublicIP example from https://github.com/pion/stun // GetPublicIP example from https://github.com/pion/stun
func GetPublicIP() (net.IP, error) { func GetPublicIP() (net.IP, error) {
c, err := stun.Dial("udp", "stun.l.google.com:19302") c, err := stun.Dial("udp", "stun.l.google.com:19302")
@@ -63,6 +90,15 @@ func GetPublicIP() (net.IP, error) {
return xorAddr.IP, nil return xorAddr.IP, nil
} }
func IsIP(host string) bool {
for _, i := range host {
if i >= 'A' {
return false
}
}
return true
}
func MimeType(codec *streamer.Codec) string { func MimeType(codec *streamer.Codec) string {
switch codec.Name { switch codec.Name {
case streamer.CodecH264: case streamer.CodecH264: