mirror of
https://github.com/mpolden/echoip.git
synced 2025-01-27 18:35:04 +01:00
Add JSON output
This commit is contained in:
parent
0f33287ed9
commit
fabbf68ced
18
ifconfig.go
18
ifconfig.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
IP net.IP
|
IP net.IP
|
||||||
|
JSON string
|
||||||
Header http.Header
|
Header http.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +54,18 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
ip := parseRealIP(req)
|
ip := parseRealIP(req)
|
||||||
header := pathToKey(req.URL.Path)
|
header := pathToKey(req.URL.Path)
|
||||||
|
|
||||||
if isCli(req.UserAgent()) {
|
if isJson(req) {
|
||||||
|
if header == "all" {
|
||||||
|
b, _ := json.MarshalIndent(req.Header, "", " ")
|
||||||
|
io.WriteString(w, fmt.Sprintf("%s\n", b))
|
||||||
|
} else {
|
||||||
|
m := map[string]string{
|
||||||
|
header: req.Header.Get(header),
|
||||||
|
}
|
||||||
|
b, _ := json.MarshalIndent(m, "", " ")
|
||||||
|
io.WriteString(w, fmt.Sprintf("%s\n", b))
|
||||||
|
}
|
||||||
|
} else if isCli(req.UserAgent()) {
|
||||||
if header == "" || header == "ip" {
|
if header == "" || header == "ip" {
|
||||||
io.WriteString(w, fmt.Sprintf("%s\n", ip))
|
io.WriteString(w, fmt.Sprintf("%s\n", ip))
|
||||||
} else {
|
} else {
|
||||||
@ -67,7 +80,8 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
New("index.html").
|
New("index.html").
|
||||||
Funcs(funcMap).
|
Funcs(funcMap).
|
||||||
ParseFiles("index.html")
|
ParseFiles("index.html")
|
||||||
client := &Client{IP: ip, Header: req.Header}
|
b, _ := json.MarshalIndent(req.Header, "", " ")
|
||||||
|
client := &Client{IP: ip, JSON: string(b), Header: req.Header}
|
||||||
t.Execute(w, client)
|
t.Execute(w, client)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,8 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
<td><code><span class="command">curl</span> ifconfig/all.json</code></td>
|
||||||
|
<td class="response"><pre>{{ .JSON }}</pre></td>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user