2024-10-19 05:25:26 -06:00
|
|
|
import cors from "cors"
|
|
|
|
import dotenv from "dotenv"
|
2024-10-19 15:12:52 -06:00
|
|
|
import { Sandbox } from "e2b"
|
2024-10-19 05:25:26 -06:00
|
|
|
import express, { Express } from "express"
|
|
|
|
import fs from "fs"
|
|
|
|
import { createServer } from "http"
|
|
|
|
import { Server } from "socket.io"
|
2024-10-19 15:43:18 -06:00
|
|
|
import { AIWorker } from "./AIWorker"
|
2024-10-24 16:34:13 -06:00
|
|
|
import { CONTAINER_TIMEOUT } from "./constants"
|
2024-10-19 15:12:52 -06:00
|
|
|
import { DokkuClient } from "./DokkuClient"
|
|
|
|
import { FileManager, SandboxFiles } from "./FileManager"
|
2024-04-30 01:56:43 -04:00
|
|
|
import {
|
2024-05-05 12:55:34 -07:00
|
|
|
createFileRL,
|
2024-05-11 18:03:42 -07:00
|
|
|
createFolderRL,
|
2024-05-05 12:55:34 -07:00
|
|
|
deleteFileRL,
|
|
|
|
renameFileRL,
|
|
|
|
saveFileRL,
|
2024-10-19 05:25:26 -06:00
|
|
|
} from "./ratelimit"
|
2024-10-19 15:12:52 -06:00
|
|
|
import { SecureGitClient } from "./SecureGitClient"
|
2024-10-24 17:37:34 -06:00
|
|
|
import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware
|
2024-10-24 17:15:58 -06:00
|
|
|
import { handleCloseTerminal, handleCreateFile, handleCreateFolder, handleCreateTerminal, handleDeleteFile, handleDeleteFolder, handleDeploy, handleGenerateCode, handleGetFile, handleGetFolder, handleHeartbeat, handleListApps, handleMoveFile, HandlerContext, handleRenameFile, handleResizeTerminal, handleSaveFile, handleTerminalData } from "./SocketHandlers"
|
2024-10-19 15:12:52 -06:00
|
|
|
import { TerminalManager } from "./TerminalManager"
|
|
|
|
import { LockManager } from "./utils"
|
2024-04-18 16:40:08 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Handle uncaught exceptions
|
2024-10-19 05:25:26 -06:00
|
|
|
process.on("uncaughtException", (error) => {
|
|
|
|
console.error("Uncaught Exception:", error)
|
2024-09-30 02:55:22 -07:00
|
|
|
// Do not exit the process
|
|
|
|
// You can add additional logging or recovery logic here
|
2024-10-19 05:25:26 -06:00
|
|
|
})
|
2024-09-30 02:55:22 -07:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Handle unhandled promise rejections
|
2024-10-19 05:25:26 -06:00
|
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
|
|
console.error("Unhandled Rejection at:", promise, "reason:", reason)
|
2024-09-30 02:55:22 -07:00
|
|
|
// Do not exit the process
|
|
|
|
// You can also handle the rejected promise here if needed
|
2024-10-19 05:25:26 -06:00
|
|
|
})
|
2024-09-30 02:55:22 -07:00
|
|
|
|
2024-10-24 17:37:34 -06:00
|
|
|
// Check if the sandbox owner is connected
|
|
|
|
function isOwnerConnected(sandboxId: string): boolean {
|
|
|
|
return (connections[sandboxId] ?? 0) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize containers and managers
|
|
|
|
const containers: Record<string, Sandbox> = {}
|
|
|
|
const connections: Record<string, number> = {}
|
|
|
|
const fileManagers: Record<string, FileManager> = {}
|
|
|
|
const terminalManagers: Record<string, TerminalManager> = {}
|
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Load environment variables
|
2024-10-19 05:25:26 -06:00
|
|
|
dotenv.config()
|
2024-04-18 16:40:08 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Initialize Express app and create HTTP server
|
2024-10-19 05:25:26 -06:00
|
|
|
const app: Express = express()
|
|
|
|
const port = process.env.PORT || 4000
|
|
|
|
app.use(cors())
|
|
|
|
const httpServer = createServer(app)
|
2024-04-18 16:40:08 -04:00
|
|
|
const io = new Server(httpServer, {
|
|
|
|
cors: {
|
|
|
|
origin: "*",
|
|
|
|
},
|
2024-10-19 05:25:26 -06:00
|
|
|
})
|
2024-04-18 16:40:08 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Middleware for socket authentication
|
2024-10-24 17:37:34 -06:00
|
|
|
io.use(socketAuth) // Use the new socketAuth middleware
|
2024-04-18 16:40:08 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Initialize lock manager
|
2024-10-19 05:25:26 -06:00
|
|
|
const lockManager = new LockManager()
|
2024-06-28 02:39:03 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Check for required environment variables
|
2024-10-19 05:25:26 -06:00
|
|
|
if (!process.env.DOKKU_HOST)
|
2024-10-24 17:38:43 -06:00
|
|
|
console.warn("Environment variable DOKKU_HOST is not defined")
|
2024-10-19 05:25:26 -06:00
|
|
|
if (!process.env.DOKKU_USERNAME)
|
2024-10-24 17:38:43 -06:00
|
|
|
console.warn("Environment variable DOKKU_USERNAME is not defined")
|
2024-10-19 05:25:26 -06:00
|
|
|
if (!process.env.DOKKU_KEY)
|
2024-10-24 17:38:43 -06:00
|
|
|
console.warn("Environment variable DOKKU_KEY is not defined")
|
2024-08-01 09:29:42 -07:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Initialize Dokku client
|
2024-10-24 17:15:58 -06:00
|
|
|
const dokkuClient =
|
2024-08-01 09:29:42 -07:00
|
|
|
process.env.DOKKU_HOST && process.env.DOKKU_KEY && process.env.DOKKU_USERNAME
|
|
|
|
? new DokkuClient({
|
2024-10-24 16:20:24 -06:00
|
|
|
host: process.env.DOKKU_HOST,
|
|
|
|
username: process.env.DOKKU_USERNAME,
|
|
|
|
privateKey: fs.readFileSync(process.env.DOKKU_KEY),
|
|
|
|
})
|
2024-10-19 05:25:26 -06:00
|
|
|
: null
|
2024-10-24 17:15:58 -06:00
|
|
|
dokkuClient?.connect()
|
2024-08-01 09:29:42 -07:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Initialize Git client used to deploy Dokku apps
|
2024-10-24 17:15:58 -06:00
|
|
|
const gitClient =
|
2024-10-19 05:25:26 -06:00
|
|
|
process.env.DOKKU_HOST && process.env.DOKKU_KEY
|
|
|
|
? new SecureGitClient(
|
2024-10-24 16:20:24 -06:00
|
|
|
`dokku@${process.env.DOKKU_HOST}`,
|
|
|
|
process.env.DOKKU_KEY
|
|
|
|
)
|
2024-10-19 05:25:26 -06:00
|
|
|
: null
|
2024-07-23 22:17:26 -04:00
|
|
|
|
2024-10-19 15:43:18 -06:00
|
|
|
// Add this near the top of the file, after other initializations
|
|
|
|
const aiWorker = new AIWorker(
|
|
|
|
process.env.AI_WORKER_URL!,
|
|
|
|
process.env.CF_AI_KEY!,
|
|
|
|
process.env.DATABASE_WORKER_URL!,
|
|
|
|
process.env.WORKERS_KEY!
|
|
|
|
)
|
|
|
|
|
2024-10-24 16:20:24 -06:00
|
|
|
// Handle a client connecting to the server
|
2024-04-18 16:40:08 -04:00
|
|
|
io.on("connection", async (socket) => {
|
2024-06-28 02:39:03 -04:00
|
|
|
try {
|
|
|
|
const data = socket.data as {
|
2024-10-19 05:25:26 -06:00
|
|
|
userId: string
|
|
|
|
sandboxId: string
|
|
|
|
isOwner: boolean
|
|
|
|
}
|
2024-04-21 22:55:49 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Handle connection based on user type (owner or not)
|
2024-06-28 02:39:03 -04:00
|
|
|
if (data.isOwner) {
|
2024-10-19 05:25:26 -06:00
|
|
|
connections[data.sandboxId] = (connections[data.sandboxId] ?? 0) + 1
|
2024-06-28 02:39:03 -04:00
|
|
|
} else {
|
2024-10-19 15:12:52 -06:00
|
|
|
if (!isOwnerConnected(data.sandboxId)) {
|
2024-10-19 05:25:26 -06:00
|
|
|
socket.emit("disableAccess", "The sandbox owner is not connected.")
|
|
|
|
return
|
2024-06-28 02:39:03 -04:00
|
|
|
}
|
2024-05-07 22:40:59 -07:00
|
|
|
}
|
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Create or retrieve container
|
2024-10-19 05:25:26 -06:00
|
|
|
const createdContainer = await lockManager.acquireLock(
|
|
|
|
data.sandboxId,
|
|
|
|
async () => {
|
|
|
|
try {
|
|
|
|
// Start a new container if the container doesn't exist or it timed out.
|
|
|
|
if (
|
|
|
|
!containers[data.sandboxId] ||
|
|
|
|
!(await containers[data.sandboxId].isRunning())
|
|
|
|
) {
|
|
|
|
containers[data.sandboxId] = await Sandbox.create({
|
|
|
|
timeoutMs: CONTAINER_TIMEOUT,
|
|
|
|
})
|
|
|
|
console.log("Created container ", data.sandboxId)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(`Error creating container ${data.sandboxId}:`, e)
|
2024-10-22 03:39:50 -06:00
|
|
|
socket.emit("error", `Error: container creation. ${e.message ?? e}`)
|
2024-06-28 02:39:03 -04:00
|
|
|
}
|
|
|
|
}
|
2024-10-19 05:25:26 -06:00
|
|
|
)
|
2024-04-27 19:12:25 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Function to send loaded event
|
2024-10-19 15:12:52 -06:00
|
|
|
const sendLoadedEvent = (files: SandboxFiles) => {
|
|
|
|
socket.emit("loaded", files.files)
|
2024-10-19 05:25:26 -06:00
|
|
|
}
|
2024-09-06 18:13:42 -07:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Initialize file and terminal managers if container was created
|
2024-09-29 20:54:09 -07:00
|
|
|
if (createdContainer) {
|
2024-10-19 15:12:52 -06:00
|
|
|
fileManagers[data.sandboxId] = new FileManager(
|
|
|
|
data.sandboxId,
|
|
|
|
containers[data.sandboxId],
|
|
|
|
sendLoadedEvent
|
|
|
|
)
|
|
|
|
terminalManagers[data.sandboxId] = new TerminalManager(
|
|
|
|
containers[data.sandboxId]
|
|
|
|
)
|
2024-10-23 11:55:38 +01:00
|
|
|
console.log(`terminal manager set up for ${data.sandboxId}`)
|
|
|
|
await fileManagers[data.sandboxId].initialize()
|
2024-10-19 05:25:26 -06:00
|
|
|
}
|
2024-04-27 19:12:25 -04:00
|
|
|
|
2024-10-19 15:12:52 -06:00
|
|
|
const fileManager = fileManagers[data.sandboxId]
|
|
|
|
const terminalManager = terminalManagers[data.sandboxId]
|
2024-10-19 05:25:26 -06:00
|
|
|
|
2024-10-19 15:12:52 -06:00
|
|
|
// Load file list from the file manager into the editor
|
|
|
|
sendLoadedEvent(fileManager.sandboxFiles)
|
2024-05-11 17:23:45 -07:00
|
|
|
|
2024-10-24 17:15:58 -06:00
|
|
|
const handlerContext: HandlerContext = {
|
|
|
|
fileManager,
|
|
|
|
terminalManager,
|
|
|
|
aiWorker,
|
|
|
|
dokkuClient,
|
|
|
|
gitClient,
|
|
|
|
lockManager,
|
|
|
|
sandboxManager: containers[data.sandboxId],
|
|
|
|
}
|
|
|
|
|
2024-10-24 22:18:01 -06:00
|
|
|
// Helper function to handle socket events with error handling and optional rate limiting
|
|
|
|
const handleSocketEvent = (
|
|
|
|
event: string,
|
|
|
|
handler: any,
|
|
|
|
rateLimiter: any | null = null
|
|
|
|
) => {
|
|
|
|
socket.on(event, async (options: any, callback?: (response: any) => void) => {
|
|
|
|
try {
|
|
|
|
// Consume rate limiter if provided
|
|
|
|
if (rateLimiter) {
|
|
|
|
await rateLimiter.consume(data.userId, 1); // Adjust as needed for the specific rate limiter
|
|
|
|
}
|
|
|
|
const response = await handler(options, handlerContext)
|
|
|
|
callback?.(response);
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(`Error processing event "${event}":`, e);
|
|
|
|
socket.emit("error", `Error: ${event}. ${e.message ?? e}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register socket events with optional rate limiters
|
|
|
|
handleSocketEvent("heartbeat", handleHeartbeat);
|
|
|
|
handleSocketEvent("getFile", handleGetFile);
|
|
|
|
handleSocketEvent("getFolder", handleGetFolder);
|
|
|
|
handleSocketEvent("saveFile", handleSaveFile, saveFileRL);
|
|
|
|
handleSocketEvent("moveFile", handleMoveFile);
|
|
|
|
handleSocketEvent("list", handleListApps);
|
|
|
|
handleSocketEvent("deploy", handleDeploy);
|
|
|
|
handleSocketEvent("createFile", handleCreateFile, createFileRL);
|
|
|
|
handleSocketEvent("createFolder", handleCreateFolder, createFolderRL);
|
|
|
|
handleSocketEvent("renameFile", handleRenameFile, renameFileRL);
|
|
|
|
handleSocketEvent("deleteFile", handleDeleteFile, deleteFileRL);
|
|
|
|
handleSocketEvent("deleteFolder", handleDeleteFolder);
|
|
|
|
handleSocketEvent("createTerminal", handleCreateTerminal);
|
|
|
|
handleSocketEvent("resizeTerminal", handleResizeTerminal);
|
|
|
|
handleSocketEvent("terminalData", handleTerminalData);
|
|
|
|
handleSocketEvent("closeTerminal", handleCloseTerminal);
|
|
|
|
handleSocketEvent("generateCode", handleGenerateCode);
|
2024-05-06 23:34:45 -07:00
|
|
|
|
2024-05-13 23:22:06 -07:00
|
|
|
|
2024-06-28 02:39:03 -04:00
|
|
|
socket.on("disconnect", async () => {
|
|
|
|
try {
|
2024-10-24 17:10:23 -06:00
|
|
|
if (data.isOwner) {
|
|
|
|
connections[data.sandboxId]--
|
|
|
|
}
|
|
|
|
|
|
|
|
await terminalManager.closeAllTerminals()
|
|
|
|
await fileManager.closeWatchers()
|
|
|
|
|
|
|
|
if (data.isOwner && connections[data.sandboxId] <= 0) {
|
|
|
|
socket.broadcast.emit(
|
|
|
|
"disableAccess",
|
|
|
|
"The sandbox owner has disconnected."
|
|
|
|
)
|
|
|
|
}
|
2024-06-28 02:39:03 -04:00
|
|
|
} catch (e: any) {
|
2024-10-19 05:25:26 -06:00
|
|
|
console.log("Error disconnecting:", e)
|
2024-10-22 03:39:50 -06:00
|
|
|
socket.emit("error", `Error: disconnecting. ${e.message ?? e}`)
|
2024-06-28 02:39:03 -04:00
|
|
|
}
|
2024-10-19 05:25:26 -06:00
|
|
|
})
|
2024-06-28 02:39:03 -04:00
|
|
|
} catch (e: any) {
|
2024-10-19 05:25:26 -06:00
|
|
|
console.error("Error connecting:", e)
|
2024-10-22 03:39:50 -06:00
|
|
|
socket.emit("error", `Error: connection. ${e.message ?? e}`)
|
2024-06-28 02:39:03 -04:00
|
|
|
}
|
2024-10-19 05:25:26 -06:00
|
|
|
})
|
2024-04-18 16:40:08 -04:00
|
|
|
|
2024-10-19 15:16:24 -06:00
|
|
|
// Start the server
|
2024-04-18 16:40:08 -04:00
|
|
|
httpServer.listen(port, () => {
|
2024-10-19 05:25:26 -06:00
|
|
|
console.log(`Server running on port ${port}`)
|
|
|
|
})
|