Skip to content

Commit

Permalink
Fix setting the Host header (#1)
Browse files Browse the repository at this point in the history
* Update to hit localhost

This change will allow us to add 127.0.0.1 to the list or allowed hosts in
Rails 6

* Turns out, go overwrites the Host header

It looks at the requests Host property, and if it's not set it falls back to the
URL's host

* We don't need to change the ip when setting the host property
  • Loading branch information
maddiesch authored and bensie committed Jul 15, 2019
1 parent 0b8639a commit 3d50786
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/elb_health_check
/elb_health_check_*
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: build
build:
go build -o elb_health_check .

.PHONY: release
release:
GOOS=linux GOARCH=amd64 $(MAKE) build
mv elb_health_check elb_health_check_linux_amd64
GOOS=linux GOARCH=386 $(MAKE) build
mv elb_health_check elb_health_check_linux_386
GOOS=darwin GOARCH=amd64 $(MAKE) build
mv elb_health_check elb_health_check_darwin_amd64
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/bensie/elb_health_check_go

go 1.12
26 changes: 18 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
)

// Hostnames contain the list of hosts that are being checked agains.
var Hostnames = strings.Split(os.Getenv("HEALTH_CHECK_HOSTNAMES"), ",")

type HttpResponse struct {
// HTTPResponse contains the http response data to the specified host
type HTTPResponse struct {
hostname string
response *http.Response
err error
check string
}

// CheckResponse contains the HTTP status response from the specified host
type CheckResponse struct {
Status int `json:"status"`
Check string `json:"check"`
}

func main() {
port := ""
flag.StringVar(&port, "port", "9292", "the port to listen on")

flag.Parse()

fmt.Fprintln(os.Stderr, "starting listener on port", port)
http.HandleFunc("/", mainHandler)
log.Fatal(http.ListenAndServe(":9292", nil))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

func mainHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -37,13 +47,13 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
hostnamesAllowedToFail := strings.Split(allowedToFailQueryParam, ",")
hostnamesMustSucceed := strings.Split(mustSucceedQueryParam, ",")

ch := make(chan *HttpResponse, len(Hostnames))
ch := make(chan *HTTPResponse, len(Hostnames))
for _, hostname := range Hostnames {
go makeRequest(hostname, ch)
}

// Block until we get responses for all hostnames
responses := []*HttpResponse{}
responses := []*HTTPResponse{}
for range Hostnames {
responses = append(responses, <-ch)
}
Expand All @@ -57,7 +67,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
httpResponseData[r.hostname] = CheckResponse{code, r.check}
}

filteredResponses := []*HttpResponse{}
filteredResponses := []*HTTPResponse{}
if len(hostnamesAllowedToFail) > 0 {
for _, r := range responses {
if !contains(hostnamesAllowedToFail, r.hostname) {
Expand Down Expand Up @@ -94,11 +104,11 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(json))
}

func makeRequest(hostname string, ch chan<- *HttpResponse) {
func makeRequest(hostname string, ch chan<- *HTTPResponse) {
client := &http.Client{}

req, _ := http.NewRequest("HEAD", "http://0.0.0.0:80/health_check", nil)
req.Header.Add("Host", hostname)
req.Host = hostname
req.Header.Add("X-Forwarded-Proto", "https")

resp, err := client.Do(req)
Expand All @@ -112,7 +122,7 @@ func makeRequest(hostname string, ch chan<- *HttpResponse) {
}
}

ch <- &HttpResponse{hostname, resp, err, check}
ch <- &HTTPResponse{hostname, resp, err, check}
}

func contains(stringSlice []string, searchString string) bool {
Expand Down
8 changes: 0 additions & 8 deletions release.sh

This file was deleted.

0 comments on commit 3d50786

Please sign in to comment.