-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiplookupstandalone.go
More file actions
123 lines (104 loc) · 3.03 KB
/
Copy pathiplookupstandalone.go
File metadata and controls
123 lines (104 loc) · 3.03 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Package is to support getting an IP address from a client's browser and returing IP Address
It provides functionality comparable to http://jsonip.appspot.com
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/LukeMauldin/goext/applog"
"net/http"
"strings"
)
var (
listenAddress = flag.String("listenAddress", ":8080", "HTTP listen address")
)
func main() {
//Log any panics
defer func() {
if err := recover(); err != nil {
applog.Errorf("Panic: %v", err)
}
}()
//Parse flag variables
flag.Parse()
//Start HTTP server
applog.Infof("Listening on address: %v", *listenAddress)
http.HandleFunc("/GetClientIPAddress", errorHandler(handlerGetClientIPAddress))
err := http.ListenAndServe(*listenAddress, nil)
if err != nil {
applog.Errorf("%v", err)
}
}
func handlerGetClientIPAddress(w http.ResponseWriter, r *http.Request) error {
//Verify that request has an origin handler
if r.Header.Get("Origin") == "" {
return newErrorHttp(http.StatusBadRequest, "Cross domain request require Origin header")
}
//Verify that the request method is a GET
if r.Method != "GET" {
return newErrorHttp(http.StatusBadRequest, "Cross domain request only supports GET")
}
//Set response headers
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Methods", "GET")
w.Header().Add("Content-Type", "application/json")
w.Header().Add("Cache-Control", "no-cache")
//Get IP address from request and set it in return data
retData := &getClientIPAddressReturn{IP: stripPort(r.RemoteAddr)}
encodedRetData, err := json.Marshal(retData)
if err != nil {
return newErrorHttp(http.StatusInternalServerError, err.Error())
}
//Return the response to the user
_, err = w.Write(encodedRetData)
if err != nil {
return newErrorHttp(http.StatusInternalServerError, err.Error())
}
return nil
}
func errorHandler(f httpHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
if err, ok := rec.(error); ok {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
http.Error(w, fmt.Sprintf("%v", rec), http.StatusInternalServerError)
}
}
}()
err := f(w, r)
if err != nil {
switch err := err.(type) {
case *errorHttp:
http.Error(w, err.message, err.code)
default:
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
}
type getClientIPAddressReturn struct {
IP string
}
type errorHttp struct {
code int
message string
}
func (f *errorHttp) Error() string {
return fmt.Sprintf("%d - %s", f.code, f.message)
}
func newErrorHttp(code int, message string) error {
return &errorHttp{code: code, message: message}
}
type httpHandler func(http.ResponseWriter, *http.Request) error
func stripPort(ipAddress string) string {
index := strings.Index(ipAddress, ":")
if index == -1 {
return ipAddress
} else {
return ipAddress[0:index]
}
}