Netcat over Javascript - javascript

I'm working under one cloud solution, that's allow user to print receipts on ESC/POS printer. So it's actually pretty easy to print on it like
echo "Hello world!" | nc 192.168.1.37 9100
But, I need to do the same with from user's browser. So i've tried like this:
var connection = new WebSocket('ws://IPAddress:Port');
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
And almost done, but WS send whole stack of HTTP headers starts with
GET / HTTP/1.1
...
Is there way to send it without headers? Or other way to send data to printer?

netcat or nc use TCP protocol and it doesn't use header like "POST /1 HTTP/1.1\r\n" like http requests do. You are probably thinking about using net.Socket() and not WebSocket
I do not want to confuse you because you aim to use javascript in browser, but i used net.Socket() successfully with node.js. The point of this comment is, that you aim to establish TCP connection and not WS.

I think that HTTP always starts sending GET and HTTP Headers.
Maybe you can use some old technology like signed Java Applets or Flash.
In addition, you can download a binary file to the client PC to communicate.

Related

How to handle tcp/ip raw requests and http requests on the same server

I am working on a gps tracking system and have built a server on node js.
This is how the file looks like for reference.
const net = require('net');
const lora_packet = require('lora-packet');
const dataParser = require('./dataParser');
const clients = [];
net.createServer(function(socket) {
socket.name = socket.remoteAddress + ":" + socket.remotePort;
clients.push(socket);
socket.on('data', function(data) {
console.log("Buffer sent by terminal : ");
console.log(data);
const packet = lora_packet.fromWire(data, 'hex');
const str = packet._packet.PHYPayload.toString('hex');
dataParser.parse_data(str, socket);
});
socket.on('end', function() {
clients.splice(clients.indexOf(socket), 1);
//broadcast(socket.name + "has left the cartel.\n");
});
function broadcast(message, sender) {
clients.forEach(function(client) {
if (client === sender) client.write(message + "\nsent\n");
return;
client.write(message);
});
process.stdout.write(message);
}
}).listen(8080);
console.log("cartel is running on the port 8080\n");
This server file handles only requests from the hardware and processes raw tcp/ip requests.
I want the server to handle http requests also and want to incorporate routing feature in the server too for client side applicarions for browser.
1) Is there any way that http requests can also be handled by the same server or should I open another port and deploy an express node js app on that?
2) If I use the same 8080 port for http, how can the routing be achieved?
3) If I use different ports for http and raw tcp/ip, what would be the best way for communication between the two server. The communication between tcp/ip server and http server should happen via socket(sending data dynamically).
From http server using socket, data has to be sent dynamically to browser to update live location
So is the flow right?
Hardware (<---->)TCP/IP server(<--->)Http server(<--->)Browser
If more information is needed to solve the query, I'll provide with that!
Thank you
It's very complicated to try to speak multiple protocols on the same port. It requires some sort of scheme at the beginning of each connection to sample the incoming data and identify which protocol it is and then shunt that connection off to the right code to handle that protocol. I wouldn't suggest it.
It is way, way easier to just open a second server on a different port for an Express server to field your http requests. Very simple. You can do it right in the same app. Because both servers can be in the same app, you can just directly read from one connection and write to the other. There's no need for interprocess communication.
Is there any way that http requests can also be handled by the same server or should I open another port and deploy an express node js app on that?
Open another port. No need to write another app unless you have a specific reason to use two processes. You can put both the plain TCP server and the Express server in the same node.js app.
If I use the same 8080 port for http, how can the routing be achieved?
It's not easy. Not suggest to use the same port for multiple protocols.
If I use different ports for http and raw tcp/ip, what would be the best way for communication between the two server. The communication between tcp/ip server and http server should happen via socket(sending data dynamically).
You can put both servers in the same node.js app and then you can just read/write directly from one to the other with the same code. No need for interprocess communication.
From http server using socket, data has to be sent dynamically to browser to update live location
Sending data dynamically to a browser usually means you want the browser to hold something like a webSocket or socket.io connection to your server so you can then send data to the browser at any time over the existing connection. Otherwise, you would have to "wait" for the browser to request data and then respond with the data when it asks.

