summaryrefslogtreecommitdiff
path: root/ip.go
blob: 6a0243ecdc815c9157b0af935a09df250db749fe (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
package main

import (
	"io"
	"net"
	"net/http"
)

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

func ip(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	if remote := r.Header.Get("X-Forwarded-For"); remote != "" {
		io.WriteString(w, remote)
		return
	}
	addr, _, err := net.SplitHostPort(r.RemoteAddr)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	io.WriteString(w, addr)
}