-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create/join will retrieve ice config from serverless functions
- Loading branch information
Showing
9 changed files
with
208 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ coverage/ | |
.env.development | ||
.env.development.local | ||
.env.production | ||
functions/.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
XIRSYS_API_IDENT='' | ||
XIRSYS_API_SECRET='' | ||
XIRSYS_API_CHANNEL='' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,87 @@ | ||
/** | ||
* Import function triggers from their respective submodules: | ||
* | ||
* import {onCall} from "firebase-functions/v2/https"; | ||
* import {onDocumentWritten} from "firebase-functions/v2/firestore"; | ||
* | ||
* See a full list of supported triggers at https://firebase.google.com/docs/functions | ||
*/ | ||
|
||
import { onRequest } from "firebase-functions/v2/https"; | ||
import { HttpsError, onCall } from "firebase-functions/v2/https"; | ||
import * as logger from "firebase-functions/logger"; | ||
import { defineString } from "firebase-functions/params"; | ||
|
||
// Start writing functions | ||
// https://firebase.google.com/docs/functions/typescript | ||
export const getIceServersConfig = onCall( | ||
async (request): Promise<{ iceServersConfig: RTCIceServer }> => { | ||
if (!request.auth) { | ||
throw new HttpsError( | ||
"failed-precondition", | ||
"No authentication detected." | ||
); | ||
} | ||
|
||
export const helloWorld = onRequest((request, response) => { | ||
logger.info("Hello logs!", { structuredData: true }); | ||
response.send("Hello from Firebase!"); | ||
}); | ||
const userEmail = request.auth.token.email; | ||
if (!request.auth.token.email) { | ||
throw new HttpsError("failed-precondition", "User with no email."); | ||
} | ||
|
||
logger.info("Retrieving ICE servers for", userEmail); | ||
|
||
try { | ||
const iceServersConfig = await fetchIceFromXirsys(); | ||
return { iceServersConfig }; | ||
} catch (error) { | ||
logger.error( | ||
"ICE provider error:", | ||
(error as Error).message || "(no message)" | ||
); | ||
throw new HttpsError( | ||
"unavailable", | ||
"No successful response from ICE server integration." | ||
); | ||
} | ||
} | ||
); | ||
|
||
async function fetchIceFromXirsys(): Promise<RTCIceServer> { | ||
const ident: string = defineString("XIRSYS_API_IDENT", { | ||
default: "", | ||
description: "Xirsys API user identification for retrieving ICE servers.", | ||
}).value(); | ||
const secret: string = defineString("XIRSYS_API_SECRET", { | ||
default: "", | ||
description: "Xirsys API account secret for retrieving ICE servers.", | ||
}).value(); | ||
const channel: string = defineString("XIRSYS_API_CHANNEL", { | ||
default: "", | ||
description: "Xirsys API channel name for retrieving ICE servers.", | ||
}).value(); | ||
|
||
if (!secret || !ident || !channel) { | ||
throw new Error("Missing Xirsys API credentials."); | ||
} | ||
|
||
const response = await fetch(`https://global.xirsys.net/_turn/${channel}`, { | ||
method: "PUT", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: | ||
"Basic " + Buffer.from(`${ident}:${secret}`).toString("base64"), | ||
}, | ||
body: JSON.stringify({ | ||
format: "urls", | ||
}), | ||
}); | ||
const data = await response.json(); | ||
|
||
if (data.s !== "ok") { | ||
throw new Error(data.v); | ||
} | ||
|
||
if (!data.v.iceServers) { | ||
// these "s" and "v" are weird, so lets just double check | ||
throw new Error( | ||
"Unknown content found in ICE server integration response." | ||
); | ||
} | ||
|
||
if (Array.isArray(data.v.iceServers)) { | ||
// the name is plural but it should not be an array | ||
throw new Error( | ||
"Unexpected array content found in ICE server integration response." | ||
); | ||
} | ||
|
||
return data.v.iceServers; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters