mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(device-pairing): pair phones and browsers with a QR code
Settings -> Account gets "Connect a device", a paired-devices list, and a remote access headers editor, so the mobile app and other browsers can sign in without typing a password and pass a reverse proxy such as Cloudflare Access. - one-time pairing codes (3 min TTL) minted by a Better Auth plugin and redeemed into a new session that records the device name - POST /api/device-pairing/links builds the ocm://link app link with the stored headers as h= parameters and a browser /pair link; plain-HTTP and loopback origins are blocked or warned about - remote access headers are stored encrypted, returned by name only, and validated (8 headers, 512-char values, 1800 encoded characters, reserved transport names blocked); a Cloudflare Access service token preset fills CF-Access-Client-Id and CF-Access-Client-Secret - login offers "Sign in with QR code" and /pair redeems a scanned code, returning to the login redirect target - revoking a device deletes its session, closes its live connections and removes its push subscriptions; the current session cannot be revoked
This commit is contained in:
1 parent
5b0e629182
commit
f8e4cfe136
79 files changed
+5398
-110
No files matched your search
@@ -7,3 +7,4 @@ export * from './semver'
|
||||
export * from './terminal-title'
|
||||
export * from './unified-diff'
|
||||
export * from './url'
|
||||
export * from './user-agent'
|
||||
@@ -9,6 +9,50 @@ export function haveSameUrlOrigin(first: string, second: string): boolean {
|
||||
return origin !== null && origin === readUrlOrigin(second)
|
||||
}
|
||||
|
||||
/** Reports whether a hostname refers to the local machine. */
|
||||
export function isLoopbackHost(hostname: string): boolean {
|
||||
const host = normalizeHostname(hostname)
|
||||
if (host === 'localhost' || host === '::1') return true
|
||||
const octets = parseIpv4Octets(host)
|
||||
return octets !== null && octets[0] === 127
|
||||
}
|
||||
|
||||
/** Reports whether a hostname is an RFC 1918, link-local, or CGNAT address. */
|
||||
export function isPrivateNetworkHost(hostname: string): boolean {
|
||||
const host = normalizeHostname(hostname)
|
||||
const octets = parseIpv4Octets(host)
|
||||
if (octets !== null) {
|
||||
const [first, second] = octets
|
||||
return (
|
||||
first === 10 ||
|
||||
(first === 172 && second >= 16 && second <= 31) ||
|
||||
(first === 192 && second === 168) ||
|
||||
(first === 169 && second === 254) ||
|
||||
(first === 100 && second >= 64 && second <= 127)
|
||||
)
|
||||
}
|
||||
if (!host.includes(':')) return false
|
||||
const firstHextet = Number.parseInt(host.split(':')[0] ?? '', 16)
|
||||
return firstHextet >= 0xfc00 && firstHextet <= 0xfdff
|
||||
}
|
||||
|
||||
function normalizeHostname(hostname: string): string {
|
||||
return hostname.trim().toLowerCase().replace(/^\[/, '').replace(/\]$/, '')
|
||||
}
|
||||
|
||||
function parseIpv4Octets(host: string): [number, number, number, number] | null {
|
||||
const parts = host.split('.')
|
||||
if (parts.length !== 4) return null
|
||||
const octets: [number, number, number, number] = [0, 0, 0, 0]
|
||||
for (const [index, part] of parts.entries()) {
|
||||
if (!/^\d{1,3}$/.test(part)) return null
|
||||
const value = Number(part)
|
||||
if (value > 255) return null
|
||||
octets[index] = value
|
||||
}
|
||||
return octets
|
||||
}
|
||||
|
||||
function readUrlOrigin(value: string): string | null {
|
||||
try {
|
||||
return new URL(value).origin
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
const UNKNOWN_DEVICE = 'Unknown device'
|
||||
const RAW_USER_AGENT_MAX_LENGTH = 80
|
||||
|
||||
/** Describes a user agent as "browser on platform", falling back to the raw string. */
|
||||
export function describeUserAgent(userAgent: string | null | undefined): string {
|
||||
const value = userAgent?.trim()
|
||||
if (!value) return UNKNOWN_DEVICE
|
||||
|
||||
const platform = detectPlatform(value)
|
||||
const browser = detectBrowser(value)
|
||||
if (browser && platform) return `${browser} on ${platform}`
|
||||
return browser ?? platform ?? value.slice(0, RAW_USER_AGENT_MAX_LENGTH)
|
||||
}
|
||||
|
||||
function detectPlatform(userAgent: string): string | null {
|
||||
if (userAgent.includes('iPhone')) return 'iPhone'
|
||||
if (userAgent.includes('iPad')) return 'iPad'
|
||||
if (userAgent.includes('Android')) return 'Android'
|
||||
if (userAgent.includes('Macintosh')) return 'macOS'
|
||||
if (userAgent.includes('Windows')) return 'Windows'
|
||||
if (userAgent.includes('Linux')) return 'Linux'
|
||||
return null
|
||||
}
|
||||
|
||||
function detectBrowser(userAgent: string): string | null {
|
||||
if (userAgent.includes('Edg/')) return 'Edge'
|
||||
if (userAgent.includes('OPR/')) return 'Opera'
|
||||
if (userAgent.includes('Firefox/') || userAgent.includes('FxiOS/')) return 'Firefox'
|
||||
if (userAgent.includes('CriOS/') || userAgent.includes('Chrome/')) return 'Chrome'
|
||||
if (userAgent.includes('Safari/') && userAgent.includes('Version/')) return 'Safari'
|
||||
return null
|
||||
}
|
||||
Reference in new issue
Block a user