summaryrefslogtreecommitdiff
path: root/ip.go
blob: f3ec3f1641eeb16303c90c15d415cbb9a3e31452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"fmt"
	"net"
	"net/http"
)

func init() {
	http.HandleFunc("/ip", ip)
}

func ip(w http.ResponseWriter, r *http.Request) {
	addr, _, err := net.SplitHostPort(r.RemoteAddr)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Fprintln(w, addr)
	host, err := net.LookupAddr(addr)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Fprintln(w, host)
}