BGP.guru

BGP.guru

Nerd blog.

14 Dec 2015

ipquail-go

I’ve been waiting to add some features to ipquail.com for a while, but the way in which I was handling API endpoints at the moment needed to change before I could accommodate anything fancier – at the moment I was using simple Server Side Includes (“SSI” in 90’s Apache web server terminology) and some mime-type modifications to fake API endpoints. This needed to change.

I’d seen some code for how to handle this type of request using pilu/traffic, and thought “this should work” to myself. Several hours later I had pushed a new project to my github, ipquail-go – a go rewrite of ipquail.com.

The main functionality of the web application is handled thru the nginx configs mentioned in the README.md. This basically sets up nginx to handle all files in the ipquail/public directory as the webroot, and then nginx passes a few more specific URLs (/ip, /ptr, and /api/*) on to the new Go backend.

The main() function in the backend sets up the 4 API endpoints which will be passed on to it, and which handlers will be taking care of each endpoint.

  • /ip - echos straight IP
  • /ptr - echos PTR if exists, “none” otherwise
  • /api/ip - echos { “ip”: “<IP_STRING>” } w/ proper JSON headers
  • /api/ptr - echos { “ptr”: “<PTR_STRING>” } w/ proper JSON headers
func main() {
  router := traffic.New()

  // add a route for each page you add to the site
  // make sure you create a route handler for it
  router.Get("/ip", ipHandler)
  router.Get("/ptr", ptrHandler)
  router.Get("/api/ip", ipapiHandler)
  router.Get("/api/ptr", ptrapiHandler)
  router.Run()
}

An example handler, used to print just the IP that the request came from. Having request IP as a header other than Host lets IPv4/IPv6 termination be handled by nginx, and the backend just simply print it out.

func ipHandler(w traffic.ResponseWriter, r *traffic.Request) {
  traffic.Logger().Print( r.Header.Get("X-Forwarded-For") ) 
  w.Header().Add("Content-type", "text/plain")
  w.WriteText( r.Header.Get("X-Forwarded-For") )
}

Theodore Baschak - Theo is a network engineer with experience operating core internet technologies like HTTP, HTTPS and DNS. He has extensive experience running service provider networks with OSPF, MPLS, and BGP.