-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·158 lines (135 loc) · 3.3 KB
/
main.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
)
func EnvHandler(rw http.ResponseWriter, req *http.Request) {
fmt.Printf("%s\n", "EnvHandler was called")
environment := make(map[string]string)
for _, item := range os.Environ() {
splits := strings.Split(item, "=")
key := splits[0]
val := strings.Join(splits[1:], "=")
environment[key] = val
}
//ex := os.Executable()
//environment["executable binary"] = ex
pid := strconv.Itoa(os.Getpid())
ppid := strconv.Itoa(os.Getppid())
environment["PID"] = pid
environment["PPID"] = ppid
ifaces, err := net.Interfaces()
if err != nil {
fmt.Printf("%s\n", err)
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
fmt.Printf("%s\n", err)
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
environment["IPNET"] = ip.String()
ipv4 := ip.To4()
if ipv4 != nil {
environment["IPNETv4"] = ipv4.String()
}
case *net.IPAddr:
ip = v.IP
environment["IPAddr"] = ip.String()
ipv4 := ip.To4()
if ipv4 != nil {
environment["IPAddrv4"] = ipv4.String()
}
}
}
}
envJSON := HandleError(json.MarshalIndent(environment, "", " ")).([]byte)
rw.Write(envJSON)
}
func HandleError(result interface{}, err error) (r interface{}) {
if err != nil {
panic(err)
}
return result
}
type Result struct {
Status string
Response string
}
func PingCommands(rw http.ResponseWriter, req *http.Request) {
fmt.Printf("%s\n", "PingCommands was called")
var status string
ip := mux.Vars(req)["ip"]
cmd := exec.Command("ping", ip, "-c", "3")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
status = "Failure"
//ubuntu result vs mac result
} else if strings.Contains(out.String(), " 0% packet loss") ||
strings.Contains(out.String(), "0.0% packet loss") {
status = "Success"
} else {
status = "Failure"
}
result := Result{status, out.String()}
js, err := json.Marshal(result)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
fmt.Println("Result: " + out.String())
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func CurlCommands(rw http.ResponseWriter, req *http.Request) {
fmt.Printf("%s\n", "CurlCommands was called")
var status string
ip := mux.Vars(req)["ip"]
cmd := exec.Command("curl", "-I", ip)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
status = "Failure"
} else if strings.Contains(out.String(), "200 OK") {
status = "Success"
} else {
status = "Failure"
}
result := Result{status, out.String()}
js, err := json.Marshal(result)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("Result: " + out.String())
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func main() {
r := mux.NewRouter()
r.Path("/env").Methods("GET").HandlerFunc(EnvHandler)
r.Path("/ping/{ip}").Methods("GET").HandlerFunc(PingCommands)
r.Path("/curl/{ip}").Methods("GET").HandlerFunc(CurlCommands)
n := negroni.Classic()
n.UseHandler(r)
n.Run(":3000")
}