Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from DNSFilter/feature/more-ip-headers
Browse files Browse the repository at this point in the history
Support for more client headers
  • Loading branch information
MikeSchroll authored Dec 7, 2017
2 parents bc85a1c + 4a6019f commit 604756a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
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/DNSFilter/ipify-api/models"
"github.com/julienschmidt/httprouter"
)

// 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/DNSFilter/ipify-api/api"
"github.com/julienschmidt/httprouter"
"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))

}

0 comments on commit 604756a

Please sign in to comment.