Initial commit

This commit is contained in:
monoadmin
2026-04-10 15:36:33 -07:00
commit d6d7338a39
134 changed files with 16232 additions and 0 deletions

16
lib/db/index.ts Normal file
View File

@@ -0,0 +1,16 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import * as schema from './schema'
// In Next.js, avoid creating multiple connections in development due to hot reload
declare global {
// eslint-disable-next-line no-var
var _pgClient: ReturnType<typeof postgres> | undefined
}
const connectionString = process.env.DATABASE_URL!
const client = globalThis._pgClient ?? postgres(connectionString)
if (process.env.NODE_ENV !== 'production') globalThis._pgClient = client
export const db = drizzle(client, { schema })

79
lib/db/schema.ts Normal file
View File

@@ -0,0 +1,79 @@
import {
pgTable,
uuid,
text,
boolean,
timestamp,
integer,
} from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'
export const users = pgTable('users', {
id: uuid('id').primaryKey().default(sql`gen_random_uuid()`),
email: text('email').unique().notNull(),
passwordHash: text('password_hash').notNull(),
fullName: text('full_name'),
company: text('company'),
role: text('role').default('user').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
})
export const machines = pgTable('machines', {
id: uuid('id').primaryKey().default(sql`gen_random_uuid()`),
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade' }).notNull(),
name: text('name').notNull(),
hostname: text('hostname'),
os: text('os'),
osVersion: text('os_version'),
agentVersion: text('agent_version'),
ipAddress: text('ip_address'),
accessKey: text('access_key').unique().notNull(),
isOnline: boolean('is_online').default(false).notNull(),
lastSeen: timestamp('last_seen', { withTimezone: true }),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
})
export const sessionCodes = pgTable('session_codes', {
id: uuid('id').primaryKey().default(sql`gen_random_uuid()`),
code: text('code').notNull(),
machineId: uuid('machine_id').references(() => machines.id, { onDelete: 'cascade' }).notNull(),
createdBy: uuid('created_by').references(() => users.id, { onDelete: 'set null' }),
expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
isActive: boolean('is_active').default(true).notNull(),
usedAt: timestamp('used_at', { withTimezone: true }),
usedBy: uuid('used_by').references(() => users.id, { onDelete: 'set null' }),
})
export const sessions = pgTable('sessions', {
id: uuid('id').primaryKey().default(sql`gen_random_uuid()`),
machineId: uuid('machine_id').references(() => machines.id, { onDelete: 'set null' }),
machineName: text('machine_name'),
viewerUserId: uuid('viewer_user_id').references(() => users.id, { onDelete: 'set null' }),
connectionType: text('connection_type'),
sessionCode: text('session_code'),
startedAt: timestamp('started_at', { withTimezone: true }).defaultNow().notNull(),
endedAt: timestamp('ended_at', { withTimezone: true }),
durationSeconds: integer('duration_seconds'),
notes: text('notes'),
})
export const invites = pgTable('invites', {
id: uuid('id').primaryKey().default(sql`gen_random_uuid()`),
token: uuid('token').unique().notNull().default(sql`gen_random_uuid()`),
email: text('email').notNull(),
createdBy: uuid('created_by').references(() => users.id, { onDelete: 'set null' }),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
expiresAt: timestamp('expires_at', { withTimezone: true })
.notNull()
.default(sql`now() + interval '7 days'`),
usedAt: timestamp('used_at', { withTimezone: true }),
usedBy: uuid('used_by').references(() => users.id, { onDelete: 'set null' }),
})
export type User = typeof users.$inferSelect
export type Machine = typeof machines.$inferSelect
export type SessionCode = typeof sessionCodes.$inferSelect
export type Session = typeof sessions.$inferSelect
export type Invite = typeof invites.$inferSelect

6
lib/utils.ts Normal file
View File

@@ -0,0 +1,6 @@
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

202
lib/webrtc/connection.ts Normal file
View File

