How a web page can send a message to the local network

Our web application has a button that should send data to a server on the local network, which, in turn, prints something on the printer.

Until now, it was simple: the button ran an AJAX POST request to http://printerserver/print.php with a token, this page was connected to a web application to check the token and receive data for printing and printing. A.

However, now we are delivering our web application via HTTP (and I would prefer not to return to HTTP for this), and newer versions of Chrome and Firefox no longer make a request for an HTTP address, they do not even send a request for checking CORS headers.

Now, what is a modern alternative to the XHR cross protocol? Do Websockets suffer from the same problem? (A Google search did not indicate what the current state is here.) Can I already use TCP sockets? I would prefer not to switch to GET requests because the action is not idempotent, and this can have practical consequences with preloading and caching.

I can change the application on the printer server in any way (so I could replace it with NodeJS or something like that), but I can not change the users browsers (for example, trust a self-signed certificate for the printer server).

+6
source share
4 answers

You can queue print requests on a web server and periodically print print servers for print requests.

If this is not possible, I would establish a tunnel or VPN between the web server and the print server networks. Thus, you can make a print request from a web server on the server side instead of the client. If you use curl, there are flags to ignore invalid SSL certificates, etc. (I still suspect that it is better to submit the queue anyway, so print requests are not blocked).

If the web server can connect ssh to something on the network where the print server is running, you can do something like: ssh params user @host here is some curl command.

The third option I can think of if printserver can communicate, for example, with a subdomain of a web server domain, for example: print.somedomain.com, you can trust its certificate somedomain.com, IIRC you have to create a CSR (signature request certificate) from printserver certificate and signature with somedomain.com certificate. It may not even have to be a subdomain for this as such, but it may be a requirement for the browser to do this on the client side.

+1
source
  • The server hosting https webapp may cancel the proxy print server, but since the printer is local to the user, this may not work.
  • The print server must have the correct CORS headers.

     Access-Control-Allow-Origin: * 

    or

     Access-Control-Allow-Origin: https://www.example.com 

However, there are pitfalls using a wildcard .

+1
source

The easiest way is to add a route to webapp, which no more than sends a request to the print server. So make an AJAX POST request at https://myapp.com/print , and power on the server side, which will send the request to http://printerserver/print.php , with the same POST content that it received. As @dnozay said, this is commonly called a reverse proxy . Yes, for this you will have to reconfigure your print server to receive (authenticate) requests from the web server.

Alternatively, you can switch the print server to https and directly call it from the client.

Please note that an insecure (http) connection to web sockets on a secure page (https) may not work . And not in vain: it is usually a bad idea to mislead people by making insecure connections with what seems to them to be a reliable page.

+1
source

From what I understand from the question, printserver is not accessible from a web application, so the reverse proxy solution will not work here.

You are not allowed to make requests from the browser to the print server using the cross-origin policy.

If you want to communicate with the print server from an HTTPS page, you will need a print server to set print.php as HTTPS too.

You can create a DNS A record as a subdomain of your web application that resolves the internal address of your print server. A.

With these steps, you can refresh your print server page to respond with CORS enable headers that your browser must respect. I don’t think that the browser will even issue CORS requests using various protocol schemes (HTTPS vs HTTP) or internal domains without TLDs.

+1
source

Source: https://habr.com/ru/post/977370/


All Articles