From ab8f90431be7174e95251b53662afb8cc1732fdc Mon Sep 17 00:00:00 2001 From: Thatcher Date: Mon, 14 Dec 2020 19:02:35 +0100 Subject: [PATCH] New template (#121) --- .dockerignore | 2 + Dockerfile | 8 +- Makefile | 3 + README.md | 2 +- cmd/echoip/main.go | 7 +- html/index.html | 332 ++++++++++++++++++++++++++++++++++++++++ html/leafcloud-logo.svg | 13 ++ html/script.html | 89 +++++++++++ html/styles.html | 185 ++++++++++++++++++++++ http/http.go | 6 +- index.html | 197 ------------------------ 11 files changed, 640 insertions(+), 204 deletions(-) create mode 100644 .dockerignore create mode 100644 html/index.html create mode 100644 html/leafcloud-logo.svg create mode 100644 html/script.html create mode 100644 html/styles.html delete mode 100644 index.html diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..30ecf57 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +Dockerfile +Dockerfile.geoip \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3c2e089..e6f18a2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,9 +9,9 @@ RUN make # Run FROM scratch EXPOSE 8080 -COPY --from=build \ - /go/bin/echoip \ - /go/src/github.com/mpolden/echoip/index.html \ - /opt/echoip/ + +COPY --from=build /go/bin/echoip /opt/echoip/ +COPY html /opt/echoip/html + WORKDIR /opt/echoip ENTRYPOINT ["/opt/echoip/echoip"] diff --git a/Makefile b/Makefile index 8f6eff6..d51a752 100644 --- a/Makefile +++ b/Makefile @@ -69,3 +69,6 @@ ifndef DEST_PATH endif rsync -a $(GOPATH)/bin/$(XBIN) $(DEST_PATH)/$(XBIN) @sha256sum $(GOPATH)/bin/$(XBIN) + +run: + go run cmd/echoip/main.go -a data/asn.mmdb -c data/city.mmdb -f data/country.mmdb -H x-forwarded-for -r -s diff --git a/README.md b/README.md index a74b6e4..1284fcb 100644 --- a/README.md +++ b/README.md @@ -127,5 +127,5 @@ Usage of echoip: -p Enable port lookup -r Perform reverse hostname lookups -t string - Path to template (default "index.html") + Path to template directory (default "html") ``` diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go index ecd1e7a..737a4f4 100644 --- a/cmd/echoip/main.go +++ b/cmd/echoip/main.go @@ -41,9 +41,10 @@ func main() { listen := flag.String("l", ":8080", "Listening address") reverseLookup := flag.Bool("r", false, "Perform reverse hostname lookups") portLookup := flag.Bool("p", false, "Enable port lookup") - template := flag.String("t", "index.html", "Path to template") + template := flag.String("t", "html", "Path to template dir") cacheSize := flag.Int("C", 0, "Size of response cache. Set to 0 to disable") profile := flag.Bool("P", false, "Enables profiling handlers") + sponsor := flag.Bool("s", false, "Show sponsor logo") var headers multiValueFlag flag.Var(&headers, "H", "Header to trust for remote IP, if present (e.g. X-Real-IP)") flag.Parse() @@ -72,6 +73,10 @@ func main() { log.Println("Enabling port lookup") server.LookupPort = iputil.LookupPort } + if *sponsor { + log.Println("Enabling sponsor logo") + server.Sponsor = *sponsor + } if len(headers) > 0 { log.Printf("Trusting remote IP from header(s): %s", headers.String()) } diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..2dc79b7 --- /dev/null +++ b/html/index.html @@ -0,0 +1,332 @@ + + + + + What is my IP address? — {{ .Host }} + + + + + + + + + {{ template "script.html" . }} {{ template "styles.html" . }} + + +
+
+
+
+

{{ .Host }} — What is my IP address?

+

{{ .IP }}

+

+ The best tool to find your own IP address, and information about + it. +

+
+
+
+ {{ if .Sponsor }} + + {{ end }} +
+
+ +
+ +
+
+

What do we know about this IP Address?

+ + + + + + + + + + {{ if .Country }} + + + + + {{ end }} {{ if .CountryISO }} + + + + + {{ end }} {{ if .CountryEU }} + + + + + {{ end }} {{ if .RegionName }} + + + + + {{ end }} {{ if .RegionCode }} + + + + + {{ end }} {{ if .MetroCode }} + + + + + {{ end }} {{ if .PostalCode }} + + + + + {{ end }} {{ if .City }} + + + + + {{ end }} {{ if .Latitude }} + + + + + {{ end }} {{ if .Longitude }} + + + + + {{ end }} {{ if .Timezone }} + + + + + {{ end }} {{ if .ASN }} + + + + + {{ end }} {{ if .ASNOrg }} + + + + + {{ end }} {{ if .Hostname }} + + + + + {{ end }} {{ if .UserAgent }} {{ if .UserAgent.Comment }} + + + + + {{ end }} {{ if .UserAgent.Comment }} + + + + + {{ end }} {{ if .UserAgent.RawValue }} + + + + + {{ end }} {{ end }} +
IP Address{{ .IP }}
IP Decimal{{ .IPDecimal }}
Country{{ .Country }}
CountryISO{{ .CountryISO }}
CountryEU{{ .CountryEU }}
RegionName{{ .RegionName }}
RegionCode{{ .RegionCode }}
MetroCode{{ .MetroCode }}
PostalCode{{ .PostalCode }}
City{{ .City }}
Latitude{{ .Latitude }}
Longitude{{ .Longitude }}
Timezone{{ .Timezone }}
ASN{{ .ASN }}
ASNOrg{{ .ASNOrg }}
Hostname{{ .Hostname }}
User Agent{{ .UserAgent.Product }}/{{ .UserAgent.Version }}
Comment{{ .UserAgent.Comment }}
Raw Value{{ .UserAgent.RawValue }}
+ {{ if .Country }} +

+ This information is provided from the GeoLite2 database created by + MaxMind, available from + www.maxmind.com +

+ {{ end }} {{ if .Latitude }} +
+

Map

+ +
+ {{ end }} +
+
+ +
+
+

How do I get this programmatically?

+

+ With the widget below you can build your query, and see what the + result will look like. +

+
+ +
+ + + + + + + + +
+
+ +
+
+ +
+
+ + +
+
+
+ + +
+

FAQ

+

How do I force IPv4 or IPv6 lookup?

+

+ As of 2018-07-25 it's no longer possible to force protocol using + the + v4 and v6 subdomains. IPv4 or IPv6 still can be + forced by passing the appropiate flag to your client, e.g + curl -4 or curl -6. +

+

Can I force getting JSON?

+

+ Setting the Accept: application/json header works + as expected. +

+ +

Is automated use of this service permitted?

+

+ Yes, as long as the rate limit is respected. The rate limit is + in place to ensure a fair service for all. +

+

+ Please limit automated requests to 1 request per minute. No guarantee is made for requests that exceed this limit. + They may be rate-limited, with a 429 status code, or dropped + entirely. +

+ +

Can I run my own service?

+

+ Yes, the source code and documentation is available on + GitHub. +

+
+
+
+
+
+ + diff --git a/html/leafcloud-logo.svg b/html/leafcloud-logo.svg new file mode 100644 index 0000000..9e9f4a7 --- /dev/null +++ b/html/leafcloud-logo.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/html/script.html b/html/script.html new file mode 100644 index 0000000..1e4b7f8 --- /dev/null +++ b/html/script.html @@ -0,0 +1,89 @@ + diff --git a/html/styles.html b/html/styles.html new file mode 100644 index 0000000..c2e0b4b --- /dev/null +++ b/html/styles.html @@ -0,0 +1,185 @@ + diff --git a/http/http.go b/http/http.go index d7ed8a0..9a56953 100644 --- a/http/http.go +++ b/http/http.go @@ -34,6 +34,7 @@ type Server struct { cache *Cache gr geo.Reader profile bool + Sponsor bool } type Response struct { @@ -322,7 +323,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro if err != nil { return badRequest(err).WithMessage(err.Error()) } - t, err := template.ParseFiles(s.Template) + t, err := template.ParseGlob(s.Template + "/*") if err != nil { return internalServerError(err) } @@ -330,6 +331,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro if err != nil { return internalServerError(err) } + var data = struct { Response Host string @@ -339,6 +341,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro BoxLonRight float64 JSON string Port bool + Sponsor bool }{ response, r.Host, @@ -348,6 +351,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro response.Longitude + 0.05, string(json), s.LookupPort != nil, + s.Sponsor, } if err := t.Execute(w, &data); err != nil { return internalServerError(err) diff --git a/index.html b/index.html deleted file mode 100644 index 8256fcd..0000000 --- a/index.html +++ /dev/null @@ -1,197 +0,0 @@ - - - - - What is my IP address? — {{ .Host }} - - - - - - - - -
-
-

What is my IP address?

-

{{ .IP }}

-

Multiple command line HTTP clients are supported, - including curl, httpie, httpie-go, GNU - Wget, fetch, and bat.

-

All endpoints, with the exception of /port, can return information about a custom IP address specified via ?ip= query parameter.

-
-
-
-
-

CLI examples

-
-$ curl {{ .Host }}
-{{ .IP }}
-
-$ http -b {{ .Host }}
-{{ .IP }}
-
-$ ht -b {{ .Host }}
-{{ .IP }}
-
-$ wget -qO- {{ .Host }}
-{{ .IP }}
-
-$ fetch -qo- http://{{ .Host }}
-{{ .IP }}
-
-$ bat -print=b {{ .Host }}/ip
-{{ .IP }}
-{{ if .Country }} -

Country lookup

-
-$ http {{ .Host }}/country
-{{ .Country }}
-
-$ http {{ .Host }}/country-iso
-{{ .CountryISO }}
-{{ end }} -{{ if .City }} -

City lookup

-
-$ http {{ .Host }}/city
-{{ .City }}
-{{ end }} -{{ if .ASN }} -

ASN lookup

-
-$ http {{ .Host }}/asn
-{{ .ASN }}
-{{ if .ASNOrg }}
-{{ end }} -{{ end }} -
-
-

JSON output

-
-$ http {{ .Host }}/json
-{{ .JSON }}
-

Setting the Accept: application/json header also works as expected.

-

Plain output

-

Always returns the IP address including a trailing newline, regardless of user agent.

-
-$ http {{ .Host }}/ip
-{{ .IP }}
-{{ if .Port }} -

Port testing

-
-$ http {{ .Host }}/port/8080
-{
-  "ip": "{{ .IP }}",
-  "port": 8080,
-  "reachable": false
-}
-{{ end }} -
-{{ if .City }} -
-

Map

- -
-{{ end }} -
-

FAQ

-

How do I force IPv4 or IPv6 lookup?

-

As of 2018-07-25 it's no longer possible to force protocol using - the v4 and v6 subdomains. IPv4 or IPv6 still can be forced - by passing the appropiate flag to your client, e.g curl -4 - or curl -6.

- -

Is automated use of this service permitted?

-

- Yes, as long as the rate limit is respected. The rate limit is in - place to ensure a fair service for all. -

-

- Please limit automated requests to 1 request per minute. No - guarantee is made for requests that exceed this limit. They may be - rate-limited, with a 429 status code, or dropped entirely. -

- -

Can I run my own service?

-

Yes, the source code and documentation is available on GitHub.

-
-
- - {{ if or .Country .City .ASN .ASNOrg }} -
- -
- {{ end }} - -