-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
242 lines (222 loc) · 6.22 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
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
package main
import (
fe "./fe"
models "./models"
settings "./settings"
"fmt"
"github.com/go-macaron/binding"
"github.com/go-macaron/cache"
"github.com/go-macaron/session"
"gopkg.in/macaron.v1"
"log"
"runtime"
"strings"
)
var DEFAULT_API_ERROR_RESPONSE = models.GenericResp{
models.GenericRespBody{false, "Not Supported"},
}
type SessionInfo struct {
User string
Password string
FileExplorer fe.FileExplorer
Uid string
}
func main() {
configRuntime()
startServer()
}
func configRuntime() {
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
fmt.Printf("Running with %d CPUs\n", numCPU)
}
func startServer() {
settings.Load()
macaron.Classic()
m := macaron.New()
m.Use(macaron.Logger())
m.Use(macaron.Recovery())
if len(settings.Server.Statics) > 0 {
m.Use(macaron.Statics(macaron.StaticOptions{
Prefix: "static",
SkipLogging: false,
}, settings.Server.Statics...))
}
m.Use(cache.Cacher())
m.Use(session.Sessioner())
m.Use(macaron.Renderer())
m.Use(Contexter())
m.Post("/api/_", binding.Bind(models.GenericReq{}), apiHandler)
m.Post("/bridges/php/handler.php", binding.Bind(models.GenericReq{}), apiHandler)
m.Get("/", mainHandler)
m.Get("/login", loginHandler)
m.Post("/api/download", defaultHandler)
m.Post("/api/upload", defaultHandler)
if settings.Server.Type == "http" {
bind := strings.Split(settings.Server.Bind, ":")
if len(bind) == 1 {
m.Run(bind[0])
}
if len(bind) == 2 {
m.Run(bind[0], bind[1])
}
}
}
func mainHandler(ctx *macaron.Context) {
ctx.HTML(200, "index")
}
func loginHandler(ctx *macaron.Context) {
ctx.HTML(200, "login")
}
func defaultHandler(ctx *macaron.Context) {
ctx.JSON(200, DEFAULT_API_ERROR_RESPONSE)
}
func apiHandler(c *macaron.Context, json models.GenericReq, sessionInfo SessionInfo) {
if json.Params.Mode == "list" {
ls, err := sessionInfo.FileExplorer.ListDir(json.Params.Path)
if err == nil {
c.JSON(200, models.ListDirResp{ls})
} else {
ApiErrorResponse(c, 400, err)
}
} else if json.Params.Mode == "rename" { // path, newPath
err := sessionInfo.FileExplorer.Move(json.Params.Path, json.Params.NewPath)
if err == nil {
ApiSuccessResponse(c, "")
} else {
ApiErrorResponse(c, 400, err)
}
} else if json.Params.Mode == "copy" { // path, newPath
err := sessionInfo.FileExplorer.Copy(json.Params.Path, json.Params.NewPath)
if err == nil {
ApiSuccessResponse(c, "")
} else {
ApiErrorResponse(c, 400, err)
}
} else if json.Params.Mode == "delete" { // path
err := sessionInfo.FileExplorer.Delete(json.Params.Path)
if err == nil {
ApiSuccessResponse(c, "")
} else {
ApiErrorResponse(c, 400, err)
}
} else if json.Params.Mode == "savefile" { // content, path
c.JSON(200, DEFAULT_API_ERROR_RESPONSE)
} else if json.Params.Mode == "editfile" { // path
c.JSON(200, DEFAULT_API_ERROR_RESPONSE)
} else if json.Params.Mode == "addfolder" { // name, path
err := sessionInfo.FileExplorer.Mkdir(json.Params.Path, json.Params.Name)
if err == nil {
ApiSuccessResponse(c, "")
} else {
ApiErrorResponse(c, 400, err)
}
} else if json.Params.Mode == "changepermissions" { // path, perms, permsCode, recursive
err := sessionInfo.FileExplorer.Chmod(json.Params.Path, json.Params.Perms)
if err == nil {
ApiSuccessResponse(c, "")
} else {
ApiErrorResponse(c, 400, err)
}
} else if json.Params.Mode == "compress" { // path, destination
c.JSON(200, DEFAULT_API_ERROR_RESPONSE)
} else if json.Params.Mode == "extract" { // path, destination, sourceFile
c.JSON(200, DEFAULT_API_ERROR_RESPONSE)
}
}
func IsApiPath(url string) bool {
return strings.HasPrefix(url, "/api/") || strings.HasPrefix(url, "/bridges/php/handler.php")
}
func Contexter() macaron.Handler {
return func(c *macaron.Context, cache cache.Cache, session session.Store, f *session.Flash) {
isSigned := false
sessionInfo := SessionInfo{}
uid := session.Get("uid")
if uid == nil {
isSigned = false
} else {
sessionInfoObj := cache.Get(uid.(string))
if sessionInfoObj == nil {
isSigned = false
} else {
sessionInfo = sessionInfoObj.(SessionInfo)
if sessionInfo.User == "" || sessionInfo.Password == "" {
isSigned = false
} else {
isSigned = true
c.Data["User"] = sessionInfo.User
c.Map(sessionInfo)
if sessionInfo.FileExplorer == nil {
fe, err := BackendConnect(sessionInfo.User, sessionInfo.Password)
sessionInfo.FileExplorer = fe
if err != nil {
isSigned = false
if IsApiPath(c.Req.URL.Path) {
ApiErrorResponse(c, 500, err)
} else {
AuthError(c, f, err)
}
}
}
}
}
}
if isSigned == false {
if strings.HasPrefix(c.Req.URL.Path, "/login") {
if c.Req.Method == "POST" {
username := c.Query("username")
password := c.Query("password")
fe, err := BackendConnect(username, password)
if err != nil {
AuthError(c, f, err)
} else {
uid := username // TODO: ??
sessionInfo = SessionInfo{username, password, fe, uid}
cache.Put(uid, sessionInfo, 100000000000)
session.Set("uid", uid)
c.Data["User"] = sessionInfo.User
c.Map(sessionInfo)
c.Redirect("/")
}
}
} else {
c.Redirect("/login")
}
} else {
if strings.HasPrefix(c.Req.URL.Path, "/logout") {
sessionInfo.FileExplorer.Close()
session.Delete("uid")
cache.Delete(uid.(string))
c.SetCookie("MacaronSession", "")
c.Redirect("/login")
}
}
}
}
func BackendConnect(username string, password string) (fe.FileExplorer, error) {
fe := fe.NewSSHFileExplorer(settings.Backend.Host, username, password)
err := fe.Init()
if err == nil {
return fe, nil
}
log.Println(err)
return nil, err
}
func ApiErrorResponse(c *macaron.Context, code int, obj interface{}) {
var message string
if err, ok := obj.(error); ok {
message = err.Error()
} else {
message = obj.(string)
}
c.JSON(code, models.GenericResp{models.GenericRespBody{false, message}})
}
func ApiSuccessResponse(c *macaron.Context, message string) {
c.JSON(200, models.GenericResp{models.GenericRespBody{true, message}})
}
func AuthError(c *macaron.Context, f *session.Flash, err error) {
f.Set("ErrorMsg", err.Error())
c.Data["Flash"] = f
c.Data["ErrorMsg"] = err.Error()
c.Redirect("/login")
}