ยง2024-11-05
- What is socat?
socat stands for SOcket CAT. It is a utility for data transfer between two addresses.
What makes socat so versatile is the fact that an address can represent a network socket, any file descriptor, a Unix domain datagram or stream socket, TCP and UDP (over both IPv4 and IPv6), SOCKS 4/4a over IPv4/IPv6, SCTP, PTY, datagram and stream sockets, named and unnamed pipes, raw IP sockets, OpenSSL, or on Linux even any arbitrary network device.
- ubunt installation
sudo apt install socat
- Usage
socat [options] <address1> <address2>
-
address1 is the source
-
address2 is the destination address
-
socat -d -d - TCP4:www.example.com:80
-d -d
options-
first addres `TCP4:www.example.com:80
second address
- example
- if you have nginx running on localhost:80
$ curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- then run on terminal 1
# socat -v TCP4-LISTEN:8080,fork TCP4:localhost:80
- then from terminal 2
$ curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- then on terminal 1
# socat -v TCP4-LISTEN:8080,fork TCP4:localhost:80
> 2024/11/05 08:09:50.000182890 length=77 from=0 to=76
GET / HTTP/1.1\r
Host: localhost:8080\r
User-Agent: curl/8.5.0\r
Accept: */*\r
\r
< 2024/11/05 08:09:50.000183684 length=862 from=0 to=861
HTTP/1.1 200 OK\r
Server: nginx/1.24.0 (Ubuntu)\r
Date: Tue, 05 Nov 2024 00:09:50 GMT\r
Content-Type: text/html\r
Content-Length: 615\r
Last-Modified: Mon, 04 Nov 2024 05:46:38 GMT\r
Connection: keep-alive\r
ETag: "67285fbe-267"\r
Accept-Ranges: bytes\r
\r
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4. example
$ echo -e "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" | socat - TCP:localhost:80 HTTP/1.1 200 OK Server: nginx/1.24.0 (Ubuntu) Date: Mon, 04 Nov 2024 21:59:44 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Mon, 04 Nov 2024 05:46:38 GMT Connection: close ETag: "67285fbe-267" Accept-Ranges: bytes
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
```