UNPKG

ssh-terminal

Version:

SSH Terminal component based on xterm.js for multiple frontend frameworks

147 lines (146 loc) 6.81 kB
class l { /** * Xác thực SSH configuration */ static validateSSHConfig(e) { const t = []; return e.host ? typeof e.host != "string" ? t.push("Host phải là string") : e.host.length > 255 ? t.push("Host quá dài (tối đa 255 ký tự)") : this.isValidHost(e.host) || t.push("Host không hợp lệ") : t.push("Host là bắt buộc"), e.username ? typeof e.username != "string" ? t.push("Username phải là string") : e.username.length > 32 ? t.push("Username quá dài (tối đa 32 ký tự)") : this.isValidUsername(e.username) || t.push("Username chứa ký tự không hợp lệ") : t.push("Username là bắt buộc"), e.password ? typeof e.password != "string" ? t.push("Password phải là string") : e.password.length < 1 ? t.push("Password không được để trống") : e.password.length > 128 && t.push("Password quá dài (tối đa 128 ký tự)") : t.push("Password là bắt buộc"), e.port !== void 0 && (Number.isInteger(e.port) ? (e.port < 1 || e.port > 65535) && t.push("Port phải trong khoảng 1-65535") : t.push("Port phải là số nguyên")), e.wsUrl && !this.isValidWebSocketURL(e.wsUrl) && t.push("WebSocket URL không hợp lệ"), { isValid: t.length === 0, errors: t }; } /** * Kiểm tra host hợp lệ (IP hoặc domain) */ static isValidHost(e) { return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(e) || /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/.test(e) ? !0 : /^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/.test(e); } /** * Kiểm tra username hợp lệ */ static isValidUsername(e) { return /^[a-zA-Z0-9_-]+$/.test(e); } /** * Kiểm tra WebSocket URL hợp lệ (khuyến nghị wss:// cho bảo mật) */ static isValidWebSocketURL(e) { try { const t = new URL(e); return t.protocol === "ws:" || t.protocol === "wss:"; } catch (t) { return !1; } } /** * Xác thực encryption readiness */ static validateEncryptionReadiness(e, t) { const s = []; return e || s.push("Encryption service not initialized"), t || s.push("Server public key not available"), { isValid: s.length === 0, errors: s }; } /** * Xác thực computing configuration */ static validateComputingConfig(e) { const t = []; return e.computingId ? typeof e.computingId != "string" ? t.push("Computing ID must be a string") : /^[0-9.]+$/.test(e.computingId) ? e.computingId.length > 50 && t.push("Computing ID must not exceed 50 characters") : t.push("Computing ID must contain only numbers and dots") : t.push("Computing ID is required"), e.userToken ? typeof e.userToken != "string" ? t.push("User token must be a string") : e.userToken.length > 8192 && t.push("User token is too large") : t.push("User token is required"), e.wsUrl && !this.isValidWebSocketURL(e.wsUrl) && t.push("Invalid WebSocket URL"), { isValid: t.length === 0, errors: t }; } /** * Xác thực rằng connection chỉ sử dụng encrypted protocols */ static validateSecureConnection(e) { const t = [], s = []; if (!e) return t.push("WebSocket URL is required"), { isValid: !1, errors: t, warnings: s }; try { const r = new URL(e); r.protocol === "ws:" ? s.push("Warning: Using unencrypted WebSocket (ws://). Consider using wss:// for better security.") : r.protocol !== "wss:" && t.push("Invalid WebSocket protocol. Only ws:// and wss:// are supported."); } catch (r) { t.push("Invalid WebSocket URL format"); } return { isValid: t.length === 0, errors: t, warnings: s }; } /** * Làm sạch string input */ static sanitizeString(e, t = 255) { return typeof e != "string" ? "" : e.trim().slice(0, t).replace(/[\x00-\x1F\x7F]/g, ""); } /** * Xác thực terminal options */ static validateTerminalOptions(e) { const t = []; if (typeof e != "object" || e === null) return { isValid: !1, errors: ["Options phải là object"] }; if (e.fontSize !== void 0 && (!Number.isInteger(e.fontSize) || e.fontSize < 8 || e.fontSize > 72) && t.push("fontSize phải là số nguyên từ 8-72"), e.scrollback !== void 0 && (!Number.isInteger(e.scrollback) || e.scrollback < 0 || e.scrollback > 1e4) && t.push("scrollback phải là số nguyên từ 0-10000"), e.theme !== void 0) if (typeof e.theme != "object") t.push("theme phải là object"); else { const s = ["background", "foreground", "cursor", "cursorAccent", "selection"]; for (const [r, n] of Object.entries(e.theme)) s.includes(r) && typeof n == "string" && (this.isValidColor(n) || t.push(`theme.${r} không phải màu hợp lệ`)); } return e.reconnection !== void 0 && (typeof e.reconnection != "object" ? t.push("reconnection phải là object") : (e.reconnection.maxAttempts !== void 0 && (!Number.isInteger(e.reconnection.maxAttempts) || e.reconnection.maxAttempts < 0 || e.reconnection.maxAttempts > 20) && t.push("reconnection.maxAttempts phải là số nguyên từ 0-20"), e.reconnection.heartbeatInterval !== void 0 && (!Number.isInteger(e.reconnection.heartbeatInterval) || e.reconnection.heartbeatInterval < 1e3 || e.reconnection.heartbeatInterval > 3e5) && t.push("reconnection.heartbeatInterval phải là số nguyên từ 1000-300000ms"))), { isValid: t.length === 0, errors: t }; } /** * Kiểm tra màu hợp lệ (hex, rgb, rgba) */ static isValidColor(e) { return /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(e) ? !0 : /^rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(?:,\s*[\d.]+\s*)?\)$/.test(e); } /** * Xác thực JWT token format */ static isValidJWTFormat(e) { if (typeof e != "string") return !1; const t = e.split("."); return t.length === 3 && t.every((s) => s.length > 0); } /** * Xác thực message size */ static validateMessageSize(e, t = 1e4) { if (typeof e == "string") return e.length <= t; if (e instanceof ArrayBuffer) return e.byteLength <= t; try { return JSON.stringify(e).length <= t; } catch (s) { return !1; } } /** * Làm sạch object để logging an toàn */ static sanitizeForLogging(e) { if (typeof e != "object" || e === null) return e; const t = ["password", "token", "key", "secret", "auth", "credential"], s = {}; for (const [r, n] of Object.entries(e)) { const i = r.toLowerCase(); t.some((a) => i.includes(a)) ? s[r] = "***HIDDEN***" : typeof n == "object" && n !== null ? s[r] = this.sanitizeForLogging(n) : s[r] = n; } return s; } } const o = l; export { o as default };