Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions beeping.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func handlerCheck(c *gin.Context) {
log.Println("[WARN] Invalid target:", err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
} else {
response, err := CheckHTTP(check)
response, err := CheckHTTP(check, c.Request)
if err != nil {
log.Println("[WARN] Check failed:", err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
Expand All @@ -179,7 +179,7 @@ func handlerCheck(c *gin.Context) {
}
}
} else {
response, err := CheckHTTP(check)
response, err := CheckHTTP(check, c.Request)
if err != nil {
log.Println("[WARN] Check failed:", err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
Expand All @@ -195,7 +195,7 @@ func handlerCheck(c *gin.Context) {
}

// CheckHTTP do HTTP check and return a beeping response
func CheckHTTP(check *Check) (*Response, error) {
func CheckHTTP(check *Check, request *http.Request) (*Response, error) {
var response = NewResponse()
var conn net.Conn

Expand All @@ -204,6 +204,24 @@ func CheckHTTP(check *Check) (*Response, error) {
return nil, err
}

// Add the Forwarded header to track remote IP address in requests.
src := request.RemoteAddr
if strings.Count(src, ":") > 1 {
// IPv6 address. Remove port from header (unneeded information disclosure)
srcSlice := strings.Split(src, ":")
srcIP := "["
for index := 0; index < len(srcSlice)-2; index++ {
srcIP += fmt.Sprintf("%s:", srcSlice[index])
}
srcIP += fmt.Sprintf("%s]", srcSlice[len(srcSlice)-2])
req.Header.Set("Forwarded", fmt.Sprintf("for=\"%s\"", srcIP))
} else {
// IPv4 address. Remove port from header (unneeded information disclosure)
srcSlice := strings.Split(src, ":")
srcIP := srcSlice[0]
req.Header.Set("Forwarded", fmt.Sprintf("for=%s", srcIP))
}

req.Header.Set("User-Agent", USERAGENT)
// Create go-httpstat powered context and pass it to http.Request
var result httpstat.Result
Expand Down