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

@@ -91,12 +91,41 @@ http {
} }
location ~ ^/database(/.*)?$ { location ~ ^/database(/.*)?$ {
client_max_body_size 5G;
client_body_timeout 300s;
proxy_pass http://127.0.0.1:30001; proxy_pass http://127.0.0.1:30001;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_request_buffering off;
proxy_buffering off;
}
location ~ ^/db(/.*)?$ {
client_max_body_size 5G;
client_body_timeout 300s;
proxy_pass http://127.0.0.1:30001;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_request_buffering off;
proxy_buffering off;
} }
location ~ ^/encryption(/.*)?$ { location ~ ^/encryption(/.*)?$ {

View File

@@ -79,12 +79,41 @@ http {
} }
location ~ ^/database(/.*)?$ { location ~ ^/database(/.*)?$ {
client_max_body_size 5G;
client_body_timeout 300s;
proxy_pass http://127.0.0.1:30001; proxy_pass http://127.0.0.1:30001;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_request_buffering off;
proxy_buffering off;
}
location ~ ^/db(/.*)?$ {
client_max_body_size 5G;
client_body_timeout 300s;
proxy_pass http://127.0.0.1:30001;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_request_buffering off;
proxy_buffering off;
} }
location ~ ^/encryption(/.*)?$ { location ~ ^/encryption(/.*)?$ {

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.use(cookieParser());
app.get("/health", (req, res) => { app.get("/health", (req, res) => {
@@ -494,14 +496,29 @@ app.post("/database/export", authenticateJWT, async (req, res) => {
process.env.NODE_ENV === "production" process.env.NODE_ENV === "production"
? path.join(process.env.DATA_DIR || "./db/data", ".temp", "exports") ? path.join(process.env.DATA_DIR || "./db/data", ".temp", "exports")
: path.join(os.tmpdir(), "termix-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 timestamp = new Date().toISOString().replace(/[:.]/g, "-");
const filename = `termix-export-${user[0].username}-${timestamp}.sqlite`; const filename = `termix-export-${user[0].username}-${timestamp}.sqlite`;
const tempPath = path.join(tempDir, filename); const tempPath = path.join(tempDir, filename);
apiLogger.info("Creating export database", {
operation: "export_db_creation",
userId,
tempPath,
});
const exportDb = new Database(tempPath); const exportDb = new Database(tempPath);
try { try {
@@ -844,21 +861,40 @@ app.post("/database/export", authenticateJWT, async (req, res) => {
res.setHeader("Content-Disposition", `attachment; filename="${filename}"`); res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
const fileStream = fs.createReadStream(tempPath); 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", () => { fileStream.on("end", () => {
apiLogger.success("User data exported as SQLite successfully", {
operation: "user_data_sqlite_export_success",
userId,
filename,
});
fs.unlink(tempPath, (err) => { fs.unlink(tempPath, (err) => {
if (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", { fileStream.pipe(res);
operation: "user_data_sqlite_export_success",
userId,
filename,
});
} catch (error) { } catch (error) {
apiLogger.error("User data SQLite export failed", error, { apiLogger.error("User data SQLite export failed", error, {
operation: "user_data_sqlite_export_failed", operation: "user_data_sqlite_export_failed",

View File

@@ -54,9 +54,29 @@ export class DatabaseMigration {
let reason = ""; let reason = "";
if (hasEncryptedDb && hasUnencryptedDb) { if (hasEncryptedDb && hasUnencryptedDb) {
needsMigration = false; const unencryptedSize = fs.statSync(this.unencryptedDbPath).size;
reason = const encryptedSize = fs.statSync(this.encryptedDbPath).size;
"Both encrypted and unencrypted databases exist. Skipping migration for safety. Manual intervention may be required.";
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) { } else if (hasEncryptedDb && !hasUnencryptedDb) {
needsMigration = false; needsMigration = false;
reason = "Only encrypted database exists. No migration needed."; reason = "Only encrypted database exists. No migration needed.";

View File

@@ -26,6 +26,20 @@ class SystemCrypto {
return; 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(); await this.generateAndGuideUser();
} catch (error) { } catch (error) {
databaseLogger.error("Failed to initialize JWT secret", error, { databaseLogger.error("Failed to initialize JWT secret", error, {
@@ -50,6 +64,20 @@ class SystemCrypto {
return; 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(); await this.generateAndGuideDatabaseKey();
} catch (error) { } catch (error) {
databaseLogger.error("Failed to initialize database key", error, { databaseLogger.error("Failed to initialize database key", error, {
@@ -74,6 +102,20 @@ class SystemCrypto {
return; 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(); await this.generateAndGuideInternalAuthToken();
} catch (error) { } catch (error) {
databaseLogger.error("Failed to initialize internal auth token", error, { databaseLogger.error("Failed to initialize internal auth token", error, {

View File

@@ -267,6 +267,7 @@
"newVersionAvailable": "A new version ({{version}}) is available.", "newVersionAvailable": "A new version ({{version}}) is available.",
"failedToFetchUpdateInfo": "Failed to fetch update information", "failedToFetchUpdateInfo": "Failed to fetch update information",
"preRelease": "Pre-release", "preRelease": "Pre-release",
"loginFailed": "Login failed",
"noReleasesFound": "No releases found.", "noReleasesFound": "No releases found.",
"yourBackupCodes": "Your Backup Codes", "yourBackupCodes": "Your Backup Codes",
"sendResetCode": "Send Reset Code", "sendResetCode": "Send Reset Code",
@@ -1406,6 +1407,10 @@
}, },
"mobile": { "mobile": {
"selectHostToStart": "Select a host to start your terminal session", "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}}) 可用。", "newVersionAvailable": "有新版本 ({{version}}) 可用。",
"failedToFetchUpdateInfo": "获取更新信息失败", "failedToFetchUpdateInfo": "获取更新信息失败",
"preRelease": "预发布版本", "preRelease": "预发布版本",
"loginFailed": "登录失败",
"noReleasesFound": "未找到发布版本。", "noReleasesFound": "未找到发布版本。",
"yourBackupCodes": "您的备份代码", "yourBackupCodes": "您的备份代码",
"sendResetCode": "发送重置代码", "sendResetCode": "发送重置代码",
@@ -1313,6 +1314,10 @@
}, },
"mobile": { "mobile": {
"selectHostToStart": "选择一个主机以开始您的终端会话", "selectHostToStart": "选择一个主机以开始您的终端会话",
"limitedSupportMessage": "移动端支持目前有限。我们即将推出专门的移动应用以提升您的体验。" "limitedSupportMessage": "网站移动端支持仍在开发中。使用移动应用以获得更好的体验。",
"mobileAppInProgress": "移动应用开发中",
"mobileAppInProgressDesc": "我们正在开发专门的移动应用,为移动设备提供更好的体验。",
"viewMobileAppDocs": "安装移动应用",
"mobileAppDocumentation": "移动应用文档"
} }
} }

View File

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

View File

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

View File

@@ -534,6 +534,28 @@ export function HomepageAuth({
</div> </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 && ( {!internalLoggedIn && !authLoading && !totpRequired && (
<> <>
<div className="flex gap-2 mb-6"> <div className="flex gap-2 mb-6">
@@ -855,6 +877,17 @@ export function HomepageAuth({
<LanguageSwitcher /> <LanguageSwitcher />
</div> </div>
</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> </div>

View File

@@ -162,6 +162,12 @@ const AppContent: FC = () => {
<p className="text-sm text-gray-300 max-w-xs"> <p className="text-sm text-gray-300 max-w-xs">
{t("mobile.limitedSupportMessage")} {t("mobile.limitedSupportMessage")}
</p> </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>
)} )}
</div> </div>