feat: add ICE servers and local IP address returned by the API to fix connectivity issues behind NAT

This commit is contained in:
Siyuan Miao 2025-02-10 17:09:22 +01:00
parent 8ffe66a1bc
commit 92e0a06dee
4 changed files with 43 additions and 19 deletions

View File

@ -7,13 +7,14 @@ import (
"fmt"
"net/http"
"net/url"
"github.com/coder/websocket/wsjson"
"time"
"github.com/coder/websocket/wsjson"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/gin-gonic/gin"
"github.com/coder/websocket"
"github.com/gin-gonic/gin"
)
type CloudRegisterRequest struct {
@ -187,7 +188,7 @@ func handleSessionRequest(ctx context.Context, c *websocket.Conn, req WebRTCSess
return fmt.Errorf("google identity mismatch")
}
session, err := newSession()
session, err := newSession(req.ICEServers, req.IP)
if err != nil {
_ = wsjson.Write(context.Background(), c, gin.H{"error": err})
return err

View File

@ -22,6 +22,7 @@ type Config struct {
LocalAuthToken string `json:"local_auth_token"`
LocalAuthMode string `json:"localAuthMode"` //TODO: fix it with migration
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
FallbackICEServers []string `json:"fallback_ice_servers"`
}
const configPath = "/userdata/kvm_config.json"
@ -29,6 +30,11 @@ const configPath = "/userdata/kvm_config.json"
var defaultConfig = &Config{
CloudURL: "https://api.jetkvm.com",
AutoUpdateEnabled: true, // Set a default value
FallbackICEServers: []string{
"stun:stun.cloudflare.com:3478",
"stun:stun.cloudflare.com:53",
"stun:stun.l.google.com:19302",
},
}
var config *Config

4
web.go
View File

@ -19,6 +19,8 @@ var staticFiles embed.FS
type WebRTCSessionRequest struct {
Sd string `json:"sd"`
OidcGoogle string `json:"OidcGoogle,omitempty"`
IP string `json:"ip,omitempty"`
ICEServers []string `json:"iceServers,omitempty"`
}
type SetPasswordRequest struct {
@ -116,7 +118,7 @@ func handleWebRTCSession(c *gin.Context) {
return
}
session, err := newSession()
session, err := newSession(nil, "")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
return

View File

@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"net"
"strings"
"github.com/pion/webrtc/v4"
@ -61,9 +62,23 @@ func (s *Session) ExchangeOffer(offerStr string) (string, error) {
return base64.StdEncoding.EncodeToString(localDescription), nil
}
func newSession() (*Session, error) {
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{{}},
func newSession(iceServers []string, localIP string) (*Session, error) {
if iceServers == nil {
iceServers = config.FallbackICEServers
fmt.Printf("ICE Servers not provided, using fallback %v\n", iceServers)
}
webrtcSettingEngine := webrtc.SettingEngine{}
if localIP != "" || net.ParseIP(localIP) == nil {
fmt.Printf("Local IP address not provided or invalid, won't set NAT1To1IPs\n")
} else {
webrtcSettingEngine.SetNAT1To1IPs([]string{localIP}, webrtc.ICECandidateTypeSrflx)
}
// create
api := webrtc.NewAPI(webrtc.WithSettingEngine(webrtcSettingEngine))
peerConnection, err := api.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{{URLs: iceServers}},
})
if err != nil {
return nil, err