-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyamp.go
343 lines (291 loc) · 8.49 KB
/
syamp.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"log"
"os"
"fmt"
"net/http"
"html/template"
"github.com/kampsy/Go/contype"
"strings"
"io/ioutil"
"encoding/json"
"time"
"github.com/syamp/ubusuma"
"github.com/syamp/random"
)
type WebPage struct {
Title string
First_Name string
Message string
Distributor string
Description string
Release string
Codename string
}
func main() {
http.HandleFunc("/home", home)
http.HandleFunc("/login", login)
http.HandleFunc("/logout", logout)
http.HandleFunc("/", static)
host := "localhost:2016"
if len(os.Args) > 1 {
host = strings.Join(os.Args[1:], " ")
}
fmt.Printf("https://%s\n", host)
err := http.ListenAndServeTLS(host, "cert/Kalibu-Tech.crt", "cert/Kalibu-Tech.key", nil)
if err != nil {
log.Printf("%v\n", err)
}
}
// this function reads files form the drive and returns a slice of bytes
func dirReader(f string) []byte {
cont, err := ioutil.ReadFile(f)
if err != nil {
log.Printf("err : %v\n", err)
panic("problem when reading")
}
return cont
}
// user account type
type UsrStr map[string]string
func rootUsr(cookie string) ([]string, error) {
jsn := dirReader("usr/root.json")
var rootJsn UsrStr
err := json.Unmarshal(jsn, &rootJsn)
if err != nil {
return nil, err
}
var usr_cookie []string
usr_cookie = append(usr_cookie, rootJsn["First_Name"])
return usr_cookie, nil
}
// Returns a string cookie
func rootCoo(cookie string) (string, error) {
jsn := dirReader("usr/root.json")
var rootJsn UsrStr
err := json.Unmarshal(jsn, &rootJsn)
if err != nil {
return random.RandStr(5), err // what are olds?
}
kie := fmt.Sprintf("%s", rootJsn["Cookie_Key"])
return kie, nil
}
// This reads Template html files, creates the page and then writes
// the writes to the response writer
func Build(w http.ResponseWriter, p WebPage, tmpFiles []string) {
tmp := template.New("template")
// header part of the WebPage
reVtop, err := ioutil.ReadFile("reVres/tmp/top.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var str_reVtop string = string(reVtop)
tmp.New("reVtop").Parse(str_reVtop)
// Window material part of the WebPage
reVmaterial, err := ioutil.ReadFile(fmt.Sprintf("reVres/tmp/%s.html", tmpFiles[0]))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var str_reVmaterial string = string(reVmaterial)
tmp.New("reVmaterial").Parse(str_reVmaterial)
// body part of the web page
reVbody, err := ioutil.ReadFile(fmt.Sprintf("reVres/tmp/%s.html", tmpFiles[1]))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var str_reVbody string = string(reVbody)
tmp.New("reVbody").Parse(str_reVbody)
// bottom part of the web page.
reVbot, err := ioutil.ReadFile("reVres/tmp/bottom.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var str_reVbot string = string(reVbot)
tmp.New("reVbot").Parse(str_reVbot)
tmp.Lookup("reVbody").Execute(w, p)
}
// The home page of syamp
func home(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s \n", r.Method, r.URL.Path)
cookie, err := r.Cookie("syamp")
if err != nil {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
// Checks for cookie injections
cook, err := rootCoo(cookie.Value)
if err != nil {
log.Fatal(err)
}
if cookie.Value != cook {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
// Use the contype to get the write media type from
// Url
cont := contype.FileType(r.URL.Path)
var page WebPage
page.Title = "Home"
// Add the name of root user to Webpage struct
yugi, err := rootUsr(cookie.Value)
if err != nil {
log.Fatal(err)
}
page.First_Name = yugi[0]
// Metal returns s reciver Channel of type []slice with value to add to
// the webpage struct
metalOut := ubusuma.Metal()
metalVal := <-metalOut
page.Distributor = metalVal[0]
page.Description = metalVal[1]
page.Release = metalVal[2]
page.Codename = metalVal[3]
switch r.Method {
case "GET":
query := r.FormValue("stdout")
if query == "std" {
// RunningUser returns s reciver Channel of type string.
running := ubusuma.RunningUser()
fmt.Fprintf(w, <-running)
return
}
// RunningUser returns s reciver Channel of type string.
pid := r.FormValue("term")
if pid != "" {
ter_msg := ubusuma.Kill(pid)
fmt.Fprintf(w, <-ter_msg)
return
}
// RunningUser returns s reciver Channel of type string.
con_cmd := r.FormValue("cmd")
if con_cmd != "" {
console := ubusuma.Term(con_cmd)
fmt.Fprintf(w, <-console)
return
}
w.Header().Set("Content-Type", cont)
slice := []string {
"home-window-material",
"home-body",
}
Build(w, page, slice)
case "POST":
// This does nothing
fmt.Fprintf(w, "post home")
}
}
// This reads Template html files, creates the page and then writes
// the writes to the response writer
func reVtmp(w http.ResponseWriter, p WebPage, body string) {
tmp := template.New("template")
reVtop, err := ioutil.ReadFile("reVres/tmp/top.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var str_reVtop string = string(reVtop)
tmp.New("reVtop").Parse(str_reVtop)
reVbody, err := ioutil.ReadFile(body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var str_reVbody string = string(reVbody)
tmp.New("reVbody").Parse(str_reVbody)
reVbot, err := ioutil.ReadFile("reVres/tmp/bottom.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var str_reVbot string = string(reVbot)
tmp.New("reVbot").Parse(str_reVbot)
tmp.Lookup("reVbody").Execute(w, p)
}
// Returns a slice of strings user data
func queryUser() ([]string, error) {
jsn := dirReader("usr/root.json")// remember dirReader from line 46
var rootJsn UsrStr// remember the UsrStr type on line 56?
err := json.Unmarshal(jsn, &rootJsn)
if err != nil {
return nil, err
}
var usr_data []string
usr_data = append(usr_data, rootJsn["First_Name"])
usr_data = append(usr_data, rootJsn["Last_Name"])
usr_data = append(usr_data, rootJsn["Password"])
usr_data = append(usr_data, rootJsn["Cookie_Key"])
usr_data = append(usr_data, rootJsn["Access"])
return usr_data, nil
}
// Login page
func login(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s \n", r.Method, r.URL.Path)
var page WebPage
page.Title = "Login"
cont := contype.FileType(r.URL.Path)
w.Header().Set("Content-Type", cont)
w.Header().Set("Server", "Syamp")
switch r.Method {
case "GET":
reVtmp(w, page, "reVres/tmp/login-body.html")
case "POST":
xusr := r.FormValue("syamp_name")
xpas := r.FormValue("syamp_pass")
var check bool
acc, err := queryUser()
if err != nil {
log.Fatal(err)
}
if xusr == string(acc[0]) && xpas == string(acc[2]) {
check = true
}
if check == true {
expiration := time.Now().Add(360 * 24 * time.Hour)
snack := string(acc[3])
cookie := http.Cookie{Name: "syamp", Value: snack, Expires: expiration}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/home", http.StatusFound)
return
}else {
page.Message = "syam{p} thinks the information was wrong"
reVtmp(w, page, "reVres/tmp/login-body.html")
}
}
}
//////--logout page---//////
func logout(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s \n", r.Method, r.URL.Path)
_, err := r.Cookie("syamp")
if err != nil {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
expiration := time.Unix(1, 0)
cookie := http.Cookie{Name: "syamp", MaxAge: -1, Expires: expiration}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/home", http.StatusFound)
}
// config map
type Config map[string]string
func static(w http.ResponseWriter, r *http.Request) {
jsn := dirReader("etc/config.json")
var cfig Config
err := json.Unmarshal(jsn, &cfig)
if err != nil {
log.Printf("error: %v\n", err)
}
check := strings.Contains(r.URL.Path, cfig["dir"])
if check == false {
http.Redirect(w, r, "/home", http.StatusFound)
return
}
path := r.URL.Path[1:]
data := dirReader(path)
cont := contype.FileType(r.URL.Path)
w.Header().Set("Content-Type", cont)
w.Write(data)
}