iMasters.com - Knowledge for Developers.
Follow Us
Home Backend How to find out client and server IP using Golang
Backend

How to find out client and server IP using Golang

Recently I ran into an interesting problem. I needed to upload a small REST service to manage some hardware: all without human contact and with several network interfaces.

Read the original article here

At a certain point, my system has to upload an auxiliary service and inform the client which IP and port he should access. Usually, this would be done with a config file somewhere, but one requirement is that I couldn’t guess my IP because it could come from any of the network interfaces, and leaving all this parameterized would be too complex.

The solution was to automate the whole process within my code, and for that, I needed to know both my IP and the client’s, so I would know which network to respond to, and so on.

As Go has a pretty good net library, I also wanted to support ipv6, and it’s not difficult. Just be careful to use what Go provides and not assume the IP and port format.

Handling IP

There are two functions from the net package that aren’t exported, so I copied them into my code to help parse the result.

The last function is to extract the last part of a string given a separator. In this case, we are using it to extract the zone in IPv6 addresses. This zone is usually the name of the network interface.

The splitHostZone function returns the host and the zone separately. If there is no zone, it will be an empty string. I need it because when I join the IP with the port again. I don’t want to include the zone.

Getting the server IP

There are several ways to find the server’s IP, but the simpler “Go style” I found was taking the address placed in the context of the request.

Getting the client’s IP, it’s easier because it’s already in the request, so it’s just handling the return as we did before.

Here is a gist with the example code.

Written by
Cesar Gimenes

He has been working with technology since the 1990s. He has worked in the field of education and participated in high-volume mobility projects for pharmaceutical laboratories. He created games for both PC and iOS. Today it is focusing its efforts on Embedded Systems platforms, IoT, microservices, and cloud computing. He is an enthusiast of technologies such as Golang and Docker.

Leave a comment

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Related Articles

Backend

How to Create a Skill for Amazon’s Virtual Assistant Alexa

If you didn’t know, it is not necessary for an Amazon Echo...

Backend

The APIs role in a 5G world

5G is about to revolutionize how we connect and use technology daily....

Backend

EF Core using AsNoTracking with Identity Resolution

Today we will see the consequences of using AsNoTracking with the Identity...

Backend

Understand key features added in ASP.NET Core 7.0

Version 7.0 of the .NET platform brought many new features in this...