Skip to content

Commit

Permalink
fix: update server object properties
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Jan 6, 2024
1 parent 6e7c410 commit 762b209
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions src/index.js
@@ -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;

0 comments on commit 762b209

Please sign in to comment.