264 lines
8.6 KiB
TypeScript
Raw Normal View History

import { Sandbox as E2BSandbox } from "e2b"
import { Socket } from "socket.io"
2024-10-24 23:58:28 -06:00
import { CONTAINER_TIMEOUT } from "./constants"
import { DokkuClient } from "./DokkuClient"
import { FileManager } from "./FileManager"
2024-10-24 23:58:28 -06:00
import {
2024-11-16 21:35:06 -05:00
createFileRL,
createFolderRL,
deleteFileRL,
renameFileRL,
saveFileRL,
2024-10-24 23:58:28 -06:00
} from "./ratelimit"
import { SecureGitClient } from "./SecureGitClient"
import { TerminalManager } from "./TerminalManager"
import { TFile, TFolder } from "./types"
2024-10-24 23:58:28 -06:00
import { LockManager } from "./utils"
const lockManager = new LockManager()
2024-10-24 23:58:28 -06:00
// Define a type for SocketHandler functions
2024-11-16 21:35:06 -05:00
type SocketHandler<T = Record<string, any>> = (args: T) => any
2024-10-24 23:58:28 -06:00
// Extract port number from a string
function extractPortNumber(inputString: string): number | null {
2024-11-16 21:35:06 -05:00
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-24 23:58:28 -06:00
}
type ServerContext = {
2024-11-16 21:35:06 -05:00
dokkuClient: DokkuClient | null
gitClient: SecureGitClient | null
}
2024-10-24 23:58:28 -06:00
export class Sandbox {
2024-11-16 21:35:06 -05:00
// Sandbox properties:
sandboxId: string
type: string
fileManager: FileManager | null
terminalManager: TerminalManager | null
container: E2BSandbox | null
// Server context:
dokkuClient: DokkuClient | null
gitClient: SecureGitClient | null
constructor(
sandboxId: string,
type: string,
{ dokkuClient, gitClient }: ServerContext
2024-11-16 21:35:06 -05:00
) {
// Sandbox properties:
2024-11-16 21:35:06 -05:00
this.sandboxId = sandboxId
this.type = type
this.fileManager = null
this.terminalManager = null
this.container = null
// Server context:
2024-11-16 21:35:06 -05:00
this.dokkuClient = dokkuClient
this.gitClient = gitClient
}
// Initializes the container for the sandbox environment
async initialize(
fileWatchCallback: ((files: (TFolder | TFile)[]) => void) | undefined
) {
// Acquire a lock to ensure exclusive access to the sandbox environment
await lockManager.acquireLock(this.sandboxId, async () => {
// Check if a container already exists and is running
if (this.container && (await this.container.isRunning())) {
console.log(`Found existing container ${this.sandboxId}`)
} else {
console.log("Creating container", this.sandboxId)
// 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, {
timeoutMs: CONTAINER_TIMEOUT,
})
2024-11-16 21:35:06 -05:00
}
})
// Ensure a container was successfully created
if (!this.container) throw new Error("Failed to create container")
// Initialize the terminal manager if it hasn't been set up yet
if (!this.terminalManager) {
this.terminalManager = new TerminalManager(this.container)
console.log(`Terminal manager set up for ${this.sandboxId}`)
}
2024-11-16 21:35:06 -05:00
// Initialize the file manager if it hasn't been set up yet
if (!this.fileManager) {
this.fileManager = new FileManager(
this.sandboxId,
this.container,
fileWatchCallback ?? null
)
// Initialize the file manager and emit the initial files
await this.fileManager.initialize()
}
}
// Called when the client disconnects from the Sandbox
async disconnect() {
// Close all terminals managed by the terminal manager
await this.terminalManager?.closeAllTerminals()
// 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
await this.fileManager?.closeWatchers()
// This way the file manager will be set up again if we reconnect
this.fileManager = null
}
handlers(connection: { userId: string; isOwner: boolean; socket: Socket }) {
// Handle heartbeat from a socket connection
const handleHeartbeat: SocketHandler = (_: any) => {
// Only keep the sandbox alive if the owner is still connected
if (connection.isOwner) {
this.container?.setTimeout(CONTAINER_TIMEOUT)
}
2024-10-24 23:58:28 -06:00
}
2024-11-16 21:35:06 -05:00
// Handle getting a file
const handleGetFile: SocketHandler = ({ fileId }: any) => {
return this.fileManager?.getFile(fileId)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle getting a folder
const handleGetFolder: SocketHandler = ({ folderId }: any) => {
return this.fileManager?.getFolder(folderId)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle saving a file
const handleSaveFile: SocketHandler = async ({ fileId, body }: any) => {
await saveFileRL.consume(connection.userId, 1)
return this.fileManager?.saveFile(fileId, body)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle moving a file
const handleMoveFile: SocketHandler = ({ fileId, folderId }: any) => {
return this.fileManager?.moveFile(fileId, folderId)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05: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() }
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle deploying code
const handleDeploy: SocketHandler = async (_: any) => {
if (!this.gitClient) throw Error("No git client")
if (!this.fileManager) throw Error("No file manager")
await this.gitClient.pushFiles(
await this.fileManager?.loadFileContent(),
this.sandboxId
)
2024-11-16 21:35:06 -05:00
return { success: true }
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle creating a file
const handleCreateFile: SocketHandler = async ({ name }: any) => {
await createFileRL.consume(connection.userId, 1)
return { success: await this.fileManager?.createFile(name) }
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle creating a folder
const handleCreateFolder: SocketHandler = async ({ name }: any) => {
await createFolderRL.consume(connection.userId, 1)
return { success: await this.fileManager?.createFolder(name) }
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle renaming a file
const handleRenameFile: SocketHandler = async ({
fileId,
newName,
}: any) => {
await renameFileRL.consume(connection.userId, 1)
return this.fileManager?.renameFile(fileId, newName)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle deleting a file
const handleDeleteFile: SocketHandler = async ({ fileId }: any) => {
await deleteFileRL.consume(connection.userId, 1)
return this.fileManager?.deleteFile(fileId)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle deleting a folder
const handleDeleteFolder: SocketHandler = ({ folderId }: any) => {
return this.fileManager?.deleteFolder(folderId)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle creating a terminal session
const handleCreateTerminal: SocketHandler = async ({ id }: any) => {
await lockManager.acquireLock(this.sandboxId, async () => {
await this.terminalManager?.createTerminal(
id,
(responseString: string) => {
connection.socket.emit("terminalResponse", {
id,
data: responseString,
2024-10-24 23:58:28 -06:00
})
2024-11-16 21:35:06 -05:00
const port = extractPortNumber(responseString)
if (port) {
connection.socket.emit(
"previewURL",
"https://" + this.container?.getHost(port)
)
}
}
)
})
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle resizing a terminal
const handleResizeTerminal: SocketHandler = ({ dimensions }: any) => {
this.terminalManager?.resizeTerminal(dimensions)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle sending data to a terminal
const handleTerminalData: SocketHandler = ({ id, data }: any) => {
return this.terminalManager?.sendTerminalData(id, data)
}
2024-10-24 23:58:28 -06:00
2024-11-16 21:35:06 -05:00
// Handle closing a terminal
const handleCloseTerminal: SocketHandler = ({ id }: any) => {
return this.terminalManager?.closeTerminal(id)
}
2024-10-31 10:27:23 +03:00
2024-11-16 21:35:06 -05:00
// Handle downloading files by download button
const handleDownloadFiles: SocketHandler = async () => {
if (!this.fileManager) throw Error("No file manager")
2024-10-31 10:27:23 +03:00
// Get the Base64 encoded ZIP string
const zipBase64 = await this.fileManager.getFilesForDownload()
2024-10-24 23:58:28 -06:00
return { zipBlob: zipBase64 }
2024-10-24 23:58:28 -06:00
}
2024-11-16 21:35:06 -05:00
return {
heartbeat: handleHeartbeat,
getFile: handleGetFile,
downloadFiles: handleDownloadFiles,
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,
}
}
}