Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/autocertifier-server/src/AutoCertifierServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ export class AutoCertifierServer implements RestInterface, ChallengeManager {
const fqdn = subdomain + '.' + this.domainName

if (this.route53Api !== undefined) {
const subdomains = await this.database!.getSubdomainsByIpAndPort(ipAddress, streamrWebSocketPort)
logger.info('Deleting all subdomains from ip: ' + ipAddress + ' port: '
+ streamrWebSocketPort + ' number of subdomains: ' + subdomains.length, { subdomains })
await Promise.all(subdomains.map((subdomain) =>
this.route53Api!.deleteRecord(
RRType.A,
subdomain.subdomainName + '.' + this.domainName,
ipAddress,
300
)
))
await this.route53Api.upsertRecord(RRType.A, fqdn, ipAddress, 300)
}

Expand Down
15 changes: 15 additions & 0 deletions packages/autocertifier-server/src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Database {
private updateSubdomainIpStatement?: Statement
private getSubdomainAcmeChallengeStatement?: Statement
private updateSubdomainAcmeChallengeStatement?: Statement
private getSubdomainsByIpAndPortStatement?: Statement
private databaseFilePath: string

constructor(filePath: string) {
Expand Down Expand Up @@ -59,6 +60,16 @@ export class Database {
return ret
}

public async getSubdomainsByIpAndPort(ip: string, port: string): Promise<Subdomain[]> {
let ret: Subdomain[]
try {
ret = await this.getSubdomainsByIpAndPortStatement!.all(ip, port)
} catch (e) {
throw new DatabaseError(`Failed to get subdomains for IP ${ip} and port ${port}`, e)
}
return ret || []
}

private async getSubdomainWithToken(subdomain: string, token: string): Promise<Subdomain | undefined> {
let ret: Subdomain | undefined
try {
Expand Down Expand Up @@ -115,6 +126,7 @@ export class Database {
this.updateSubdomainIpStatement = await this.db.prepare("UPDATE subdomains SET ip = ?, port = ? WHERE subdomainName = ? AND token = ?")
this.getSubdomainAcmeChallengeStatement = await this.db.prepare("SELECT acmeChallenge FROM subdomains WHERE subdomainName = ?")
this.updateSubdomainAcmeChallengeStatement = await this.db.prepare("UPDATE subdomains SET acmeChallenge = ? WHERE subdomainName = ?")
this.getSubdomainsByIpAndPortStatement = await this.db.prepare("SELECT * FROM subdomains WHERE ip = ? AND port = ?")

logger.info('Database is running')
}
Expand All @@ -141,6 +153,9 @@ export class Database {
if (this.updateSubdomainAcmeChallengeStatement) {
await this.updateSubdomainAcmeChallengeStatement.finalize()
}
if (this.getSubdomainsByIpAndPortStatement) {
await this.getSubdomainsByIpAndPortStatement.finalize()
}
if (this.db) {
await this.db.close()
}
Expand Down