Skip to content

Commit

Permalink
feat: add RemoteAddr to /api
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jul 8, 2023
1 parent 5a6136c commit 09b74d7
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -56,13 +55,14 @@ func init() {
}

type Data struct {
Hostname string `json:"hostname,omitempty"`
IP []string `json:"ip,omitempty"`
Headers http.Header `json:"headers,omitempty"`
URL string `json:"url,omitempty"`
Host string `json:"host,omitempty"`
Method string `json:"method,omitempty"`
Name string `json:"name,omitempty"`
Hostname string `json:"hostname,omitempty"`
IP []string `json:"ip,omitempty"`
Headers http.Header `json:"headers,omitempty"`
URL string `json:"url,omitempty"`
Host string `json:"host,omitempty"`
Method string `json:"method,omitempty"`
Name string `json:"name,omitempty"`
RemoteAddr string `json:"remoteAddr,omitempty"`
}

func main() {
Expand Down Expand Up @@ -164,8 +164,7 @@ func printBinary(s []byte) {
}

func dataHandler(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(r.URL.String())
queryParams := u.Query()
queryParams := r.URL.Query()

size, err := strconv.ParseInt(queryParams.Get("size"), 10, 64)
if err != nil {
Expand Down Expand Up @@ -206,9 +205,8 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
}
}

func whoamiHandler(w http.ResponseWriter, req *http.Request) {
u, _ := url.Parse(req.URL.String())
wait := u.Query().Get("wait")
func whoamiHandler(w http.ResponseWriter, r *http.Request) {
wait := r.URL.Query().Get("wait")
if len(wait) > 0 {
duration, err := time.ParseDuration(wait)
if err == nil {
Expand All @@ -227,24 +225,25 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
_, _ = fmt.Fprintln(w, "IP:", ip)
}

_, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr)
if err := req.Write(w); err != nil {
_, _ = fmt.Fprintln(w, "RemoteAddr:", r.RemoteAddr)
if err := r.Write(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func apiHandler(w http.ResponseWriter, req *http.Request) {
func apiHandler(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()

data := Data{
Hostname: hostname,
IP: getIPs(),
Headers: req.Header,
URL: req.URL.RequestURI(),
Host: req.Host,
Method: req.Method,
Name: name,
Hostname: hostname,
IP: getIPs(),
Headers: r.Header,
URL: r.URL.RequestURI(),
Host: r.Host,
Method: r.Method,
Name: name,
RemoteAddr: r.RemoteAddr,
}

w.Header().Set("Content-Type", "application/json")
Expand Down

0 comments on commit 09b74d7

Please sign in to comment.