Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,702 changes: 1,660 additions & 42 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"react-markdown": "^10.1.0",
"react-router-dom": "^6.8.1",
"tailwind-merge": "^3.3.1",
"tunnelmole": "^2.4.0",
"qrcode": "^1.5.3",
"ws": "^8.14.2",
"xterm": "^5.3.0",
"xterm-addon-fit": "^0.8.0"
Expand Down
20 changes: 18 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ import { spawnClaude, abortClaudeSession } from './claude-cli.js';
import gitRoutes from './routes/git.js';
import authRoutes from './routes/auth.js';
import mcpRoutes from './routes/mcp.js';
import tunnelRoutes from './routes/tunnel.js';
import { initializeDatabase } from './database/db.js';
import { validateApiKey, authenticateToken, authenticateWebSocket } from './middleware/auth.js';
import tunnelService from './services/tunnelService.js';

// File system watcher for projects folder
let projectsWatcher = null;
Expand Down Expand Up @@ -175,6 +177,9 @@ app.use('/api/git', authenticateToken, gitRoutes);
// MCP API Routes (protected)
app.use('/api/mcp', authenticateToken, mcpRoutes);

// Tunnel API Routes (protected)
app.use('/api/tunnel', authenticateToken, tunnelRoutes);

// Static files served after API routes
app.use(express.static(path.join(__dirname, '../dist')));

Expand All @@ -185,9 +190,13 @@ app.get('/api/config', authenticateToken, (req, res) => {

console.log('Config API called - Returning host:', host, 'Protocol:', protocol);

// Get tunnel status
const tunnelStatus = tunnelService.getStatus();

res.json({
serverPort: PORT,
wsUrl: `${protocol}://${host}`
wsUrl: `${protocol}://${host}`,
tunnel: tunnelStatus
});
});

Expand Down Expand Up @@ -889,7 +898,14 @@ app.post('/api/projects/:projectName/upload-images', authenticateToken, async (r

// Serve React app for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../dist/index.html'));
if (process.env.NODE_ENV === 'development') {
// In development, redirect to Vite dev server
const vitePort = process.env.VITE_PORT || 3001;
res.redirect(`http://localhost:${vitePort}${req.url}`);
} else {
// In production, serve the built index.html
res.sendFile(path.join(__dirname, '../dist/index.html'));
}
});

// Helper function to convert permissions to rwx format
Expand Down
96 changes: 96 additions & 0 deletions server/routes/tunnel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import express from 'express';
import tunnelService from '../services/tunnelService.js';

const router = express.Router();

// Start tunnel
router.post('/start', async (req, res) => {
try {
// In development, we need to tunnel to the Vite dev server port
// In production, we tunnel to the Express server port
const isDevelopment = process.env.NODE_ENV === 'development';
const tunnelPort = isDevelopment ? (process.env.VITE_PORT || 3001) : (process.env.PORT || 3000);

console.log(`Starting tunnel in ${isDevelopment ? 'development' : 'production'} mode on port ${tunnelPort}`);

const result = await tunnelService.start(tunnelPort);

res.json({
success: true,
...result
});
} catch (error) {
console.error('Error starting tunnel:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});

// Stop tunnel
router.post('/stop', async (req, res) => {
try {
const result = await tunnelService.stop();

res.json({
success: true,
...result
});
} catch (error) {
console.error('Error stopping tunnel:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});

// Get tunnel status
router.get('/status', (req, res) => {
try {
const status = tunnelService.getStatus();

res.json({
success: true,
...status
});
} catch (error) {
console.error('Error getting tunnel status:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});

// Get shareable link
router.post('/share', (req, res) => {
try {
const url = tunnelService.getShareableUrl();

if (!url) {
return res.status(400).json({
success: false,
error: 'No active tunnel'
});
}

// Generate a simple shareable response
// In the future, we could generate QR codes or shortened URLs here
res.json({
success: true,
url: url,
shareText: `Access Claude Code UI from: ${url}`,
qrCodeData: null // Placeholder for future QR code implementation
});
} catch (error) {
console.error('Error generating share link:', error);
res.status(500).json({
success: false,
error: error.message
});
}
});

export default router;
90 changes: 90 additions & 0 deletions server/services/tunnelService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { tunnelmole } from 'tunnelmole';

class TunnelService {
constructor() {
this.tunnel = null;
this.url = null;
this.isActive = false;
this.startTime = null;
this.error = null;
}

async start(port) {
if (this.isActive) {
console.log('Tunnel already active at:', this.url);
return { url: this.url, startTime: this.startTime };
}

try {
console.log(`Starting Tunnelmole on port ${port}...`);

// Start tunnelmole
this.url = await tunnelmole({
port: port
});

this.isActive = true;
this.startTime = new Date();
this.error = null;

console.log(`Tunnelmole started successfully: ${this.url}`);

return {
url: this.url,
startTime: this.startTime,
status: 'active'
};
} catch (error) {
console.error('Failed to start Tunnelmole:', error);
this.error = error.message;
this.isActive = false;
throw error;
}
}

async stop() {
if (!this.isActive) {
console.log('Tunnel is not active');
return { status: 'stopped' };
}

try {
// Tunnelmole doesn't provide a direct stop method in the API
// The connection will be closed when the process ends
// For now, we just mark it as inactive
this.isActive = false;
this.url = null;
this.startTime = null;

console.log('Tunnel stopped');

return { status: 'stopped' };
} catch (error) {
console.error('Error stopping tunnel:', error);
throw error;
}
}

getStatus() {
return {
isActive: this.isActive,
url: this.url,
startTime: this.startTime,
error: this.error,
uptime: this.isActive && this.startTime ?
Math.floor((Date.now() - this.startTime.getTime()) / 1000) : 0
};
}

getShareableUrl() {
if (!this.url) {
return null;
}

// Return the public URL that can be shared
return this.url;
}
}

// Export singleton instance
export default new TunnelService();
Loading