Skip to content

Use TURN to establish external IP #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions pkg/sip/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
package sip

import (
"encoding/json"
"context"
"fmt"
"io"
"net"
"net/http"
"net/netip"
"time"

"github.com/livekit/mediatransportutil/pkg/rtcconfig"
"github.com/livekit/protocol/logger"
"github.com/livekit/sip/pkg/config"
"github.com/pkg/errors"
)

func GetServiceConfig(conf *config.Config) (*ServiceConfig, error) {
Expand Down Expand Up @@ -65,29 +67,18 @@ func GetServiceConfig(conf *config.Config) (*ServiceConfig, error) {
}

func getPublicIP() (netip.Addr, error) {
req, err := http.Get("http://ip-api.com/json/")
if err != nil {
return netip.Addr{}, err
}
defer req.Body.Close()

body, err := io.ReadAll(req.Body)
if err != nil {
return netip.Addr{}, err
}

ip := struct {
Query string
}{}
if err = json.Unmarshal(body, &ip); err != nil {
return netip.Addr{}, err
}

if ip.Query == "" {
return netip.Addr{}, fmt.Errorf("Query entry was not populated")
var err error
for i := 0; i < 3; i++ {
var ip string
ip, err = rtcconfig.GetExternalIP(context.Background(), rtcconfig.DefaultStunServers, nil)
if err == nil {
return netip.ParseAddr(ip)
} else {
time.Sleep(500 * time.Millisecond)
}
}

return netip.ParseAddr(ip.Query)
logger.Warnw("could not resolve external IP", err)
return netip.Addr{}, errors.Errorf("could not resolve external IP: %v", err)
}

func getLocalIP(localNet string) (netip.Addr, error) {
Expand Down
Loading