mirror of https://github.com/mpolden/echoip
Only display enabled features on web page
This commit is contained in:
parent
100131e420
commit
95b5b76ef7
14
api/api.go
14
api/api.go
|
@ -29,6 +29,9 @@ type API struct {
|
||||||
lookupCountry func(net.IP) (string, error)
|
lookupCountry func(net.IP) (string, error)
|
||||||
testPort func(net.IP, uint64) error
|
testPort func(net.IP, uint64) error
|
||||||
ipFromRequest func(*http.Request) (net.IP, error)
|
ipFromRequest func(*http.Request) (net.IP, error)
|
||||||
|
reverseLookup bool
|
||||||
|
countryLookup bool
|
||||||
|
portTesting bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
@ -60,15 +63,18 @@ func (a *API) EnableCountryLookup(filepath string) error {
|
||||||
a.lookupCountry = func(ip net.IP) (string, error) {
|
a.lookupCountry = func(ip net.IP) (string, error) {
|
||||||
return lookupCountry(db, ip)
|
return lookupCountry(db, ip)
|
||||||
}
|
}
|
||||||
|
a.countryLookup = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) EnableReverseLookup() {
|
func (a *API) EnableReverseLookup() {
|
||||||
a.lookupAddr = net.LookupAddr
|
a.lookupAddr = net.LookupAddr
|
||||||
|
a.reverseLookup = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) EnablePortTesting() {
|
func (a *API) EnablePortTesting() {
|
||||||
a.testPort = testPort
|
a.testPort = testPort
|
||||||
|
a.portTesting = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func ipFromRequest(r *http.Request) (net.IP, error) {
|
func ipFromRequest(r *http.Request) (net.IP, error) {
|
||||||
|
@ -199,7 +205,13 @@ func (a *API) DefaultHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return internalServerError(err)
|
return internalServerError(err)
|
||||||
}
|
}
|
||||||
if err := t.Execute(w, &response); err != nil {
|
var data = struct {
|
||||||
|
Response
|
||||||
|
ReverseLookup bool
|
||||||
|
CountryLookup bool
|
||||||
|
PortTesting bool
|
||||||
|
}{response, a.reverseLookup, a.countryLookup, a.portTesting}
|
||||||
|
if err := t.Execute(w, &data); err != nil {
|
||||||
return internalServerError(err)
|
return internalServerError(err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -58,11 +58,13 @@ $ wget -qO- ifconfig.co
|
||||||
$ fetch -qo- http://ifconfig.co
|
$ fetch -qo- http://ifconfig.co
|
||||||
{{ .IP }}
|
{{ .IP }}
|
||||||
</pre>
|
</pre>
|
||||||
|
{{ if .CountryLookup }}
|
||||||
<p>Country lookup:</p>
|
<p>Country lookup:</p>
|
||||||
<pre>
|
<pre>
|
||||||
$ http ifconfig.co/country
|
$ http ifconfig.co/country
|
||||||
{{ .Country }}
|
{{ .Country }}
|
||||||
</pre>
|
</pre>
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
<div class="pure-u-1-2">
|
<div class="pure-u-1-2">
|
||||||
<p>JSON output:</p>
|
<p>JSON output:</p>
|
||||||
|
@ -73,11 +75,13 @@ Content-Length: 61
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
Date: Fri, 15 Apr 2016 17:26:53 GMT
|
Date: Fri, 15 Apr 2016 17:26:53 GMT
|
||||||
|
|
||||||
{
|
{ {{ if .CountryLookup }}
|
||||||
"country": "{{ .Country }}",
|
"country": "{{ .Country }}",{{ end }}{{ if .ReverseLookup }}
|
||||||
|
"hostname": "{{ .Hostname }}",{{ end }}
|
||||||
"ip": "{{ .IP }}"
|
"ip": "{{ .IP }}"
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
{{ if .PortTesting }}
|
||||||
<p>Testing port connectivity (only supports JSON output):</p>
|
<p>Testing port connectivity (only supports JSON output):</p>
|
||||||
<pre>
|
<pre>
|
||||||
http --json localhost:8080/port/8080
|
http --json localhost:8080/port/8080
|
||||||
|
@ -92,6 +96,7 @@ Date: Fri, 15 Apr 2016 18:47:20 GMT
|
||||||
"reachable": true
|
"reachable": true
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="pure-g">
|
<div class="pure-g">
|
||||||
|
|
Loading…
Reference in New Issue