Remove js

This commit is contained in:
Martin Polden 2014-12-22 16:46:36 +01:00
parent a038e34eff
commit 558ee4366f
4 changed files with 69 additions and 30 deletions

View File

@ -1,14 +0,0 @@
/*jslint es5: true, indent: 2, browser: true*/
(function () {
'use strict';
var onLoad = function (event) {
var select = document.querySelector('#select-command');
select.addEventListener('change', function () {
[].forEach.call(document.querySelectorAll('.command'), function (el) {
el.innerHTML = this.value;
}, this);
});
}
document.addEventListener('DOMContentLoaded', onLoad);
})();

View File

@ -10,6 +10,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strings"
@ -21,12 +22,22 @@ type Client struct {
IP net.IP
JSON string
Header http.Header
Cmd
}
type Cmd struct {
Name string
Args string
}
type Ifconfig struct {
DB *geoip2.Reader
}
func (c *Cmd) String() string {
return c.Name + " " + c.Args
}
func isCLI(userAgent string) bool {
return agentExp.MatchString(userAgent)
}
@ -91,6 +102,22 @@ func (i *Ifconfig) Plain(req *http.Request, key string, ip net.IP) string {
return fmt.Sprintf("%s\n", req.Header.Get(key))
}
func lookupCmd(values url.Values) Cmd {
cmd, exists := values["cmd"]
if !exists || len(cmd) == 0 {
return Cmd{Name: "curl"}
}
switch cmd[0] {
case "curl":
return Cmd{Name: "curl"}
case "fetch":
return Cmd{Name: "fetch", Args: "-qo -"}
case "wget":
return Cmd{Name: "wget", Args: "-qO -"}
}
return Cmd{Name: "curl"}
}
func (i *Ifconfig) handler(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(w, "Invalid request method", 405)
@ -98,6 +125,7 @@ func (i *Ifconfig) handler(w http.ResponseWriter, req *http.Request) {
}
ip := parseRealIP(req)
key := pathToKey(req.URL.Path)
cmd := lookupCmd(req.URL.Query())
country, err := i.LookupCountry(ip)
if err != nil {
log.Print(err)
@ -127,7 +155,12 @@ func (i *Ifconfig) handler(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Failed to marshal JSON", 500)
return
}
client := &Client{IP: ip, JSON: string(b), Header: req.Header}
client := &Client{
IP: ip,
JSON: string(b),
Header: req.Header,
Cmd: cmd,
}
t.Execute(w, client)
}
}
@ -158,10 +191,7 @@ func main() {
log.Fatal(err)
}
http.Handle("/assets/", http.StripPrefix("/assets/",
http.FileServer(http.Dir("assets/"))))
http.HandleFunc("/", i.handler)
log.Printf("Listening on %s", opts.Listen)
if err := http.ListenAndServe(opts.Listen, nil); err != nil {
log.Fatal("ListenAndServe: ", err)

View File

@ -1,6 +1,9 @@
package main
import "testing"
import (
"net/url"
"testing"
)
func TestIsCLI(t *testing.T) {
userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)",
@ -31,3 +34,26 @@ func TestPathToKey(t *testing.T) {
t.Fatalf("Expected 'all', got '%s'", key)
}
}
func TestLookupCmd(t *testing.T) {
values := url.Values{"cmd": []string{"curl"}}
if v := lookupCmd(values); v.Name != "curl" {
t.Fatalf("Expected 'curl', got '%s'", v)
}
values = url.Values{"cmd": []string{"foo"}}
if v := lookupCmd(values); v.Name != "curl" {
t.Fatalf("Expected 'curl', got '%s'", v)
}
values = url.Values{}
if v := lookupCmd(values); v.Name != "curl" {
t.Fatalf("Expected 'curl', got '%s'", v)
}
values = url.Values{"cmd": []string{"wget"}}
if v := lookupCmd(values); v.Name != "wget" {
t.Fatalf("Expected 'wget', got '%s'", v)
}
values = url.Values{"cmd": []string{"fetch"}}
if v := lookupCmd(values); v.Name != "fetch" {
t.Fatalf("Expected 'fetch', got '%s'", v)
}
}

View File

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="What is my IP address?">
<link href="//fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.3.0/pure-min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.5.0/pure-min.css">
<style>
body {
font-family: "Oswald", sans-serif;
@ -37,38 +37,35 @@
<h1>What is my IP address?</h1>
<h2>Your IP:</h2>
<p><code class="ip">{{ .IP }}</code></p>
<select id="select-command">
<option value="curl">curl</option>
<option value="wget -qO -">wget</option>
<option value="fetch -qo -">fetch</option>
</select>
<a href="?cmd=curl" class="pure-button{{ if eq .Cmd.Name "curl" }} pure-button-active pure-button-primary{{end}}">curl</a>
<a href="?cmd=wget" class="pure-button{{ if eq .Cmd.Name "wget" }} pure-button-active pure-button-primary{{end}}">wget</a>
<a href="?cmd=fetch" class="pure-button{{ if eq .Cmd.Name "fetch" }} pure-button-active pure-button-primary{{end}}">fetch</a>
</div>
<table class="pure-table pure-table-bordered pure-table-striped">
<thead>
<tr>
<th style="width: 160px">Command</th>
<th style="width: 350px">Command</th>
<th>Response</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><span class="command">curl</span> ifconfig.co</code></td>
<td><code><span class="command">{{ .Cmd.String }}</span> ifconfig.co</code></td>
<td class="response">{{ .IP }}</td>
</tr>
{{ if $self := . }}
{{ range $key, $value := .Header }}
<tr>
<td><code><span class="command">curl</span> ifconfig.co/{{ ToLower $key }}</code></td>
<td><code><span class="command">{{ $self.Cmd.String }}</span> ifconfig.co/{{ ToLower $key }}</code></td>
<td class="response">{{ index $self.Header $key 0 }}</td>
</tr>
{{end}}
{{end}}
<td><code><span class="command">curl</span> ifconfig.co/all.json</code></td>
<td><code><span class="command">{{ .Cmd.String }}</span> ifconfig.co/all.json</code></td>
<td><pre class="response">{{ .JSON }}</pre></td>
</tbody>
</table>
</div>
<a href="https://github.com/martinp/ifconfig"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<script src="/assets/js/app.js"></script>
</body>
</html>