-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (76 loc) · 1.91 KB
/
main.go
File metadata and controls
82 lines (76 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var (
httpOn bool
port int
data []string
resp Response
err error
)
type Response struct {
Code uint8 `json:"code"`
Msg string `json:"msg"`
Data struct {
ClientIP string `json:"client_ip"`
ServerIP string `json:"server_ip"`
Hostname string `json:"hostname"`
} `json:"data"`
}
func init() {
flag.BoolVar(&httpOn, "http", false, "Start HTTP Server.")
flag.IntVar(&port, "p", 80, "The listening port.")
flag.Parse()
}
func main() {
if resp.Data.Hostname, err = os.Hostname(); err != nil {
log.Printf("os.Hostname() error(%v)", err)
return
}
var ipResp *http.Response
if ipResp, err = http.Get("http://ifconfig.co/ip"); err != nil {
log.Printf("http.Get(ifconfig.co/ip) error(%v)", err)
return
}
var bs []byte
if bs, err = ioutil.ReadAll(ipResp.Body); err != nil {
log.Printf("ioutil.ReadAll() error(%v)", err)
return
}
resp.Data.ServerIP = strings.TrimSpace(string(bs))
data = []string{
fmt.Sprintf("ServerIP: %s", resp.Data.ServerIP),
fmt.Sprintf("Hostname: %s", resp.Data.Hostname),
}
if httpOn {
http.HandleFunc("/", handleRoot)
log.Fatalln(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
return
}
fmt.Println(strings.Join(data, "\n"))
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var bs []byte
if r.Form.Get("format") == "json" {
resp.Data.ClientIP = r.RemoteAddr
bs, _ = json.Marshal(&resp)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
} else {
clientIP := fmt.Sprintf("ClientIP: %s", r.RemoteAddr)
bs = []byte(strings.Join(append([]string{clientIP}, data...), "\n"))
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
}
if _, err = w.Write(bs); err != nil {
log.Printf("resp.Write() error(%v)", err)
return
}
}