2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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.
26 var dir, file = path.Split(path.Clean(os.Getenv("_")))
27 var kFrontEndFiles string = path.Join(dir, "fe")
28 var gConfig *config.Configuration
30 func SetConfig(aConfig *config.Configuration) {
34 func indexHandler(response http.ResponseWriter, request *http.Request) {
35 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
37 fmt.Print("Error opening file ", err.Error(), "\n")
42 response.Header().Set("Content-Type", "text/html")
46 func serviceHandler(response http.ResponseWriter, request *http.Request) {
47 if request.Method != "POST" {
48 io.WriteString(response, "Error: Not a POST request")
52 switch request.FormValue("action") {
54 files, err := ListPath(request.FormValue("path"))
56 errorResponse(response, err.Error())
58 okResponse(response, files)
61 err := RemovePath(request.FormValue("path"))
63 errorResponse(response, err.Error())
65 data := map[string]int{
68 okResponse(response, data)
71 source := request.FormValue("source")
72 target := request.FormValue("target")
73 err := MovePath(source, target)
75 errorResponse(response, err.Error())
77 data := map[string]interface{}{
81 okResponse(response, data)
84 path := request.FormValue("path")
87 errorResponse(response, err.Error())
89 data := map[string]interface{}{
93 okResponse(response, data)
96 newPath, err := RenameTVEpisode(request.FormValue("path"))
98 errorResponse(response, err.Error())
100 data := map[string]interface{}{
104 okResponse(response, data)
107 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
108 errorResponse(response, "Unhandled action")
112 func proxyHandler(response http.ResponseWriter, request *http.Request) {
113 rawURL := request.FormValue("url")
118 var validURL bool = false
119 for i := range gConfig.ProxyURLs {
120 allowedURL := gConfig.ProxyURLs[i]
121 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
125 errorResponse(response, "URL is not in proxy whitelist")
129 url_, err := url.Parse(rawURL)
131 errorResponse(response, err.Error())
134 err = performProxy(url_, response, request)
136 errorResponse(response, err.Error())
140 func performProxy(url_ *url.URL, response http.ResponseWriter, origRequest *http.Request) error {
141 conn, err := net.Dial("tcp", url_.Host+":http")
145 client := httputil.NewClientConn(conn, nil)
146 request, err := http.NewRequest("GET", url_.String(), nil)
150 request.Header.Set("User-Agent", origRequest.UserAgent())
151 err = client.Write(request)
155 var proxyResponse *http.Response
156 proxyResponse, err = client.Read(request)
157 if err != nil && err != httputil.ErrPersistEOF {
160 _, err = io.Copy(response, proxyResponse.Body)
164 func downloadHandler(response http.ResponseWriter, request *http.Request) {
165 valid, fullPath := IsValidPath(request.FormValue("path"))
167 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
169 http.Error(response, "Path is a directory", http.StatusBadRequest)
171 http.ServeFile(response, request, fullPath)
174 http.NotFound(response, request)
178 func errorResponse(response http.ResponseWriter, message string) {
179 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
180 data := map[string]interface{}{
184 json_data, err := json.Marshal(data)
186 response.Header().Set("Content-Type", "text/json")
188 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
190 response.Write(json_data)
194 func okResponse(response http.ResponseWriter, data interface{}) {
195 response.Header().Set("Content-Type", "text/json")
196 json_data, err := json.Marshal(data)
198 errorResponse(response, "Internal encoding error")
200 response.Write(json_data)
205 mux := http.NewServeMux()
206 mux.HandleFunc("/", indexHandler)
207 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
208 mux.HandleFunc("/service", serviceHandler)
209 mux.HandleFunc("/download", downloadHandler)
210 mux.HandleFunc("/proxy", proxyHandler)
212 error := http.ListenAndServe(fmt.Sprintf(":%d", gConfig.Port), mux)
213 fmt.Printf("error %v", error)