v1.6.0 #221

Merged
LukeGus merged 74 commits from dev-1.6.0 into main 2025-09-12 19:42:00 +00:00
21 changed files with 658 additions and 669 deletions
Showing only changes of commit 4c33b43a0f - Show all commits

View File

@@ -7,7 +7,7 @@
"dev": "vite",
"build": "vite build",
"build:backend": "tsc -p tsconfig.node.json",
"dev:backend": "tsc -p tsconfig.node.json && node ./dist/backend/starter.js",
"dev:backend": "tsc -p tsconfig.node.json && node ./dist/backend/backend/starter.js",
"lint": "eslint .",
"preview": "vite preview"
},

View File

@@ -1,6 +1,6 @@
{
"credentials": {
"credentialsManager": "Credentials Manager",
"credentialsViewer": "Credentials Viewer",
"manageYourSSHCredentials": "Manage your SSH credentials securely",
"addCredential": "Add Credential",
"createCredential": "Create Credential",

View File

@@ -35,23 +35,20 @@ class GitHubCache {
timestamp: now,
expiresAt: now + this.CACHE_DURATION
});
databaseLogger.debug(`Cache entry set`, { operation: 'cache_set', key, expiresIn: this.CACHE_DURATION });
// Cache entry set
}
get(key: string): any | null {
const entry = this.cache.get(key);
if (!entry) {
databaseLogger.debug(`Cache miss`, { operation: 'cache_get', key });
return null;
}
if (Date.now() > entry.expiresAt) {
this.cache.delete(key);
databaseLogger.debug(`Cache entry expired`, { operation: 'cache_get', key, expired: true });
return null;
}
databaseLogger.debug(`Cache hit`, { operation: 'cache_get', key, age: Date.now() - entry.timestamp });
return entry.data;
}
}
@@ -83,7 +80,6 @@ interface GitHubRelease {
async function fetchGitHubAPI(endpoint: string, cacheKey: string): Promise<any> {
const cachedData = githubCache.get(cacheKey);
if (cachedData) {
databaseLogger.debug(`Using cached GitHub API data`, { operation: 'github_api', endpoint, cached: true });
return {
data: cachedData,
cached: true,
@@ -92,7 +88,6 @@ async function fetchGitHubAPI(endpoint: string, cacheKey: string): Promise<any>
}
try {
databaseLogger.info(`Fetching from GitHub API`, { operation: 'github_api', endpoint });
const response = await fetch(`${GITHUB_API_BASE}${endpoint}`, {
headers: {
'Accept': 'application/vnd.github+json',
@@ -108,7 +103,6 @@ async function fetchGitHubAPI(endpoint: string, cacheKey: string): Promise<any>
const data = await response.json();
githubCache.set(cacheKey, data);
databaseLogger.success(`GitHub API data fetched successfully`, { operation: 'github_api', endpoint, dataSize: JSON.stringify(data).length });
return {
data: data,
cached: false
@@ -122,12 +116,10 @@ async function fetchGitHubAPI(endpoint: string, cacheKey: string): Promise<any>
app.use(bodyParser.json());
app.get('/health', (req, res) => {
apiLogger.info(`Health check requested`, { operation: 'health_check' });
res.json({status: 'ok'});
});
app.get('/version', async (req, res) => {
apiLogger.info(`Version check requested`, { operation: 'version_check' });
let localVersion = process.env.VERSION;
if (!localVersion) {
@@ -135,7 +127,6 @@ app.get('/version', async (req, res) => {
const packagePath = path.resolve(process.cwd(), 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
localVersion = packageJson.version;
databaseLogger.debug(`Version read from package.json`, { operation: 'version_check', localVersion });
} catch (error) {
databaseLogger.error('Failed to read version from package.json', error, { operation: 'version_check' });
}
@@ -163,13 +154,6 @@ app.get('/version', async (req, res) => {
}
const isUpToDate = localVersion === remoteVersion;
databaseLogger.info(`Version comparison completed`, {
operation: 'version_check',
localVersion,
remoteVersion,
isUpToDate,
cached: releaseData.cached
});
const response = {
status: isUpToDate ? 'up_to_date' : 'requires_update',
@@ -198,7 +182,7 @@ app.get('/releases/rss', async (req, res) => {
const per_page = Math.min(parseInt(req.query.per_page as string) || 20, 100);
const cacheKey = `releases_rss_${page}_${per_page}`;
apiLogger.info(`RSS releases requested`, { operation: 'rss_releases', page, per_page });
// RSS releases requested
const releasesData = await fetchGitHubAPI(
`/repos/${REPO_OWNER}/${REPO_NAME}/releases?page=${page}&per_page=${per_page}`,
@@ -235,13 +219,7 @@ app.get('/releases/rss', async (req, res) => {
cache_age: releasesData.cache_age
};
databaseLogger.success(`RSS releases generated successfully`, {
operation: 'rss_releases',
itemCount: rssItems.length,
page,
per_page,
cached: releasesData.cached
});
// RSS releases generated successfully
res.json(response);
} catch (error) {

View File

@@ -165,13 +165,6 @@ const migrateSchema = () => {
addColumnIfNotExists('users', 'issuer_url', 'TEXT');
addColumnIfNotExists('users', 'authorization_url', 'TEXT');
addColumnIfNotExists('users', 'token_url', 'TEXT');
try {
databaseLogger.debug('Attempting to drop redirect_uri column', { operation: 'schema_migration', table: 'users' });
sqlite.prepare(`ALTER TABLE users DROP COLUMN redirect_uri`).run();
databaseLogger.success('redirect_uri column dropped', { operation: 'schema_migration', table: 'users' });
} catch (e) {
databaseLogger.debug('redirect_uri column does not exist or could not be dropped', { operation: 'schema_migration', table: 'users' });
}
addColumnIfNotExists('users', 'identifier_path', 'TEXT');
addColumnIfNotExists('users', 'name_path', 'TEXT');

View File

@@ -63,11 +63,8 @@ async function fetchAlertsFromGitHub(): Promise<TermixAlert[]> {
const cacheKey = 'termix_alerts';
const cachedData = alertCache.get(cacheKey);
if (cachedData) {
authLogger.info('Returning cached alerts from GitHub', { operation: 'alerts_fetch', cacheKey, alertCount: cachedData.length });
return cachedData;
}
authLogger.info('Fetching alerts from GitHub', { operation: 'alerts_fetch', url: `${GITHUB_RAW_BASE}/${REPO_OWNER}/${REPO_NAME}/${ALERTS_FILE}` });
try {
const url = `${GITHUB_RAW_BASE}/${REPO_OWNER}/${REPO_NAME}/${ALERTS_FILE}`;
@@ -84,7 +81,6 @@ async function fetchAlertsFromGitHub(): Promise<TermixAlert[]> {
}
const alerts: TermixAlert[] = await response.json() as TermixAlert[];
authLogger.info('Successfully fetched alerts from GitHub', { operation: 'alerts_fetch', totalAlerts: alerts.length });
const now = new Date();
@@ -94,9 +90,7 @@ async function fetchAlertsFromGitHub(): Promise<TermixAlert[]> {
return isValid;
});
authLogger.info('Filtered alerts by expiry date', { operation: 'alerts_fetch', totalAlerts: alerts.length, validAlerts: validAlerts.length });
alertCache.set(cacheKey, validAlerts);
authLogger.success('Alerts cached successfully', { operation: 'alerts_fetch', alertCount: validAlerts.length });
return validAlerts;
} catch (error) {
authLogger.error('Failed to fetch alerts from GitHub', { operation: 'alerts_fetch', error: error instanceof Error ? error.message : 'Unknown error' });

View File

@@ -76,16 +76,12 @@ router.get('/db/host/internal', async (req: Request, res: Response) => {
// POST /ssh/host
router.post('/db/host', authenticateJWT, upload.single('key'), async (req: Request, res: Response) => {
const userId = (req as any).userId;
sshLogger.info('SSH host creation request received', { operation: 'host_create', userId, contentType: req.headers['content-type'] });
let hostData: any;
if (req.headers['content-type']?.includes('multipart/form-data')) {
sshLogger.info('Processing multipart form data for SSH host creation', { operation: 'host_create', userId });
if (req.body.data) {
try {
hostData = JSON.parse(req.body.data);
sshLogger.info('Successfully parsed JSON data from multipart request', { operation: 'host_create', userId, hasKey: !!req.file });
} catch (err) {
sshLogger.warn('Invalid JSON data in multipart request', { operation: 'host_create', userId, error: err });
return res.status(400).json({error: 'Invalid JSON data'});
@@ -97,11 +93,9 @@ router.post('/db/host', authenticateJWT, upload.single('key'), async (req: Reque
if (req.file) {
hostData.key = req.file.buffer.toString('utf8');
sshLogger.info('SSH key file processed from multipart request', { operation: 'host_create', userId, keySize: req.file.size });
}
} else {
hostData = req.body;
sshLogger.info('Processing JSON data for SSH host creation', { operation: 'host_create', userId });
}
const {
@@ -147,11 +141,11 @@ router.post('/db/host', authenticateJWT, upload.single('key'), async (req: Reque
username,
authType: effectiveAuthType,
credentialId: credentialId || null,
pin: !pin ? 1 : 0,
enableTerminal: !enableTerminal ? 1 : 0,
enableTunnel: !enableTunnel ? 1 : 0,
pin: pin ? 1 : 0,
enableTerminal: enableTerminal ? 1 : 0,
enableTunnel: enableTunnel ? 1 : 0,
tunnelConnections: Array.isArray(tunnelConnections) ? JSON.stringify(tunnelConnections) : null,
enableFileManager: !!enableFileManager ? 1 : 0,
enableFileManager: enableFileManager ? 1 : 0,
defaultPath: defaultPath || null,
};
@@ -160,20 +154,35 @@ router.post('/db/host', authenticateJWT, upload.single('key'), async (req: Reque
sshDataObj.key = null;
sshDataObj.keyPassword = null;
sshDataObj.keyType = null;
sshLogger.info('SSH host configured for password authentication', { operation: 'host_create', userId, name, ip, port });
} else if (effectiveAuthType === 'key') {
sshDataObj.key = key || null;
sshDataObj.keyPassword = keyPassword || null;
sshDataObj.keyType = keyType;
sshDataObj.password = null;
sshLogger.info('SSH host configured for key authentication', { operation: 'host_create', userId, name, ip, port, keyType });
}
try {
sshLogger.info('Attempting to save SSH host to database', { operation: 'host_create', userId, name, ip, port, authType: effectiveAuthType });
await db.insert(sshData).values(sshDataObj);
sshLogger.success('SSH host created successfully', { operation: 'host_create', userId, name, ip, port, authType: effectiveAuthType, enableTerminal, enableTunnel, enableFileManager });
res.json({message: 'SSH data created'});
const result = await db.insert(sshData).values(sshDataObj).returning();
if (result.length === 0) {
sshLogger.warn('No host returned after creation', { operation: 'host_create', userId, name, ip, port });
return res.status(500).json({error: 'Failed to create host'});
}
const createdHost = result[0];
const baseHost = {
...createdHost,
tags: typeof createdHost.tags === 'string' ? (createdHost.tags ? createdHost.tags.split(',').filter(Boolean) : []) : [],
pin: !!createdHost.pin,
enableTerminal: !!createdHost.enableTerminal,
enableTunnel: !!createdHost.enableTunnel,
tunnelConnections: createdHost.tunnelConnections ? JSON.parse(createdHost.tunnelConnections) : [],
enableFileManager: !!createdHost.enableFileManager,
};
const resolvedHost = await resolveHostCredentials(baseHost) || baseHost;
res.json(resolvedHost);
} catch (err) {
sshLogger.error('Failed to save SSH host to database', err, { operation: 'host_create', userId, name, ip, port, authType: effectiveAuthType });
res.status(500).json({error: 'Failed to save SSH data'});
@@ -185,16 +194,12 @@ router.post('/db/host', authenticateJWT, upload.single('key'), async (req: Reque
router.put('/db/host/:id', authenticateJWT, upload.single('key'), async (req: Request, res: Response) => {
const hostId = req.params.id;
const userId = (req as any).userId;
sshLogger.info('SSH host update request received', { operation: 'host_update', hostId: parseInt(hostId), userId, contentType: req.headers['content-type'] });
let hostData: any;
if (req.headers['content-type']?.includes('multipart/form-data')) {
sshLogger.info('Processing multipart form data for SSH host update', { operation: 'host_update', hostId: parseInt(hostId), userId });
if (req.body.data) {
try {
hostData = JSON.parse(req.body.data);
sshLogger.info('Successfully parsed JSON data from multipart request', { operation: 'host_update', hostId: parseInt(hostId), userId, hasKey: !!req.file });
} catch (err) {
sshLogger.warn('Invalid JSON data in multipart request', { operation: 'host_update', hostId: parseInt(hostId), userId, error: err });
return res.status(400).json({error: 'Invalid JSON data'});
@@ -206,11 +211,9 @@ router.put('/db/host/:id', authenticateJWT, upload.single('key'), async (req: Re
if (req.file) {
hostData.key = req.file.buffer.toString('utf8');
sshLogger.info('SSH key file processed from multipart request', { operation: 'host_update', hostId: parseInt(hostId), userId, keySize: req.file.size });
}
} else {
hostData = req.body;
sshLogger.info('Processing JSON data for SSH host update', { operation: 'host_update', hostId: parseInt(hostId), userId });
}
const {
@@ -256,11 +259,11 @@ router.put('/db/host/:id', authenticateJWT, upload.single('key'), async (req: Re
username,
authType: effectiveAuthType,
credentialId: credentialId || null,
pin: !pin ? 1 : 0,
enableTerminal: !enableTerminal ? 1 : 0,
enableTunnel: !enableTunnel ? 1 : 0,
pin: pin ? 1 : 0,
enableTerminal: enableTerminal ? 1 : 0,
enableTunnel: enableTunnel ? 1 : 0,
tunnelConnections: Array.isArray(tunnelConnections) ? JSON.stringify(tunnelConnections) : null,
enableFileManager: !enableFileManager ? 1 : 0,
enableFileManager: enableFileManager ? 1 : 0,
defaultPath: defaultPath || null,
};
@@ -271,7 +274,6 @@ router.put('/db/host/:id', authenticateJWT, upload.single('key'), async (req: Re
sshDataObj.key = null;
sshDataObj.keyPassword = null;
sshDataObj.keyType = null;
sshLogger.info('SSH host update configured for password authentication', { operation: 'host_update', hostId: parseInt(hostId), userId, name, ip, port });
} else if (effectiveAuthType === 'key') {
if (key) {
sshDataObj.key = key;
@@ -283,16 +285,37 @@ router.put('/db/host/:id', authenticateJWT, upload.single('key'), async (req: Re
sshDataObj.keyType = keyType;
}
sshDataObj.password = null;
sshLogger.info('SSH host update configured for key authentication', { operation: 'host_update', hostId: parseInt(hostId), userId, name, ip, port, keyType });
}
try {
sshLogger.info('Attempting to update SSH host in database', { operation: 'host_update', hostId: parseInt(hostId), userId, name, ip, port, authType: effectiveAuthType });
await db.update(sshData)
.set(sshDataObj)
.where(and(eq(sshData.id, Number(hostId)), eq(sshData.userId, userId)));
sshLogger.success('SSH host updated successfully', { operation: 'host_update', hostId: parseInt(hostId), userId, name, ip, port, authType: effectiveAuthType, enableTerminal, enableTunnel, enableFileManager });
res.json({message: 'SSH data updated'});
const updatedHosts = await db
.select()
.from(sshData)
.where(and(eq(sshData.id, Number(hostId)), eq(sshData.userId, userId)));
if (updatedHosts.length === 0) {
sshLogger.warn('Updated host not found after update', { operation: 'host_update', hostId: parseInt(hostId), userId });
return res.status(404).json({error: 'Host not found after update'});
}
const updatedHost = updatedHosts[0];
const baseHost = {
...updatedHost,
tags: typeof updatedHost.tags === 'string' ? (updatedHost.tags ? updatedHost.tags.split(',').filter(Boolean) : []) : [],
pin: !!updatedHost.pin,
enableTerminal: !!updatedHost.enableTerminal,
enableTunnel: !!updatedHost.enableTunnel,
tunnelConnections: updatedHost.tunnelConnections ? JSON.parse(updatedHost.tunnelConnections) : [],
enableFileManager: !!updatedHost.enableFileManager,
};
const resolvedHost = await resolveHostCredentials(baseHost) || baseHost;
res.json(resolvedHost);
} catch (err) {
sshLogger.error('Failed to update SSH host in database', err, { operation: 'host_update', hostId: parseInt(hostId), userId, name, ip, port, authType: effectiveAuthType });
res.status(500).json({error: 'Failed to update SSH data'});
@@ -303,19 +326,16 @@ router.put('/db/host/:id', authenticateJWT, upload.single('key'), async (req: Re
// GET /ssh/host
router.get('/db/host', authenticateJWT, async (req: Request, res: Response) => {
const userId = (req as any).userId;
sshLogger.info('SSH hosts fetch request received', { operation: 'host_fetch', userId });
if (!isNonEmptyString(userId)) {
sshLogger.warn('Invalid userId for SSH data fetch', { operation: 'host_fetch', userId });
return res.status(400).json({error: 'Invalid userId'});
}
try {
sshLogger.info('Fetching SSH hosts from database', { operation: 'host_fetch', userId });
const data = await db
.select()
.from(sshData)
.where(eq(sshData.userId, userId));
sshLogger.info('Processing SSH hosts and resolving credentials', { operation: 'host_fetch', userId, hostCount: data.length });
const result = await Promise.all(data.map(async (row: any) => {
const baseHost = {
...row,
@@ -330,7 +350,6 @@ router.get('/db/host', authenticateJWT, async (req: Request, res: Response) => {
return await resolveHostCredentials(baseHost) || baseHost;
}));
sshLogger.success('SSH hosts fetched successfully', { operation: 'host_fetch', userId, hostCount: result.length });
res.json(result);
} catch (err) {
sshLogger.error('Failed to fetch SSH hosts from database', err, { operation: 'host_fetch', userId });
@@ -343,14 +362,12 @@ router.get('/db/host', authenticateJWT, async (req: Request, res: Response) => {
router.get('/db/host/:id', authenticateJWT, async (req: Request, res: Response) => {
const hostId = req.params.id;
const userId = (req as any).userId;
sshLogger.info('SSH host fetch by ID request received', { operation: 'host_fetch_by_id', hostId: parseInt(hostId), userId });
if (!isNonEmptyString(userId) || !hostId) {
sshLogger.warn('Invalid userId or hostId for SSH host fetch by ID', { operation: 'host_fetch_by_id', hostId: parseInt(hostId), userId });
return res.status(400).json({error: 'Invalid userId or hostId'});
}
try {
sshLogger.info('Fetching SSH host by ID from database', { operation: 'host_fetch_by_id', hostId: parseInt(hostId), userId });
const data = await db
.select()
.from(sshData)
@@ -372,7 +389,6 @@ router.get('/db/host/:id', authenticateJWT, async (req: Request, res: Response)
enableFileManager: !!host.enableFileManager,
};
sshLogger.success('SSH host fetched by ID successfully', { operation: 'host_fetch_by_id', hostId: parseInt(hostId), userId, hostName: result.name });
res.json(await resolveHostCredentials(result) || result);
} catch (err) {
sshLogger.error('Failed to fetch SSH host by ID from database', err, { operation: 'host_fetch_by_id', hostId: parseInt(hostId), userId });
@@ -385,17 +401,14 @@ router.get('/db/host/:id', authenticateJWT, async (req: Request, res: Response)
router.delete('/db/host/:id', authenticateJWT, async (req: Request, res: Response) => {
const userId = (req as any).userId;
const hostId = req.params.id;
sshLogger.info('SSH host deletion request received', { operation: 'host_delete', hostId: parseInt(hostId), userId });
if (!isNonEmptyString(userId) || !hostId) {
sshLogger.warn('Invalid userId or hostId for SSH host delete', { operation: 'host_delete', hostId: parseInt(hostId), userId });
return res.status(400).json({error: 'Invalid userId or id'});
}
try {
sshLogger.info('Attempting to delete SSH host from database', { operation: 'host_delete', hostId: parseInt(hostId), userId });
const result = await db.delete(sshData)
.where(and(eq(sshData.id, Number(hostId)), eq(sshData.userId, userId)));
sshLogger.success('SSH host deleted successfully', { operation: 'host_delete', hostId: parseInt(hostId), userId });
res.json({message: 'SSH host deleted'});
} catch (err) {
sshLogger.error('Failed to delete SSH host from database', err, { operation: 'host_delete', hostId: parseInt(hostId), userId });
@@ -722,4 +735,66 @@ async function resolveHostCredentials(host: any): Promise<any> {
}
}
// Route: Rename folder (requires JWT)
// PUT /ssh/db/folders/rename
router.put('/folders/rename', authenticateJWT, async (req: Request, res: Response) => {
const userId = (req as any).userId;
const { oldName, newName } = req.body;
if (!isNonEmptyString(userId) || !oldName || !newName) {
sshLogger.warn('Invalid data for folder rename');
return res.status(400).json({error: 'Old name and new name are required'});
}
if (oldName === newName) {
return res.json({message: 'Folder name unchanged'});
}
try {
// Update all hosts with the old folder name
const updatedHosts = await db
.update(sshData)
.set({
folder: newName,
updatedAt: new Date().toISOString()
})
.where(and(
eq(sshData.userId, userId),
eq(sshData.folder, oldName)
))
.returning();
// Update all credentials with the old folder name
const updatedCredentials = await db
.update(sshCredentials)
.set({
folder: newName,
updatedAt: new Date().toISOString()
})
.where(and(
eq(sshCredentials.userId, userId),
eq(sshCredentials.folder, oldName)
))
.returning();
sshLogger.success('Folder renamed successfully', {
operation: 'folder_rename',
userId,
oldName,
newName,
updatedHosts: updatedHosts.length,
updatedCredentials: updatedCredentials.length
});
res.json({
message: 'Folder renamed successfully',
updatedHosts: updatedHosts.length,
updatedCredentials: updatedCredentials.length
});
} catch (err) {
sshLogger.error('Failed to rename folder', err, { operation: 'folder_rename', userId, oldName, newName });
res.status(500).json({error: 'Failed to rename folder'});
}
});
export default router;

View File

@@ -119,7 +119,7 @@ function authenticateJWT(req: Request, res: Response, next: NextFunction) {
try {
const payload = jwt.verify(token, jwtSecret) as JWTPayload;
(req as any).userId = payload.userId;
authLogger.debug('JWT authentication successful', { operation: 'auth', userId: payload.userId, method: req.method, url: req.url });
// JWT authentication successful
next();
} catch (err) {
authLogger.warn('Invalid or expired token', { operation: 'auth', method: req.method, url: req.url, error: err });

View File

@@ -49,7 +49,7 @@ function scheduleSessionCleanup(sessionId: string) {
app.post('/ssh/file_manager/ssh/connect', async (req, res) => {
const {sessionId, hostId, ip, port, username, password, sshKey, keyPassword, authType, credentialId, userId} = req.body;
fileLogger.info('File manager SSH connection request received', { operation: 'file_connect', sessionId, hostId, ip, port, username, authType, hasCredentialId: !!credentialId });
// Connection request received
if (!sessionId || !ip || !username || !port) {
fileLogger.warn('Missing SSH connection parameters for file manager', { operation: 'file_connect', sessionId, hasIp: !!ip, hasUsername: !!username, hasPort: !!port });
@@ -57,14 +57,12 @@ app.post('/ssh/file_manager/ssh/connect', async (req, res) => {
}
if (sshSessions[sessionId]?.isConnected) {
fileLogger.info('Cleaning up existing SSH session', { operation: 'file_connect', sessionId });
cleanupSession(sessionId);
}
const client = new SSHClient();
let resolvedCredentials = {password, sshKey, keyPassword, authType};
if (credentialId && hostId && userId) {
fileLogger.info('Resolving credentials from database for file manager', { operation: 'file_connect', sessionId, hostId, credentialId, userId });
try {
const credentials = await db
.select()
@@ -82,15 +80,14 @@ app.post('/ssh/file_manager/ssh/connect', async (req, res) => {
keyPassword: credential.keyPassword,
authType: credential.authType
};
fileLogger.success('Credentials resolved successfully for file manager', { operation: 'file_connect', sessionId, hostId, credentialId, authType: credential.authType });
} else {
fileLogger.warn('No credentials found in database for file manager', { operation: 'file_connect', sessionId, hostId, credentialId });
fileLogger.warn('No credentials found in database for file manager', { operation: 'file_connect', sessionId, hostId, credentialId, userId });
}
} catch (error) {
fileLogger.warn('Failed to resolve credentials from database for file manager', { operation: 'file_connect', sessionId, hostId, credentialId, error: error instanceof Error ? error.message : 'Unknown error' });
}
} else {
fileLogger.info('Using direct credentials for file manager connection', { operation: 'file_connect', sessionId, hostId, authType });
} else if (credentialId && hostId) {
fileLogger.warn('Missing userId for credential resolution in file manager', { operation: 'file_connect', sessionId, hostId, credentialId, hasUserId: !!userId });
}
const config: any = {
@@ -137,7 +134,6 @@ app.post('/ssh/file_manager/ssh/connect', async (req, res) => {
};
if (resolvedCredentials.sshKey && resolvedCredentials.sshKey.trim()) {
fileLogger.info('Configuring SSH key authentication for file manager', { operation: 'file_connect', sessionId, hostId, hasKeyPassword: !!resolvedCredentials.keyPassword });
try {
if (!resolvedCredentials.sshKey.includes('-----BEGIN') || !resolvedCredentials.sshKey.includes('-----END')) {
throw new Error('Invalid private key format');
@@ -149,13 +145,11 @@ app.post('/ssh/file_manager/ssh/connect', async (req, res) => {
if (resolvedCredentials.keyPassword) config.passphrase = resolvedCredentials.keyPassword;
fileLogger.success('SSH key authentication configured successfully for file manager', { operation: 'file_connect', sessionId, hostId });
} catch (keyError) {
fileLogger.error('SSH key format error for file manager', { operation: 'file_connect', sessionId, hostId, error: keyError.message });
return res.status(400).json({error: 'Invalid SSH key format'});
}
} else if (resolvedCredentials.password && resolvedCredentials.password.trim()) {
fileLogger.info('Configuring password authentication for file manager', { operation: 'file_connect', sessionId, hostId });
config.password = resolvedCredentials.password;
} else {
fileLogger.warn('No authentication method provided for file manager', { operation: 'file_connect', sessionId, hostId });
@@ -167,7 +161,6 @@ app.post('/ssh/file_manager/ssh/connect', async (req, res) => {
client.on('ready', () => {
if (responseSent) return;
responseSent = true;
fileLogger.success('SSH connection established for file manager', { operation: 'file_connect', sessionId, hostId, ip, port, username, authType: resolvedCredentials.authType });
sshSessions[sessionId] = {client, isConnected: true, lastActive: Date.now()};
res.json({status: 'success', message: 'SSH connection established'});
});
@@ -377,7 +370,6 @@ app.post('/ssh/file_manager/ssh/writeFile', (req, res) => {
writeStream.on('finish', () => {
if (hasError || hasFinished) return;
hasFinished = true;
fileLogger.success(`File written successfully via SFTP: ${filePath}`);
if (!res.headersSent) {
res.json({message: 'File written successfully', path: filePath});
}
@@ -386,7 +378,6 @@ app.post('/ssh/file_manager/ssh/writeFile', (req, res) => {
writeStream.on('close', () => {
if (hasError || hasFinished) return;
hasFinished = true;
fileLogger.success(`File written successfully via SFTP: ${filePath}`);
if (!res.headersSent) {
res.json({message: 'File written successfully', path: filePath});
}
@@ -440,7 +431,6 @@ app.post('/ssh/file_manager/ssh/writeFile', (req, res) => {
if (outputData.includes('SUCCESS')) {
fileLogger.success(`File written successfully via fallback: ${filePath}`);
if (!res.headersSent) {
res.json({message: 'File written successfully', path: filePath});
}
@@ -536,8 +526,6 @@ app.post('/ssh/file_manager/ssh/uploadFile', (req, res) => {
writeStream.on('finish', () => {
if (hasError || hasFinished) return;
hasFinished = true;
fileLogger.success(`File uploaded successfully via SFTP: ${fullPath}`);
if (!res.headersSent) {
res.json({message: 'File uploaded successfully', path: fullPath});
}
@@ -546,8 +534,6 @@ app.post('/ssh/file_manager/ssh/uploadFile', (req, res) => {
writeStream.on('close', () => {
if (hasError || hasFinished) return;
hasFinished = true;
fileLogger.success(`File uploaded successfully via SFTP: ${fullPath}`);
if (!res.headersSent) {
res.json({message: 'File uploaded successfully', path: fullPath});
}

View File

@@ -101,8 +101,6 @@ async function fetchHostById(id: number): Promise<SSHHostWithCredentials | undef
async function resolveHostCredentials(host: any): Promise<SSHHostWithCredentials | undefined> {
try {
statsLogger.info('Resolving credentials for host', { operation: 'host_credential_resolve', hostId: host.id, hostName: host.name, hasCredentialId: !!host.credentialId });
const baseHost: any = {
id: host.id,
name: host.name,
@@ -124,7 +122,6 @@ async function resolveHostCredentials(host: any): Promise<SSHHostWithCredentials
};
if (host.credentialId) {
statsLogger.info('Fetching credentials from database', { operation: 'host_credential_resolve', hostId: host.id, credentialId: host.credentialId, userId: host.userId });
try {
const credentials = await db
.select()
@@ -152,6 +149,7 @@ async function resolveHostCredentials(host: any): Promise<SSHHostWithCredentials
if (credential.keyType) {
baseHost.keyType = credential.keyType;
}
} else {
statsLogger.warn(`Credential ${host.credentialId} not found for host ${host.id}, using legacy data`);
addLegacyCredentials(baseHost, host);
@@ -437,23 +435,19 @@ function tcpPing(host: string, port: number, timeoutMs = 5000): Promise<boolean>
}
async function pollStatusesOnce(): Promise<void> {
statsLogger.info('Starting status polling for all hosts', { operation: 'status_poll' });
const hosts = await fetchAllHosts();
if (hosts.length === 0) {
statsLogger.warn('No hosts retrieved for status polling', { operation: 'status_poll' });
return;
}
statsLogger.info('Polling status for hosts', { operation: 'status_poll', hostCount: hosts.length, hostIds: hosts.map(h => h.id) });
const now = new Date().toISOString();
const checks = hosts.map(async (h) => {
statsLogger.info('Checking host status', { operation: 'status_poll', hostId: h.id, hostName: h.name, ip: h.ip, port: h.port });
const isOnline = await tcpPing(h.ip, h.port, 5000);
const now = new Date().toISOString();
const statusEntry: StatusEntry = {status: isOnline ? 'online' : 'offline', lastChecked: now};
hostStatuses.set(h.id, statusEntry);
statsLogger.info('Host status check completed', { operation: 'status_poll', hostId: h.id, hostName: h.name, status: isOnline ? 'online' : 'offline' });
return isOnline;
});

View File

@@ -21,7 +21,6 @@ wss.on('connection', (ws: WebSocket) => {
ws.on('close', () => {
sshLogger.info('WebSocket connection closed', { operation: 'websocket_disconnect' });
cleanupSSH();
});
@@ -53,7 +52,6 @@ wss.on('connection', (ws: WebSocket) => {
break;
case 'disconnect':
sshLogger.info('SSH disconnect requested', { operation: 'ssh_disconnect' });
cleanupSSH();
break;
@@ -127,14 +125,14 @@ wss.on('connection', (ws: WebSocket) => {
}, 60000);
let resolvedCredentials = {password, key, keyPassword, keyType, authType};
if (credentialId && id) {
if (credentialId && id && hostConfig.userId) {
try {
const credentials = await db
.select()
.from(sshCredentials)
.where(and(
eq(sshCredentials.id, credentialId),
eq(sshCredentials.userId, hostConfig.userId || '')
eq(sshCredentials.userId, hostConfig.userId)
));
if (credentials.length > 0) {
@@ -146,15 +144,18 @@ wss.on('connection', (ws: WebSocket) => {
keyType: credential.keyType,
authType: credential.authType
};
} else {
sshLogger.warn(`No credentials found for host ${id}`, { operation: 'ssh_credentials', hostId: id, credentialId, userId: hostConfig.userId });
}
} catch (error) {
sshLogger.warn(`Failed to resolve credentials for host ${id}`, { operation: 'ssh_credentials', hostId: id, credentialId, error: error instanceof Error ? error.message : 'Unknown error' });
}
} else if (credentialId && id) {
sshLogger.warn('Missing userId for credential resolution in terminal', { operation: 'ssh_credentials', hostId: id, credentialId, hasUserId: !!hostConfig.userId });
}
sshConn.on('ready', () => {
clearTimeout(connectionTimeout);
sshLogger.success('SSH connection established', { operation: 'ssh_connect', hostId: id, ip, port, username, authType: resolvedCredentials.authType });
sshConn!.shell({
@@ -175,7 +176,6 @@ wss.on('connection', (ws: WebSocket) => {
});
stream.on('close', () => {
sshLogger.info('SSH stream closed', { operation: 'ssh_stream', hostId: id, ip, port, username });
ws.send(JSON.stringify({type: 'disconnected', message: 'Connection lost'}));
});
@@ -219,7 +219,6 @@ wss.on('connection', (ws: WebSocket) => {
sshConn.on('close', () => {
clearTimeout(connectionTimeout);
cleanupSSH(connectionTimeout);
});

View File

@@ -382,7 +382,9 @@ async function connectSSHTunnel(tunnelConfig: TunnelConfig, retryAttempt = 0): P
const tunnelName = tunnelConfig.name;
const tunnelMarker = getTunnelMarker(tunnelName);
tunnelLogger.info('SSH tunnel connection attempt started', { operation: 'tunnel_connect', tunnelName, retryAttempt, sourceIP: tunnelConfig.sourceIP, sourcePort: tunnelConfig.sourceSSHPort });
if (retryAttempt === 0) {
tunnelLogger.info('SSH tunnel connection attempt started', { operation: 'tunnel_connect', tunnelName, sourceIP: tunnelConfig.sourceIP, sourcePort: tunnelConfig.sourceSSHPort });
}
if (manualDisconnects.has(tunnelName)) {
tunnelLogger.info('Tunnel connection cancelled due to manual disconnect', { operation: 'tunnel_connect', tunnelName });
@@ -394,14 +396,10 @@ async function connectSSHTunnel(tunnelConfig: TunnelConfig, retryAttempt = 0): P
if (retryAttempt === 0) {
retryExhaustedTunnels.delete(tunnelName);
retryCounters.delete(tunnelName);
tunnelLogger.info('Reset retry state for tunnel', { operation: 'tunnel_connect', tunnelName });
} else {
tunnelLogger.warn('Tunnel connection retry attempt', { operation: 'tunnel_connect', tunnelName, retryAttempt });
}
const currentStatus = connectionStatus.get(tunnelName);
if (!currentStatus || currentStatus.status !== CONNECTION_STATES.WAITING) {
tunnelLogger.info('Broadcasting tunnel connecting status', { operation: 'tunnel_connect', tunnelName, retryAttempt });
broadcastTunnelStatus(tunnelName, {
connected: false,
status: CONNECTION_STATES.CONNECTING,
@@ -428,7 +426,6 @@ async function connectSSHTunnel(tunnelConfig: TunnelConfig, retryAttempt = 0): P
};
if (tunnelConfig.sourceCredentialId && tunnelConfig.sourceUserId) {
tunnelLogger.info('Resolving source credentials from database', { operation: 'tunnel_connect', tunnelName, credentialId: tunnelConfig.sourceCredentialId, userId: tunnelConfig.sourceUserId });
try {
const credentials = await db
.select()
@@ -447,15 +444,12 @@ async function connectSSHTunnel(tunnelConfig: TunnelConfig, retryAttempt = 0): P
keyType: credential.keyType,
authMethod: credential.authType
};
tunnelLogger.success('Source credentials resolved successfully', { operation: 'tunnel_connect', tunnelName, credentialId: credential.id, authType: credential.authType });
} else {
tunnelLogger.warn('No source credentials found in database', { operation: 'tunnel_connect', tunnelName, credentialId: tunnelConfig.sourceCredentialId });
}
} catch (error) {
tunnelLogger.warn('Failed to resolve source credentials from database', { operation: 'tunnel_connect', tunnelName, credentialId: tunnelConfig.sourceCredentialId, error: error instanceof Error ? error.message : 'Unknown error' });
}
} else {
tunnelLogger.info('Using direct source credentials from tunnel config', { operation: 'tunnel_connect', tunnelName, authMethod: tunnelConfig.sourceAuthMethod });
}
// Resolve endpoint credentials if tunnel config has endpointCredentialId
@@ -486,10 +480,14 @@ async function connectSSHTunnel(tunnelConfig: TunnelConfig, retryAttempt = 0): P
keyType: credential.keyType,
authMethod: credential.authType
};
} else {
tunnelLogger.warn('No endpoint credentials found in database', { operation: 'tunnel_connect', tunnelName, credentialId: tunnelConfig.endpointCredentialId });
}
} catch (error) {
tunnelLogger.warn(`Failed to resolve endpoint credentials for tunnel ${tunnelName}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
} else if (tunnelConfig.endpointCredentialId) {
tunnelLogger.warn('Missing userId for endpoint credential resolution', { operation: 'tunnel_connect', tunnelName, credentialId: tunnelConfig.endpointCredentialId, hasUserId: !!tunnelConfig.endpointUserId });
}
const conn = new Client();

View File

@@ -12,12 +12,6 @@ import { systemLogger } from './utils/logger.js';
try {
systemLogger.info("Initializing backend services...", { operation: 'startup' });
systemLogger.info("Loading database service...", { operation: 'database_init' });
systemLogger.info("Loading SSH terminal service...", { operation: 'terminal_init' });
systemLogger.info("Loading SSH tunnel service...", { operation: 'tunnel_init' });
systemLogger.info("Loading file manager service...", { operation: 'file_manager_init' });
systemLogger.info("Loading server stats service...", { operation: 'stats_init' });
systemLogger.success("All backend services initialized successfully", {
operation: 'startup_complete',
services: ['database', 'terminal', 'tunnel', 'file_manager', 'stats']
@@ -25,13 +19,11 @@ import { systemLogger } from './utils/logger.js';
process.on('SIGINT', () => {
systemLogger.info("Received SIGINT signal, initiating graceful shutdown...", { operation: 'shutdown' });
systemLogger.info("Shutting down all services...", { operation: 'shutdown' });
process.exit(0);
});
process.on('SIGTERM', () => {
systemLogger.info("Received SIGTERM signal, initiating graceful shutdown...", { operation: 'shutdown' });
systemLogger.info("Shutting down all services...", { operation: 'shutdown' });
process.exit(0);
});

View File

@@ -1,6 +1,6 @@
/**
* Frontend Logger - A comprehensive logging utility for the frontend
* Based on the backend logger patterns but adapted for browser environment
* Enhanced with better formatting, readability, and request/response grouping
*/
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'success';
@@ -45,7 +45,7 @@ class FrontendLogger {
private formatMessage(level: LogLevel, message: string, context?: LogContext): string {
const timestamp = this.getTimeStamp();
const levelTag = this.getLevelTag(level);
const serviceTag = this.serviceIcon;
const serviceTag = this.getServiceTag();
let contextStr = '';
if (context && this.isDevelopment) {
@@ -78,6 +78,10 @@ class FrontendLogger {
return `${symbols[level]} [${level.toUpperCase()}]`;
}
private getServiceTag(): string {
return `${this.serviceIcon} [${this.serviceName}]`;
}
private shouldLog(level: LogLevel): boolean {
if (level === 'debug' && !this.isDevelopment) {
return false;
@@ -181,64 +185,136 @@ class FrontendLogger {
this.warn(`SECURITY: ${message}`, { ...context, operation: 'security' });
}
// Specialized logging methods for different scenarios
// Enhanced API request/response logging methods
requestStart(method: string, url: string, context?: LogContext): void {
this.request(`Starting ${method.toUpperCase()} request`, {
const cleanUrl = this.sanitizeUrl(url);
const shortUrl = this.getShortUrl(cleanUrl);
console.group(`🚀 ${method.toUpperCase()} ${shortUrl}`);
this.request(`→ Starting request to ${cleanUrl}`, {
...context,
method: method.toUpperCase(),
url: this.sanitizeUrl(url)
url: cleanUrl
});
}
requestSuccess(method: string, url: string, status: number, responseTime: number, context?: LogContext): void {
this.response(`Request completed successfully`, {
const cleanUrl = this.sanitizeUrl(url);
const shortUrl = this.getShortUrl(cleanUrl);
const statusIcon = this.getStatusIcon(status);
const performanceIcon = this.getPerformanceIcon(responseTime);
this.response(`${statusIcon} ${status} ${performanceIcon} ${responseTime}ms`, {
...context,
method: method.toUpperCase(),
url: this.sanitizeUrl(url),
url: cleanUrl,
status,
responseTime
});
console.groupEnd();
}
requestError(method: string, url: string, status: number, errorMessage: string, responseTime?: number, context?: LogContext): void {
this.error(`Request failed`, undefined, {
const cleanUrl = this.sanitizeUrl(url);
const shortUrl = this.getShortUrl(cleanUrl);
const statusIcon = this.getStatusIcon(status);
this.error(`${statusIcon} ${status} ${errorMessage}`, undefined, {
...context,
method: method.toUpperCase(),
url: this.sanitizeUrl(url),
url: cleanUrl,
status,
errorMessage,
responseTime
});
console.groupEnd();
}
networkError(method: string, url: string, errorMessage: string, context?: LogContext): void {
this.error(`Network error occurred`, undefined, {
const cleanUrl = this.sanitizeUrl(url);
const shortUrl = this.getShortUrl(cleanUrl);
this.error(`🌐 Network Error: ${errorMessage}`, undefined, {
...context,
method: method.toUpperCase(),
url: this.sanitizeUrl(url),
url: cleanUrl,
errorMessage,
errorCode: 'NETWORK_ERROR'
});
console.groupEnd();
}
authError(method: string, url: string, context?: LogContext): void {
this.security(`Authentication failed`, {
const cleanUrl = this.sanitizeUrl(url);
const shortUrl = this.getShortUrl(cleanUrl);
this.security(`🔐 Authentication Required`, {
...context,
method: method.toUpperCase(),
url: this.sanitizeUrl(url),
url: cleanUrl,
errorCode: 'AUTH_REQUIRED'
});
console.groupEnd();
}
retryAttempt(method: string, url: string, attempt: number, maxAttempts: number, context?: LogContext): void {
this.retry(`Retry attempt ${attempt}/${maxAttempts}`, {
const cleanUrl = this.sanitizeUrl(url);
const shortUrl = this.getShortUrl(cleanUrl);
this.retry(`🔄 Retry ${attempt}/${maxAttempts}`, {
...context,
method: method.toUpperCase(),
url: this.sanitizeUrl(url),
url: cleanUrl,
retryCount: attempt
});
}
// Enhanced logging for API operations
apiOperation(operation: string, details: string, context?: LogContext): void {
this.info(`🔧 ${operation}: ${details}`, { ...context, operation: 'api_operation' });
}
// Log request summary for better debugging
requestSummary(method: string, url: string, status: number, responseTime: number, context?: LogContext): void {
const cleanUrl = this.sanitizeUrl(url);
const shortUrl = this.getShortUrl(cleanUrl);
const statusIcon = this.getStatusIcon(status);
const performanceIcon = this.getPerformanceIcon(responseTime);
console.log(`%c📊 ${method} ${shortUrl} ${statusIcon} ${status} ${performanceIcon} ${responseTime}ms`,
'color: #666; font-style: italic; font-size: 0.9em;',
context
);
}
// New helper methods for better formatting
private getShortUrl(url: string): string {
try {
const urlObj = new URL(url);
const path = urlObj.pathname;
const query = urlObj.search;
return `${urlObj.hostname}${path}${query}`;
} catch {
return url.length > 50 ? url.substring(0, 47) + '...' : url;
}
}
private getStatusIcon(status: number): string {
if (status >= 200 && status < 300) return '✅';
if (status >= 300 && status < 400) return '↩️';
if (status >= 400 && status < 500) return '⚠️';
if (status >= 500) return '❌';
return '❓';
}
private getPerformanceIcon(responseTime: number): string {
if (responseTime < 100) return '⚡';
if (responseTime < 500) return '🚀';
if (responseTime < 1000) return '🏃';
if (responseTime < 3000) return '🚶';
return '🐌';
}
private sanitizeUrl(url: string): string {
// Remove sensitive information from URLs for logging
try {

View File

@@ -297,7 +297,7 @@ export interface CredentialViewerProps {
export interface CredentialSelectorProps {
value?: number | null;
onChange: (value: number | null) => void;
onValueChange: (value: number | null) => void;
}
export interface HostManagerProps {
@@ -384,10 +384,6 @@ export interface FolderStats {
}>;
}
export interface FolderManagerProps {
onFolderChanged?: () => void;
}
// ============================================================================
// BACKEND TYPES
// ============================================================================

View File

@@ -21,7 +21,7 @@ import { Alert, AlertDescription } from "@/components/ui/alert"
import { toast } from "sonner"
import { createCredential, updateCredential, getCredentials, getCredentialDetails } from '@/ui/main-axios'
import { useTranslation } from "react-i18next"
import type { Credential, CredentialEditorProps } from '../../../types/index.js'
import type { Credential, CredentialEditorProps, CredentialData } from '../../../types/index.js'
export function CredentialEditor({ editingCredential, onFormSubmit }: CredentialEditorProps) {
const { t } = useTranslation();
@@ -31,6 +31,7 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
const [fullCredentialDetails, setFullCredentialDetails] = useState<Credential | null>(null);
const [authTab, setAuthTab] = useState<'password' | 'key'>('password');
const [keyInputMethod, setKeyInputMethod] = useState<'upload' | 'paste'>('upload');
useEffect(() => {
const fetchData = async () => {
@@ -84,9 +85,15 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
key: z.any().optional().nullable(),
keyPassword: z.string().optional(),
keyType: z.enum([
'rsa',
'ecdsa',
'ed25519'
'auto',
'ssh-rsa',
'ssh-ed25519',
'ecdsa-sha2-nistp256',
'ecdsa-sha2-nistp384',
'ecdsa-sha2-nistp521',
'ssh-dss',
'ssh-rsa-sha2-256',
'ssh-rsa-sha2-512',
]).optional(),
}).superRefine((data, ctx) => {
if (data.authType === 'password') {
@@ -122,14 +129,13 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
password: "",
key: null,
keyPassword: "",
keyType: "rsa",
keyType: "auto",
}
});
useEffect(() => {
if (editingCredential && fullCredentialDetails) {
const defaultAuthType = fullCredentialDetails.authType;
setAuthTab(defaultAuthType);
form.reset({
@@ -142,11 +148,10 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
password: fullCredentialDetails.password || "",
key: null,
keyPassword: fullCredentialDetails.keyPassword || "",
keyType: (fullCredentialDetails.keyType as any) || "rsa",
keyType: (fullCredentialDetails.keyType as any) || "auto",
});
} else if (!editingCredential) {
setAuthTab('password');
form.reset({
name: "",
description: "",
@@ -157,52 +162,43 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
password: "",
key: null,
keyPassword: "",
keyType: "rsa",
keyType: "auto",
});
}
}, [editingCredential, fullCredentialDetails, form]);
}, [editingCredential?.id, fullCredentialDetails]);
const onSubmit = async (data: any) => {
const onSubmit = async (data: FormData) => {
try {
const formData = data as FormData;
if (!formData.name || formData.name.trim() === '') {
formData.name = formData.username;
if (!data.name || data.name.trim() === '') {
data.name = data.username;
}
const submitData: any = {
name: formData.name,
description: formData.description,
folder: formData.folder,
tags: formData.tags,
authType: formData.authType,
username: formData.username,
keyType: formData.keyType
const submitData: CredentialData = {
name: data.name,
description: data.description,
folder: data.folder,
tags: data.tags,
authType: data.authType,
username: data.username,
keyType: data.keyType
};
if (formData.password !== undefined) {
submitData.password = formData.password;
}
if (formData.key !== undefined) {
if (formData.key instanceof File) {
const keyContent = await formData.key.text();
submitData.key = keyContent;
} else {
submitData.key = formData.key;
}
}
if (formData.keyPassword !== undefined) {
submitData.keyPassword = formData.keyPassword;
if (data.authType === 'password') {
submitData.password = data.password;
submitData.key = undefined;
submitData.keyPassword = undefined;
} else if (data.authType === 'key') {
submitData.key = data.key instanceof File ? await data.key.text() : data.key;
submitData.keyPassword = data.keyPassword;
submitData.password = undefined;
}
if (editingCredential) {
await updateCredential(editingCredential.id, submitData);
toast.success(t('credentials.credentialUpdatedSuccessfully', { name: formData.name }));
toast.success(t('credentials.credentialUpdatedSuccessfully', { name: data.name }));
} else {
await createCredential(submitData);
toast.success(t('credentials.credentialAddedSuccessfully', { name: formData.name }));
toast.success(t('credentials.credentialAddedSuccessfully', { name: data.name }));
}
if (onFormSubmit) {
@@ -256,9 +252,15 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
}, [folderDropdownOpen]);
const keyTypeOptions = [
{ value: 'rsa', label: t('credentials.keyTypeRSA') },
{ value: 'ecdsa', label: t('credentials.keyTypeECDSA') },
{ value: 'ed25519', label: t('credentials.keyTypeEd25519') },
{ value: 'auto', label: t('hosts.autoDetect') },
{ value: 'ssh-rsa', label: t('hosts.rsa') },
{ value: 'ssh-ed25519', label: t('hosts.ed25519') },
{ value: 'ecdsa-sha2-nistp256', label: t('hosts.ecdsaNistP256') },
{ value: 'ecdsa-sha2-nistp384', label: t('hosts.ecdsaNistP384') },
{ value: 'ecdsa-sha2-nistp521', label: t('hosts.ecdsaNistP521') },
{ value: 'ssh-dss', label: t('hosts.dsa') },
{ value: 'ssh-rsa-sha2-256', label: t('hosts.rsaSha2256') },
{ value: 'ssh-rsa-sha2-512', label: t('hosts.rsaSha2512') },
];
const [keyTypeDropdownOpen, setKeyTypeDropdownOpen] = useState(false);
@@ -436,13 +438,16 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
<Tabs
value={authTab}
onValueChange={(value) => {
setAuthTab(value as 'password' | 'key');
form.setValue('authType', value as 'password' | 'key');
const newAuthType = value as 'password' | 'key';
setAuthTab(newAuthType);
form.setValue('authType', newAuthType);
// Clear other auth fields when switching
if (value === 'password') {
if (newAuthType === 'password') {
form.setValue('key', null);
form.setValue('keyPassword', '');
} else if (value === 'key') {
form.setValue('keyType', 'auto');
} else if (newAuthType === 'key') {
form.setValue('password', '');
}
}}
@@ -467,103 +472,206 @@ export function CredentialEditor({ editingCredential, onFormSubmit }: Credential
/>
</TabsContent>
<TabsContent value="key">
<div className="grid grid-cols-15 gap-4">
<Controller
control={form.control}
name="key"
render={({ field }) => (
<FormItem className="col-span-4 overflow-hidden min-w-0">
<FormLabel>{t('credentials.sshPrivateKey')}</FormLabel>
<FormControl>
<div className="relative min-w-0">
<input
id="key-upload"
type="file"
accept=".pem,.key,.txt,.ppk"
onChange={(e) => {
const file = e.target.files?.[0];
field.onChange(file || null);
}}
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
/>
<Button
type="button"
variant="outline"
className="w-full min-w-0 overflow-hidden px-3 py-2 text-left"
>
<span className="block w-full truncate"
title={field.value?.name || t('credentials.upload')}>
{field.value ? (editingCredential ? t('credentials.updateKey') : field.value.name) : t('credentials.upload')}
</span>
</Button>
</div>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="keyPassword"
render={({ field }) => (
<FormItem className="col-span-8">
<FormLabel>{t('credentials.keyPassword')}</FormLabel>
<FormControl>
<Input
placeholder={t('placeholders.keyPassword')}
type="password"
{...field}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="keyType"
render={({ field }) => (
<FormItem className="relative col-span-3">
<FormLabel>{t('credentials.keyType')}</FormLabel>
<FormControl>
<div className="relative">
<Button
ref={keyTypeButtonRef}
type="button"
variant="outline"
className="w-full justify-start text-left rounded-md px-2 py-2 bg-[#18181b] border border-input text-foreground"
onClick={() => setKeyTypeDropdownOpen((open) => !open)}
>
{keyTypeOptions.find((opt) => opt.value === field.value)?.label || t('credentials.keyTypeRSA')}
</Button>
{keyTypeDropdownOpen && (
<div
ref={keyTypeDropdownRef}
className="absolute bottom-full left-0 z-50 mb-1 w-full bg-[#18181b] border border-input rounded-md shadow-lg max-h-40 overflow-y-auto p-1"
>
<div className="grid grid-cols-1 gap-1 p-0">
{keyTypeOptions.map((opt) => (
<Button
key={opt.value}
type="button"
variant="ghost"
size="sm"
className="w-full justify-start text-left rounded-md px-2 py-1.5 bg-[#18181b] text-foreground hover:bg-white/15 focus:bg-white/20 focus:outline-none"
onClick={() => {
field.onChange(opt.value);
setKeyTypeDropdownOpen(false);
}}
>
{opt.label}
</Button>
))}
</div>
<Tabs
value={keyInputMethod}
onValueChange={(value) => {
setKeyInputMethod(value as 'upload' | 'paste');
// Clear the other field when switching
if (value === 'upload') {
form.setValue('key', null);
} else {
form.setValue('key', '');
}
}}
className="w-full"
>
<TabsList className="inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground">
<TabsTrigger value="upload">{t('hosts.uploadFile')}</TabsTrigger>
<TabsTrigger value="paste">{t('hosts.pasteKey')}</TabsTrigger>
</TabsList>
<TabsContent value="upload" className="mt-4">
<div className="grid grid-cols-15 gap-4">
<Controller
control={form.control}
name="key"
render={({ field }) => (
<FormItem className="col-span-4 overflow-hidden min-w-0">
<FormLabel>{t('credentials.sshPrivateKey')}</FormLabel>
<FormControl>
<div className="relative min-w-0">
<input
id="key-upload"
type="file"
accept=".pem,.key,.txt,.ppk"
onChange={(e) => {
const file = e.target.files?.[0];
field.onChange(file || null);
}}
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
/>
<Button
type="button"
variant="outline"
className="w-full min-w-0 overflow-hidden px-3 py-2 text-left"
>
<span className="block w-full truncate"
title={field.value?.name || t('credentials.upload')}>
{field.value ? (editingCredential ? t('credentials.updateKey') : field.value.name) : t('credentials.upload')}
</span>
</Button>
</div>
)}
</div>
</FormControl>
</FormItem>
)}
/>
</div>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="keyPassword"
render={({ field }) => (
<FormItem className="col-span-8">
<FormLabel>{t('credentials.keyPassword')}</FormLabel>
<FormControl>
<Input
placeholder={t('placeholders.keyPassword')}
type="password"
{...field}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="keyType"
render={({ field }) => (
<FormItem className="relative col-span-3">
<FormLabel>{t('credentials.keyType')}</FormLabel>
<FormControl>
<div className="relative">
<Button
ref={keyTypeButtonRef}
type="button"
variant="outline"
className="w-full justify-start text-left rounded-md px-2 py-2 bg-[#18181b] border border-input text-foreground"
onClick={() => setKeyTypeDropdownOpen((open) => !open)}
>
{keyTypeOptions.find((opt) => opt.value === field.value)?.label || t('credentials.keyTypeRSA')}
</Button>
{keyTypeDropdownOpen && (
<div
ref={keyTypeDropdownRef}
className="absolute bottom-full left-0 z-50 mb-1 w-full bg-[#18181b] border border-input rounded-md shadow-lg max-h-40 overflow-y-auto p-1"
>
<div className="grid grid-cols-1 gap-1 p-0">
{keyTypeOptions.map((opt) => (
<Button
key={opt.value}
type="button"
variant="ghost"
size="sm"
className="w-full justify-start text-left rounded-md px-2 py-1.5 bg-[#18181b] text-foreground hover:bg-white/15 focus:bg-white/20 focus:outline-none"
onClick={() => {
field.onChange(opt.value);
setKeyTypeDropdownOpen(false);
}}
>
{opt.label}
</Button>
))}
</div>
</div>
)}
</div>
</FormControl>
</FormItem>
)}
/>
</div>
</TabsContent>
<TabsContent value="paste" className="mt-4">
<Controller
control={form.control}
name="key"
render={({ field }) => (
<FormItem className="mb-4">
<FormLabel>{t('credentials.sshPrivateKey')}</FormLabel>
<FormControl>
<textarea
placeholder={t('placeholders.pastePrivateKey')}
className="flex min-h-[120px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
value={typeof field.value === 'string' ? field.value : ''}
onChange={(e) => field.onChange(e.target.value)}
/>
</FormControl>
</FormItem>
)}
/>
<div className="grid grid-cols-15 gap-4 mt-4">
<FormField
control={form.control}
name="keyPassword"
render={({ field }) => (
<FormItem className="col-span-8">
<FormLabel>{t('credentials.keyPassword')}</FormLabel>
<FormControl>
<Input
placeholder={t('placeholders.keyPassword')}
type="password"
{...field}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="keyType"
render={({ field }) => (
<FormItem className="relative col-span-3">
<FormLabel>{t('credentials.keyType')}</FormLabel>
<FormControl>
<div className="relative">
<Button
ref={keyTypeButtonRef}
type="button"
variant="outline"
className="w-full justify-start text-left rounded-md px-2 py-2 bg-[#18181b] border border-input text-foreground"
onClick={() => setKeyTypeDropdownOpen((open) => !open)}
>
{keyTypeOptions.find((opt) => opt.value === field.value)?.label || t('credentials.keyTypeRSA')}
</Button>
{keyTypeDropdownOpen && (
<div
ref={keyTypeDropdownRef}
className="absolute bottom-full left-0 z-50 mb-1 w-full bg-[#18181b] border border-input rounded-md shadow-lg max-h-40 overflow-y-auto p-1"
>
<div className="grid grid-cols-1 gap-1 p-0">
{keyTypeOptions.map((opt) => (
<Button
key={opt.value}
type="button"
variant="ghost"
size="sm"
className="w-full justify-start text-left rounded-md px-2 py-1.5 bg-[#18181b] text-foreground hover:bg-white/15 focus:bg-white/20 focus:outline-none"
onClick={() => {
field.onChange(opt.value);
setKeyTypeDropdownOpen(false);
}}
>
{opt.label}
</Button>
))}
</div>
</div>
)}
</div>
</FormControl>
</FormItem>
)}
/>
</div>
</TabsContent>
</Tabs>
</TabsContent>
</Tabs>
</TabsContent>

View File

@@ -19,7 +19,6 @@ import {
import { getCredentials, deleteCredential } from '@/ui/main-axios';
import { toast } from 'sonner';
import { useTranslation } from 'react-i18next';
import {CredentialEditor} from './CredentialEditor';
import CredentialViewer from './CredentialViewer';
import type { Credential, CredentialsManagerProps } from '../../../types/index.js';

View File

@@ -1,243 +0,0 @@
import React, { useState, useEffect, useMemo } from 'react';
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Input } from "@/components/ui/input";
import { ScrollArea } from "@/components/ui/scroll-area";
import {
Folder,
Edit,
Search,
Trash2,
Users
} from 'lucide-react';
import { getFoldersWithStats, renameFolder } from '@/ui/main-axios';
import { toast } from 'sonner';
import { useTranslation } from 'react-i18next';
import type { FolderManagerProps } from '../../../types/index.js';
interface FolderStats {
name: string;
hostCount: number;
hosts: Array<{
id: number;
name?: string;
ip: string;
}>;
}
export function FolderManager({ onFolderChanged }: FolderManagerProps) {
const { t } = useTranslation();
const [folders, setFolders] = useState<FolderStats[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [searchQuery, setSearchQuery] = useState('');
// Rename state
const [renameLoading, setRenameLoading] = useState(false);
useEffect(() => {
fetchFolders();
}, []);
const fetchFolders = async () => {
try {
setLoading(true);
const data = await getFoldersWithStats();
setFolders(data || []);
setError(null);
} catch (err) {
setError('Failed to fetch folder statistics');
} finally {
setLoading(false);
}
};
const handleRename = async (folder: FolderStats) => {
const newName = prompt(
`Enter new name for folder "${folder.name}":\n\nThis will update ${folder.hostCount} host(s) that use this folder.`,
folder.name
);
if (!newName || newName.trim() === '' || newName === folder.name) {
return;
}
if (window.confirm(
`Are you sure you want to rename folder "${folder.name}" to "${newName.trim()}"?\n\n` +
`This will update ${folder.hostCount} host(s) that currently use this folder.`
)) {
try {
setRenameLoading(true);
await renameFolder(folder.name, newName.trim());
toast.success(`Folder renamed from "${folder.name}" to "${newName.trim()}"`, {
description: `Updated ${folder.hostCount} host(s)`
});
// Refresh folder list
await fetchFolders();
// Notify parent component about folder change
if (onFolderChanged) {
onFolderChanged();
}
// Emit event for other components to refresh
window.dispatchEvent(new CustomEvent('folders:changed'));
} catch (err) {
toast.error('Failed to rename folder');
} finally {
setRenameLoading(false);
}
}
};
const filteredFolders = useMemo(() => {
if (!searchQuery.trim()) {
return folders;
}
const query = searchQuery.toLowerCase();
return folders.filter(folder =>
folder.name.toLowerCase().includes(query) ||
folder.hosts.some(host =>
(host.name?.toLowerCase().includes(query)) ||
host.ip.toLowerCase().includes(query)
)
);
}, [folders, searchQuery]);
if (loading) {
return (
<div className="flex items-center justify-center h-full">
<div className="text-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-white mx-auto mb-2"></div>
<p className="text-muted-foreground">Loading folders...</p>
</div>
</div>
);
}
if (error) {
return (
<div className="flex items-center justify-center h-full">
<div className="text-center">
<p className="text-red-500 mb-4">{error}</p>
<Button onClick={fetchFolders} variant="outline">
Retry
</Button>
</div>
</div>
);
}
if (folders.length === 0) {
return (
<div className="flex items-center justify-center h-full">
<div className="text-center">
<Folder className="h-12 w-12 text-muted-foreground mx-auto mb-4"/>
<h3 className="text-lg font-semibold mb-2">No Folders Found</h3>
<p className="text-muted-foreground mb-4">
Create some hosts with folders to manage them here
</p>
</div>
</div>
);
}
return (
<div className="flex flex-col h-full min-h-0">
<div className="flex items-center justify-between mb-2">
<div>
<h2 className="text-xl font-semibold">Folder Management</h2>
<p className="text-muted-foreground">
{filteredFolders.length} folder(s) found
</p>
</div>
<div className="flex items-center gap-2">
<Button onClick={fetchFolders} variant="outline" size="sm">
Refresh
</Button>
</div>
</div>
<div className="relative mb-3">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground"/>
<Input
placeholder="Search folders..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="pl-10"
/>
</div>
<ScrollArea className="flex-1 min-h-0">
<div className="space-y-3 pb-20">
{filteredFolders.map((folder) => (
<div
key={folder.name}
className="bg-[#222225] border border-input rounded-lg p-4 hover:shadow-md transition-shadow"
>
<div className="flex items-start justify-between mb-3">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<Folder className="h-5 w-5 text-blue-500" />
<h3 className="font-medium text-lg truncate">
{folder.name}
</h3>
<Badge variant="secondary" className="ml-auto">
<Users className="h-3 w-3 mr-1" />
{folder.hostCount} host(s)
</Badge>
</div>
</div>
<div className="flex gap-1 flex-shrink-0 ml-2">
<Button
size="sm"
variant="ghost"
onClick={() => handleRename(folder)}
className="h-8 w-8 p-0"
title="Rename folder"
disabled={renameLoading}
>
{renameLoading ? (
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current"></div>
) : (
<Edit className="h-4 w-4" />
)}
</Button>
</div>
</div>
<div className="space-y-1">
<p className="text-sm text-muted-foreground mb-2">
Hosts using this folder:
</p>
<div className="grid grid-cols-1 gap-1 max-h-32 overflow-y-auto">
{folder.hosts.slice(0, 5).map((host) => (
<div key={host.id} className="flex items-center gap-2 text-sm bg-muted/20 rounded px-2 py-1">
<span className="font-medium">
{host.name || host.ip}
</span>
{host.name && (
<span className="text-muted-foreground">
({host.ip})
</span>
)}
</div>
))}
{folder.hosts.length > 5 && (
<div className="text-sm text-muted-foreground px-2 py-1">
... and {folder.hosts.length - 5} more host(s)
</div>
)}
</div>
</div>
</div>
))}
</div>
</ScrollArea>
</div>
);
}

View File

@@ -1,18 +1,19 @@
import React, {useState} from "react";
import {HostManagerHostViewer} from "@/ui/Desktop/Apps/Host Manager/HostManagerHostViewer.tsx"
import {HostManagerViewer} from "@/ui/Desktop/Apps/Host Manager/HostManagerViewer.tsx"
import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs.tsx";
import {Separator} from "@/components/ui/separator.tsx";
import {HostManagerHostEditor} from "@/ui/Desktop/Apps/Host Manager/HostManagerHostEditor.tsx";
import {HostManagerEditor} from "@/ui/Desktop/Apps/Host Manager/HostManagerEditor.tsx";
import {CredentialsManager} from "@/ui/Desktop/Apps/Credentials/CredentialsManager.tsx";
import {CredentialEditor} from "@/ui/Desktop/Apps/Credentials/CredentialEditor.tsx";
import {useSidebar} from "@/components/ui/sidebar.tsx";
import {useTranslation} from "react-i18next";
import type { SSHHost, HostManagerProps } from '../../../types/index.js';
import type { SSHHost, HostManagerProps } from '../../../types/index';
export function HostManager({onSelectView, isTopbarOpen}: HostManagerProps): React.ReactElement {
const {t} = useTranslation();
const [activeTab, setActiveTab] = useState("host_viewer");
const [editingHost, setEditingHost] = useState<SSHHost | null>(null);
const [editingCredential, setEditingCredential] = useState<any | null>(null);
const {state: sidebarState} = useSidebar();
@@ -21,8 +22,12 @@ export function HostManager({onSelectView, isTopbarOpen}: HostManagerProps): Rea
setActiveTab("add_host");
};
const handleFormSubmit = () => {
setEditingHost(null);
const handleFormSubmit = (updatedHost?: SSHHost) => {
if (updatedHost) {
setEditingHost(updatedHost);
} else {
setEditingHost(null);
}
setActiveTab("host_viewer");
};
@@ -71,19 +76,20 @@ export function HostManager({onSelectView, isTopbarOpen}: HostManagerProps): Rea
<TabsTrigger value="add_host">
{editingHost ? t('hosts.editHost') : t('hosts.addHost')}
</TabsTrigger>
<TabsTrigger value="credentials">{t('credentials.credentialsManager')}</TabsTrigger>
<div className="h-6 w-px bg-[#303032] mx-1"></div>
<TabsTrigger value="credentials">{t('credentials.credentialsViewer')}</TabsTrigger>
<TabsTrigger value="add_credential">
{editingCredential ? t('credentials.editCredential') : t('credentials.addCredential')}
</TabsTrigger>
</TabsList>
<TabsContent value="host_viewer" className="flex-1 flex flex-col h-full min-h-0">
<Separator className="p-0.25 -mt-0.5 mb-1"/>
<HostManagerHostViewer onEditHost={handleEditHost}/>
<HostManagerViewer onEditHost={handleEditHost}/>
</TabsContent>
<TabsContent value="add_host" className="flex-1 flex flex-col h-full min-h-0">
<Separator className="p-0.25 -mt-0.5 mb-1"/>
<div className="flex flex-col h-full min-h-0">
<HostManagerHostEditor
<HostManagerEditor
editingHost={editingHost}
onFormSubmit={handleFormSubmit}
/>

View File

@@ -50,10 +50,10 @@ interface SSHHost {
interface SSHManagerHostEditorProps {
editingHost?: SSHHost | null;
onFormSubmit?: () => void;
onFormSubmit?: (updatedHost?: SSHHost) => void;
}
export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHostEditorProps) {
export function HostManagerEditor({editingHost, onFormSubmit}: SSHManagerHostEditorProps) {
const {t} = useTranslation();
const [hosts, setHosts] = useState<SSHHost[]>([]);
const [folders, setFolders] = useState<string[]>([]);
@@ -62,6 +62,7 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
const [authTab, setAuthTab] = useState<'password' | 'key' | 'credential'>('password');
const [keyInputMethod, setKeyInputMethod] = useState<'upload' | 'paste'>('upload');
const isSubmittingRef = useRef(false);
// Ref for the IP address input to manage focus
const ipInputRef = useRef<HTMLInputElement>(null);
@@ -182,24 +183,24 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
const form = useForm<FormData>({
resolver: zodResolver(formSchema) as any,
defaultValues: {
name: editingHost?.name || "",
ip: editingHost?.ip || "",
port: editingHost?.port || 22,
username: editingHost?.username || "",
folder: editingHost?.folder || "",
tags: editingHost?.tags || [],
pin: editingHost?.pin || false,
authType: (editingHost?.authType as 'password' | 'key' | 'credential') || "password",
credentialId: editingHost?.credentialId || null,
name: "",
ip: "",
port: 22,
username: "",
folder: "",
tags: [],
pin: false,
authType: "password" as const,
credentialId: null,
password: "",
key: null,
keyPassword: "",
keyType: "auto",
enableTerminal: editingHost?.enableTerminal !== false,
enableTunnel: editingHost?.enableTunnel !== false,
enableFileManager: editingHost?.enableFileManager !== false,
defaultPath: editingHost?.defaultPath || "/",
tunnelConnections: editingHost?.tunnelConnections || [],
keyType: "auto" as const,
enableTerminal: true,
enableTunnel: true,
enableFileManager: true,
defaultPath: "/",
tunnelConnections: [],
}
});
@@ -207,30 +208,32 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
if (editingHost) {
const defaultAuthType = editingHost.credentialId ? 'credential' : (editingHost.key ? 'key' : 'password');
setAuthTab(defaultAuthType);
form.reset({
const formData = {
name: editingHost.name || "",
ip: editingHost.ip || "",
port: editingHost.port || 22,
username: editingHost.username || "",
folder: editingHost.folder || "",
tags: editingHost.tags || [],
pin: editingHost.pin || false,
pin: Boolean(editingHost.pin),
authType: defaultAuthType as 'password' | 'key' | 'credential',
credentialId: editingHost.credentialId || null,
password: editingHost.password || "",
key: null,
keyPassword: editingHost.keyPassword || "",
keyType: (editingHost.keyType as any) || "auto",
enableTerminal: editingHost.enableTerminal !== false,
enableTunnel: editingHost.enableTunnel !== false,
enableFileManager: editingHost.enableFileManager !== false,
enableTerminal: Boolean(editingHost.enableTerminal),
enableTunnel: Boolean(editingHost.enableTunnel),
enableFileManager: Boolean(editingHost.enableFileManager),
defaultPath: editingHost.defaultPath || "/",
tunnelConnections: editingHost.tunnelConnections || [],
});
};
form.reset(formData);
} else {
setAuthTab('password');
form.reset({
const defaultFormData = {
name: "",
ip: "",
port: 22,
@@ -238,22 +241,23 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
folder: "",
tags: [],
pin: false,
authType: "password",
authType: "password" as const,
credentialId: null,
password: "",
key: null,
keyPassword: "",
keyType: "auto",
keyType: "auto" as const,
enableTerminal: true,
enableTunnel: true,
enableFileManager: true,
defaultPath: "/",
tunnelConnections: [],
});
};
form.reset(defaultFormData);
}
}, [editingHost, form]);
}, [editingHost?.id]);
// Focus the IP address field when the component mounts or when editingHost changes
useEffect(() => {
const focusTimer = setTimeout(() => {
if (ipInputRef.current) {
@@ -261,83 +265,79 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
}
}, 300);
return () => clearTimeout(focusTimer);
}, []); // Focus on mount
// Also focus when editingHost changes (for tab switching)
useEffect(() => {
const focusTimer = setTimeout(() => {
if (ipInputRef.current) {
ipInputRef.current.focus();
}
}, 300);
return () => clearTimeout(focusTimer);
}, [editingHost]);
const onSubmit = async (data: any) => {
const onSubmit = async (data: FormData) => {
try {
const formData = data as FormData;
if (!formData.name || formData.name.trim() === '') {
formData.name = `${formData.username}@${formData.ip}`;
isSubmittingRef.current = true;
if (!data.name || data.name.trim() === '') {
data.name = `${data.username}@${data.ip}`;
}
const submitData: any = {
name: formData.name,
ip: formData.ip,
port: formData.port,
username: formData.username,
folder: formData.folder,
tags: formData.tags,
pin: formData.pin,
authType: formData.authType,
enableTerminal: formData.enableTerminal,
enableTunnel: formData.enableTunnel,
enableFileManager: formData.enableFileManager,
defaultPath: formData.defaultPath,
tunnelConnections: formData.tunnelConnections
name: data.name,
ip: data.ip,
port: data.port,
username: data.username,
folder: data.folder || "",
tags: data.tags || [],
pin: Boolean(data.pin),
authType: data.authType,
enableTerminal: Boolean(data.enableTerminal),
enableTunnel: Boolean(data.enableTunnel),
enableFileManager: Boolean(data.enableFileManager),
defaultPath: data.defaultPath || "/",
tunnelConnections: data.tunnelConnections || []
};
if (formData.authType === 'credential') {
submitData.credentialId = formData.credentialId;
if (data.authType === 'credential') {
submitData.credentialId = data.credentialId;
submitData.password = null;
submitData.key = null;
submitData.keyPassword = null;
submitData.keyType = null;
} else if (formData.authType === 'password') {
} else if (data.authType === 'password') {
submitData.credentialId = null;
submitData.password = formData.password;
submitData.password = data.password;
submitData.key = null;
submitData.keyPassword = null;
submitData.keyType = null;
} else if (formData.authType === 'key') {
} else if (data.authType === 'key') {
submitData.credentialId = null;
submitData.password = null;
if (formData.key instanceof File) {
const keyContent = await formData.key.text();
if (data.key instanceof File) {
const keyContent = await data.key.text();
submitData.key = keyContent;
} else {
submitData.key = formData.key;
submitData.key = data.key;
}
submitData.keyPassword = formData.keyPassword;
submitData.keyType = formData.keyType;
submitData.keyPassword = data.keyPassword;
submitData.keyType = data.keyType;
}
if (editingHost) {
await updateSSHHost(editingHost.id, submitData);
toast.success(t('hosts.hostUpdatedSuccessfully', { name: formData.name }));
const updatedHost = await updateSSHHost(editingHost.id, submitData);
toast.success(t('hosts.hostUpdatedSuccessfully', { name: data.name }));
if (onFormSubmit) {
onFormSubmit(updatedHost);
}
} else {
await createSSHHost(submitData);
toast.success(t('hosts.hostAddedSuccessfully', { name: formData.name }));
}
if (onFormSubmit) {
onFormSubmit();
const newHost = await createSSHHost(submitData);
toast.success(t('hosts.hostAddedSuccessfully', { name: data.name }));
if (onFormSubmit) {
onFormSubmit(newHost);
}
}
window.dispatchEvent(new CustomEvent('ssh-hosts:changed'));
} catch (error) {
toast.error(t('hosts.failedToSaveHost'));
} finally {
isSubmittingRef.current = false;
}
};
@@ -659,20 +659,24 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
<Tabs
value={authTab}
onValueChange={(value) => {
setAuthTab(value as 'password' | 'key' | 'credential');
form.setValue('authType', value as 'password' | 'key' | 'credential');
const newAuthType = value as 'password' | 'key' | 'credential';
setAuthTab(newAuthType);
form.setValue('authType', newAuthType);
// Clear other auth fields when switching
if (value === 'password') {
if (newAuthType === 'password') {
form.setValue('key', null);
form.setValue('keyPassword', '');
form.setValue('keyType', 'auto');
form.setValue('credentialId', null);
} else if (value === 'key') {
} else if (newAuthType === 'key') {
form.setValue('password', '');
form.setValue('credentialId', null);
} else if (value === 'credential') {
} else if (newAuthType === 'credential') {
form.setValue('password', '');
form.setValue('key', null);
form.setValue('keyPassword', '');
form.setValue('keyType', 'auto');
}
}}
className="flex-1 flex flex-col h-full min-h-0"
@@ -710,7 +714,7 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
}}
className="w-full"
>
<TabsList className="inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground">
<TabsList className="inline-flex items-center justify-center rounded-md bg-muted p-1 text-muted-foreground">
<TabsTrigger value="upload">{t('hosts.uploadFile')}</TabsTrigger>
<TabsTrigger value="paste">{t('hosts.pasteKey')}</TabsTrigger>
</TabsList>

View File

@@ -29,7 +29,7 @@ import {
import {Separator} from "@/components/ui/separator.tsx";
import type { SSHHost, SSHManagerHostViewerProps } from '../../../types/index.js';
export function HostManagerHostViewer({onEditHost}: SSHManagerHostViewerProps) {
export function HostManagerViewer({onEditHost}: SSHManagerHostViewerProps) {
const {t} = useTranslation();
const [hosts, setHosts] = useState<SSHHost[]>([]);
const [loading, setLoading] = useState(true);

View File

@@ -75,6 +75,22 @@ interface OIDCAuthorize {
// UTILITY FUNCTIONS
// ============================================================================
function getLoggerForService(serviceName: string) {
if (serviceName.includes('SSH') || serviceName.includes('ssh')) {
return sshLogger;
} else if (serviceName.includes('TUNNEL') || serviceName.includes('tunnel')) {
return tunnelLogger;
} else if (serviceName.includes('FILE') || serviceName.includes('file')) {
return fileLogger;
} else if (serviceName.includes('STATS') || serviceName.includes('stats')) {
return statsLogger;
} else if (serviceName.includes('AUTH') || serviceName.includes('auth')) {
return authLogger;
} else {
return apiLogger;
}
}
export function setCookie(name: string, value: string, days = 7): void {
const isElectron = (window as any).IS_ELECTRON === true || (window as any).electronAPI?.isElectron === true;
@@ -116,13 +132,25 @@ function createApiInstance(baseURL: string, serviceName: string = 'API'): AxiosI
(config as any).requestId = requestId;
const token = getCookie('jwt');
const method = config.method?.toUpperCase() || 'UNKNOWN';
const url = config.url || 'UNKNOWN';
const fullUrl = `${config.baseURL}${url}`;
const context: LogContext = {
requestId,
method: config.method?.toUpperCase(),
url: config.url,
method,
url: fullUrl,
operation: 'request_start'
};
// Get the appropriate logger for this service
const logger = getLoggerForService(serviceName);
// Log request start with grouping
if (process.env.NODE_ENV === 'development') {
logger.requestStart(method, fullUrl, context);
}
if (token) {
config.headers.Authorization = `Bearer ${token}`;
} else if (process.env.NODE_ENV === 'development') {
@@ -140,38 +168,31 @@ function createApiInstance(baseURL: string, serviceName: string = 'API'): AxiosI
const requestId = (response.config as any).requestId;
const responseTime = Math.round(endTime - startTime);
const method = response.config.method?.toUpperCase() || 'UNKNOWN';
const url = response.config.url || 'UNKNOWN';
const fullUrl = `${response.config.baseURL}${url}`;
const context: LogContext = {
requestId,
method: response.config.method?.toUpperCase(),
url: response.config.url,
method,
url: fullUrl,
status: response.status,
statusText: response.statusText,
responseTime,
operation: 'request_success'
};
// Only log successful requests in development and for slow requests
// Get the appropriate logger for this service
const logger = getLoggerForService(serviceName);
// Log successful requests in development
if (process.env.NODE_ENV === 'development') {
const method = response.config.method?.toUpperCase() || 'UNKNOWN';
const url = response.config.url || 'UNKNOWN';
// Log based on service type
if (serviceName.includes('SSH') || serviceName.includes('ssh')) {
sshLogger.info(`${method} ${url} - ${response.status} (${responseTime}ms)`, context);
} else if (serviceName.includes('TUNNEL') || serviceName.includes('tunnel')) {
tunnelLogger.info(`${method} ${url} - ${response.status} (${responseTime}ms)`, context);
} else if (serviceName.includes('FILE') || serviceName.includes('file')) {
fileLogger.info(`${method} ${url} - ${response.status} (${responseTime}ms)`, context);
} else if (serviceName.includes('STATS') || serviceName.includes('stats')) {
statsLogger.info(`${method} ${url} - ${response.status} (${responseTime}ms)`, context);
} else {
apiLogger.info(`${method} ${url} - ${response.status} (${responseTime}ms)`, context);
}
logger.requestSuccess(method, fullUrl, response.status, responseTime, context);
}
// Performance logging for slow requests
if (responseTime > 3000) {
apiLogger.warn(`Slow request: ${responseTime}ms`, context);
logger.warn(`🐌 Slow request: ${responseTime}ms`, context);
}
return response;
@@ -184,6 +205,7 @@ function createApiInstance(baseURL: string, serviceName: string = 'API'): AxiosI
const method = error.config?.method?.toUpperCase() || 'UNKNOWN';
const url = error.config?.url || 'UNKNOWN';
const fullUrl = error.config ? `${error.config.baseURL}${url}` : url;
const status = error.response?.status;
const message = (error.response?.data as any)?.error || (error as Error).message || 'Unknown error';
const errorCode = (error.response?.data as any)?.code || error.code;
@@ -191,7 +213,7 @@ function createApiInstance(baseURL: string, serviceName: string = 'API'): AxiosI
const context: LogContext = {
requestId,
method,
url,
url: fullUrl,
status,
responseTime,
errorCode,
@@ -199,7 +221,21 @@ function createApiInstance(baseURL: string, serviceName: string = 'API'): AxiosI
operation: 'request_error'
};
// Only handle auth token clearing here, let handleApiError do the logging
// Get the appropriate logger for this service
const logger = getLoggerForService(serviceName);
// Log errors with appropriate method based on error type
if (process.env.NODE_ENV === 'development') {
if (status === 401) {
logger.authError(method, fullUrl, context);
} else if (status === 0 || !status) {
logger.networkError(method, fullUrl, message, context);
} else {
logger.requestError(method, fullUrl, status || 0, message, responseTime, context);
}
}
// Handle auth token clearing
if (status === 401) {
document.cookie = 'jwt=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
localStorage.removeItem('jwt');
@@ -385,17 +421,16 @@ export async function createSSHHost(hostData: SSHHostData): Promise<SSHHost> {
username: hostData.username,
folder: hostData.folder || '',
tags: hostData.tags || [],
pin: hostData.pin || false,
authMethod: hostData.authType,
pin: Boolean(hostData.pin),
authType: hostData.authType,
password: hostData.authType === 'password' ? hostData.password : '',
password: hostData.authType === 'password' ? hostData.password : null,
key: hostData.authType === 'key' ? hostData.key : null,
keyPassword: hostData.authType === 'key' ? hostData.keyPassword : '',
keyType: hostData.authType === 'key' ? hostData.keyType : '',
keyPassword: hostData.authType === 'key' ? hostData.keyPassword : null,
keyType: hostData.authType === 'key' ? hostData.keyType : null,
credentialId: hostData.authType === 'credential' ? hostData.credentialId : null,
enableTerminal: hostData.enableTerminal !== false,
enableTunnel: hostData.enableTunnel !== false,
enableFileManager: hostData.enableFileManager !== false,
enableTerminal: Boolean(hostData.enableTerminal),
enableTunnel: Boolean(hostData.enableTunnel),
enableFileManager: Boolean(hostData.enableFileManager),
defaultPath: hostData.defaultPath || '/',
tunnelConnections: hostData.tunnelConnections || [],
};
@@ -438,17 +473,16 @@ export async function updateSSHHost(hostId: number, hostData: SSHHostData): Prom
username: hostData.username,
folder: hostData.folder || '',
tags: hostData.tags || [],
pin: hostData.pin || false,
authMethod: hostData.authType,
pin: Boolean(hostData.pin),
authType: hostData.authType,
password: hostData.authType === 'password' ? hostData.password : '',
password: hostData.authType === 'password' ? hostData.password : null,
key: hostData.authType === 'key' ? hostData.key : null,
keyPassword: hostData.authType === 'key' ? hostData.keyPassword : '',
keyType: hostData.authType === 'key' ? hostData.keyType : '',
keyPassword: hostData.authType === 'key' ? hostData.keyPassword : null,
keyType: hostData.authType === 'key' ? hostData.keyType : null,
credentialId: hostData.authType === 'credential' ? hostData.credentialId : null,
enableTerminal: hostData.enableTerminal !== false,
enableTunnel: hostData.enableTunnel !== false,
enableFileManager: hostData.enableFileManager !== false,
enableTerminal: Boolean(hostData.enableTerminal),
enableTunnel: Boolean(hostData.enableTunnel),
enableFileManager: Boolean(hostData.enableFileManager),
defaultPath: hostData.defaultPath || '/',
tunnelConnections: hostData.tunnelConnections || [],
};
@@ -1234,7 +1268,7 @@ export async function getFoldersWithStats(): Promise<any> {
export async function renameFolder(oldName: string, newName: string): Promise<any> {
try {
const response = await authApi.put('/ssh/db/folders/rename', {
const response = await authApi.put('/ssh/folders/rename', {
oldName,
newName
});