Re-add old signaling for when upgrading (#339)

This commit is contained in:
Aveline 2025-04-09 22:27:01 +02:00 committed by GitHub
commit bf89e038ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 47 additions and 0 deletions

47
web.go
View File

@ -99,6 +99,22 @@ func setupRouter() *gin.Engine {
protected := r.Group("/") protected := r.Group("/")
protected.Use(protectedMiddleware()) protected.Use(protectedMiddleware())
{ {
/*
* Legacy WebRTC session endpoint
*
* This endpoint is maintained for backward compatibility when users upgrade from a version
* using the legacy HTTP-based signaling method to the new WebSocket-based signaling method.
*
* During the upgrade process, when the "Rebooting device after update..." message appears,
* the browser still runs the previous JavaScript code which polls this endpoint to establish
* a new WebRTC session. Once the session is established, the page will automatically reload
* with the updated code.
*
* Without this endpoint, the stale JavaScript would fail to establish a connection,
* causing users to see the "Rebooting device after update..." message indefinitely
* until they manually refresh the page, leading to a confusing user experience.
*/
protected.POST("/webrtc/session", handleWebRTCSession)
protected.GET("/webrtc/signaling/client", handleLocalWebRTCSignal) protected.GET("/webrtc/signaling/client", handleLocalWebRTCSignal)
protected.POST("/cloud/register", handleCloudRegister) protected.POST("/cloud/register", handleCloudRegister)
protected.GET("/cloud/state", handleCloudState) protected.GET("/cloud/state", handleCloudState)
@ -126,6 +142,37 @@ func setupRouter() *gin.Engine {
// TODO: support multiple sessions? // TODO: support multiple sessions?
var currentSession *Session var currentSession *Session
func handleWebRTCSession(c *gin.Context) {
var req WebRTCSessionRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
session, err := newSession(SessionConfig{})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
return
}
sd, err := session.ExchangeOffer(req.Sd)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err})
return
}
if currentSession != nil {
writeJSONRPCEvent("otherSessionConnected", nil, currentSession)
peerConn := currentSession.peerConnection
go func() {
time.Sleep(1 * time.Second)
_ = peerConn.Close()
}()
}
currentSession = session
c.JSON(http.StatusOK, gin.H{"sd": sd})
}
func handleLocalWebRTCSignal(c *gin.Context) { func handleLocalWebRTCSignal(c *gin.Context) {
cloudLogger.Infof("new websocket connection established") cloudLogger.Infof("new websocket connection established")
// Create WebSocket options with InsecureSkipVerify to bypass origin check // Create WebSocket options with InsecureSkipVerify to bypass origin check