Transforming TCP Sockets to HTTP With Go
Use Go to help your apps communicate!
Join the DZone community and get the full member experience.Join For Free
Sometimes, we need to work with legacy applications, and legacy applications can be hard to rewrite and change. Imagine, for example, we have an application that is using raw TCP sockets to communicate with another process. Raw TCP sockets are fast, but they have various problems. For example, all data is sent in plain text over the network and without authentication (if we don’t implement a protocol).
One solution is to use HTTPS connections instead. We can also authenticate those requests with an Authentication Bearer. For example, I’ve created a simple HTTP server with Python and Flask:
Now we only need to change our legacy application to use one HTTP client instead of raw TCP sockets. But sometimes, that's not possible. Imagine, for example, if this application runs on an old OS without HTTPS support or we cannot find and compile an HTTP client in the legacy application.
One possible solution is to isolate the application and change only the destination of the TCP socket. Instead of the original IP address, we can use localhost. and we can create a proxy at localhost that listens to TCP sockets and sends the information to the HTTP server.
And that’s all. We can upgrade our legacy application almost without changing the code.
The source code is available in my GitHub.
Published at DZone with permission of Gonzalo Ayuso, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.