2 // Armadillo File Manager
3 // Copyright (c) 2010-2012, Robert Sesek <http://www.bluestatic.org>
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
13 "github.com/rsesek/armadillo/config"
26 var dir, file = path.Split(path.Clean(os.Getenv("_")))
27 var kFrontEndFiles string = path.Join(dir, "frontend")
28 var gConfig *config.Configuration
30 func indexHandler(response http.ResponseWriter, request *http.Request) {
31 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
33 fmt.Print("Error opening file ", err.Error(), "\n")
38 response.Header().Set("Content-Type", "text/html")
42 func serviceHandler(response http.ResponseWriter, request *http.Request) {
43 if request.Method != "POST" {
44 io.WriteString(response, "Error: Not a POST request")
48 switch request.FormValue("action") {
50 files, err := ListPath(request.FormValue("path"))
52 errorResponse(response, err.Error())
54 okResponse(response, files)
57 err := RemovePath(request.FormValue("path"))
59 errorResponse(response, err.Error())
61 data := map[string]int{
64 okResponse(response, data)
67 source := request.FormValue("source")
68 target := request.FormValue("target")
69 err := MovePath(source, target)
71 errorResponse(response, err.Error())
73 data := map[string]interface{}{
77 okResponse(response, data)
80 path := request.FormValue("path")
83 errorResponse(response, err.Error())
85 data := map[string]interface{}{
89 okResponse(response, data)
92 newPath, err := RenameTVEpisode(request.FormValue("path"))
94 errorResponse(response, err.Error())
96 data := map[string]interface{}{
100 okResponse(response, data)
103 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
104 errorResponse(response, "Unhandled action")
108 func proxyHandler(response http.ResponseWriter, request *http.Request) {
109 rawURL := request.FormValue("url")
114 var validURL bool = false
115 for i := range gConfig.ProxyURLs {
116 allowedURL := gConfig.ProxyURLs[i]
117 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
121 errorResponse(response, "URL is not in proxy whitelist")
125 url_, err := url.Parse(rawURL)
127 errorResponse(response, err.Error())
130 err = performProxy(url_, response, request)
132 errorResponse(response, err.Error())
136 func performProxy(url_ *url.URL, response http.ResponseWriter, origRequest *http.Request) error {
137 conn, err := net.Dial("tcp", url_.Host+":http")
141 client := httputil.NewClientConn(conn, nil)
142 request, err := http.NewRequest("GET", url_.String(), nil)
146 request.Header.Set("User-Agent", origRequest.UserAgent())
147 err = client.Write(request)
151 var proxyResponse *http.Response
152 proxyResponse, err = client.Read(request)
153 if err != nil && err != httputil.ErrPersistEOF {
156 _, err = io.Copy(response, proxyResponse.Body)
160 func downloadHandler(response http.ResponseWriter, request *http.Request) {
161 valid, fullPath := IsValidPath(request.FormValue("path"))
163 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
165 http.Error(response, "Path is a directory", http.StatusBadRequest)
167 http.ServeFile(response, request, fullPath)
170 http.NotFound(response, request)
174 func errorResponse(response http.ResponseWriter, message string) {
175 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
176 data := map[string]interface{}{
180 json_data, err := json.Marshal(data)
182 response.Header().Set("Content-Type", "text/json")
184 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
186 response.Write(json_data)
190 func okResponse(response http.ResponseWriter, data interface{}) {
191 response.Header().Set("Content-Type", "text/json")
192 json_data, err := json.Marshal(data)
194 errorResponse(response, "Internal encoding error")
196 response.Write(json_data)
200 func RunBackEnd(c *config.Configuration) {
203 mux := http.NewServeMux()
204 mux.HandleFunc("/", indexHandler)
205 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
206 mux.HandleFunc("/service", serviceHandler)
207 mux.HandleFunc("/download", downloadHandler)
208 mux.HandleFunc("/proxy", proxyHandler)
210 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
211 fmt.Printf("error %v", error)