-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathstart-relay-pty.js
More file actions
executable file
·195 lines (164 loc) · 6.9 KB
/
start-relay-pty.js
File metadata and controls
executable file
·195 lines (164 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
/**
* Claude-Code-Remote PTY Relay Startup Script
* Start node-pty based email command relay service
*/
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
// Check environment configuration
function checkConfig() {
const envPath = path.join(__dirname, '.env');
if (!fs.existsSync(envPath)) {
console.error('❌ Error: .env configuration file not found');
console.log('\nPlease first copy .env.example to .env and configure your email information:');
console.log(' cp .env.example .env');
console.log(' Then edit .env file to fill in your email configuration\n');
process.exit(1);
}
// Load environment variables
require('dotenv').config();
// Check required configuration
const required = ['IMAP_HOST', 'IMAP_USER', 'IMAP_PASS'];
const missing = required.filter(key => !process.env[key]);
if (missing.length > 0) {
console.error('❌ Error: Missing required environment variables:');
missing.forEach(key => console.log(` - ${key}`));
console.log('\nPlease edit .env file and fill in all required configurations\n');
process.exit(1);
}
console.log('✅ Configuration check passed');
console.log(`📧 IMAP server: ${process.env.IMAP_HOST}`);
console.log(`👤 Email account: ${process.env.IMAP_USER}`);
console.log(`🔒 Whitelist senders: ${process.env.ALLOWED_SENDERS || '(Not set, will accept all emails)'}`);
console.log(`💾 Session storage path: ${process.env.SESSION_MAP_PATH || '(Using default path)'}`);
console.log('');
}
// Create example session
function createExampleSession() {
const sessionMapPath = process.env.SESSION_MAP_PATH || path.join(__dirname, 'src/data/session-map.json');
const sessionDir = path.dirname(sessionMapPath);
// Ensure directory exists
if (!fs.existsSync(sessionDir)) {
fs.mkdirSync(sessionDir, { recursive: true });
}
// If session file doesn't exist, create an example
if (!fs.existsSync(sessionMapPath)) {
const exampleToken = 'TEST123';
const exampleSession = {
[exampleToken]: {
type: 'pty',
createdAt: Math.floor(Date.now() / 1000),
expiresAt: Math.floor((Date.now() + 24 * 60 * 60 * 1000) / 1000), // Expires after 24 hours
cwd: process.cwd(),
description: 'Test session - Include [Claude-Code-Remote #TEST123] in email subject when sending'
}
};
fs.writeFileSync(sessionMapPath, JSON.stringify(exampleSession, null, 2));
console.log(`📝 Created example session file: ${sessionMapPath}`);
console.log(`🔑 Test Token: ${exampleToken}`);
console.log(' When sending test email, include in subject: [Claude-Code-Remote #TEST123]');
console.log('');
}
}
// PID file path
const PID_FILE = path.join(__dirname, 'relay-pty.pid');
// Check if an instance is already running
function checkSingleInstance() {
if (fs.existsSync(PID_FILE)) {
try {
const oldPid = parseInt(fs.readFileSync(PID_FILE, 'utf8'));
// Check if process is actually running
process.kill(oldPid, 0);
// If no error thrown, process is still running
console.error('❌ Error: relay-pty service is already running (PID: ' + oldPid + ')');
console.log('\nIf you\'re sure the service is not running, you can delete the PID file:');
console.log(' rm ' + PID_FILE);
console.log('\nOr stop existing service:');
console.log(' kill ' + oldPid);
process.exit(1);
} catch (err) {
// Process doesn't exist, delete old PID file
fs.unlinkSync(PID_FILE);
}
}
// Write current process PID
fs.writeFileSync(PID_FILE, process.pid.toString());
}
// Clean up PID file
function cleanupPidFile() {
if (fs.existsSync(PID_FILE)) {
fs.unlinkSync(PID_FILE);
}
}
// Start service
function startService() {
// Check single instance
checkSingleInstance();
console.log('🚀 Starting Claude-Code-Remote PTY Relay service...\n');
const relayPath = path.join(__dirname, 'src/relay/relay-pty.js');
// Use node to run directly, so we can see complete log output
const relay = spawn('node', [relayPath], {
stdio: 'inherit',
env: {
...process.env,
INJECTION_MODE: 'pty'
}
});
// Handle exit
process.on('SIGINT', () => {
console.log('\n⏹️ Stopping service...');
relay.kill('SIGINT');
cleanupPidFile();
process.exit(0);
});
process.on('exit', cleanupPidFile);
process.on('SIGTERM', cleanupPidFile);
relay.on('error', (error) => {
console.error('❌ Startup failed:', error.message);
cleanupPidFile();
process.exit(1);
});
relay.on('exit', (code, signal) => {
cleanupPidFile();
if (signal) {
console.log(`\nService stopped (signal: ${signal})`);
} else if (code !== 0) {
console.error(`\nService exited abnormally (code: ${code})`);
process.exit(code);
}
});
}
// Show usage instructions
function showInstructions() {
console.log('📖 Usage instructions:');
console.log('1. When executing tasks in Claude Code, reminder emails containing Token will be sent');
console.log('2. Reply to that email with the commands to execute');
console.log('3. Supported command formats:');
console.log(' - Enter command text directly');
console.log(' - Use CMD: prefix, like "CMD: continue"');
console.log(' - Use code block wrapping, like:');
console.log(' ```');
console.log(' your command');
console.log(' ```');
console.log('4. System will automatically extract commands and inject them into corresponding Claude Code session');
console.log('\n⌨️ Press Ctrl+C to stop service\n');
console.log('━'.repeat(60) + '\n');
}
// Main function
function main() {
console.log('╔══════════════════════════════════════════════════════════╗');
console.log('║ Claude-Code-Remote PTY Relay Service ║');
console.log('║ Email Command Relay Service - node-pty based PTY mode ║');
console.log('╚══════════════════════════════════════════════════════════╝\n');
// Check configuration
checkConfig();
// Create example session
createExampleSession();
// Show usage instructions
showInstructions();
// Start service
startService();
}
// Run
main();