Check telnet connection with server IP and Port number in JScript

I need to test telnet connection to the server in my local pc. My pc don't have any server side languages like PHP, NodeJS,.. I want to do this in basic JavaScript or chrome extension from latest chrome. I have checked with websocket HTML5. But, it was not working. Here's my code,
<script>
var connection = new WebSocket('ws://10.0.30.1:80');
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
</script>
Lets explain what's my mistake and give me a solution if any other possible.
You can't. Unless you set up a proxy server, a websocket can only communicate with a websocket server.

Javascript Server Side Events with C-client Server Program

Im looking for feasibility of calling C object(for copying a file from client to server) via Javascript Eventsource.
Ex:
I have a C-Client Program which can be executed as below:
./client ip
executing above file will
send a file from client machine to server running at port 8888.
Server will be running at 8888 will receive the file and will write at /folder1/receivedfile.
./server ip
I need to do this in Javascript Event source.
Javascript code example:
if(window.EventSource){
var source =new EventSource("c-object");
}else{
// Result to xhr polling :( xhttprequest
}
It is feasible. Your second line would be something like this:
var source = new EventSource("http://myserver.example.com:8888/c-object");
Your server must be running HTTP protocol, of course. If going down this route, be aware that calling a resource on a different origin will need all the CORS workarounds. In this case the resource is c-object, and the different origin is because of using a different port to where the HTML was served from.
Alternatively you could use Apache, and start c-object as a cgi program. Then it just needs to interact on stdin/stdout.
But, taking a step back, are you sure it is EventSource you want? If you are just trying to send a signal to the server to tell it to copy a file, and not receiving any data, then use a normal AJAX request. SSE is for the server to stream data to the client, one-way, continuously. After the initial connection is made, SSE cannot send anything to the server.

Get time from time.nist.gov NTP server using JavaScript?

How to do? I am a beginner coder - full references and code would help. I literally spent like 5 hours trying to find a solution to this - there are some references online but nothing works! And I don't have access to the NTP server and yes I have to use a public server - such as time.nist.gov.
Help!!!
Short answer:
Not doable.
Long answer:
Even if cross-origin policy would allow it, there's no way to get NTP directly via Ajax without PHP (or something) relaying your request. First reason is that time servers normally stay on UDP port 123; there's no way for Ajax to do UDP; if that's not enough, when Ajax sends a request to a server, it expects to see in response some HTTP headers, some status codes, a response body, etc. NTP doesn't keep that structure, it only sends a string. And there is no raw socket connection support in HTML5 either.
But what you can do with Ajax is look at the request headers because most responses come back with a header that looks like this:
Date:Mon, 21 May 2012 15:30:58 GMT
And there's your time.

Security error on attempted access to twisted rpc server from javascript in the same domain but served from different port

I have a django server that serves the basic site with user, auth etc and a twisted web server that 'should' deliver live content as json streams.
Django server is running on 127.0.0.1:8080
Twisted 127.0.0.1:9897
The problem is that when ever I try to make an http request to twisted server from a page in the django site, I get a Security Error. Apparently the same origin policy forbids this sort of communication (???) If that is the case then, are there any alternatives ? Any hints, solution .. Orbited does it successfully, any idea how ?
Thanks
A common workaround for this problem is to tunnel those requests through a script that acts as a proxy.
Here is a trivial example...
Php proxy script - proxy.php
<?php
echo file_get_contents(urldecode($_REQUEST['requestedUrl']));
?>
Some nice js code that needs to make a request to the twisted server from the django site.
// This remote request can't be made from the browser, lets forward it to the local proxy
var twistedRequestUrl = 'http://127.0.0.1:9897/someSpecialApiCall?withAnArgument=andAnImportantValue';
$.ajax({
url : 'proxy.php?requestedUrl=' + encode(twistedRequestUrl),
success : function(data)
{
alert('yay, the twisted call returned:' + data + ' yay!');
}
});

Categories

Resources