Add browser support with Socket.io transport, authentication, performance-optimized IPC, and CI/CD (#925)
This commit is contained in:
65
app/src/components/BrowserAuthWrapper.tsx
Normal file
65
app/src/components/BrowserAuthWrapper.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import * as React from 'react'
|
||||
import { LoginDialog } from './LoginDialog'
|
||||
|
||||
interface BrowserAuthWrapperProps {
|
||||
children: React.ReactNode
|
||||
}
|
||||
|
||||
const isBrowserMode =
|
||||
typeof window !== 'undefined' &&
|
||||
(typeof process === 'undefined' || process.env?.BROWSER_MODE === 'true')
|
||||
|
||||
export function BrowserAuthWrapper(props: BrowserAuthWrapperProps) {
|
||||
const [isAuthenticated, setIsAuthenticated] = React.useState(false)
|
||||
const [loginError, setLoginError] = React.useState<string | undefined>()
|
||||
const [showLogin, setShowLogin] = React.useState(false)
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!isBrowserMode) {
|
||||
// Not in browser mode, skip authentication
|
||||
setIsAuthenticated(true)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if already authenticated
|
||||
const username = sessionStorage.getItem('mqtt-explorer-username')
|
||||
const password = sessionStorage.getItem('mqtt-explorer-password')
|
||||
|
||||
if (username && password) {
|
||||
// Try to use stored credentials
|
||||
setIsAuthenticated(true)
|
||||
} else {
|
||||
// Show login dialog
|
||||
setShowLogin(true)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleLogin = async (username: string, password: string) => {
|
||||
try {
|
||||
// Store credentials in session storage
|
||||
sessionStorage.setItem('mqtt-explorer-username', username)
|
||||
sessionStorage.setItem('mqtt-explorer-password', password)
|
||||
|
||||
// The socket will use these credentials on next connection
|
||||
setIsAuthenticated(true)
|
||||
setShowLogin(false)
|
||||
setLoginError(undefined)
|
||||
|
||||
// Reload to reinitialize socket with new auth
|
||||
window.location.reload()
|
||||
} catch (error) {
|
||||
setLoginError('Login failed. Please check your credentials.')
|
||||
}
|
||||
}
|
||||
|
||||
if (!isBrowserMode) {
|
||||
// Not in browser mode, render children directly
|
||||
return <>{props.children}</>
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <LoginDialog open={showLogin} onLogin={handleLogin} error={loginError} />
|
||||
}
|
||||
|
||||
return <>{props.children}</>
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import * as React from 'react'
|
||||
import ClearAdornment from '../helper/ClearAdornment'
|
||||
import Lock from '@material-ui/icons/Lock'
|
||||
import { bindActionCreators } from 'redux'
|
||||
import { Button, Theme, Tooltip, Typography } from '@material-ui/core'
|
||||
import { CertificateParameters, ConnectionOptions } from '../../model/ConnectionOptions'
|
||||
import { CertificateTypes } from '../../actions/ConnectionManager'
|
||||
import { connect } from 'react-redux'
|
||||
import { connectionManagerActions } from '../../actions'
|
||||
import { withStyles } from '@material-ui/styles'
|
||||
import { rendererRpc } from '../../../../events'
|
||||
import { RpcEvents } from '../../../../events/EventsV2'
|
||||
|
||||
function BrowserCertificateFileSelection(props: {
|
||||
certificateType: CertificateTypes
|
||||
title: string
|
||||
certificate?: CertificateParameters
|
||||
classes: any
|
||||
actions: {
|
||||
connectionManager: typeof connectionManagerActions
|
||||
}
|
||||
connection: ConnectionOptions
|
||||
}) {
|
||||
const fileInputRef = React.useRef<HTMLInputElement>(null)
|
||||
|
||||
const clearCertificate = React.useCallback(() => {
|
||||
props.actions.connectionManager.updateConnection(props.connection.id, {
|
||||
[props.certificateType]: undefined,
|
||||
})
|
||||
}, [props.connection, props.certificateType])
|
||||
|
||||
const handleFileSelect = React.useCallback(
|
||||
async (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = event.target.files?.[0]
|
||||
if (!file) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// Read file content
|
||||
const reader = new FileReader()
|
||||
reader.onload = async e => {
|
||||
const content = e.target?.result
|
||||
if (typeof content === 'string') {
|
||||
// Convert to base64
|
||||
const base64Data = content.split(',')[1] || content
|
||||
|
||||
// Upload via IPC instead of HTTP POST
|
||||
const result = await rendererRpc.call(RpcEvents.uploadCertificate, {
|
||||
filename: file.name,
|
||||
data: base64Data,
|
||||
})
|
||||
|
||||
// Create certificate parameters
|
||||
const certificate: CertificateParameters = {
|
||||
name: result.name,
|
||||
data: result.data,
|
||||
}
|
||||
|
||||
// Update connection
|
||||
props.actions.connectionManager.updateConnection(props.connection.id, {
|
||||
[props.certificateType]: certificate,
|
||||
})
|
||||
}
|
||||
}
|
||||
reader.readAsDataURL(file)
|
||||
} catch (error) {
|
||||
console.error('Error uploading certificate:', error)
|
||||
}
|
||||
|
||||
// Reset input
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = ''
|
||||
}
|
||||
},
|
||||
[props.connection.id, props.certificateType, props.actions.connectionManager]
|
||||
)
|
||||
|
||||
const handleButtonClick = () => {
|
||||
fileInputRef.current?.click()
|
||||
}
|
||||
|
||||
return (
|
||||
<span>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept=".pem,.crt,.cer,.key"
|
||||
style={{ display: 'none' }}
|
||||
onChange={handleFileSelect}
|
||||
/>
|
||||
<Tooltip title="Select certificate" placement="top">
|
||||
<Button variant="contained" className={props.classes.button} onClick={handleButtonClick}>
|
||||
<Lock /> {props.title}
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<ClearCertificate classes={props.classes} certificate={props.certificate} action={clearCertificate} />
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function ClearCertificate(props: { classes: any; certificate?: CertificateParameters; action: () => void }) {
|
||||
if (!props.certificate) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<Tooltip title={props.certificate.name}>
|
||||
<Typography className={props.classes.certificateName}>
|
||||
<ClearAdornment action={props.action} value={props.certificate.name} />
|
||||
{props.certificate.name}
|
||||
</Typography>
|
||||
</Tooltip>
|
||||
)
|
||||
}
|
||||
|
||||
const mapDispatchToProps = (dispatch: any) => {
|
||||
return {
|
||||
actions: {
|
||||
connectionManager: bindActionCreators(connectionManagerActions, dispatch),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const styles = (theme: Theme) => ({
|
||||
certificateName: {
|
||||
width: '100%',
|
||||
height: 'calc(1em + 4px)',
|
||||
overflow: 'hidden' as 'hidden',
|
||||
whiteSpace: 'nowrap' as 'nowrap',
|
||||
textOverflow: 'ellipsis' as 'ellipsis',
|
||||
color: theme.palette.text.hint,
|
||||
},
|
||||
button: {
|
||||
marginTop: theme.spacing(3),
|
||||
marginRight: theme.spacing(2),
|
||||
},
|
||||
})
|
||||
|
||||
export default connect(undefined, mapDispatchToProps)(withStyles(styles)(BrowserCertificateFileSelection))
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as React from 'react'
|
||||
import CertificateFileSelection from './CertificateFileSelection'
|
||||
import BrowserCertificateFileSelection from './BrowserCertificateFileSelection'
|
||||
import Undo from '@material-ui/icons/Undo'
|
||||
import { bindActionCreators } from 'redux'
|
||||
import { Button, Grid } from '@material-ui/core'
|
||||
@@ -8,6 +9,12 @@ import { connectionManagerActions } from '../../actions'
|
||||
import { ConnectionOptions } from '../../model/ConnectionOptions'
|
||||
import { Theme, withStyles } from '@material-ui/core/styles'
|
||||
|
||||
// Check if we're in browser mode
|
||||
const isBrowserMode =
|
||||
typeof window !== 'undefined' &&
|
||||
(typeof process === 'undefined' || process.env?.BROWSER_MODE === 'true')
|
||||
const CertSelector = isBrowserMode ? BrowserCertificateFileSelection : CertificateFileSelection
|
||||
|
||||
interface Props {
|
||||
connection: ConnectionOptions
|
||||
classes: any
|
||||
@@ -45,7 +52,7 @@ class Certificates extends React.PureComponent<Props, State> {
|
||||
<form noValidate={true} autoComplete="off">
|
||||
<Grid container={true} spacing={3}>
|
||||
<Grid item={true} xs={12} className={classes.gridPadding}>
|
||||
<CertificateFileSelection
|
||||
<CertSelector
|
||||
connection={this.props.connection}
|
||||
certificate={this.props.connection.selfSignedCertificate}
|
||||
title="Server Certificate (CA)"
|
||||
@@ -53,7 +60,7 @@ class Certificates extends React.PureComponent<Props, State> {
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item={true} xs={12} className={classes.gridPadding}>
|
||||
<CertificateFileSelection
|
||||
<CertSelector
|
||||
connection={this.props.connection}
|
||||
certificate={this.props.connection.clientCertificate}
|
||||
title="Client Certificate"
|
||||
@@ -61,7 +68,7 @@ class Certificates extends React.PureComponent<Props, State> {
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item={true} xs={12} className={classes.gridPadding}>
|
||||
<CertificateFileSelection
|
||||
<CertSelector
|
||||
connection={this.props.connection}
|
||||
certificate={this.props.connection.clientKey}
|
||||
title="Client Key"
|
||||
|
||||
57
app/src/components/LoginDialog.tsx
Normal file
57
app/src/components/LoginDialog.tsx
Normal file
@@ -0,0 +1,57 @@
|
||||
import * as React from 'react'
|
||||
import { Dialog, DialogTitle, DialogContent, DialogActions, TextField, Button, Typography } from '@material-ui/core'
|
||||
|
||||
interface LoginDialogProps {
|
||||
open: boolean
|
||||
onLogin: (username: string, password: string) => void
|
||||
error?: string
|
||||
}
|
||||
|
||||
export function LoginDialog(props: LoginDialogProps) {
|
||||
const [username, setUsername] = React.useState('')
|
||||
const [password, setPassword] = React.useState('')
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
props.onLogin(username, password)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={props.open} disableEscapeKeyDown disableBackdropClick>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<DialogTitle>Login to MQTT Explorer</DialogTitle>
|
||||
<DialogContent>
|
||||
{props.error && (
|
||||
<Typography color="error" style={{ marginBottom: 16 }}>
|
||||
{props.error}
|
||||
</Typography>
|
||||
)}
|
||||
<TextField
|
||||
autoFocus
|
||||
margin="dense"
|
||||
label="Username"
|
||||
type="text"
|
||||
fullWidth
|
||||
value={username}
|
||||
onChange={e => setUsername(e.target.value)}
|
||||
required
|
||||
/>
|
||||
<TextField
|
||||
margin="dense"
|
||||
label="Password"
|
||||
type="password"
|
||||
fullWidth
|
||||
value={password}
|
||||
onChange={e => setPassword(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button type="submit" color="primary" variant="contained">
|
||||
Login
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</form>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import compareVersions from 'compare-versions'
|
||||
import electron from 'electron'
|
||||
import os from 'os'
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Close from '@material-ui/icons/Close'
|
||||
@@ -182,9 +181,10 @@ class UpdateNotifier extends React.PureComponent<Props, State> {
|
||||
|
||||
private assetForCurrentPlatform(asset: GithubAsset) {
|
||||
let regex: RegExp
|
||||
if (os.platform() === 'darwin') {
|
||||
const platform = this.getPlatform()
|
||||
if (platform === 'darwin') {
|
||||
regex = /\.dmg$/
|
||||
} else if (os.platform() === 'win32') {
|
||||
} else if (platform === 'win32') {
|
||||
regex = /\.exe$/
|
||||
} else {
|
||||
regex = /\.AppImage$/
|
||||
@@ -193,6 +193,14 @@ class UpdateNotifier extends React.PureComponent<Props, State> {
|
||||
return regex.test(asset.name)
|
||||
}
|
||||
|
||||
private getPlatform(): string {
|
||||
if (typeof window === 'undefined') return 'linux'
|
||||
const userAgent = window.navigator.userAgent.toLowerCase()
|
||||
if (userAgent.includes('mac')) return 'darwin'
|
||||
if (userAgent.includes('win')) return 'win32'
|
||||
return 'linux'
|
||||
}
|
||||
|
||||
private renderDownloads() {
|
||||
const latestUpdate = this.state.newerVersions[0]
|
||||
if (!latestUpdate || !latestUpdate.assets) {
|
||||
|
||||
Reference in New Issue
Block a user