Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update server object properties
- Loading branch information
1 parent
6e7c410
commit 762b209
Showing
1 changed file
with
28 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,39 @@ | ||
const Https = require('https'); | ||
const Http = require('http'); | ||
const fs = require('fs'); | ||
const Https = require('https'); | ||
const tls = require('tls'); | ||
const acme = require('@cocreate/acme') | ||
const fs = require('fs'); | ||
|
||
let server = { | ||
acme: new acme(), // Initially an empty object or some placeholder functionality | ||
|
||
loadCertificates(domain) { | ||
try { | ||
return { | ||
key: fs.readFileSync(`/etc/certificates/${domain}/private-key.pem`), | ||
cert: fs.readFileSync(`/etc/certificates/${domain}/fullchain.pem`), | ||
}; | ||
} catch (error) { | ||
console.error("Error loading certificates for domain:", domain); | ||
throw error; // Or handle it by returning default certificates | ||
} | ||
}, | ||
acme: new acme(), | ||
http: Http.createServer(), | ||
https: Https.createServer({ SNICallback: sniCallback }) | ||
}; | ||
|
||
async sniCallback(domain, cb) { | ||
try { | ||
console.log('sni') | ||
await server.acme.checkCertificate(domain); // Referencing `this.acme` | ||
function loadCertificates(domain) { | ||
try { | ||
return { | ||
key: fs.readFileSync(`/etc/certificates/${domain}/private-key.pem`), | ||
cert: fs.readFileSync(`/etc/certificates/${domain}/fullchain.pem`), | ||
}; | ||
} catch (error) { | ||
console.error("Error loading certificates for domain:", domain); | ||
throw error; // Or handle it by returning default certificates | ||
} | ||
} | ||
|
||
const sslContext = tls.createSecureContext(server.loadCertificates(domain)); | ||
cb(null, sslContext); | ||
} catch (error) { | ||
console.error("Error in SNI callback for domain:", domain, error); | ||
cb(error); // handle error or use default context | ||
} | ||
}, | ||
async function sniCallback(domain, cb) { | ||
try { | ||
console.log('sni') | ||
await server.acme.checkCertificate(domain); // Referencing `this.acme` | ||
|
||
https: null, // Will be set after defining `sniCallback` | ||
http: Http.createServer() | ||
}; | ||
const sslContext = tls.createSecureContext(loadCertificates(domain)); | ||
cb(null, sslContext); | ||
|
||
// Creating the HTTPS server with the SNI callback | ||
server.https = Https.createServer({ SNICallback: server.sniCallback }); | ||
} catch (error) { | ||
console.error("Error in SNI callback for domain:", domain, error); | ||
cb(error); // handle error or use default context | ||
} | ||
} | ||
|
||
module.exports = server; |