-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Migrate to typescript - Fix account removal - Break circular dependency with tunnel-service
- Loading branch information
1 parent
eda5d72
commit c54a7f7
Showing
14 changed files
with
345 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Tunnel from "../tunnel/tunnel.js"; | ||
import Account from "./account.js"; | ||
import Storage from '../storage/index.js'; | ||
import AccountService from "./account-service.js"; | ||
import { TunnelConfig } from "../tunnel/tunnel-config.js"; | ||
|
||
export default class AccountTunnelService { | ||
|
||
private storage: Storage; | ||
|
||
constructor() { | ||
this.storage = new Storage("account"); | ||
} | ||
|
||
public async destroy(): Promise<void> { | ||
await this.storage.destroy(); | ||
} | ||
|
||
public async assignTunnel(tunnelConfig: TunnelConfig): Promise<boolean> { | ||
|
||
const res = await this.storage.update(AccountService.normalizeId(tunnelConfig.account), Account, (account: Account) => { | ||
if (!account.tunnels.includes(tunnelConfig.account)) { | ||
account.tunnels.push(tunnelConfig.id); | ||
} | ||
account.updated_at = new Date().toISOString(); | ||
return true; | ||
}); | ||
return res instanceof Account; | ||
} | ||
|
||
public async unassignTunnel(tunnelConfig: TunnelConfig): Promise<boolean> { | ||
|
||
const res = await this.storage.update(AccountService.normalizeId(tunnelConfig.account), Account, (account: Account) => { | ||
const pos = account.tunnels.indexOf(tunnelConfig.id); | ||
if (pos >= 0) { | ||
account.tunnels.splice(pos, 1); | ||
} | ||
account.updated_at = new Date().toISOString(); | ||
return true; | ||
}); | ||
return res instanceof Account; | ||
} | ||
|
||
public async authorizedAccount(tunnel: Tunnel): Promise<Account> { | ||
const account = await this.storage.read(AccountService.normalizeId(tunnel.account), Account); | ||
if (!(account instanceof Account)) { | ||
throw new Error("dangling_account"); | ||
} | ||
if (!account.tunnels.includes(tunnel.id)) { | ||
this.assignTunnel(tunnel.config); | ||
} | ||
return account; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Serializable } from '../storage/serializer.js'; | ||
|
||
type AccountStatus = { | ||
disabled: boolean, | ||
disabled_at?: string, | ||
disabled_reason?: string, | ||
} | ||
|
||
class Account implements Serializable { | ||
public accountId: string; | ||
public id: string; | ||
public created_at?: string; | ||
public updated_at?: string; | ||
public tunnels: Array<String>; | ||
public status: AccountStatus; | ||
|
||
constructor(accountId: string) { | ||
this.accountId = accountId; | ||
this.id = accountId; | ||
this.created_at = undefined; | ||
this.updated_at = undefined; | ||
this.tunnels = []; | ||
this.status = { | ||
disabled: false, | ||
disabled_at: undefined, | ||
disabled_reason: undefined, | ||
}; | ||
} | ||
} | ||
|
||
export default Account; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.