-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
231 lines (185 loc) 路 4.71 KB
/
Copy pathmain.go
File metadata and controls
231 lines (185 loc) 路 4.71 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
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
package main
/*
#cgo CFLAGS: -O3 -march=native -flto
#include "embed/ansi.c"
*/
import "C"
import (
"fmt"
"time"
"net/http"
"net"
"os"
"strings"
"encoding/base64"
_ "embed"
)
var (
pass = "admin"
root = "./"
port = "8080"
DECODE = strings.NewReplacer(
"<0>", "#",
"<1>", "%",
"<2>", "&",
"<3>", "+",
"<4>", ";",
)
// ENCODE = strings.NewReplacer(
// "#", "<0>",
// "%", "<1>",
// "&", "<2>",
// "+", "<3>",
// ";", "<4>",
// )
SLASH = strings.NewReplacer(
"/", " /",
)
//go:embed embed/main.css
CSS string
//go:embed embed/main.js
JS string
//go:embed embed/login.js
LJS string
//go:embed embed/chat.js
CJS string
//go:embed embed/main.png
PNG []byte
icon = base64.StdEncoding.EncodeToString(PNG)
//go:embed embed/font/CourierPrime-Regular.ttf
FONT []byte
font = base64.StdEncoding.EncodeToString(FONT)
clint = NewStringSet()
)
const ZIP = 100 * 1024 // 100 KB
type StringSet struct {
items map[string]struct{}
}
func NewStringSet() *StringSet {
return &StringSet{
items: make(map[string]struct{}),
}
}
func (ss *StringSet) Add(s string) {
ss.items[s] = struct{}{}
}
func (ss *StringSet) Remove(s string) {
delete(ss.items, s)
}
func (ss *StringSet) Contains(s string) bool {
_, exists := ss.items[s]
return exists
}
// func (ss *StringSet) ContainsFold(s string) bool {
// s = strings.ToLower(s)
// for key := range ss.items {
// if strings.ToLower(key) == s {
// return true
// }
// }
// return false
// }
// func (ss *StringSet) GetAll() []string {
// result := make([]string, 0, len(ss.items))
// for key := range ss.items {
// result = append(result, key)
// }
// return result
// }
// func (ss *StringSet) Length() int {
// return len(ss.items)
// }
func func_exists(path string) bool {
info, err := os.Stat(path)
if err != nil || !info.IsDir() {
return false
} else {
return true
}
}
func func_decode(path string) string {
return DECODE.Replace(path)
}
// func func_encode(path string) string {
// return ENCODE.Replace(path)
// }
func func_log(color string, addr string, mode string, path string) {
fmt.Printf("| %s%s - %s - %s - %s \033[0m\n", color, time.Now().Format("15:04:05"), addr, mode, path)
}
func main() {
C.enable_ansi() // enable console ansi
clint.Add("127.0.0.1") // bypass login on localhost
// get and set arguments
if len(os.Args) >= 4 {
port = os.Args[1]
pass = os.Args[2]
root = os.Args[3]
}
// set handler
if func_exists(root) {
http.HandleFunc("/path", handler_index)
http.HandleFunc("/login", handler_login)
http.HandleFunc("/set", handler_upload)
http.HandleFunc("/get", handler_download)
http.HandleFunc("/chat", handler_chat)
http.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
get_ip := r.RemoteAddr
get_ip = get_ip[:strings.LastIndexByte(get_ip, ':')]
if clint.Contains(get_ip) {
clint.Remove(get_ip)
func_log("\033[97m", r.RemoteAddr, "[LOGOUT]", get_ip)
http.Redirect(w, r, "/login", http.StatusFound)
return
} else {
html := fmt.Sprintf(
error_body,
icon,
"/404",
font,
CSS,
`<b>Clint not found. </b>`,
)
w.Write([]byte(html))
return
}
})
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
r.Write(w)
return
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, "/path?fo=/", http.StatusFound)
return
}
})
fmt.Println("\n+- LanDrive -------")
fmt.Printf("| root: \033[94m%s\033[0m ...\n", root)
fmt.Printf("| port: \033[94m%s\033[0m ...\n", port)
fmt.Printf("| pass: \033[94m%s\033[0m ...\n", pass)
fmt.Println("+------------------")
fmt.Printf("| url: \033[92mhttp://127.0.0.1:%s/path?fo=/\033[0m ...\n", port)
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Printf("\033[91mError @ network interface !!\033[0m\n")
os.Exit(1)
}
for _, addr := range addrs {
// check for ip address [not loopback]
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ip4 := ipNet.IP.To4(); ip4 != nil {
fmt.Printf("| url: \033[92mhttp://%s:%s/path?fo=/\033[0m ...\n", ip4.String(), port) // print IPv4
}
}
}
fmt.Println("+- Logs -----------")
ip := fmt.Sprintf(":%s", port)
if err := http.ListenAndServe(ip, nil); err != nil {
fmt.Printf("\033[91mError @ unknown port !!\033[0m\n")
os.Exit(1)
}
} else {
fmt.Printf("\033[91mError @ unknown root !!\033[0m\n", root)
os.Exit(1)
}
}