前提
ElectronにてNodeJS向けのtunnel-sshというモジュールを使い、SSHトンネリングを行っています。
js
1const params = { 2 username: "…", 3 password: "…", 4 host: "…", 5 /* … */ 6} 7}; 8 tunnel({...params, ...{ 9 keepAlive: true, 10 }}, (error, serv) => { 11 if(error){ 12 result = `${error.name} ${error.message}`; 13 }else{ 14 result = ""; 15 server = serv; 16 server.on("connection", function(stream){ 17 /* 1 */ 18 console.log("connection created"); 19 }); 20 server.on("listening", function(){ 21 console.log("listening"); 22 }); 23 server.on("error", function(err){ 24 console.log("error occured"); 25 console.log(err); 26 }); 27 server.on("close", function(){ 28 log.debug("connection closed"); 29 console.log("connection closed"); 30 }); 31 } 32 });
現状上記コードで期待通り接続を行うことには成功しており、通信を行うことも出来る状態になっています。
実現したいこと
このたび、SSHでの接続を確立できたタイミングで、別の処理を行えるよう、接続確立のタイミングをイベントで補足したいと考えています。
ただ、サーバーのconnectionイベント(上記コード中の1)は接続が確立できたタイミングより前に実行されるようで、実はパラメータの接続先が過っていてタイムアウトしてしまった場合も、パスワードが間違っていてエラーとなった場合も、かならず実行されてしまいます。
それではとconnectionイベント内の引数streamのconnectイベントを参照してみました(下記コード2)が、接続確立後もコードが実行されませんでした。
js
1 server.on("connection", function(stream){ 2 console.log("connection created"); 3 /* 2 */ 4 stream.on("connect", () => { 5 console.log("connect"); 6 }); 7 });
リファレンスによるとほぼ同じタイミングで発生するとあるreadyイベントや、接続後に呼び出されるというlookupイベントも参照してみましたが、変わらず応答はありませんでした(なお、同じ記法でcloseイベントは参照できているようですので、記述自体に大きな誤りはないものと思われます)。
tunnel-sshではこのタイミングでの処理を行うことはできないのでしょうか?ご存じの方、ご教示頂けますと幸いです。
補足情報(FW/ツールのバージョンなど)
- nodejs:18.3.0
- Electron:18.2.3
- tunnel-ssh:4.1.6
あなたの回答
tips
プレビュー