hmt 3 سال پیش
والد
کامیت
2f6fcf92e3
1فایلهای تغییر یافته به همراه22 افزوده شده و 28 حذف شده
  1. 22 28
      app.ts

+ 22 - 28
app.ts

@@ -1,4 +1,4 @@
-import { createError, opine, ErrorRequestHandler, Router, createHash, server, proxy } from "./deps.ts";
+import { createError, opine, ErrorRequestHandler, Router, createHash, server } from "./deps.ts";
 import { BBB } from './bbb.ts';
 
 // give your tinyscale server a secret so it looks like a BBB server
@@ -22,7 +22,7 @@ servers.forEach(async s => {
     if (!res.ok) throw "Connection error. Please check your host configuration"
     const body = await res.text()
     const ok = body.includes('SUCCESS')
-    console.log(`${s.host} is ${ok ? 'ok':'misconfigured. Please check your secret in servers.json'}`)
+    console.log(`${s.host} is ${ok ? 'ok' : 'misconfigured. Please check your secret in servers.json'}`)
     if (!ok) throw "Configuration error. Exiting …"
   } catch (e) {
     // exit tinyscale if an error is encountered in servers.json
@@ -44,13 +44,11 @@ function get_available_server(): server {
 
 const router = Router()
 // the api itself answering to every call
-
-// @ts-ignore
-const api = async (req, res, next) => {
+router.all("/:call", async (req, res, next) => {
   const handler = new BBB(req)
   if (!handler.authenticated(secret)) {
-    // res.setStatus(401).end()
-    // return
+    res.setStatus(401).end()
+    return
   }
   let server: server
   try {
@@ -61,18 +59,17 @@ const api = async (req, res, next) => {
   }
   console.log(`Redirecting to ${server.host}`)
   const redirect = handler.rewritten_query(server)
-  console.log("redirect to: ", redirect)
-  req.new_address = redirect
-  next()
-};
-// @ts-ignore
-const redirect = (req, res, next) => { console.log(req.new_address);return req.new_address}
-// @ts-ignore
-router.all("/bigbluebutton/api/:call", proxy("http://google.com"))
+  if (handler.call === 'join') {
+    res.redirect(redirect)
+  } else {
+    const data = await fetch(redirect)
+    const body = await data.text()
+    res.send(body)
+  }
+});
 // the fake answering machine to make sure we are recognized as a proper api
-router.get("/bigbluebutton/api", (req, res, next) => {
+router.get("/", (req, res, next) => {
   console.log('sending fake xml response')
-  res.set('Content-Type', 'text/xml');
   res.send(`<response>
 <returncode>SUCCESS</returncode>
 <version>2.0</version>
@@ -82,19 +79,16 @@ router.get("/bigbluebutton/api", (req, res, next) => {
 const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
   res.setStatus(err.status ?? 500);
   console.log(res.status, req.originalUrl)
-  console.log(err)
   res.end();
 };
 
 const app = opine()
-.use(async (req, res, next) => {
-  console.log(req.method, req.originalUrl)
-  next()
-})
-.use(api)
-            // .use("/", router)
-            .get("/bigbluebutton/api/:call", proxy("http://google.com"))
-            .use((req, res, next) => { next(createError(404)); })
-            .use(errorHandler);
+  .use((req, res, next) => {
+    res.set('Content-Type', 'text/xml');
+    next()
+  })
+  .use("/bigbluebutton/api", router)
+  .use((req, res, next) => { next(createError(404)); })
+  .use(errorHandler);
 
-export default app;
+export default app;