Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to handle CF-Connecting-IP header #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
# GoLand / IntelliJ
.idea
29 changes: 20 additions & 9 deletions api/get_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ package api
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"github.com/rdegges/ipify-api/models"
"net"
"net/http"
"strings"

"github.com/julienschmidt/httprouter"
"github.com/rdegges/ipify-api/models"
)

// GetIP returns a user's public facing IP address (IPv4 OR IPv6).
Expand All @@ -26,16 +27,26 @@ func GetIP(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
panic(err)
}

// We'll always grab the first IP address in the X-Forwarded-For header
// list. We do this because this is always the *origin* IP address, which
// is the *true* IP of the user. For more information on this, see the
// Wikipedia page: https://en.wikipedia.org/wiki/X-Forwarded-For
ip := net.ParseIP(strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]).String()
// We test in order of priority valid client IP headers
ip := net.ParseIP(r.Header.Get("CF-Connecting-IP"))

if ip == nil {
// We'll always grab the first IP address in the X-Forwarded-For header
// list. We do this because this is always the *origin* IP address, which
// is the *true* IP of the user. For more information on this, see the
// Wikipedia page: https://en.wikipedia.org/wiki/X-Forwarded-For
ip = net.ParseIP(strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0])
}

if ip == nil {
host, _, _ := net.SplitHostPort(r.RemoteAddr)
ip = net.ParseIP(host)
}

// If the user specifies a 'format' querystring, we'll try to return the
// user's IP address in the specified format.
if format, ok := r.Form["format"]; ok && len(format) > 0 {
jsonStr, _ := json.Marshal(models.IPAddress{ip})
jsonStr, _ := json.Marshal(models.IPAddress{ip.String()})

switch format[0] {
case "json":
Expand All @@ -59,5 +70,5 @@ func GetIP(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// If no 'format' querystring was specified, we'll default to returning the
// IP in plain text.
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, ip)
fmt.Fprintf(w, ip.String())
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
package main

import (
"github.com/julienschmidt/httprouter"
"github.com/rdegges/ipify-api/api"
"github.com/rs/cors"
"log"
"net/http"
"os"

"github.com/julienschmidt/httprouter"
"github.com/rdegges/ipify-api/api"
"github.com/rs/cors"
)

// main launches our web server which runs indefinitely.
Expand All @@ -39,5 +40,4 @@ func main() {

log.Println("Starting HTTP server on port:", port)
log.Fatal(http.ListenAndServe(":"+port, handler))

}