Skip to content

Commit 872424a

Browse files
committed
Disable custom IP lookup
This adds a flag, `-L` which will disable the ability to do custom IP lookups. When `-L` is set we: - Remove the interactive text input and form elements - Remove the button(s) related to lookup - Don't process incoming `?ip=` - Remove some help text related to the functionality
1 parent 746db5e commit 872424a

4 files changed

Lines changed: 20 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Usage of echoip:
142142
Path to GeoIP country database
143143
-l string
144144
Listening address (default ":8080")
145+
-L Disable custom IP lookup
145146
-p Enable port lookup
146147
-r Perform reverse hostname lookups
147148
-s Show sponsor logo

cmd/echoip/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func main() {
3535
listen := flag.String("l", ":8080", "Listening address")
3636
reverseLookup := flag.Bool("r", false, "Perform reverse hostname lookups")
3737
portLookup := flag.Bool("p", false, "Enable port lookup")
38+
noCustomIP := flag.Bool("L", false, "Disable custom IP lookup")
3839
template := flag.String("t", "html", "Path to template dir")
3940
cacheSize := flag.Int("C", 0, "Size of response cache. Set to 0 to disable")
4041
profile := flag.Bool("P", false, "Enables profiling handlers")
@@ -71,6 +72,10 @@ func main() {
7172
log.Println("Enabling sponsor logo")
7273
server.Sponsor = *sponsor
7374
}
75+
if *noCustomIP {
76+
log.Println("Disabling custom IP lookup")
77+
server.NoCustomIP = true
78+
}
7479
if len(headers) > 0 {
7580
log.Printf("Trusting remote IP from header(s): %s", headers.String())
7681
}

html/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
</div>
3232
{{ end }}
3333
<h1 class="site-title">❯ curl {{ .Host }}</h1>
34+
{{ if .NoCustomIP }}
35+
<div class="ip-form">
36+
<input class="ip-input" type="text" value="{{ .IP }}" readonly>
37+
</div>
38+
{{ else }}
3439
<form class="ip-form" action="/" method="GET">
3540
<input class="ip-input" type="text" name="ip" value="{{ .IP }}">
3641
<div class="ip-buttons">
@@ -40,6 +45,7 @@ <h1 class="site-title">❯ curl {{ .Host }}</h1>
4045
{{ end }}
4146
</div>
4247
</form>
48+
{{ end }}
4349
<p class="ip-meta">
4450
{{- if .City }}{{ .City }}{{ if .Country }}, {{ end }}{{ end -}}
4551
{{- .Country -}}
@@ -222,7 +228,9 @@ <h3>Plain text</h3>
222228
<div class="note">
223229
<ul>
224230
<li>Set the <code>Accept: application/json</code> header to request response as JSON.</li>
231+
{{ if not .NoCustomIP }}
225232
<li>Append <code>?ip=IP</code> to the request URL to lookup information for a different IP. This is not supported for the <code>/port</code> endpoint.</li>
233+
{{ end }}
226234
</ul>
227235
</div>
228236
<h3>JSON</h3>

http/http.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Server struct {
3535
gr geo.Reader
3636
profile bool
3737
Sponsor bool
38+
NoCustomIP bool
3839
}
3940

4041
type Response struct {
@@ -129,7 +130,7 @@ func userAgentFromRequest(r *http.Request) *useragent.UserAgent {
129130
}
130131

131132
func (s *Server) newResponse(r *http.Request) (Response, error) {
132-
ip, err := ipFromRequest(s.IPHeaders, r, true)
133+
ip, err := ipFromRequest(s.IPHeaders, r, !s.NoCustomIP)
133134
if err != nil {
134135
return Response{}, err
135136
}
@@ -193,7 +194,7 @@ func (s *Server) newPortResponse(r *http.Request) (PortResponse, error) {
193194
}
194195

195196
func (s *Server) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
196-
ip, err := ipFromRequest(s.IPHeaders, r, true)
197+
ip, err := ipFromRequest(s.IPHeaders, r, !s.NoCustomIP)
197198
if err != nil {
198199
return badRequest(err).WithMessage(err.Error()).AsJSON()
199200
}
@@ -364,6 +365,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro
364365
Port bool
365366
Sponsor bool
366367
ExplicitLookup bool
368+
NoCustomIP bool
367369
}{
368370
response,
369371
r.Host,
@@ -374,7 +376,8 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro
374376
string(json),
375377
s.LookupPort != nil,
376378
s.Sponsor,
377-
r.URL.Query().Has("ip"),
379+
!s.NoCustomIP && r.URL.Query().Has("ip"),
380+
s.NoCustomIP,
378381
}
379382
if err := t.Execute(w, &data); err != nil {
380383
return internalServerError(err)

0 commit comments

Comments
 (0)