-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (32 loc) · 769 Bytes
/
main.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
)
func main() {
var host string
flag.StringVar(&host, "host", "", "the hostname")
var port int
flag.IntVar(&port, "port", 8010, "the port")
flag.Parse()
http.HandleFunc("/", homeHandler)
log.Printf("Starting echo-ip service on %s:%v", host, port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%v", host, port), nil))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, ReadUserIP(r))
}
func ReadUserIP(r *http.Request) string {
IPAddress := r.Header.Get("X-Real-Ip")
if len(IPAddress) == 0 {
IPAddress = r.Header.Get("X-Forwarded-For")
if len(IPAddress) == 0 {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
IPAddress = ip
}
}
return IPAddress
}