google-app-enginegoogle-domain-api

Set custom "A" resource record with google app engine to avoid redirection


I have a web service hosted on a home PC which is on a public dynamic ip. I also have a domain name hosted by google domains (I will refer in this post as www.example.com).

Via Google App Engine I'm able to store the dynamic IP on memcache and once a client points to myhome subdomain it will be redirected to the host ip (e.g. myhome.example.com -> 111.111.1.100).

Here is my python code running on App Engine:

import json
from webapp2 import RequestHandler, WSGIApplication
from google.appengine.api import memcache

class MainPage(RequestHandler):
    # Memcache keys
    key_my_current_ip = "my_current_ip"

    def get(self):
        response_text = "example.com is online!"
        # MyHome request
        if (self.request.host.lower().startswith('myhome.example.com') or
            self.request.host.lower().startswith('myhome-xyz.appspot.com')):
            my_current_ip = memcache.get(self.key_my_current_ip)
            if my_current_ip:
                url = self.request.url.replace(self.request.host, my_current_ip)
                # move to https
                if url.startswith("http://"):
                    url = url.replace("http://", "https://")
                return self.redirect(url, True)
            response_text = "myhome is offline!"
        # Default site
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(response_text)

    def post(self):
        try:
            # Store request remote ip
            body = json.loads(self.request.body)
            command = body['command']
            if command == 'ping':
                memcache.set(key=self.key_my_current_ip, value=self.request.remote_addr)
        except:
            raise Exception("bad request!")

app = WSGIApplication([('/.*', MainPage),], debug=True)

What I found is that, by adding an "A" domain record, myhome.example.com will point directly to that ip without need to redirect it.

enter image description here

Is there a way I can update that "A" domain record using Google App Engine or Domain APIs?

def post(self):
    try:
        # Store request remote ip
        body = json.loads(self.request.body)
        command = body['command']
        if command == 'ping':
            **--> UPDATE "A" Record with this IP: <self.request.remote_addr>**
    except:
        raise Exception("bad request!")

Solution

  • As google stated you can use an https POST to achive this.

    You can perform updates manually with the API by making making a POST request (GET is also allowed) to the following url: https://domains.google.com/nic/update

    The API requires HTTPS. Here’s an example request: https://username:password@domains.google.com/nic/update?hostname=subdomain.yourdomain.com&myip=1.2.3.4

    Note: You must set a user agent in your request as well. Web browsers will generally add this for you when testing via the above url. In any case, the final HTTP request sent to our servers should look something like this:

    Example HTTP query: POST /nic/update?hostname=subdomain.yourdomain.com&myip=1.2.3.4 HTTP/1.1 Host: domains.google.com Authorization: Basic base64-encoded-auth-string User-Agent: Chrome/41.0 your_email@yourdomain.com