2014-07-08 23:00:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2014-07-09 00:15:09 +00:00
|
|
|
"net/http"
|
2014-07-09 15:21:12 +00:00
|
|
|
"net/http/fcgi"
|
2015-05-16 00:33:27 +00:00
|
|
|
"os"
|
2014-07-09 00:15:09 +00:00
|
|
|
"path"
|
2014-07-08 23:00:49 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-11-16 21:16:06 +00:00
|
|
|
"strconv"
|
2014-07-09 00:15:09 +00:00
|
|
|
|
2014-07-08 23:00:49 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2015-11-30 16:23:27 +00:00
|
|
|
"github.com/brandfolder/gin-gorelic"
|
2014-07-08 23:00:49 +00:00
|
|
|
)
|
|
|
|
|
2014-07-09 01:27:05 +00:00
|
|
|
// Logger is a simple log handler, out puts in the standard of apache access log common.
|
|
|
|
// See http://httpd.apache.org/docs/2.2/logs.html#accesslog
|
2014-07-08 23:00:49 +00:00
|
|
|
func Logger() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
t := time.Now()
|
2014-08-10 19:02:21 +00:00
|
|
|
ip, err := net.ResolveTCPAddr("tcp", c.Request.RemoteAddr)
|
2014-07-08 23:00:49 +00:00
|
|
|
if err != nil {
|
2015-03-02 00:14:22 +00:00
|
|
|
c.Abort()
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// before request
|
|
|
|
c.Next()
|
|
|
|
// after request
|
|
|
|
|
2014-07-09 00:32:45 +00:00
|
|
|
user := "-"
|
2014-08-10 19:02:21 +00:00
|
|
|
if c.Request.URL.User != nil {
|
|
|
|
user = c.Request.URL.User.Username()
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
latency := time.Since(t)
|
|
|
|
|
|
|
|
// This is the format of Apache Log Common, with an additional field of latency
|
|
|
|
fmt.Printf("%v - %v [%v] \"%v %v %v\" %v %v %v\n",
|
2014-08-10 19:02:21 +00:00
|
|
|
ip.IP, user, t.Format(time.RFC3339), c.Request.Method, c.Request.URL.Path,
|
|
|
|
c.Request.Proto, c.Writer.Status(), c.Request.ContentLength, latency)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 18:34:34 +00:00
|
|
|
func stringInSlice(a string, list []string) bool {
|
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-16 21:16:06 +00:00
|
|
|
func testRemoteTCPPort(address string) bool {
|
|
|
|
_, err := net.DialTimeout("tcp", address, 3 * time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-07-08 23:00:49 +00:00
|
|
|
func mainHandler(c *gin.Context) {
|
|
|
|
fields := strings.Split(c.Params.ByName("field"), ".")
|
2014-08-10 19:02:21 +00:00
|
|
|
ip, err := net.ResolveTCPAddr("tcp", c.Request.RemoteAddr)
|
2014-07-08 23:00:49 +00:00
|
|
|
if err != nil {
|
2015-03-02 00:14:22 +00:00
|
|
|
c.Abort()
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
2015-03-02 00:14:22 +00:00
|
|
|
|
|
|
|
cfIP := net.ParseIP(c.Request.Header.Get("CF-Connecting-IP"))
|
|
|
|
if cfIP != nil {
|
|
|
|
ip.IP = cfIP
|
|
|
|
}
|
|
|
|
|
2015-11-16 21:16:06 +00:00
|
|
|
if fields[0] == "porttest" {
|
|
|
|
if len(fields) >= 2 {
|
|
|
|
if port, err := strconv.Atoi(fields[1]); err == nil && port > 0 && port <= 65535 {
|
|
|
|
c.String(200, fmt.Sprintln(testRemoteTCPPort(ip.IP.String() + ":" + fields[1])))
|
|
|
|
} else {
|
|
|
|
c.String(400, "Invalid Port Number")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.String(400, "Need Port")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-08 23:00:49 +00:00
|
|
|
c.Set("ip", ip.IP.String())
|
|
|
|
c.Set("port", ip.Port)
|
2014-08-10 19:02:21 +00:00
|
|
|
c.Set("ua", c.Request.UserAgent())
|
|
|
|
c.Set("lang", c.Request.Header.Get("Accept-Language"))
|
|
|
|
c.Set("encoding", c.Request.Header.Get("Accept-Encoding"))
|
|
|
|
c.Set("method", c.Request.Method)
|
|
|
|
c.Set("mime", c.Request.Header.Get("Accept"))
|
|
|
|
c.Set("referer", c.Request.Header.Get("Referer"))
|
|
|
|
c.Set("forwarded", c.Request.Header.Get("X-Forwarded-For"))
|
2015-03-04 13:15:24 +00:00
|
|
|
c.Set("country_code", c.Request.Header.Get("CF-IPCountry"))
|
2014-07-08 23:00:49 +00:00
|
|
|
|
2015-10-26 20:30:36 +00:00
|
|
|
ua := strings.Split(c.Request.UserAgent(), "/")
|
|
|
|
|
2014-07-10 18:34:34 +00:00
|
|
|
// Only lookup hostname if the results are going to need it.
|
2015-10-26 20:30:36 +00:00
|
|
|
if stringInSlice(fields[0], []string{"all", "host"}) || (fields[0] == "" && ua[0] != "curl") {
|
2014-07-10 18:34:34 +00:00
|
|
|
hostnames, err := net.LookupAddr(ip.IP.String())
|
|
|
|
if err != nil {
|
|
|
|
c.Set("host", "")
|
|
|
|
} else {
|
|
|
|
c.Set("host", hostnames[0])
|
|
|
|
}
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wantsJSON := false
|
|
|
|
if len(fields) >= 2 && fields[1] == "json" {
|
|
|
|
wantsJSON = true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch fields[0] {
|
|
|
|
case "":
|
|
|
|
//If the user is using curl, then we should just return the IP, else we show the home page.
|
|
|
|
if ua[0] == "curl" {
|
|
|
|
c.String(200, fmt.Sprintln(ip.IP))
|
|
|
|
} else {
|
2014-07-09 00:15:09 +00:00
|
|
|
c.HTML(200, "index.html", c.Keys)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
case "request":
|
2014-08-10 19:02:21 +00:00
|
|
|
c.JSON(200, c.Request)
|
2014-07-08 23:00:49 +00:00
|
|
|
return
|
|
|
|
case "all":
|
|
|
|
if wantsJSON {
|
|
|
|
c.JSON(200, c.Keys)
|
|
|
|
} else {
|
|
|
|
c.String(200, "%v", c.Keys)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:30:36 +00:00
|
|
|
fieldResult, exists := c.Get(fields[0])
|
|
|
|
if !exists {
|
2014-07-08 23:00:49 +00:00
|
|
|
c.String(404, "Not Found")
|
2015-10-26 20:30:36 +00:00
|
|
|
return
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
c.String(200, fmt.Sprintln(fieldResult))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-09 00:32:45 +00:00
|
|
|
// FileServer is a basic file serve handler, this is just here as an example.
|
|
|
|
// gin.Static() should be used instead
|
2014-07-08 23:00:49 +00:00
|
|
|
func FileServer(root string) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
file := c.Params.ByName("file")
|
|
|
|
if !strings.HasPrefix(file, "/") {
|
2014-07-09 00:15:09 +00:00
|
|
|
file = "/" + file
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
2014-08-10 19:02:21 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(root, path.Clean(file)))
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := gin.New()
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
r.Use(Logger())
|
2015-10-26 20:30:36 +00:00
|
|
|
r.LoadHTMLGlob("templates/*")
|
2014-07-08 23:00:49 +00:00
|
|
|
|
2015-11-30 16:23:27 +00:00
|
|
|
if NEWRELIC_LICENSE_KEY := os.Getenv("NEWRELIC_LICENSE_KEY"); NEWRELIC_LICENSE_KEY != "" {
|
|
|
|
var NEWRELIC_APPLICATION_NAME string
|
|
|
|
if NEWRELIC_APPLICATION_NAME = os.Getenv("NEWRELIC_APPLICATION_NAME"); NEWRELIC_APPLICATION_NAME == "" {
|
|
|
|
NEWRELIC_APPLICATION_NAME = "ifconfig.io"
|
|
|
|
}
|
|
|
|
gorelic.InitNewrelicAgent(NEWRELIC_LICENSE_KEY, NEWRELIC_APPLICATION_NAME, true)
|
|
|
|
r.Use(gorelic.Handler)
|
|
|
|
}
|
|
|
|
|
2014-07-08 23:00:49 +00:00
|
|
|
r.GET("/:field", mainHandler)
|
|
|
|
r.GET("/", mainHandler)
|
|
|
|
|
2014-07-09 15:21:12 +00:00
|
|
|
// Create a listener for FCGI
|
|
|
|
fcgi_listen, err := net.Listen("tcp", "127.0.0.1:4000")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
2015-05-16 00:33:27 +00:00
|
|
|
errc := make(chan error)
|
|
|
|
go func(errc chan error) {
|
|
|
|
for err := range errc {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}(errc)
|
|
|
|
|
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- fcgi.Serve(fcgi_listen, r)
|
|
|
|
}(errc)
|
2014-07-09 15:21:12 +00:00
|
|
|
|
2015-05-16 00:33:27 +00:00
|
|
|
port := os.Getenv("PORT")
|
|
|
|
host := os.Getenv("HOST")
|
|
|
|
if port == "" {
|
|
|
|
port = "8080"
|
|
|
|
}
|
|
|
|
errc <- r.Run(host + ":" + port)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|