Fix login and backend errors

This commit is contained in:
LukeGus
2025-09-30 20:46:04 -05:00
parent e71ed6cd3a
commit 19a5cbe43d
11 changed files with 233 additions and 24 deletions

View File

@@ -207,7 +207,9 @@ async function fetchGitHubAPI(
}
}
app.use(bodyParser.json());
app.use(bodyParser.json({ limit: "1gb" }));
app.use(bodyParser.urlencoded({ limit: "1gb", extended: true }));
app.use(bodyParser.raw({ limit: "5gb", type: "application/octet-stream" }));
app.use(cookieParser());
app.get("/health", (req, res) => {
@@ -494,14 +496,29 @@ app.post("/database/export", authenticateJWT, async (req, res) => {
process.env.NODE_ENV === "production"
? path.join(process.env.DATA_DIR || "./db/data", ".temp", "exports")
: path.join(os.tmpdir(), "termix-exports");
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
try {
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
} catch (dirError) {
apiLogger.error("Failed to create temp directory", dirError, {
operation: "export_temp_dir_error",
tempDir,
});
throw new Error(`Failed to create temp directory: ${dirError.message}`);
}
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
const filename = `termix-export-${user[0].username}-${timestamp}.sqlite`;
const tempPath = path.join(tempDir, filename);
apiLogger.info("Creating export database", {
operation: "export_db_creation",
userId,
tempPath,
});
const exportDb = new Database(tempPath);
try {
@@ -844,21 +861,40 @@ app.post("/database/export", authenticateJWT, async (req, res) => {
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
const fileStream = fs.createReadStream(tempPath);
fileStream.pipe(res);
fileStream.on("error", (streamError) => {
apiLogger.error("File stream error during export", streamError, {
operation: "export_file_stream_error",
userId,
tempPath,
});
if (!res.headersSent) {
res.status(500).json({
error: "Failed to stream export file",
details: streamError.message,
});
}
});
fileStream.on("end", () => {
apiLogger.success("User data exported as SQLite successfully", {
operation: "user_data_sqlite_export_success",
userId,
filename,
});
fs.unlink(tempPath, (err) => {
if (err) {
apiLogger.warn("Failed to clean up export file", { path: tempPath });
apiLogger.warn("Failed to clean up export file", {
operation: "export_cleanup_failed",
path: tempPath,
error: err.message
});
}
});
});
apiLogger.success("User data exported as SQLite successfully", {
operation: "user_data_sqlite_export_success",
userId,
filename,
});
fileStream.pipe(res);
} catch (error) {
apiLogger.error("User data SQLite export failed", error, {
operation: "user_data_sqlite_export_failed",

View File

@@ -54,9 +54,29 @@ export class DatabaseMigration {
let reason = "";
if (hasEncryptedDb && hasUnencryptedDb) {
needsMigration = false;
reason =
"Both encrypted and unencrypted databases exist. Skipping migration for safety. Manual intervention may be required.";
const unencryptedSize = fs.statSync(this.unencryptedDbPath).size;
const encryptedSize = fs.statSync(this.encryptedDbPath).size;
if (unencryptedSize === 0) {
needsMigration = false;
reason = "Empty unencrypted database found alongside encrypted database. Removing empty file.";
try {
fs.unlinkSync(this.unencryptedDbPath);
databaseLogger.info("Removed empty unencrypted database file", {
operation: "migration_cleanup_empty",
path: this.unencryptedDbPath,
});
} catch (error) {
databaseLogger.warn("Failed to remove empty unencrypted database", {
operation: "migration_cleanup_empty_failed",
error: error instanceof Error ? error.message : "Unknown error",
});
}
} else {
needsMigration = false;
reason =
"Both encrypted and unencrypted databases exist. Skipping migration for safety. Manual intervention may be required.";
}
} else if (hasEncryptedDb && !hasUnencryptedDb) {
needsMigration = false;
reason = "Only encrypted database exists. No migration needed.";

View File

@@ -26,6 +26,20 @@ class SystemCrypto {
return;
}
const dataDir = process.env.DATA_DIR || "./db/data";
const envPath = path.join(dataDir, ".env");
try {
const envContent = await fs.readFile(envPath, "utf8");
const jwtMatch = envContent.match(/^JWT_SECRET=(.+)$/m);
if (jwtMatch && jwtMatch[1] && jwtMatch[1].length >= 64) {
this.jwtSecret = jwtMatch[1];
process.env.JWT_SECRET = jwtMatch[1];
return;
}
} catch {
}
await this.generateAndGuideUser();
} catch (error) {
databaseLogger.error("Failed to initialize JWT secret", error, {
@@ -50,6 +64,20 @@ class SystemCrypto {
return;
}
const dataDir = process.env.DATA_DIR || "./db/data";
const envPath = path.join(dataDir, ".env");
try {
const envContent = await fs.readFile(envPath, "utf8");
const dbKeyMatch = envContent.match(/^DATABASE_KEY=(.+)$/m);
if (dbKeyMatch && dbKeyMatch[1] && dbKeyMatch[1].length >= 64) {
this.databaseKey = Buffer.from(dbKeyMatch[1], "hex");
process.env.DATABASE_KEY = dbKeyMatch[1];
return;
}
} catch {
}
await this.generateAndGuideDatabaseKey();
} catch (error) {
databaseLogger.error("Failed to initialize database key", error, {
@@ -74,6 +102,20 @@ class SystemCrypto {
return;
}
const dataDir = process.env.DATA_DIR || "./db/data";
const envPath = path.join(dataDir, ".env");
try {
const envContent = await fs.readFile(envPath, "utf8");
const tokenMatch = envContent.match(/^INTERNAL_AUTH_TOKEN=(.+)$/m);
if (tokenMatch && tokenMatch[1] && tokenMatch[1].length >= 32) {
this.internalAuthToken = tokenMatch[1];
process.env.INTERNAL_AUTH_TOKEN = tokenMatch[1];
return;
}
} catch {
}
await this.generateAndGuideInternalAuthToken();
} catch (error) {
databaseLogger.error("Failed to initialize internal auth token", error, {

View File

@@ -267,6 +267,7 @@
"newVersionAvailable": "A new version ({{version}}) is available.",
"failedToFetchUpdateInfo": "Failed to fetch update information",
"preRelease": "Pre-release",
"loginFailed": "Login failed",
"noReleasesFound": "No releases found.",
"yourBackupCodes": "Your Backup Codes",
"sendResetCode": "Send Reset Code",
@@ -1406,6 +1407,10 @@
},
"mobile": {
"selectHostToStart": "Select a host to start your terminal session",
"limitedSupportMessage": "Mobile support is currently limited. A dedicated mobile app is coming soon to enhance your experience."
"limitedSupportMessage": "Website mobile support is still in progress. Use the mobile app for a better experience.",
"mobileAppInProgress": "Mobile app is in progress",
"mobileAppInProgressDesc": "We're working on a dedicated mobile app to provide a better experience on mobile devices.",
"viewMobileAppDocs": "Install Mobile App",
"mobileAppDocumentation": "Mobile App Documentation"
}
}

View File

@@ -264,6 +264,7 @@
"newVersionAvailable": "有新版本 ({{version}}) 可用。",
"failedToFetchUpdateInfo": "获取更新信息失败",
"preRelease": "预发布版本",
"loginFailed": "登录失败",
"noReleasesFound": "未找到发布版本。",
"yourBackupCodes": "您的备份代码",
"sendResetCode": "发送重置代码",
@@ -1313,6 +1314,10 @@
},
"mobile": {
"selectHostToStart": "选择一个主机以开始您的终端会话",
"limitedSupportMessage": "移动端支持目前有限。我们即将推出专门的移动应用以提升您的体验。"
"limitedSupportMessage": "网站移动端支持仍在开发中。使用移动应用以获得更好的体验。",
"mobileAppInProgress": "移动应用开发中",
"mobileAppInProgressDesc": "我们正在开发专门的移动应用,为移动设备提供更好的体验。",
"viewMobileAppDocs": "安装移动应用",
"mobileAppDocumentation": "移动应用文档"
}
}

View File

@@ -273,7 +273,7 @@ export function AdminSettings({
try {
const apiUrl = isElectron()
? `${(window as any).configuredServerUrl}/database/export`
: "/database/export";
: `http://localhost:30001/database/export`;
const response = await fetch(apiUrl, {
method: "POST",
@@ -333,7 +333,7 @@ export function AdminSettings({
try {
const apiUrl = isElectron()
? `${(window as any).configuredServerUrl}/database/import`
: "/database/import";
: `http://localhost:30001/database/import`;
const formData = new FormData();
formData.append("file", importFile);

View File

@@ -184,7 +184,8 @@ export const Terminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
isUnmountingRef.current ||
shouldNotReconnectRef.current ||
isReconnectingRef.current ||
isConnectingRef.current
isConnectingRef.current ||
wasDisconnectedBySSH.current
) {
return;
}
@@ -213,7 +214,7 @@ export const Terminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
);
reconnectTimeoutRef.current = setTimeout(() => {
if (isUnmountingRef.current || shouldNotReconnectRef.current) {
if (isUnmountingRef.current || shouldNotReconnectRef.current || wasDisconnectedBySSH.current) {
isReconnectingRef.current = false;
return;
}
@@ -384,6 +385,7 @@ export const Terminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
terminal.clear();
}
setIsConnecting(true);
wasDisconnectedBySSH.current = false;
attemptReconnection();
return;
}
@@ -408,9 +410,9 @@ export const Terminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
if (terminal) {
terminal.clear();
}
setIsConnecting(true);
if (!isUnmountingRef.current && !shouldNotReconnectRef.current) {
attemptReconnection();
setIsConnecting(false);
if (onClose) {
onClose();
}
}
} catch (error) {
@@ -438,12 +440,13 @@ export const Terminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
return;
}
setIsConnecting(true);
setIsConnecting(false);
if (
!wasDisconnectedBySSH.current &&
!isUnmountingRef.current &&
!shouldNotReconnectRef.current
) {
wasDisconnectedBySSH.current = false;
attemptReconnection();
}
});
@@ -455,8 +458,9 @@ export const Terminal = forwardRef<any, SSHTerminalProps>(function SSHTerminal(
if (terminal) {
terminal.clear();
}
setIsConnecting(true);
setIsConnecting(false);
if (!isUnmountingRef.current && !shouldNotReconnectRef.current) {
wasDisconnectedBySSH.current = false;
attemptReconnection();
}
});

View File

@@ -534,6 +534,28 @@ export function HomepageAuth({
</div>
)}
{internalLoggedIn && !authLoading && (
<div className="flex flex-col gap-5">
<div className="mb-6 text-center">
<h2 className="text-xl font-bold mb-1">
{t("homepage.loggedInTitle")}
</h2>
<p className="text-muted-foreground">
{t("mobile.mobileAppInProgressDesc")}
</p>
</div>
<Button
type="button"
variant="outline"
className="w-full h-11 text-base font-semibold"
onClick={() => window.open("https://docs.termix.site/install", "_blank")}
>
{t("mobile.viewMobileAppDocs")}
</Button>
</div>
)}
{!internalLoggedIn && !authLoading && !totpRequired && (
<>
<div className="flex gap-2 mb-6">
@@ -855,6 +877,17 @@ export function HomepageAuth({
<LanguageSwitcher />
</div>
</div>
<div className="mt-4">
<Button
type="button"
variant="outline"
className="w-full h-11 text-base font-semibold"
onClick={() => window.open("https://docs.termix.site/install", "_blank")}
>
{t("mobile.viewMobileAppDocs")}
</Button>
</div>
</>
)}
</div>

View File

@@ -162,6 +162,12 @@ const AppContent: FC = () => {
<p className="text-sm text-gray-300 max-w-xs">
{t("mobile.limitedSupportMessage")}
</p>
<button
className="mt-4 px-6 py-3 bg-primary text-primary-foreground rounded-lg font-semibold hover:bg-primary/90 transition-colors"
onClick={() => window.open("https://docs.termix.site/install", "_blank")}
>
{t("mobile.viewMobileAppDocs")}
</button>
</div>
)}
</div>