为什么我无法从Nodejs中的证书中获取域名?
#javascript #node #帮助

正如我在stackoverflow中所问的那样

const fs = require('fs');
  const { exit } = require('process');

  let https;
  try {
    https = require('node:https');
  } catch (err) {
    console.log('https support is disabled!');
  }

  const cert_dir = '/etc/myapp/certs';
  const secureContext = {}

  fs.stat(cert_dir,(err,cb) => {
    if(err){
        console.log(err);
        exit(-1);
    }

    fs.readdir(cert_dir,(err, files) => {
        if (err){
            console.log(err);
        } else {
            files.filter(item => {
                return path.extname(item)=='.cert'
            }).forEach(file => {
                console.log(file);
                const cert = path.join(cert_dir,file);
                const key = path.join(cert_dir,(path.basename(file,".cert")+".key"))

                fs.stat(key, (err, stat) => {
                    if(err == null){
                        const x509 = new X509Certificate(fs.readFileSync(cert));
                        console.log("cert Info",x509);

                        // get domain here
                    }
                });
            });
        }
    });

  });

  const options = {
    SNICallback: function (domain, cb) {
        if (secureContext[domain]) {
            if (cb) {
                cb(null, secureContext[domain]);
            } else {
                // compatibility for older versions of node
                return secureContext[domain]; 
            }
        } else {
            throw new Error('No keys/certificates for domain requested');
        }
    },
    // must list a default key and cert because required by tls.createServer()
    key: fs.readFileSync('../path_to_key.pem'), 
    cert: fs.readFileSync('../path_to_cert.crt'), 
  };

  const server = https.createServer(options,handle);
  server.listen(449);

,但我找不到通过其域匹配证书的方法。您知道我如何从随机证书中找到域名吗?

我的意思是在Java中,我可以这样做:
https://stackoverflow.com/a/14020952/4706711

但是为什么不在nodejs中(尽管有所不同且完全相反)