Add second STUN server from Cloudflare

This commit is contained in:
Alex X
2025-12-01 14:18:45 +03:00
parent fbd5215669
commit 7eb5fe0355
6 changed files with 30 additions and 17 deletions
+3 -1
View File
@@ -18,9 +18,11 @@ type Address struct {
Priority uint32 Priority uint32
} }
var stuns []string
func (a *Address) Host() string { func (a *Address) Host() string {
if a.host == "stun" { if a.host == "stun" {
ip, err := webrtc.GetCachedPublicIP() ip, err := webrtc.GetCachedPublicIP(stuns...)
if err != nil { if err != nil {
return "" return ""
} }
+11 -1
View File
@@ -26,7 +26,7 @@ func Init() {
cfg.Mod.Listen = ":8555" cfg.Mod.Listen = ":8555"
cfg.Mod.IceServers = []pion.ICEServer{ cfg.Mod.IceServers = []pion.ICEServer{
{URLs: []string{"stun:stun.l.google.com:19302"}}, {URLs: []string{"stun:stun.cloudflare.com:3478", "stun:stun.l.google.com:19302"}},
} }
app.LoadConfig(&cfg) app.LoadConfig(&cfg)
@@ -38,6 +38,16 @@ func Init() {
address, network, _ := strings.Cut(cfg.Mod.Listen, "/") address, network, _ := strings.Cut(cfg.Mod.Listen, "/")
for _, candidate := range cfg.Mod.Candidates { for _, candidate := range cfg.Mod.Candidates {
AddCandidate(network, candidate) AddCandidate(network, candidate)
if strings.HasPrefix(candidate, "stun:") && stuns == nil {
for _, ice := range cfg.Mod.IceServers {
for _, url := range ice.URLs {
if strings.HasPrefix(url, "stun:") {
stuns = append(stuns, url[5:])
}
}
}
}
} }
var err error var err error
+12 -11
View File
@@ -185,8 +185,8 @@ func LookupIP(address string) (string, error) {
} }
// GetPublicIP example from https://github.com/pion/stun // GetPublicIP example from https://github.com/pion/stun
func GetPublicIP() (net.IP, error) { func GetPublicIP(address string) (net.IP, error) {
conn, err := net.Dial("udp", "stun.l.google.com:19302") conn, err := net.Dial("udp", address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -225,18 +225,19 @@ func GetPublicIP() (net.IP, error) {
var cachedIP net.IP var cachedIP net.IP
var cachedTS time.Time var cachedTS time.Time
func GetCachedPublicIP() (net.IP, error) { func GetCachedPublicIP(stuns ...string) (net.IP, error) {
now := time.Now() if now := time.Now(); now.After(cachedTS) {
if now.After(cachedTS) { for _, addr := range stuns {
newIP, err := GetPublicIP() if ip, _ := GetPublicIP(addr); ip != nil {
if err == nil { cachedIP = ip
cachedIP = newIP
cachedTS = now.Add(time.Minute * 5) cachedTS = now.Add(time.Minute * 5)
} else if cachedIP == nil { return ip, nil
return nil, err
} }
} }
}
if cachedIP == nil {
return nil, errors.New("webrtc: can't get public IP")
}
return cachedIP, nil return cachedIP, nil
} }
+1 -1
View File
@@ -71,7 +71,7 @@ export class VideoRTC extends HTMLElement {
*/ */
this.pcConfig = { this.pcConfig = {
bundlePolicy: 'max-bundle', bundlePolicy: 'max-bundle',
iceServers: [{urls: 'stun:stun.l.google.com:19302'}], iceServers: [{urls: ['stun:stun.cloudflare.com:3478', 'stun:stun.l.google.com:19302']}],
sdpSemantics: 'unified-plan', // important for Chromecast 1 sdpSemantics: 'unified-plan', // important for Chromecast 1
}; };
+1 -1
View File
@@ -21,7 +21,7 @@
<script> <script>
async function PeerConnection(media) { async function PeerConnection(media) {
const pc = new RTCPeerConnection({ const pc = new RTCPeerConnection({
iceServers: [{urls: 'stun:stun.l.google.com:19302'}] iceServers: [{urls: ['stun:stun.cloudflare.com:3478', 'stun:stun.l.google.com:19302']}]
}); });
document.getElementById('video').srcObject = new MediaStream([ document.getElementById('video').srcObject = new MediaStream([
+1 -1
View File
@@ -21,7 +21,7 @@
<script> <script>
async function PeerConnection(media) { async function PeerConnection(media) {
const pc = new RTCPeerConnection({ const pc = new RTCPeerConnection({
iceServers: [{urls: 'stun:stun.l.google.com:19302'}] iceServers: [{urls: ['stun:stun.cloudflare.com:3478', 'stun:stun.l.google.com:19302']}]
}); });
const localTracks = []; const localTracks = [];