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")
110 fmt.Printf("Could not dial: %v\n", err)
113 client := http.NewClientConn(conn, nil)
114 var request http.Request
116 request.Method = "GET"
117 request.UserAgent = origRequest.UserAgent
118 err = client.Write(&request)
120 fmt.Printf("Could not proxy: %v\n", err)
123 var proxyResponse *http.Response
124 proxyResponse, err = client.Read()
125 if err != nil && err != http.ErrPersistEOF {
126 fmt.Printf("Could not read proxied response: %v\n", err)
129 _, err = io.Copy(response, proxyResponse.Body)
133 func errorResponse(response http.ResponseWriter, message string) {
134 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
135 data := map[string] string {
139 json_data, err := json.Marshal(data)
141 response.SetHeader("Content-Type", "text/json")
143 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
145 response.Write(json_data)
149 func okResponse(response http.ResponseWriter, data interface{}) {
150 response.SetHeader("Content-Type", "text/json")
151 json_data, err := json.Marshal(data)
153 errorResponse(response, "Internal encoding error")
155 response.Write(json_data)
159 func RunBackEnd(config *config.Configuration) {
160 mux := http.NewServeMux()
161 mux.HandleFunc("/", indexHandler)
162 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
163 mux.HandleFunc("/service", serviceHandler)
164 mux.HandleFunc("/proxy", proxyHandler)
168 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
169 fmt.Printf("error %v", error)