NodeJS 节点服务器无法连接到NATS服务器

iih3973s  于 2023-10-17  发布在  Node.js
关注(0)|答案(1)|浏览(143)

我试图通过nodejs连接到NATS服务器,但遇到了cert问题。
NATS服务器位于公司VPN后面。不确定这是否是问题所在。我可以连接到服务器本身时,验证到VPN,但我的代码不能。
下面是我在连接时尝试的最小代码:

const creds = `-----BEGIN NATS USER JWT-----
    eyJ0eXAiOiJqdSDJB....
  ------END NATS USER JWT------

  -----BEGIN USER NKEY SEED-----
    SUAIBDPBAUTW....
  ------END USER NKEY SEED------
`;

async connectToNats(){
 let connection;
 try{
    connection = await connect({
      servers:[`nats://stage-nats1.company.net:4222`],
      debug: true,
      authenticator: credsAuthenticator(new TextEncoder().encode(creds)),
    });
    connection.subscribe('topicImInterestedIn')
}catch(e)
  console.log(e)
}

错误代码:

Error [ERR_TLS_CERT_ALTNAME_INVALID] hostname/IP does not match certificate's altnames: Host: localhost. is not the cert's altnames: DNS:*.companyName.net, DNS:companyName.net

    reason: "Host: localhost. is not in the cert's altnames: DNS:*.companyName.net, DNS:companyName.net

我很困惑,为什么这是试图连接到localhost时,我指定的服务器已经。

fhity93d

fhity93d1#

答案是,我需要两个creds,又名NATS user jwtnKey,并设置tlsOptionscert.pemcert.key文件添加到连接。

//Should really be read from file
const creds = `-----BEGIN NATS USER JWT-----
    eyJ0eXAiOiJqdSDJB....
  ------END NATS USER JWT------

  -----BEGIN USER NKEY SEED-----
    SUAIBDPBAUTW....
  ------END USER NKEY SEED------
`;

const tlsOptions = {
  key: fs.readFileSync("key.pem"),
  cert: fs.readFileSync("cert.pem")
};
const nc = await connect({
  servers: ["nats://localhost:4222"],
  tls: tlsOptions,
  authenticator: credsAuthenticator(new TextEncoder().encode(creds)),
});

相关问题