Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
daonb committed Sep 23, 2024
1 parent d1d039f commit e0b8ff4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
42 changes: 20 additions & 22 deletions src/gate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class Gate {
sshPort: number
reconnectCount: number
lastState: ServerPayload
wasSSH: boolean | undefined
constructor (props) {
// given properties
this.id = props.id
Expand Down Expand Up @@ -206,9 +207,9 @@ export class Gate {
async handleFailure(failure: Failure) {
// KeyRejected and WrongPassword are "light failure"
const shell = this.map.shell
const wasSSH = this.session?.isSSH
const closeSession = () => {
if (this.session) {
this.wasSSH = this.session.isSSH
this.session.close()
this.session = null
}
Expand All @@ -235,7 +236,6 @@ export class Gate {
this.marker = null
closeSession()
await this.connect()
// We should probably replace the next to lines with a break
return

case Failure.NoKey:
Expand All @@ -248,7 +248,7 @@ export class Gate {
return

case Failure.NotSupported:
if (wasSSH) {
if (this.wasSSH) {
this.notify("SSH status unknown")
} else
this.notify("WebRTC agent unreachable")
Expand Down Expand Up @@ -282,34 +282,30 @@ export class Gate {

}
closeSession()
if (wasSSH) {
await this.handleSSHFailure()
return
}
// TODO: move thgis to the top
if (!terminal7.isActive(this)){
this.stopBoarding()
return
}
if (this.firstConnection) {
this.onFirstConnectionDisconnect()
return
}

if (this.wasSSH) {
await this.handleSSHFailure()
return
}
if (terminal7.recovering) {
terminal7.log("retrying...")
try {
await this.reconnect()
} catch (e) {
terminal7.log("reconnect failed", e)
this.notify("reconnect failed: " + e)

} finally {
terminal7.log("reconnect done")
this.notify("Reconnect failed: " + e)
}
return
}

if (this.firstConnection) {
this.onFirstConnectionDisconnect()
return
}

let res: string
try {
res = await shell.runForm(shell.reconnectForm, "menu")
Expand Down Expand Up @@ -369,7 +365,7 @@ export class Gate {
reconnect(): Promise<void> {
return new Promise((resolve, reject) => {
const session = this.session
const isSSH = session?.isSSH
const isSSH = session?session.isSSH:this.wasSSH
const isNative = Capacitor.isNativePlatform()
console.log(`reconnecting: # ${this.reconnectCount} ${(session===null)?"null session ":"has session "} \
${isSSH?"is ssh":"is webrtc"}`)
Expand All @@ -396,22 +392,23 @@ export class Gate {
this.setLayout(JSON.parse(layout) as ServerPayload)
resolve()
}
if (!isSSH && !isNative) {
if (!isNative) {
this.session.reconnect(this.marker)
.then(layout => finish(layout))
.catch(e => {
if (this.session) {
this.wasSSH = this.session.isSSH
this.session.close()
this.session = null
}
terminal7.log("reconnect failed, calling the shell to handle it", isSSH)
terminal7.log("reconnect failed:", e)
reject(e)
// this.map.shell.onDisconnect(this, isSSH).then(resolve).catch(reject)
})
return
}
const closeSessionAndDisconnect = (e) => {
if (this.session && !this.session.isSSH) {
if (this.session) {
this.wasSSH = this.session.isSSH
this.session.close()
this.session = null
}
Expand Down Expand Up @@ -918,6 +915,7 @@ export class Gate {
}
}, 10)
if (this.session) {
this.wasSSH = this.session.isSSH
this.session.close()
this.session = null
}
Expand Down
1 change: 0 additions & 1 deletion src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ export class T7Map {
if (ev.dt < 500) {
ev.stopPropagation()
const command = (ev.target.closest(".gate-edit"))? "edit" : "connect"
console.log("tap", command)
this.shell.runCommand(command, [g.name])
.then(() => this.showLog(false))
.catch(e => terminal7.notify(e))
Expand Down
1 change: 0 additions & 1 deletion src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ export class Shell {
} else {
this.t.writeln("\n For persistent sessions & WebRTC 🍯 please install webexec")
install.splice(1, 0, { prompt: "Install" })
// only for native platforms
if (Capacitor.isNativePlatform())
install.splice(2, 0, { prompt: "Always use SSH" })
}
Expand Down
3 changes: 2 additions & 1 deletion src/terminal7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ export class Terminal7 {
this.run(() => this.recovering=false, this.conf.net.timeout)
// real work is done in updateNetworkStatus
Network.getStatus().then(async s => await this.updateNetworkStatus(s))
}
} else
this.log("app state change ignored")
}
/*
* Terminal7.open opens terminal on the given DOM element,
Expand Down

0 comments on commit e0b8ff4

Please sign in to comment.