2024-10-25 07:36:43 -06:00
|
|
|
import { Sandbox as E2BSandbox } from "e2b"
|
2024-10-25 19:03:13 -06:00
|
|
|
import { Socket } from "socket.io"
|
2024-10-24 23:58:28 -06:00
|
|
|
import { AIWorker } from "./AIWorker"
|
|
|
|
import { CONTAINER_TIMEOUT } from "./constants"
|
|
|
|
import { DokkuClient } from "./DokkuClient"
|
2024-10-25 06:57:28 -06:00
|
|
|
import { FileManager } from "./FileManager"
|
2024-10-24 23:58:28 -06:00
|
|
|
import {
|
|
|
|
createFileRL,
|
|
|
|
createFolderRL,
|
|
|
|
deleteFileRL,
|
|
|
|
renameFileRL,
|
|
|
|
saveFileRL,
|
|
|
|
} from "./ratelimit"
|
|
|
|
import { SecureGitClient } from "./SecureGitClient"
|
|
|
|
import { TerminalManager } from "./TerminalManager"
|
2024-11-02 13:27:11 -06:00
|
|
|
import { TFile, TFileData, TFolder } from "./types"
|
2024-10-24 23:58:28 -06:00
|
|
|
import { LockManager } from "./utils"
|
2024-10-25 06:38:25 -06:00
|
|
|
const lockManager = new LockManager()
|
|
|
|
|
2024-10-24 23:58:28 -06:00
|
|
|
// Define a type for SocketHandler functions
|
|
|
|
type SocketHandler<T = Record<string, any>> = (args: T) => any;
|
|
|
|
|
|
|
|
// Extract port number from a string
|
|
|
|
function extractPortNumber(inputString: string): number | null {
|
|
|
|
const cleanedString = inputString.replace(/\x1B\[[0-9;]*m/g, "")
|
|
|
|
const regex = /http:\/\/localhost:(\d+)/
|
|
|
|
const match = cleanedString.match(regex)
|
|
|
|
return match ? parseInt(match[1]) : null
|
|
|
|
}
|
|
|
|
|
2024-10-25 07:36:43 -06:00
|
|
|
type ServerContext = {
|
2024-10-24 23:58:28 -06:00
|
|
|
aiWorker: AIWorker;
|
|
|
|
dokkuClient: DokkuClient | null;
|
|
|
|
gitClient: SecureGitClient | null;
|
2024-10-25 06:38:25 -06:00
|
|
|
};
|
2024-10-24 23:58:28 -06:00
|
|
|
|
2024-10-25 07:36:43 -06:00
|
|
|
export class Sandbox {
|
2024-10-25 19:03:13 -06:00
|
|
|
// Sandbox properties:
|
|
|
|
sandboxId: string;
|
2024-11-02 13:27:11 -06:00
|
|
|
type: string;
|
2024-10-25 06:38:25 -06:00
|
|
|
fileManager: FileManager | null;
|
|
|
|
terminalManager: TerminalManager | null;
|
2024-10-25 07:36:43 -06:00
|
|
|
container: E2BSandbox | null;
|
2024-10-25 19:03:13 -06:00
|
|
|
// Server context:
|
2024-10-25 06:38:25 -06:00
|
|
|
dokkuClient: DokkuClient | null;
|
|
|
|
gitClient: SecureGitClient | null;
|
|
|
|
aiWorker: AIWorker;
|
|
|
|
|
2024-11-02 13:27:11 -06:00
|
|
|
constructor(sandboxId: string, type: string, { aiWorker, dokkuClient, gitClient }: ServerContext) {
|
2024-10-25 19:03:13 -06:00
|
|
|
// Sandbox properties:
|
|
|
|
this.sandboxId = sandboxId;
|
2024-11-02 13:27:11 -06:00
|
|
|
this.type = type;
|
2024-10-25 06:38:25 -06:00
|
|
|
this.fileManager = null;
|
|
|
|
this.terminalManager = null;
|
|
|
|
this.container = null;
|
2024-10-25 19:03:13 -06:00
|
|
|
// Server context:
|
2024-10-24 23:58:28 -06:00
|
|
|
this.aiWorker = aiWorker;
|
|
|
|
this.dokkuClient = dokkuClient;
|
|
|
|
this.gitClient = gitClient;
|
2024-10-25 06:38:25 -06:00
|
|
|
}
|
|
|
|
|
2024-10-25 14:14:50 -06:00
|
|
|
// Initializes the container for the sandbox environment
|
2024-10-25 19:03:13 -06:00
|
|
|
async initialize(
|
|
|
|
fileWatchCallback: ((files: (TFolder | TFile)[]) => void) | undefined
|
|
|
|
) {
|
2024-10-25 14:14:50 -06:00
|
|
|
// Acquire a lock to ensure exclusive access to the sandbox environment
|
2024-10-25 06:38:25 -06:00
|
|
|
await lockManager.acquireLock(this.sandboxId, async () => {
|
2024-10-25 14:14:50 -06:00
|
|
|
// Check if a container already exists and is running
|
2024-10-25 06:38:25 -06:00
|
|
|
if (this.container && await this.container.isRunning()) {
|
|
|
|
console.log(`Found existing container ${this.sandboxId}`)
|
|
|
|
} else {
|
|
|
|
console.log("Creating container", this.sandboxId)
|
2024-11-02 13:27:11 -06:00
|
|
|
// Create a new container with a specified template and timeout
|
|
|
|
const templateTypes = ["vanillajs", "reactjs", "nextjs", "streamlit"];
|
|
|
|
const template = templateTypes.includes(this.type)
|
|
|
|
? `gitwit-${this.type}`
|
|
|
|
: `base`;
|
|
|
|
this.container = await E2BSandbox.create(template, {
|
2024-10-25 06:38:25 -06:00
|
|
|
timeoutMs: CONTAINER_TIMEOUT,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2024-10-25 14:14:50 -06:00
|
|
|
// Ensure a container was successfully created
|
2024-10-25 06:38:25 -06:00
|
|
|
if (!this.container) throw new Error("Failed to create container")
|
|
|
|
|
2024-10-25 14:14:50 -06:00
|
|
|
// Initialize the terminal manager if it hasn't been set up yet
|
2024-10-25 06:38:25 -06:00
|
|
|
if (!this.terminalManager) {
|
|
|
|
this.terminalManager = new TerminalManager(this.container)
|
|
|
|
console.log(`Terminal manager set up for ${this.sandboxId}`)
|
|
|
|
}
|
|
|
|
|
2024-10-25 14:14:50 -06:00
|
|
|
// Initialize the file manager if it hasn't been set up yet
|
2024-10-25 06:38:25 -06:00
|
|
|
if (!this.fileManager) {
|
|
|
|
this.fileManager = new FileManager(
|
|
|
|
this.sandboxId,
|
|
|
|
this.container,
|
2024-10-25 19:03:13 -06:00
|
|
|
fileWatchCallback ?? null
|
2024-10-25 06:38:25 -06:00
|
|
|
)
|
2024-10-25 14:14:50 -06:00
|
|
|
// Initialize the file manager and emit the initial files
|
2024-10-25 19:03:13 -06:00
|
|
|
await this.fileManager.initialize()
|
2024-10-25 06:38:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 14:14:50 -06:00
|
|
|
// Called when the client disconnects from the Sandbox
|
2024-10-25 06:38:25 -06:00
|
|
|
async disconnect() {
|
2024-10-25 14:14:50 -06:00
|
|
|
// Close all terminals managed by the terminal manager
|
2024-10-25 06:38:25 -06:00
|
|
|
await this.terminalManager?.closeAllTerminals()
|
2024-10-25 14:14:50 -06:00
|
|
|
// This way the terminal manager will be set up again if we reconnect
|
|
|
|
this.terminalManager = null;
|
|
|
|
// Close all file watchers managed by the file manager
|
2024-10-25 06:38:25 -06:00
|
|
|
await this.fileManager?.closeWatchers()
|
2024-10-25 14:14:50 -06:00
|
|
|
// This way the file manager will be set up again if we reconnect
|
|
|
|
this.fileManager = null;
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
2024-10-25 19:03:13 -06:00
|
|
|
handlers(connection: { userId: string, isOwner: boolean, socket: Socket }) {
|
2024-10-24 23:58:28 -06:00
|
|
|
|
|
|
|
// Handle heartbeat from a socket connection
|
|
|
|
const handleHeartbeat: SocketHandler = (_: any) => {
|
2024-10-25 14:14:50 -06:00
|
|
|
// Only keep the sandbox alive if the owner is still connected
|
2024-10-25 19:03:13 -06:00
|
|
|
if (connection.isOwner) {
|
2024-10-25 14:14:50 -06:00
|
|
|
this.container?.setTimeout(CONTAINER_TIMEOUT)
|
|
|
|
}
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle getting a file
|
|
|
|
const handleGetFile: SocketHandler = ({ fileId }: any) => {
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.fileManager?.getFile(fileId)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle getting a folder
|
|
|
|
const handleGetFolder: SocketHandler = ({ folderId }: any) => {
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.fileManager?.getFolder(folderId)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle saving a file
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleSaveFile: SocketHandler = async ({ fileId, body }: any) => {
|
2024-10-25 19:03:13 -06:00
|
|
|
await saveFileRL.consume(connection.userId, 1);
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.fileManager?.saveFile(fileId, body)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle moving a file
|
|
|
|
const handleMoveFile: SocketHandler = ({ fileId, folderId }: any) => {
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.fileManager?.moveFile(fileId, folderId)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle listing apps
|
|
|
|
const handleListApps: SocketHandler = async (_: any) => {
|
|
|
|
if (!this.dokkuClient) throw Error("Failed to retrieve apps list: No Dokku client")
|
|
|
|
return { success: true, apps: await this.dokkuClient.listApps() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle deploying code
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleDeploy: SocketHandler = async (_: any) => {
|
|
|
|
if (!this.gitClient) throw Error("No git client")
|
|
|
|
if (!this.fileManager) throw Error("No file manager")
|
2024-10-25 06:59:27 -06:00
|
|
|
await this.gitClient.pushFiles(this.fileManager?.fileData, this.sandboxId)
|
2024-10-24 23:58:28 -06:00
|
|
|
return { success: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle creating a file
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleCreateFile: SocketHandler = async ({ name }: any) => {
|
2024-10-25 19:03:13 -06:00
|
|
|
await createFileRL.consume(connection.userId, 1);
|
2024-10-25 06:38:25 -06:00
|
|
|
return { "success": await this.fileManager?.createFile(name) }
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle creating a folder
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleCreateFolder: SocketHandler = async ({ name }: any) => {
|
2024-10-25 19:03:13 -06:00
|
|
|
await createFolderRL.consume(connection.userId, 1);
|
2024-10-25 06:38:25 -06:00
|
|
|
return { "success": await this.fileManager?.createFolder(name) }
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle renaming a file
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleRenameFile: SocketHandler = async ({ fileId, newName }: any) => {
|
2024-10-25 19:03:13 -06:00
|
|
|
await renameFileRL.consume(connection.userId, 1)
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.fileManager?.renameFile(fileId, newName)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle deleting a file
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleDeleteFile: SocketHandler = async ({ fileId }: any) => {
|
2024-10-25 19:03:13 -06:00
|
|
|
await deleteFileRL.consume(connection.userId, 1)
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.fileManager?.deleteFile(fileId)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle deleting a folder
|
|
|
|
const handleDeleteFolder: SocketHandler = ({ folderId }: any) => {
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.fileManager?.deleteFolder(folderId)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle creating a terminal session
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleCreateTerminal: SocketHandler = async ({ id }: any) => {
|
|
|
|
await lockManager.acquireLock(this.sandboxId, async () => {
|
|
|
|
await this.terminalManager?.createTerminal(id, (responseString: string) => {
|
2024-10-25 19:03:13 -06:00
|
|
|
connection.socket.emit("terminalResponse", { id, data: responseString })
|
2024-10-24 23:58:28 -06:00
|
|
|
const port = extractPortNumber(responseString)
|
|
|
|
if (port) {
|
2024-10-25 19:03:13 -06:00
|
|
|
connection.socket.emit(
|
2024-10-24 23:58:28 -06:00
|
|
|
"previewURL",
|
2024-10-25 06:38:25 -06:00
|
|
|
"https://" + this.container?.getHost(port)
|
2024-10-24 23:58:28 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle resizing a terminal
|
|
|
|
const handleResizeTerminal: SocketHandler = ({ dimensions }: any) => {
|
2024-10-25 06:38:25 -06:00
|
|
|
this.terminalManager?.resizeTerminal(dimensions)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle sending data to a terminal
|
|
|
|
const handleTerminalData: SocketHandler = ({ id, data }: any) => {
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.terminalManager?.sendTerminalData(id, data)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle closing a terminal
|
|
|
|
const handleCloseTerminal: SocketHandler = ({ id }: any) => {
|
2024-10-25 06:38:25 -06:00
|
|
|
return this.terminalManager?.closeTerminal(id)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle generating code
|
2024-10-25 06:38:25 -06:00
|
|
|
const handleGenerateCode: SocketHandler = ({ fileName, code, line, instructions }: any) => {
|
2024-10-25 19:03:13 -06:00
|
|
|
return this.aiWorker.generateCode(connection.userId, fileName, code, line, instructions)
|
2024-10-24 23:58:28 -06:00
|
|
|
}
|
|
|
|
|
2024-10-31 10:27:23 +03:00
|
|
|
// Handle downloading files by download button
|
|
|
|
const handleDownloadFiles: SocketHandler = async () => {
|
|
|
|
if (!this.fileManager) throw Error("No file manager")
|
|
|
|
|
|
|
|
// Get all files with their data through fileManager
|
|
|
|
const files = this.fileManager.fileData.map((file: TFileData) => ({
|
|
|
|
path: file.id.startsWith('/') ? file.id.slice(1) : file.id,
|
|
|
|
content: file.data
|
|
|
|
}))
|
|
|
|
|
|
|
|
return { files }
|
|
|
|
}
|
|
|
|
|
2024-10-24 23:58:28 -06:00
|
|
|
return {
|
|
|
|
"heartbeat": handleHeartbeat,
|
|
|
|
"getFile": handleGetFile,
|
2024-10-31 10:27:23 +03:00
|
|
|
"downloadFiles": handleDownloadFiles,
|
2024-10-24 23:58:28 -06:00
|
|
|
"getFolder": handleGetFolder,
|
|
|
|
"saveFile": handleSaveFile,
|
|
|
|
"moveFile": handleMoveFile,
|
|
|
|
"list": handleListApps,
|
|
|
|
"deploy": handleDeploy,
|
|
|
|
"createFile": handleCreateFile,
|
|
|
|
"createFolder": handleCreateFolder,
|
|
|
|
"renameFile": handleRenameFile,
|
|
|
|
"deleteFile": handleDeleteFile,
|
|
|
|
"deleteFolder": handleDeleteFolder,
|
|
|
|
"createTerminal": handleCreateTerminal,
|
|
|
|
"resizeTerminal": handleResizeTerminal,
|
|
|
|
"terminalData": handleTerminalData,
|
|
|
|
"closeTerminal": handleCloseTerminal,
|
|
|
|
"generateCode": handleGenerateCode,
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|