]>
src.bluestatic.org Git - armadillo.git/blob - src/server.go
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(connection
*http
.Conn
, 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")
35 io
.Copy(connection
, fd
)
38 func serviceHandler(connection
*http
.Conn
, request
*http
.Request
) {
39 if request
.Method
!= "POST" {
40 io
.WriteString(connection
, "Error: Not a POST request")
44 switch request
.FormValue("action") {
46 files
, err
:= paths
.List(request
.FormValue("path"))
48 errorResponse(connection
, err
.String())
50 okResponse(connection
, files
)
53 err
:= paths
.Remove(request
.FormValue("path"))
55 errorResponse(connection
, err
.String())
57 response
:= map[string] int {
60 okResponse(connection
, response
)
63 source
:= request
.FormValue("source")
64 target
:= request
.FormValue("target")
65 err
:= paths
.Move(source
, target
)
67 errorResponse(connection
, err
.String())
69 response
:= map[string] string {
72 okResponse(connection
, response
)
75 errorResponse(connection
, "Unhandled action")
79 func proxyHandler(connection
*http
.Conn
, 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(connection
, "URL is not in proxy whitelist")
96 url
, err
:= http
.ParseURL(rawURL
)
98 errorResponse(connection
, err
.String())
101 err
= performProxy(url
, connection
, request
)
103 errorResponse(connection
, err
.String())
107 func performProxy(url
*http
.URL
, response
*http
.Conn
, 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()
126 _
, err
= io
.Copy(response
, proxyResponse
.Body
)
130 func errorResponse(connection
*http
.Conn
, message
string) {
131 message
= strings
.Replace(message
, gConfig
.JailRoot
, "/", -1)
132 response
:= map[string] string {
136 json_data
, err
:= json
.Marshal(response
)
138 connection
.SetHeader("Content-Type", "text/json")
140 io
.WriteString(connection
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
142 connection
.Write(json_data
)
146 func okResponse(connection
*http
.Conn
, data
interface{}) {
147 connection
.SetHeader("Content-Type", "text/json")
148 json_data
, err
:= json
.Marshal(data
)
150 errorResponse(connection
, "Internal encoding error")
152 connection
.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
)