This repository has been archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
listener.go
174 lines (135 loc) · 4.14 KB
/
listener.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
// CodeSkulptor HTTP Request Listener
// Copyright (c) Adnan Umer <u.adnan@outlook.com>.
// All rights reserved. Follow me @MrAdnanUmer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
package codeskulptor
import (
"net/http"
"os"
"fmt"
"syscall"
"path"
"strings"
"unsafe"
"unicode/utf16"
"time"
"strconv"
)
// [BEGIN] Windows specific operations
var (
kernel = syscall.MustLoadDLL("kernel32.dll")
getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW")
)
func getExePath() string {
exe, _ := getModuleFileName()
path, _ := path.Split(strings.Replace(exe, "\\", "/", -1))
return path
}
func getModuleFileName() (string, error) {
var n uint32
b := make([]uint16, syscall.MAX_PATH)
size := uint32(len(b))
r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size))
n = uint32(r0)
if n == 0 {
return "", e1
}
return string(utf16.Decode(b[0:n])), nil
}
// [END]
var exePath string = getExePath();
var storageFolder string = exePath + "storage/";
/*
* Checks wheather specified files exits
*/
func fileExists(file string) bool {
_, err := os.Stat(file);
return !os.IsNotExist(err);
}
func log(line string) {
fmt.Printf(time.Now().Local().Format(time.RFC822) + " " + line + "\n");
}
/*
* Handles HTTP Request for /storage/
*/
func storageHandler(w http.ResponseWriter, req *http.Request) {
// We are only going to send python scripts in response if any
w.Header().Set("Content-Type", "text/plain")
requestedFile := req.URL.Path[1:]
file_path := storageFolder + req.URL.Path[9:];
if (req.Method == "POST") {
file_path += req.FormValue("path")
}
hasFile := fileExists(file_path)
switch (req.Method) {
case "HEAD":
if (hasFile) {
// OK
log("[HEAD] 200 " + requestedFile)
} else {
// NOT FOUND
http.NotFound(w, req)
log("[HEAD] 404 " + requestedFile)
}
break;
case "GET":
if (hasFile) {
// OK
http.ServeFile(w, req, file_path)
log("[GET] 200 " + requestedFile)
} else {
// NOT FOUND
http.NotFound(w, req)
log("[GET] 404 " + requestedFile)
}
break;
case "POST":
file := req.FormValue("path")
code := req.FormValue("code")
requestedFile += file
if (hasFile) {
// File already exists
http.Error(w, "A file with specified name already exists", 403)
log("[POST] 403 " + requestedFile)
log("[ERROR] Specified File already exists");
} else {
f, err := os.Create(storageFolder + file)
if err != nil {
// Failed to ceate File
http.Error(w, err.Error(), 500)
log("[POST] 500 " + requestedFile);
log("[ERROR] Error creating file: " + err.Error());
} else {
log("[POST] 200 " + requestedFile);
// Write content to file
f.WriteString(code);
f.Close();
}
}
break;
}
}
func Listen(port int) {
http.HandleFunc("/storage/", storageHandler)
http.HandleFunc("/fetch/", UrlLib2Handler)
http.Handle("/", http.FileServer(http.Dir(exePath) + "html/"))
log("[INFO] Listening and Serving on localhost:" + strconv.Itoa(port))
if err := http.ListenAndServe(":" + strconv.Itoa(port), nil); err != nil {
log("[ERROR]: " + err.Error())
}
}