Get your IP address for remote connection

During Christmas time, I used to spend time in my familly abroad. Thanks to internet, it is now easy to access my files etc. with cloud services, or better if I want to access my softwares, remote desktop. There are free services such as TeamViewer, but I need to install a server and a client which could be anoying. In a Windows environment, I could use the embedded remote desktop, but for this, I need to know my personal IP address. As this address changes frequently, it could eventually change during your journey so that I could not connect to my desktop anymore. The solution I propose here is to build a simple application that checks every hour my remote IP address, and whenever it changes, it updates a document in my Google Drive, and sends me an email.

Public IP address

The most easily part is to check for my IP. For that, I'll use the web site which gives me my public IP as a string. Here is the sample code

 Google API

Next part is a little trickier as I'll use the Google API to access my Drive and the text file where I'll store my IP. For better explanations on the API, I strongly recommand you take a look at the documentation of the API or at the Quickstart which is all we need to start this little project.

In my project, I first connect to the Google service by constructing the global service class "this.GoogleAPI" provided in the Google API. Here is the code

Note that I used here a JSON file to store my credentials for security reasons as this file can be embedded directly in the solution.

Next, I'll use this service to retrieve the text file where I want to store my IP address "MyIpAddress.txt". You could find inspiration on the Google Drive example application. Here, I'll query my Drive to find such a file, and if it does not exists, I'll create it (I suppose there exists only one suc file in my whole Drive...). When all this is done, I have the ID of my file so that I can easily, update it later.

Finally, I propose a simple method to update the document

Note that the File object is a Google API object from the namespace Google.Apis.Drive.v2.Data

From this point, I am able to check for my public IP every hour so that I always know which to use to connect remotely to my desktop !

Bonus : email sending

Finally as a bonus and not to have always to check in my Google Drive documents if my IP address has changed, the application I built sends an email when the IP has changed. For this, I used the .NET mail service in the namespace "System.Net.Mail". For security reasons, my credentials are stored in an embedded JSON file which I deserialize at runtime to a class "MailSecrets" :

To play with JSON files, there is a nice Nuget package named Json.NET which I used here. Next is nothing complicated, defining a smtp service and an email address for the sender and the receiver, then building a message to send. As I used my Gmail address to send the email and the SMTP server requires a secured connection, I had  to enable Less Secure Sign-In in my Google account.

Hope this could help to keep IP address up to date.
In a next post, I'll explore the same but using the Google Gmail API to send an email more securely just like I used the Google Drive API (both are quite similarin authentification procedure), but as it is an application I run on my own desktop, there is no problem.
Enjoy !

Leave a Reply

%d bloggers like this: