ifconfig.io/main.go

206 lines
4.7 KiB
Go
Raw Normal View History

2014-07-08 23:00:49 +00:00
package main
import (
"fmt"
"net"
"os"
2015-11-30 17:03:37 +00:00
"strconv"
2014-07-08 23:00:49 +00:00
"strings"
"time"
2014-07-08 23:00:49 +00:00
"github.com/gin-gonic/gin"
)
type Configuration struct {
hostname string // Displayed Hostname
host string // Listened Host
port string // HTTP Port
tls bool // TLS enabled
tlscert string // TLS Cert Path
tlskey string // TLS Cert Key Path
tlsport string // HTTPS Port
}
var configuration = Configuration{}
func init() {
hostname := getEnvWithDefault("HOSTNAME", "ifconfig.io")
host := getEnvWithDefault("HOST", "")
port := getEnvWithDefault("PORT", "8080")
tlsenabled := getEnvWithDefault("TLS", "0")
tlsport := getEnvWithDefault("TLSPORT", "8443")
tlscert := getEnvWithDefault("TLSCERT", "/opt/ifconfig/.cf/ifconfig.io.crt")
tlskey := getEnvWithDefault("TLSKEY", "/opt/ifconfig/.cf/ifconfig.io.key")
configuration = Configuration{
hostname: hostname,
host: host,
port: port,
tls: tlsenabled == "1",
tlscert: tlscert,
tlskey: tlskey,
tlsport: tlsport,
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func testRemoteTCPPort(address string) bool {
2015-11-30 17:03:37 +00:00
_, 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) {
2019-06-07 21:20:18 +00:00
// fields := strings.Split(c.Params.ByName("field"), ".")
2020-06-14 20:40:47 +00:00
URLFields := strings.Split(strings.Trim(c.Request.URL.EscapedPath(), "/"), "/")
fields := strings.Split(URLFields[0], ".")
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 {
c.Abort()
2014-07-08 23:00:49 +00:00
}
cfIP := net.ParseIP(c.Request.Header.Get("CF-Connecting-IP"))
if cfIP != nil {
ip.IP = cfIP
}
2015-11-30 17:03:37 +00:00
if fields[0] == "porttest" {
if len(fields) >= 2 {
if port, err := strconv.Atoi(fields[1]); err == nil && port > 0 && port <= 65535 {
2015-11-30 17:03:37 +00:00
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
2015-11-30 17:03:37 +00:00
}
2018-12-27 22:12:12 +00:00
//if strings.HasPrefix(fields[0], ".well-known/") {
// http.ServeFile(c.Writer, c.Request)
// return
//}
c.Set("ifconfig_hostname", configuration.hostname)
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"))
c.Set("country_code", c.Request.Header.Get("CF-IPCountry"))
2014-07-08 23:00:49 +00:00
ua := strings.Split(c.Request.UserAgent(), "/")
// Only lookup hostname if the results are going to need it.
2020-06-14 20:40:47 +00:00
// if stringInSlice(fields[0], []string{"all", "host"}) || (fields[0] == "" && ua[0] != "curl") {
if stringInSlice(fields[0], []string{"host"}) || (fields[0] == "" && ua[0] != "curl") {
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 {
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
2019-04-23 13:18:44 +00:00
case "headers":
c.JSON(200, c.Request.Header)
return
2014-07-08 23:00:49 +00:00
}
fieldResult, exists := c.Get(fields[0])
if !exists {
2014-07-08 23:00:49 +00:00
c.String(404, "Not Found")
return
2014-07-08 23:00:49 +00:00
}
c.String(200, fmt.Sprintln(fieldResult))
}
2020-07-24 19:00:35 +00:00
func getEnvWithDefault(key string, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
2014-07-08 23:00:49 +00:00
}
2020-07-24 19:00:35 +00:00
return value
2014-07-08 23:00:49 +00:00
}
func main() {
r := gin.New()
r.Use(gin.Recovery())
r.LoadHTMLGlob("templates/*")
2014-07-08 23:00:49 +00:00
2020-06-14 20:40:47 +00:00
for _, route := range []string{
"ip", "ua", "port", "lang", "encoding", "method",
2019-06-07 21:20:18 +00:00
"mime", "referer", "forwarded", "country_code",
"all", "headers", "porttest",
2020-06-14 20:40:47 +00:00
} {
r.GET(fmt.Sprintf("/%s", route), mainHandler)
2019-06-07 21:20:18 +00:00
r.GET(fmt.Sprintf("/%s.json", route), mainHandler)
}
2014-07-08 23:00:49 +00:00
r.GET("/", mainHandler)
errc := make(chan error)
go func(errc chan error) {
for err := range errc {
panic(err)
}
}(errc)
2019-06-07 21:20:18 +00:00
go func(errc chan error) {
errc <- r.Run(fmt.Sprintf("%s:%s", configuration.host, configuration.port))
2019-06-07 21:20:18 +00:00
}(errc)
if configuration.tls {
go func(errc chan error) {
errc <- r.RunTLS(
fmt.Sprintf("%s:%s", configuration.host, configuration.tlsport),
configuration.tlscert, configuration.tlskey)
}(errc)
}
2019-06-07 21:20:18 +00:00
2020-06-14 20:40:47 +00:00
fmt.Println(<-errc)
2014-07-08 23:00:49 +00:00
}