2 // Armadillo File Manager
3 // Copyright (c) 2010, 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.
25 var dir, file = path.Split(path.Clean(os.Getenv("_")))
26 var kFrontEndFiles string = path.Join(dir, "fe")
27 var gConfig *config.Configuration = nil
29 func indexHandler(response http.ResponseWriter, request *http.Request) {
30 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"), os.O_RDONLY, 0)
32 fmt.Print("Error opening file ", err.String(), "\n")
38 func serviceHandler(response http.ResponseWriter, request *http.Request) {
39 if request.Method != "POST" {
40 io.WriteString(response, "Error: Not a POST request")
44 switch request.FormValue("action") {
46 files, err := paths.List(request.FormValue("path"))
48 errorResponse(response, err.String())
50 okResponse(response, files)
53 err := paths.Remove(request.FormValue("path"))
55 errorResponse(response, err.String())
57 data := map[string] int {
60 okResponse(response, data)
63 source := request.FormValue("source")
64 target := request.FormValue("target")
65 err := paths.Move(source, target)
67 errorResponse(response, err.String())
69 data := map[string] string {
72 okResponse(response, data)
75 errorResponse(response, "Unhandled action")
79 func proxyHandler(response http.ResponseWriter, request *http.Request) {
80 rawURL := request.FormValue("url")
85 var validURL bool = false
86 for i := range gConfig.ProxyURLs {
87 allowedURL := gConfig.ProxyURLs[i]
88 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
92 errorResponse(response, "URL is not in proxy whitelist")
96 url, err := http.ParseURL(rawURL)
98 errorResponse(response, err.String())
101 err = performProxy(url, response, request)
103 errorResponse(response, err.String())
107 func performProxy(url *http.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
108 conn, err := net.Dial("tcp", "", url.Host + ":http")
112 client := http.NewClientConn(conn, nil)
113 var request http.Request
115 request.Method = "GET"
116 request.UserAgent = origRequest.UserAgent
117 err = client.Write(&request)
121 var proxyResponse *http.Response
122 proxyResponse, err = client.Read()
123 if err != http.ErrPersistEOF {
126 _, err = io.Copy(response, proxyResponse.Body)
130 func errorResponse(response http.ResponseWriter, message string) {
131 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
132 data := map[string] string {
136 json_data, err := json.Marshal(data)
138 response.SetHeader("Content-Type", "text/json")
140 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
142 response.Write(json_data)
146 func okResponse(response http.ResponseWriter, data interface{}) {
147 response.SetHeader("Content-Type", "text/json")
148 json_data, err := json.Marshal(data)
150 errorResponse(response, "Internal encoding error")
152 response.Write(json_data)
156 func RunBackEnd(config *config.Configuration) {
157 mux := http.NewServeMux()
158 mux.HandleFunc("/", indexHandler)
159 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
160 mux.HandleFunc("/service", serviceHandler)
161 mux.HandleFunc("/proxy", proxyHandler)
165 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
166 fmt.Printf("error %v", error)