|
|
@@ -3,19 +3,41 @@ package main |
|
|
|
import ( |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"strings" |
|
|
|
"encoding/json" |
|
|
|
) |
|
|
|
|
|
|
|
type output struct{} |
|
|
|
type jsonOutput struct { |
|
|
|
Ip string `json:"ip"` |
|
|
|
} |
|
|
|
|
|
|
|
func (o output) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
|
|
|
jsonOutput, err := json.MarshalIndent (r.RemoteAddr, "", " ") |
|
|
|
func ipJsonOutput (w http.ResponseWriter, r *http.Request) { |
|
|
|
var ip jsonOutput |
|
|
|
ip.Ip = remoteAddr(r) |
|
|
|
output, err := json.Marshal(ip) |
|
|
|
if err != nil { |
|
|
|
w.Write([]byte(err.Error())) |
|
|
|
} |
|
|
|
w.Write(jsonOutput) |
|
|
|
w.Write([]byte(output)) |
|
|
|
} |
|
|
|
|
|
|
|
func ipOutput (w http.ResponseWriter, r *http.Request) { |
|
|
|
ip := remoteAddr(r) |
|
|
|
w.Write([]byte(ip)) |
|
|
|
} |
|
|
|
|
|
|
|
func remoteAddr ( r *http.Request) string { |
|
|
|
var ip string |
|
|
|
if r.RemoteAddr[0] == '[' { |
|
|
|
ip = strings.Split(r.RemoteAddr,"]")[0][1:] |
|
|
|
} else { |
|
|
|
ip = strings.Split(r.RemoteAddr,":")[0] |
|
|
|
} |
|
|
|
return ip |
|
|
|
} |
|
|
|
|
|
|
|
func main() { |
|
|
|
log.Fatal(http.ListenAndServe(":8080", output{})) |
|
|
|
http.HandleFunc("/",ipOutput) |
|
|
|
http.HandleFunc("/json",ipJsonOutput) |
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil)) |
|
|
|
} |