| #!/opt/node/bin/node |
| |
| /* |
| Copyright 2015-2018 Rivoreo |
| |
| Permission is hereby granted, free of charge, to any person obtaining |
| a copy of this software and associated documentation files (the |
| "Software"), to deal in the Software without restriction, including |
| without limitation the rights to use, copy, modify, merge, publish, |
| distribute, sublicense, and/or sell copies of the Software, and to |
| permit persons to whom the Software is furnished to do so, subject to |
| the following conditions: |
| |
| The above copyright notice and this permission notice shall be |
| included in all copies or substantial portions of the Software. |
| |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
| FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| */ |
| |
| // TODO: check host key of the server |
| |
| const DEFAULT_PORT = 22; |
| const DEFAULT_USER_NAME = "sshout"; |
| |
| var fs = require("fs"); |
| var util = require("util"); |
| var sshout = require("sshout"); |
| var ALGORITHMS = require("ssh2-streams").constants.ALGORITHMS; |
| var os = require("os"); |
| var url = require("url"); |
| |
| var StringStartsWith = function(s, sub) { |
| if(s.length < sub.length) return false; |
| for(var i=0; i<sub.length; i++) { |
| if(s[i] != sub[i]) return false; |
| } |
| return true; |
| }; |
| |
| var print_usage = function(name) { |
| process.stderr.write(util.format("Usage: %s [-i <identify-file>] [-v] ssh://[<user-name>@]<sshout-server>[:<port>][/]\n", name)); |
| }; |
| |
| var server_url = null; |
| //var identify_files = []; |
| var identify_file = null; |
| var verbose = 0; |
| |
| for(var i=2; i<process.argv.length; i++) { |
| if(process.argv[i][0] == "-") { |
| var o = process.argv[i]; |
| for(var j=1; j<o.length; j++) switch(o[j]) { |
| case "h": |
| print_usage(process.argv[1]); |
| process.exit(0); |
| break; |
| case "i": |
| if(++i >= process.argv.length) fatal_option_require_argument("u"); |
| //identify_files.push(process.argv[i]); |
| identify_file = process.argv[i]; |
| break; |
| case "v": |
| verbose++; |
| break; |
| } |
| } else { |
| if(server_url !== null) { |
| process.stderr.write("Extra string after URL\n"); |
| process.exit(-1); |
| } |
| //url = new URL(process.argv[i]); |
| server_url = url.parse(process.argv[i], false, false); |
| if(server_url.protocol !== "ssh:") { |
| process.stderr.write("Protocol name in URL must be 'ssh'\n"); |
| process.exit(-1); |
| } |
| } |
| } |
| |
| if(server_url === null) { |
| process.stderr.write("Need an URL\n"); |
| print_usage(process.argv[1]); |
| process.exit(-1); |
| } |
| |
| var kex_list = ALGORITHMS.SUPPORTED_KEX; |
| |
| var request_user_name = server_url.auth; |
| if(request_user_name === null) { |
| request_user_name = DEFAULT_USER_NAME; |
| } else { |
| var colon_i = request_user_name.indexOf(":"); |
| if(colon_i !== -1) request_user_name = request_user_name.substring(0, last_i); |
| } |
| |
| var private_key = identify_file === null ? null : fs.readFileSync(identify_file); |
| |
| var debug_print = verbose ? function(s) { |
| if(verbose < 2 && StringStartsWith(s, "DEBUG: Parser: ")) return; |
| process.stderr.write(s); |
| process.stderr.write("\n"); |
| } : null; |
| |
| var sshout_client = new sshout.Client({ |
| algorithms:{ kex:kex_list }, |
| host:server_url.hostname, |
| port:server_url.port ? server_url.port : DEFAULT_PORT, |
| username:request_user_name, |
| privateKey:private_key, |
| tryKeyboard:true, // Needed for public chatrooms |
| keepaliveInterval:240000, |
| debug:debug_print |
| }); |
| |
| sshout_client.on("ready", function(canoncal_user_name) { |
| process.stderr.write(util.format("SSHOUT is ready, my user name is '%s'\n", canoncal_user_name)); |
| sshout_client.send_message("GLOBAL", "plain", "TEST"); |
| sshout_client.request_user_info(); |
| }); |
| sshout_client.on("error", function(e, msg) { |
| switch(typeof e) { |
| case "number": |
| process.stderr.write(util.format("SSHOUT error %d, %s\n", e, msg)); |
| break; |
| case "object": |
| console.error(e); |
| break; |
| default: |
| process.stderr.wrtie(util.format("unknown type of error event (%s)\n", typeof e)); |
| break; |
| } |
| }); |
| sshout_client.on("message", function(o) { |
| console.log(o); |
| }); |
| sshout_client.on("user-state-changed", function(state, user) { |
| process.stdout.write(util.format("User %s is %s\n", user, state ? "online" : "offline")); |
| }); |
| sshout_client.on("motd", function(s) { |
| process.stdout.write("Message of the day:\n"); |
| process.stdout.write(s); |
| }); |
| sshout_client.on("user-info", function(my_id, user_info) { |
| process.stdout.write(util.format("My ID: %d\n", my_id)); |
| console.log(user_info); |
| }); |
| sshout_client.start(); |