Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socat fake http server - use a file as server response

Tags:

socat

I've been trying to use socat to respond on each connection to a socket it's listening to with a fake HTTP reply. I cannot get it working. It might be because I'm using the cygwin version of socat? I don't know.

Part of the problem is I want the second argument <some_file_response> not to be written to. In other words because it's bidirectional it will read what's in response.txt and then write to that same file, and I don't want that. Even if I do open:response.txt,rdonly it doesn't work repeatedly. system: doesn't seem to do anything. exec seems like it works, for example I can do exec:'cat response.txt' but that never gets sent to the client connecting to port 1234.

socat -vv tcp-listen:1234,reuseaddr,fork <some_file_response>

I want it to read a file to the client that's connected and then close the connection, and do that over and over again (that's why I used fork).

I am putting a bounty on this question. Please only give me solutions that work with the cygwin version from the windows command prompt.

like image 340
loop Avatar asked Apr 20 '15 05:04

loop


People also ask

What is Socat file?

Socat (for SOcket CAT) establishes two bidirectional byte streams and transfers data between them. Data channels may be files, pipes, devices (terminal or modem, etc.), or sockets (Unix, IPv4, IPv6, raw, UDP, TCP, SSL).

Can Netcat connect to Socat?

Netcat and Socat allows you to pass simple messages between computers interactively over the network. The below setup will allow both client and server to send data to the other party. It can act like a simple ad-hoc chat program. Socat can talk to Netcat and Netcat can talk to Socat.

What is Socat command in Linux?

Socat is a flexible, multi-purpose relay tool. Its purpose is to establish a relationship between two data sources, where each data source can be a file, a Unix socket, UDP, TCP, or standard input.


2 Answers

Tested with cygwin:

 socat -vv TCP-LISTEN:1234,crlf,reuseaddr,fork SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; cat <some_file_response>"

If you do not want a complete HTTP response, leave out the echos:

socat -vv TCP-LISTEN:1234,crlf,reuseaddr,fork SYSTEM:"cat <some_file_response>"
like image 81
Jack Miller Avatar answered Oct 12 '22 01:10

Jack Miller


Taken from socat examples

socat -vv TCP-LISTEN:8000,crlf,reuseaddr,fork SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; cat"
like image 24
flotto Avatar answered Oct 12 '22 02:10

flotto