feat: add RemoteAddr to /api
This commit is contained in:
25
app.go
25
app.go
@ -10,7 +10,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -63,6 +62,7 @@ type Data struct {
|
|||||||
Host string `json:"host,omitempty"`
|
Host string `json:"host,omitempty"`
|
||||||
Method string `json:"method,omitempty"`
|
Method string `json:"method,omitempty"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
RemoteAddr string `json:"remoteAddr,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -164,8 +164,7 @@ func printBinary(s []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func dataHandler(w http.ResponseWriter, r *http.Request) {
|
func dataHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
u, _ := url.Parse(r.URL.String())
|
queryParams := r.URL.Query()
|
||||||
queryParams := u.Query()
|
|
||||||
|
|
||||||
size, err := strconv.ParseInt(queryParams.Get("size"), 10, 64)
|
size, err := strconv.ParseInt(queryParams.Get("size"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -206,9 +205,8 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func whoamiHandler(w http.ResponseWriter, req *http.Request) {
|
func whoamiHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
u, _ := url.Parse(req.URL.String())
|
wait := r.URL.Query().Get("wait")
|
||||||
wait := u.Query().Get("wait")
|
|
||||||
if len(wait) > 0 {
|
if len(wait) > 0 {
|
||||||
duration, err := time.ParseDuration(wait)
|
duration, err := time.ParseDuration(wait)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -227,24 +225,25 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
_, _ = fmt.Fprintln(w, "IP:", ip)
|
_, _ = fmt.Fprintln(w, "IP:", ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr)
|
_, _ = fmt.Fprintln(w, "RemoteAddr:", r.RemoteAddr)
|
||||||
if err := req.Write(w); err != nil {
|
if err := r.Write(w); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiHandler(w http.ResponseWriter, req *http.Request) {
|
func apiHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
|
|
||||||
data := Data{
|
data := Data{
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
IP: getIPs(),
|
IP: getIPs(),
|
||||||
Headers: req.Header,
|
Headers: r.Header,
|
||||||
URL: req.URL.RequestURI(),
|
URL: r.URL.RequestURI(),
|
||||||
Host: req.Host,
|
Host: r.Host,
|
||||||
Method: req.Method,
|
Method: r.Method,
|
||||||
Name: name,
|
Name: name,
|
||||||
|
RemoteAddr: r.RemoteAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
Reference in New Issue
Block a user