node.js simple tcp test - macos

here is the code you can find every where on net
var net = require('net');
var server = net.createServer(function (socket) {
socket.write("Echo server\r\n");
socket.pipe(socket);
});
server.listen(1337, "127.0.0.1");
A simple tcp server will echo whatever you will send it. How to send data to it? What tools/commands I need in mac to test this server?

Use nc aka netcat. In Terminal.app, while your node app is running:
$ nc localhost 1337
Echo server
Ta-da!

Related

Running an ftp server on nixos

I would like to run an ftp server on a nixos host. I am using vsftpd, though could use something else if that would make a difference.
The ftp works fine on localhost, but the firewall is blocking me for remote usage. I have allowed TCP port 21, but that is not enough.
How should I configure the firewall to allow ftp connections (including writing to the ftp server)?
Here is the code that I currently have:
{
networking.firewall = { allowedTCPPorts = [ 20 21 ];
# connectionTrackingModules = [ "ftp" ];
};
services.vsftpd = {
enable = true;
# cannot chroot && write
# chrootlocalUser = true;
writeEnable = true;
localUsers = true;
userlist = [ "martyn" "cam" ];
userlistEnable = true;
};
}
With the above, any use of ftp from off-host fails:
ftp> put dead.letter
200 PORT command successful. Consider using PASV.
425 Failed to establish connection.
Use of passive mode (e.g., with ftp -p) doesn't seem to help here:
ftp> put dead.letter
227 Entering Passive Mode (192,168,0,7,219,202).
ftp: connect: Connection timed out
Testing on a throwaway host with the firewall disabled
networking.firewall.enable = false;
Allows ftp -p to work; though of course turning off the firewall is not an attractive option.
Thanks for any help and pointers,
In passive mode the client will connect to the server with a second connection, that is used to transfer "things" (directory listings, files). In your case:
227 Entering Passive Mode (192,168,0,7,219,202)
The server requested the client to connect to it on port 219 * 256 + 202 = 56266.
This port is choosen by vsftpd dynamically and is not open in your firewall. You have to fix vsftpd to a fixed port for the passive connection and open this connection in the firewall.
vsftpd has two configuration options to set this: pasv_max_port and pasv_min_port. You should be able to set them in services.vsftpd.extraConfig. You probably want to open a small range of ports and open these in the firewall.
To open ports in the firewall, use networking.firewall.allowedTCPPorts. For example:
networking.firewall.allowedTCPPorts = [ 21 ];
services.vsftpd.extraConfig = ''
pasv_enable=Yes
pasv_min_port=51000
pasv_max_port=51999
'';
networking.firewall.allowedTCPPortRanges = [ { from = 51000; to = 51999; } ];
Firewall configuration is not automatic in NixOS, because that would defeat the purpose of having control over what traffic is allowed.
Some services have an openFirewall option to make this easier, but the vsftpd module does not seem to provide this convenience.
Edit: 20 is for the client. Only 21 needs to be opened.
Edit: Plus a range for passive mode connections.

How to check whether port is open in Powershell

I do not have telnet command in my system.
However my system is installed with Windows 10, so there must be a way to check whether particular port is open or not in a remote system. That particular remote system is accessible I had checked with ping command.
So here is my simple question,- how to check whether particular port is open or not using powershell.
Command netstat could brief for local system service & port and particular protocol either UDP or TCP is up & runnning. As I do not have telnet I need this to be sorted out and tackled by powershell. Any advise and suggestion are welcome.
Test-NetConnection ###.###.###.### -Port ##
You can use the following to try and open a port, if no error is returned the port is open:
$ipaddress = ""
$port = ""
$tcpClient = new-object Net.Sockets.TcpClient
$tcpClient.Connect("$ipaddress", $Port)
$tcpClient.Dispose()
Here's a more complete example, which returns true/false which is the way that Test-Path works:
function Test-Port
{
param
(
$Address,
$Port
)
$tcpClient = new-object Net.Sockets.TcpClient
try
{
$tcpClient.Connect("$Address", $Port)
$true
}
catch
{
$false
}
finally
{
$tcpClient.Dispose()
}
}
Test-Port -Address localhost -Port 80
Test-Port -Address localhost -Port 81
Depending on the version of Powershell/Windows you are using Test-NetConnection may be more appropriate.

TCP Minecraft Server in Ruby

I'm attempting to create a script in ruby that connects to a Minecraft server via TCP and fetches the current number of players much like the PHP script at http://www.webmaster-source.com/2012/07/05/checking-the-status-of-a-minecraft-server-with-php/
When running the code below I get �Took too long to log in
require 'socket'
server = TCPSocket.new '192.241.174.210', 25565
while line = server.gets
puts line
end
server.close
What am I doing wrong here?
you're not sending this:
fwrite($sock, "\xfe");
from the script you linked. You have to send that before you call read, like they do.
Basically the server is waiting for you to send data and when you don't after a timeout, you are disconnected.