@@ -0,0 +1,202 @@
/**
* WebRTC Connection Manager
* Handles peer-to-peer connections for remote desktop streaming
*/
export interface RTCConfig {
iceServers: RTCIceServer[]
}
export interface ConnectionEvents {
onTrack?: (stream: MediaStream) => void
onDataChannel?: (channel: RTCDataChannel) => void
onConnectionStateChange?: (state: RTCPeerConnectionState) => void
onIceCandidate?: (candidate: RTCIceCandidate | null) => void
onError?: (error: Error) => void
}
const DEFAULT_RTC_CONFIG: RTCConfig = {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:stun1.l.google.com:19302' },
{ urls: 'stun:stun2.l.google.com:19302' },
],
}
export class WebRTCConnection {
private peerConnection: RTCPeerConnection | null = null
private dataChannel: RTCDataChannel | null = null
private events: ConnectionEvents = {}
constructor(config: RTCConfig = DEFAULT_RTC_CONFIG) {
if (typeof window === 'undefined') return
this.peerConnection = new RTCPeerConnection(config)
this.setupEventHandlers()
}
private setupEventHandlers() {
if (!this.peerConnection) return
this.peerConnection.ontrack = (event) => {
this.events.onTrack?.(event.streams[0])
}
this.peerConnection.ondatachannel = (event) => {
this.dataChannel = event.channel
this.setupDataChannel()
this.events.onDataChannel?.(event.channel)
}
this.peerConnection.onconnectionstatechange = () => {
if (this.peerConnection) {
this.events.onConnectionStateChange?.(this.peerConnection.connectionState)
}
}
this.peerConnection.onicecandidate = (event) => {
this.events.onIceCandidate?.(event.candidate)
}
}
private setupDataChannel() {
if (!this.dataChannel) return
this.dataChannel.onopen = () => {
console.log('[WebRTC] Data channel opened')
}
this.dataChannel.onclose = () => {
console.log('[WebRTC] Data channel closed')
}
this.dataChannel.onerror = (error) => {
console.error('[WebRTC] Data channel error:', error)
}
}
on<K extends keyof ConnectionEvents>(event: K, handler: ConnectionEvents[K]) {
this.events[event] = handler
}
// Create offer (for viewer)
async createOffer(): Promise<RTCSessionDescriptionInit> {
if (!this.peerConnection) throw new Error('PeerConnection not initialized')
// Create data channel for input events
this.dataChannel = this.peerConnection.createDataChannel('input', {
ordered: true,
})
this.setupDataChannel()
const offer = await this.peerConnection.createOffer({
offerToReceiveVideo: true,
offerToReceiveAudio: true,
})
await this.peerConnection.setLocalDescription(offer)
return offer
}
// Set remote answer (for viewer)
async setRemoteAnswer(answer: RTCSessionDescriptionInit) {
if (!this.peerConnection) throw new Error('PeerConnection not initialized')
await this.peerConnection.setRemoteDescription(answer)
}
// Create answer (for host/agent)
async createAnswer(offer: RTCSessionDescriptionInit): Promise<RTCSessionDescriptionInit> {
if (!this.peerConnection) throw new Error('PeerConnection not initialized')
await this.peerConnection.setRemoteDescription(offer)
const answer = await this.peerConnection.createAnswer()
await this.peerConnection.setLocalDescription(answer)
return answer
}
// Add ICE candidate
async addIceCandidate(candidate: RTCIceCandidateInit) {
if (!this.peerConnection) throw new Error('PeerConnection not initialized')
await this.peerConnection.addIceCandidate(candidate)
}
// Send input event via data channel
sendInputEvent(event: InputEvent) {
if (!this.dataChannel || this.dataChannel.readyState !== 'open') return
this.dataChannel.send(JSON.stringify(event))
}
// Get connection state
getConnectionState(): RTCPeerConnectionState | null {
return this.peerConnection?.connectionState ?? null
}
// Close connection
close() {
this.dataChannel?.close()
this.peerConnection?.close()
this.dataChannel = null
this.peerConnection = null
}
}
// Input event types
export type InputEventType = 'mousemove' | 'mousedown' | 'mouseup' | 'keydown' | 'keyup' | 'scroll'
export interface InputEvent {
type: InputEventType
timestamp: number
data: MouseEventData | KeyEventData | ScrollEventData
}
export interface MouseEventData {
x: number
y: number
button?: number
}
export interface KeyEventData {
key: string
code: string
altKey?: boolean
ctrlKey?: boolean
shiftKey?: boolean
metaKey?: boolean
}
export interface ScrollEventData {
deltaX: number
deltaY: number
}
// Helper to create input events
export function createMouseEvent(type: 'mousemove' | 'mousedown' | 'mouseup', x: number, y: number, button?: number): InputEvent {
return {
type,
timestamp: Date.now(),
data: { x, y, button },
}
}
export function createKeyEvent(type: 'keydown' | 'keyup', event: KeyboardEvent): InputEvent {
return {
type,
timestamp: Date.now(),
data: {
key: event.key,
code: event.code,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
shiftKey: event.shiftKey,
metaKey: event.metaKey,
},
}
}
export function createScrollEvent(deltaX: number, deltaY: number): InputEvent {
return {
type: 'scroll',
timestamp: Date.now(),
data: { deltaX, deltaY },
}
}