-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
145 lines (132 loc) · 3.29 KB
/
handlers.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
package httptoolbox
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"cloud.google.com/go/compute/metadata"
"golang.org/x/oauth2/google"
)
func Exec(w http.ResponseWriter, req *http.Request) {
type request struct {
File string
Args []string
}
var r request
err := json.NewDecoder(req.Body).Decode(&r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
cmd := exec.Command(r.File, r.Args...)
buf := new(bytes.Buffer)
cmd.Stdout = buf
err = cmd.Run()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
}
func Metadata(w http.ResponseWriter, r *http.Request) {
result, err := metadata.Get(r.URL.Query().Get("path"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, result)
}
func Email(w http.ResponseWriter, r *http.Request) {
email, err := metadata.Get("instance/service-accounts/default/email")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, email)
}
func TokenInfo(w http.ResponseWriter, r *http.Request) {
tokenSource, err := google.DefaultTokenSource(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
token, err := tokenSource.Token()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp, err := http.Get("https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=" + token.AccessToken)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
io.Copy(w, resp.Body)
}
func ExecDmesg(w http.ResponseWriter, _ *http.Request) {
cmd := exec.Command("/bin/dmesg")
buf := new(bytes.Buffer)
cmd.Stdout = buf
err := cmd.Run()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
}
func GetHeaders(w http.ResponseWriter, req *http.Request) {
buf := new(bytes.Buffer)
for k, v := range req.Header {
fmt.Fprintf(buf, "%s=%v\n", k, v)
}
w.Header().Set("content-type", "plain/text")
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
}
func GetEnvs(w http.ResponseWriter, req *http.Request) {
buf := new(bytes.Buffer)
for _, env := range os.Environ() {
fmt.Fprintln(buf, env)
}
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
}
func ReadContent(w http.ResponseWriter, req *http.Request) {
type request struct{ File string }
var r request
err := json.NewDecoder(req.Body).Decode(&r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
bytes, err := ioutil.ReadFile(r.File)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(bytes)
}
func DumpRequest(w http.ResponseWriter, req *http.Request) {
req.Write(w)
}
func WriteEnvs(w http.ResponseWriter, req *http.Request) {
for _, env := range os.Environ() {
fmt.Println(env)
}
w.WriteHeader(http.StatusOK)
}
func WriteHeaders(w http.ResponseWriter, req *http.Request) {
for k, v := range req.Header {
fmt.Printf("%s=%v\n", k, v)
}
w.WriteHeader(http.StatusOK)
}