node.js: Run external command in new window

I'm writing a node.js script that permanently runs in the background and initiates file downloads from time to time. I want to use WGET for downloading the files, since it seems more robust until I really know what I'm doing with node.js.
The problem is that I want to see the progress of the downloads in question. But I can't find a way to start WGET through node.js in a way that a new shell window is opened and displayed for WGET.
Node's exec and spawn functions run external commands in the background and would let me access the output stream. But since my script runs in the background (hidden), there's not much I could do with the output stream.
I've tried opening a new shell window by running "cmd /c wget..." but that didn't make any difference.
Is there a way to run external command-line processes in a new window through node.js?
You can use node-progress and Node's http client (or requesT) for this, no need for wget:
https://github.com/visionmedia/node-progress
Example:
var ProgressBar = require('../')
, https = require('https');
var req = https.request({
host: 'download.github.com'
, port: 443
, path: '/visionmedia-node-jscoverage-0d4608a.zip'
});
req.on('response', function(res){
var len = parseInt(res.headers['content-length'], 10);
console.log();
var bar = new ProgressBar(' downloading [:bar] :percent :etas', {
complete: '='
, incomplete: ' '
, width: 20
, total: len
});
res.on('data', function(chunk){
bar.tick(chunk.length);
});
res.on('end', function(){
console.log('\n');
});
});
req.end();
UPDATE:
Since you want to do this in a background process and listen for the download progresses (in a separate process or what have you) you can achieve that using a pub-sub functionality, either:
use a message queue like Redis, RabbitMQ or ZeroMQ
start a TCP server on a known port / UNIX domain and listen to it
Resources:
http://nodejs.org/api/net.html

Sending an email from R using the sendmailR package

I am trying to send an email from R, using the sendmailR package. The code below works fine when I run it on my PC, and I recieve the email. However, when I run it with my macbook pro, it fails with the following error:
library(sendmailR)
from <- sprintf("<sendmailR#%s>", Sys.info()[4])
to <- "<myemail#gmail.com>"
subject <- "TEST"
sendmail(from, to, subject, body,
control=list(smtpServer="ASPMX.L.GOOGLE.COM"))
Error in socketConnection(host = server, port = port, blocking = TRUE) :
cannot open the connection
In addition: Warning message:
In socketConnection(host = server, port = port, blocking = TRUE) :
ASPMX.L.GOOGLE.COM:25 cannot be opened
Any ideas as to why this would work on a PC, but not a mac? I turned the firewall off on both machines.
Are you able to send email via the command-line?
So, first of all, fire up a Terminal and then
$ echo “Test 123” | mail -s “Test” user#domain.com
Look into /var/log/mail.log, or better use
$ tail -f /var/log/mail.log
in a different window while you send your email. If you see something like
... setting up TLS connection to smtp.gmail.com[xxx.xx.xxx.xxx]:587
... Trusted TLS connection established to smtp.gmail.com[xxx.xx.xxx.xxx]:587:\
TLSv1 with cipher RC4-MD5 (128/128 bits)
then you succeeded. Otherwise, it means you have to configure you mailing system. I use postfix with Gmail for two years now, and I never had have problem with it. Basically, you need to grab the Equifax certificates, Equifax_Secure_CA.pem from here: http://www.geotrust.com/resources/root-certificates/. (They were using Thawtee certificates before but they changed last year.) Then, assuming you used Gmail,
Create relay_password in /etc/postfix and put a single line like this (with your correct login and password):
smtp.gmail.com login#gmail.com:password
then in a Terminal,
$ sudo postmap /etc/postfix/relay_password
to update Postfix lookup table.
Add the certificates in /etc/postfix/certs, or any folder you like, then
$ sudo c_rehash /etc/postfix/certs/
(i.e., rehash the certificates with Openssl).
Edit /etc/postfix/main.cf so that it includes the following lines (adjust the paths if needed):
relayhost = smtp.gmail.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/relay_password
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
smtp_tls_CApath = /etc/postfix/certs
smtp_tls_session_cache_database = btree:/etc/postfix/smtp_scache
smtp_tls_session_cache_timeout = 3600s
smtp_tls_loglevel = 1
tls_random_source = dev:/dev/urandom
Finally, just reload the Postfix process, with e.g.
$ sudo postfix reload
(a combination of start/stop works too).
You can choose a different port for the SMTP, e.g. 465.
It’s still possible to use SASL without TLS (the above steps are basically the same), but in both case the main problem is that your login informations are available in a plan text file... Also, should you want to use your MobileMe account, just replace the Gmail SMTP server with smtp.me.com.

Resources