2024-06-14 14:57:55 +00:00
|
|
|
import express from 'express';
|
|
|
|
import path from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import bip39 from 'bip39';
|
2024-06-14 16:32:51 +00:00
|
|
|
import crypto from 'crypto';
|
2024-06-14 14:57:55 +00:00
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
const trackerUrl = process.env.TRACKER_URL || 'ws://localhost:8106';
|
2024-06-14 16:32:51 +00:00
|
|
|
const stunServerUrl = process.env.STUN_SERVER_URL;
|
|
|
|
const turnServerUrl = process.env.TURN_SERVER_URL;
|
|
|
|
const turnSecret = process.env.TURN_SECRET;
|
|
|
|
const turnTTL = 86400;
|
2024-06-14 14:57:55 +00:00
|
|
|
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.render('index', { trackerUrl });
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/generate-mnemonic/:infoHash', (req, res) => {
|
|
|
|
const infoHash = req.params.infoHash;
|
|
|
|
const mnemonic = bip39.entropyToMnemonic(infoHash);
|
|
|
|
res.json({ mnemonic });
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/get-infohash/:mnemonic', (req, res) => {
|
|
|
|
const mnemonic = req.params.mnemonic;
|
|
|
|
const infoHash = bip39.mnemonicToEntropy(mnemonic);
|
|
|
|
res.json({ infoHash });
|
|
|
|
});
|
|
|
|
|
2024-06-14 16:32:51 +00:00
|
|
|
app.get('/turn-credentials', (req, res) => {
|
|
|
|
const iceServers = [];
|
|
|
|
|
|
|
|
if (stunServerUrl) {
|
|
|
|
iceServers.push({ urls: stunServerUrl });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (turnServerUrl && turnSecret) {
|
|
|
|
const unixTimeStamp = Math.floor(Date.now() / 1000) + turnTTL;
|
|
|
|
const username = `${unixTimeStamp}:transfercoffee`;
|
|
|
|
const hmac = crypto.createHmac('sha1', turnSecret);
|
|
|
|
hmac.update(username);
|
|
|
|
const credential = hmac.digest('base64');
|
|
|
|
|
|
|
|
iceServers.push({
|
|
|
|
urls: turnServerUrl,
|
|
|
|
username: username,
|
|
|
|
credential: credential
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({ iceServers });
|
|
|
|
});
|
|
|
|
|
2024-06-14 14:57:55 +00:00
|
|
|
const PORT = process.env.PORT || 8105;
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log(`Server is running on port ${PORT}`);
|
|
|
|
});
|