2014-07-08 23:00:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-05-16 00:33:27 +00:00
|
|
|
"os"
|
2015-11-30 17:03:37 +00:00
|
|
|
"strconv"
|
2014-07-08 23:00:49 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-05 15:29:56 +00:00
|
|
|
|
2022-01-05 17:23:33 +00:00
|
|
|
json "github.com/json-iterator/go"
|
|
|
|
|
2022-01-05 15:29:56 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
proxyproto "github.com/pires/go-proxyproto"
|
2014-07-08 23:00:49 +00:00
|
|
|
)
|
|
|
|
|
2020-10-05 13:34:22 +00:00
|
|
|
type Configuration struct {
|
2021-04-05 16:26:13 +00:00
|
|
|
hostname string // Displayed Hostname
|
|
|
|
host string // Listened Host
|
|
|
|
port string // HTTP Port
|
|
|
|
proxy_listener string // Proxy Protocol Listener
|
|
|
|
ipheader string // Header to overwrite the remote IP
|
2023-01-31 01:09:51 +00:00
|
|
|
countryheader string // Header to find country code associated to remote IP
|
2021-04-05 16:26:13 +00:00
|
|
|
tls bool // TLS enabled
|
|
|
|
tlscert string // TLS Cert Path
|
|
|
|
tlskey string // TLS Cert Key Path
|
|
|
|
tlsport string // HTTPS Port
|
2020-10-05 13:34:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var configuration = Configuration{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
hostname := getEnvWithDefault("HOSTNAME", "ifconfig.io")
|
|
|
|
|
|
|
|
host := getEnvWithDefault("HOST", "")
|
|
|
|
port := getEnvWithDefault("PORT", "8080")
|
2021-04-05 16:26:13 +00:00
|
|
|
proxy_listener := getEnvWithDefault("PROXY_PROTOCOL_ADDR", "")
|
2020-10-05 13:34:22 +00:00
|
|
|
|
2021-03-29 12:29:19 +00:00
|
|
|
// Most common alternative would be X-Forwarded-For
|
|
|
|
ipheader := getEnvWithDefault("FORWARD_IP_HEADER", "CF-Connecting-IP")
|
|
|
|
|
2023-01-31 01:09:51 +00:00
|
|
|
countryheader := getEnvWithDefault("COUNTRY_CODE_HEADER", "CF-IPCountry")
|
|
|
|
|
2020-10-05 13:34:22 +00:00
|
|
|
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{
|
2021-04-05 16:26:13 +00:00
|
|
|
hostname: hostname,
|
|
|
|
host: host,
|
|
|
|
port: port,
|
|
|
|
proxy_listener: proxy_listener,
|
|
|
|
ipheader: ipheader,
|
2023-01-31 01:09:51 +00:00
|
|
|
countryheader: countryheader,
|
2021-04-05 16:26:13 +00:00
|
|
|
tls: tlsenabled == "1",
|
|
|
|
tlscert: tlscert,
|
|
|
|
tlskey: tlskey,
|
|
|
|
tlsport: tlsport,
|
2020-10-05 13:34:22 +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 {
|
2015-11-30 17:03:37 +00:00
|
|
|
_, err := net.DialTimeout("tcp", address, 3*time.Second)
|
2015-11-16 21:16:06 +00:00
|
|
|
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 {
|
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
|
|
|
|
2021-04-10 13:36:08 +00:00
|
|
|
header_ip := net.ParseIP(strings.Split(c.Request.Header.Get(configuration.ipheader), ",")[0])
|
2021-03-29 12:29:19 +00:00
|
|
|
if header_ip != nil {
|
|
|
|
ip.IP = header_ip
|
2015-03-02 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 17:03:37 +00:00
|
|
|
if fields[0] == "porttest" {
|
2015-11-16 21:16:06 +00:00
|
|
|
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])))
|
2015-11-16 21:16:06 +00:00
|
|
|
} else {
|
|
|
|
c.String(400, "Invalid Port Number")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.String(400, "Need Port")
|
|
|
|
}
|
|
|
|
return
|
2015-11-30 17:03:37 +00:00
|
|
|
}
|
2015-11-16 21:16:06 +00:00
|
|
|
|
2018-12-27 22:12:12 +00:00
|
|
|
//if strings.HasPrefix(fields[0], ".well-known/") {
|
|
|
|
// http.ServeFile(c.Writer, c.Request)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
|
2020-10-05 13:34:22 +00:00
|
|
|
c.Set("ifconfig_hostname", configuration.hostname)
|
2020-10-05 11:36:27 +00:00
|
|
|
|
2021-06-24 09:25:10 +00:00
|
|
|
ua := c.Request.UserAgent()
|
|
|
|
|
2014-07-08 23:00:49 +00:00
|
|
|
c.Set("ip", ip.IP.String())
|
|
|
|
c.Set("port", ip.Port)
|
2021-06-24 09:25:10 +00:00
|
|
|
c.Set("ua", ua)
|
2014-08-10 19:02:21 +00:00
|
|
|
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"))
|
2023-01-31 01:09:51 +00:00
|
|
|
c.Set("country_code", c.Request.Header.Get(configuration.countryheader))
|
2022-01-05 15:46:22 +00:00
|
|
|
c.Set("host", ip.IP.String())
|
2014-07-08 23:00:49 +00:00
|
|
|
|
2014-07-10 18:34:34 +00:00
|
|
|
// 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") {
|
2022-01-05 15:46:22 +00:00
|
|
|
if fields[0] == "host" || (fields[0] == "" && !isReqFromCmdLine(ua)) {
|
2014-07-10 18:34:34 +00:00
|
|
|
hostnames, err := net.LookupAddr(ip.IP.String())
|
2022-01-05 15:46:22 +00:00
|
|
|
if err == nil {
|
2014-07-10 18:34:34 +00:00
|
|
|
c.Set("host", hostnames[0])
|
|
|
|
}
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 09:25:10 +00:00
|
|
|
wantsJSON := len(fields) >= 2 && fields[1] == "json"
|
2022-01-04 14:53:48 +00:00
|
|
|
wantsJS := len(fields) >= 2 && fields[1] == "js"
|
2014-07-08 23:00:49 +00:00
|
|
|
|
|
|
|
switch fields[0] {
|
|
|
|
case "":
|
2021-06-24 09:25:10 +00:00
|
|
|
// If the user is using a command line agent like curl/HTTPie,
|
|
|
|
// then we should just return the IP, else we show the home page.
|
|
|
|
if isReqFromCmdLine(ua) {
|
2014-07-08 23:00:49 +00:00
|
|
|
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)
|
2022-01-04 14:53:48 +00:00
|
|
|
} else if wantsJS {
|
|
|
|
c.Writer.Header().Set("Content-Type", "application/javascript")
|
|
|
|
response, _ := json.Marshal(c.Keys)
|
|
|
|
c.String(200, "ifconfig_io = %v\n", string(response))
|
2014-07-08 23:00:49 +00:00
|
|
|
} else {
|
2022-01-05 17:23:33 +00:00
|
|
|
c.Writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
c.YAML(200, c.Keys)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
|
|
|
return
|
2019-04-23 13:18:44 +00:00
|
|
|
case "headers":
|
2022-01-04 22:07:48 +00:00
|
|
|
if wantsJS {
|
|
|
|
c.Writer.Header().Set("Content-Type", "application/javascript")
|
|
|
|
response, _ := json.Marshal(c.Request.Header)
|
|
|
|
c.String(200, "ifconfig_io = %v\n", string(response))
|
|
|
|
} else {
|
|
|
|
c.JSON(200, c.Request.Header)
|
|
|
|
}
|
2019-04-23 13:18:44 +00:00
|
|
|
return
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-11-28 03:33:53 +00:00
|
|
|
if wantsJSON {
|
|
|
|
c.JSON(200, fieldResult)
|
2022-01-04 21:16:53 +00:00
|
|
|
} else if wantsJS {
|
2022-01-04 22:07:48 +00:00
|
|
|
c.Writer.Header().Set("Content-Type", "application/javascript")
|
2022-01-05 15:46:52 +00:00
|
|
|
response, _ := json.Marshal(fieldResult)
|
|
|
|
c.String(200, "%v = %v\n", fields[0], string(response))
|
2022-01-04 22:07:48 +00:00
|
|
|
} else {
|
2020-11-28 03:33:53 +00:00
|
|
|
c.String(200, fmt.Sprintln(fieldResult))
|
|
|
|
}
|
2014-07-08 23:00:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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())
|
2015-10-26 20:30:36 +00:00
|
|
|
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",
|
2022-01-05 15:46:22 +00:00
|
|
|
"all", "headers", "porttest", "host",
|
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)
|
2022-01-04 21:16:53 +00:00
|
|
|
r.GET(fmt.Sprintf("/%s.js", route), mainHandler)
|
2019-06-07 21:20:18 +00:00
|
|
|
}
|
2014-07-08 23:00:49 +00:00
|
|
|
r.GET("/", mainHandler)
|
|
|
|
|
2015-05-16 00:33:27 +00:00
|
|
|
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) {
|
2020-10-05 13:34:22 +00:00
|
|
|
errc <- r.Run(fmt.Sprintf("%s:%s", configuration.host, configuration.port))
|
2019-06-07 21:20:18 +00:00
|
|
|
}(errc)
|
|
|
|
|
2020-10-05 13:34:22 +00:00
|
|
|
if configuration.tls {
|
2020-10-05 11:34:48 +00:00
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- r.RunTLS(
|
2021-03-29 12:29:19 +00:00
|
|
|
fmt.Sprintf("%s:%s", configuration.host, configuration.tlsport),
|
|
|
|
configuration.tlscert, configuration.tlskey)
|
2020-10-05 11:34:48 +00:00
|
|
|
}(errc)
|
|
|
|
}
|
2019-06-07 21:20:18 +00:00
|
|
|
|
2021-04-05 16:26:13 +00:00
|
|
|
if configuration.proxy_listener != "" {
|
|
|
|
go func(errc chan error) {
|
|
|
|
list, err := net.Listen("tcp", configuration.proxy_listener)
|
|
|
|
if err != nil {
|
|
|
|
errc <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proxyListener := &proxyproto.Listener{Listener: list}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
errc <- r.RunListener(proxyListener)
|
|
|
|
}(errc)
|
|
|
|
}
|
|
|
|
|
2020-06-14 20:40:47 +00:00
|
|
|
fmt.Println(<-errc)
|
2014-07-08 23:00:49 +00:00
|
|
|
}
|
2021-06-24 09:25:10 +00:00
|
|
|
|
|
|
|
func isReqFromCmdLine(ua string) bool {
|
2022-06-15 16:34:33 +00:00
|
|
|
|
|
|
|
// Example User Agents
|
|
|
|
// curl/7.83.1
|
|
|
|
// Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.19044; en-US) PowerShell/7.2.4
|
|
|
|
|
|
|
|
// In the case of powershell, we have to look at only the last segment.
|
|
|
|
// We could fully parse the user agent, but that would create a lot of garbage.
|
|
|
|
// We simply look at the last word.
|
|
|
|
// A micro optimization would be to do the search in reverse and break on first match, but
|
|
|
|
// I find that harder to read.
|
|
|
|
lastSpaceIndex := 0
|
|
|
|
for i, c := range ua {
|
|
|
|
// Protect if the space is the very last symbol.
|
|
|
|
if i == len(ua)-1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if string(c) == " " {
|
|
|
|
lastSpaceIndex = i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ua = ua[lastSpaceIndex:]
|
|
|
|
|
2022-01-05 15:29:56 +00:00
|
|
|
parts := strings.SplitN(ua, "/", 2)
|
|
|
|
switch parts[0] {
|
2022-06-15 15:56:59 +00:00
|
|
|
case "curl", "HTTPie", "httpie-go", "Wget", "fetch libfetch", "Go", "Go-http-client", "ddclient", "Mikrotik", "xh", "WindowsPowerShell", "PowerShell":
|
2022-01-05 15:29:56 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2022-01-04 14:53:48 +00:00
|
|
|
}
|