-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.mjs
executable file
·93 lines (77 loc) · 2.07 KB
/
client.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
/*
* And connect with a tcp client from the command line using netcat, the *nix
* utility for reading and writing across tcp/udp network connections. I've only
* used it for debugging myself.
*
* $ netcat 127.0.0.1 1337
*
* You should see:
* > Echo server
*
* */
/* Or use this example tcp client written in node.js. (Originated with
* example code from
* http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html.) */
`use strict`;
import net from 'net';
import {
env as ENV,
argv,
exit,
} from 'node:process';
const APP_PORT = ENV['PORT'] || 43;
const APP_HOST = ENV['BIND_ADDRESS'] || `127.0.0.1`;
const usage = `
Usage: whois [OPTION]... OBJECT...
-h HOST, --host HOST connect to server HOST
-p PORT, --port PORT connect to PORT
-I query whois.iana.org and follow its referral
-H hide legal disclaimers
--verbose explain what is being done
--no-recursion disable recursion from registry to registrar servers
--help display this help and exit
--version output version information and exit
`;
function onSocketClose(handle) {
handle.destroy();
// console.log(`Connection closed`);
}
function onSocketData(data) {
let result = ``;
if(data && data.length > 0) {
result = data.toString(`utf8`);
}
if(result.includes(`ENOENT`)) {
return exit(1);
}
return result;
}
function main() {
const args = argv;
const numArgs = args.length;
let query = ``;
args.forEach((arg, pos) => {
if(pos == 2) {
if(arg && arg.length > 0) {
query = arg;
}
}
});
if(!query || query && query.length < 1) {
console.log(usage);
exit(1);
}
const client = new net.Socket();
client.connect(APP_PORT, APP_HOST, function() {
// console.debug(`Connected to ${APP_HOST}:${APP_PORT}...\n`);
client.write(query);
});
client.on(`data`, (data) => {
return console.log(onSocketData(data));
});
client.on(`close`, () => {
onSocketClose(client);
});
}
main();