Skip to content

Commit

Permalink
Implements process timeout and fixes #9
Browse files Browse the repository at this point in the history
Implements process timeout and alternative method for tracking missed process. Finally fixes #9 lol 🎉
  • Loading branch information
evsar3 committed Sep 9, 2020
1 parent b32e27e commit abecc43
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
40 changes: 38 additions & 2 deletions src/renderer/ProcessHandlerWin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class ProcessHandlerWin {
]
}

console.log(cmdArgs)

if (conn.authType === 'password' || conn.authType === 'password-ask') {
cmdArgs.push('-oPreferredAuthentications=password')
cmdArgs.push('-opassword_stdin')
Expand Down Expand Up @@ -119,6 +117,44 @@ class ProcessHandlerWin {
})
})
}

getLastSpawnedProcess () {
return new Promise((resolve, reject) => {
exec(`wmic process where '(name="sshfs.exe")' get processid, creationdate /value`, (err, stdout) => {
if (!err) {
let data = stdout.toString().trim().split('\n')
let pid = null
let creationDate = null

data.forEach(item => {
let itemParts = item.split('=')

let key = itemParts[0].trim().toLowerCase()
let value = itemParts[1].trim().toLowerCase()

if (key === 'processid') {
pid = parseInt(value)
}

if (key === 'creationdate') {
let year = parseInt(value.substr(0, 4))
let month = parseInt(value.substr(4, 2)) - 1
let day = parseInt(value.substr(6, 2))
let hours = parseInt(value.substr(8, 2))
let minutes = parseInt(value.substr(10, 2))
let seconds = parseInt(value.substr(13, 2))

creationDate = new Date(year, month, day, hours, minutes, seconds)
}
})

resolve({ pid, creationDate })
} else {
reject(new Error('Process not found'))
}
})
})
}
}

export default ProcessHandlerWin
10 changes: 10 additions & 0 deletions src/renderer/ProcessManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class ProcessManager extends EventEmitter {
}

create (conn) {
this.timeoutTimer = setTimeout(() => {
this.emit('timeout', conn)
}, (this.processHandler.settings.processTrackTimeout * 1000))

return new Promise((resolve, reject) => {
this.processHandler.create(conn).then((pid, process) => {
this.emit('created', {
Expand All @@ -32,6 +36,8 @@ class ProcessManager extends EventEmitter {
resolve(pid)
}).catch(error => {
reject(error)
}).finally(() => {
clearTimeout(this.timeoutTimer)
})
})
}
Expand Down Expand Up @@ -79,6 +85,10 @@ class ProcessManager extends EventEmitter {
exists (pid) {
return this.processHandler.exists(pid)
}

getLastSpawnedProcess () {
return this.processHandler.getLastSpawnedProcess()
}
}

const settings = store.state.Settings.settings
Expand Down
25 changes: 24 additions & 1 deletion src/renderer/components/MainWindow/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
</template>

<script>
import fs from 'fs'
import { remote } from 'electron'
import { v4 as uuid } from 'uuid'
Expand Down Expand Up @@ -198,7 +199,7 @@ export default {
settings () {
const window = windowManager.createNew('settings-window', '', '/index.html#settings', null, {
height: 265,
height: 340,
width: 500,
useContentSize: true,
frame: false,
Expand Down Expand Up @@ -309,6 +310,28 @@ export default {
this.notify(`'${conn.name}' was disconnected due to a connection error.\nCheck your internet connection`, 'error-icon')
}
})
ProcessManager.on('timeout', conn => {
if (fs.existsSync(conn.mountPoint)) {
ProcessManager.getLastSpawnedProcess().then(process => {
let foundConnection = this.connections.find(i => i.pid === process.pid)
if (!foundConnection) {
conn.pid = process.pid
conn.status = 'connected'
ProcessManager.watch(process.pid)
this.notify(`'${conn.name}' tracked using alternative method`)
}
})
} else {
conn.pid = null
conn.status = 'disconnected'
this.notify(`Process Timeout: Couldn't connect to '${conn.name}'`, 'error-icon')
}
})
}
}
</script>
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/components/SettingsWindow/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<input type="text" autofocus placeholder="eg. C:\Program Files\SSHFS-Win\bin\sshfs-win.exe" v-model="data.sshfsBinary">
</div>

<div class="form-item">
<label>Process Timeout</label>
<input type="text" autofocus placeholder="Time in seconds" v-model.number="data.processTrackTimeout" style="width: 100px; text-align: right;">
</div>

<div class="form-item" style="margin: 10px 0">
<SwitchLabel label="Startup with Windows" v-model="data.startupWithOS"/>
<SwitchLabel label="Display system tray message on close" v-model="data.displayTrayMessageOnClose"/>
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/store/modules/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const state = {
settings: {
sshfsBinary: 'C:\\Program Files\\SSHFS-Win\\bin\\sshfs-win.exe',
startupWithOS: true,
displayTrayMessageOnClose: true
displayTrayMessageOnClose: true,
processTrackTimeout: 15
}
}

Expand All @@ -15,7 +16,8 @@ const mutations = {
state.settings = {
sshfsBinary: 'C:\\Program Files\\SSHFS-Win\\bin\\sshfs-win.exe',
startupWithOS: true,
displayTrayMessageOnClose: true
displayTrayMessageOnClose: true,
processTrackTimeout: 15
}
}
}
Expand Down

0 comments on commit abecc43

Please sign in to comment.