delta.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package main
  2. import auth "github.com/abbot/go-http-auth"
  3. import "syscall"
  4. import "bufio"
  5. import "path/filepath"
  6. import "html/template"
  7. import "strconv"
  8. import "os"
  9. import "io"
  10. import "log"
  11. import "fmt"
  12. import "net/http"
  13. import "strings"
  14. var (
  15. targetFolder string
  16. targetFile string
  17. searchResult []string
  18. )
  19. func css(w http.ResponseWriter, req *http.Request) {
  20. path := req.URL.Path
  21. var contentType string
  22. if strings.HasSuffix(path, ".css") {
  23. contentType = "text/css"
  24. } else if strings.HasSuffix(path, ".svg") {
  25. contentType = "image/svg+xml"
  26. } else if strings.HasSuffix(path, ".png") {
  27. contentType = "image/png"
  28. } else {
  29. contentType = "text/plain"
  30. }
  31. f, err := os.Open(path)
  32. if err == nil {
  33. defer f.Close()
  34. w.Header().Add("Content-Type", contentType)
  35. br := bufio.NewReader(f)
  36. br.WriteTo(w)
  37. } else {
  38. w.WriteHeader(404)
  39. }
  40. }
  41. func findFile(path string, fileInfo os.FileInfo, err error) error {
  42. if err != nil {
  43. fmt.Println(err)
  44. return nil
  45. }
  46. // get absolute path of the folder that we are searching
  47. absolute, err := filepath.Abs(path)
  48. if err != nil {
  49. fmt.Println(err)
  50. return nil
  51. }
  52. if fileInfo.IsDir() {
  53. // correct permission to scan folder?
  54. testDir, err := os.Open(absolute)
  55. if err != nil {
  56. if os.IsPermission(err) {
  57. fmt.Println("No permission to scan ... ", absolute)
  58. fmt.Println(err)
  59. }
  60. }
  61. testDir.Close()
  62. return nil
  63. } else {
  64. // ok, we are dealing with a file
  65. // is this the target file?
  66. // yes, need to support wildcard search as well
  67. matched, err := filepath.Match(targetFile, fileInfo.Name())
  68. if err != nil {
  69. fmt.Println(err)
  70. }
  71. if matched {
  72. // yes, add into our search result
  73. add := absolute
  74. searchResult = append(searchResult, add)
  75. }
  76. }
  77. return nil
  78. }
  79. func search(w http.ResponseWriter, r *http.Request) {
  80. if r.Method == "GET" {
  81. t, _ := template.ParseFiles("serch.gtpl")
  82. t.Execute(w, nil)
  83. } else {
  84. r.ParseForm()
  85. name := r.PostFormValue("name")
  86. targetFolder = "/apps"
  87. targetFile = name + "*"
  88. fmt.Fprintf(w, "<p> Searching for [" + targetFile + "] </p>")
  89. // sanity check
  90. testFile, err := os.Open(targetFolder)
  91. if err != nil {
  92. fmt.Println(err)
  93. }
  94. defer testFile.Close()
  95. testFileInfo, _ := testFile.Stat()
  96. if !testFileInfo.IsDir() {
  97. fmt.Println(targetFolder, " is not a directory!")
  98. }
  99. err = filepath.Walk(targetFolder, findFile)
  100. if err != nil {
  101. fmt.Println(err)
  102. }
  103. // display our search result
  104. sys := strconv.Itoa(len(searchResult))
  105. fmt.Fprintf(w, "\n\n <p> Found " + sys + " hits! </p>\n\n")
  106. for _, v := range searchResult {
  107. path := v
  108. file := filepath.Base(path)
  109. fmt.Fprintf(w, "<a href=\"" + path + "\">" + file + "</a>\n")
  110. }
  111. }
  112. searchResult = searchResult[:0]
  113. }
  114. /* this is your end, this is your end */
  115. func uploade(w http.ResponseWriter, r *http.Request) {
  116. if r.Method == "GET" {
  117. t, _ := template.ParseFiles("upload.gtpl")
  118. t.Execute(w, nil)
  119. } else {
  120. name := r.FormValue("folder")
  121. file, handler, err := r.FormFile("uploadfile")
  122. if err != nil {
  123. fmt.Println(err)
  124. return
  125. }
  126. defer file.Close()
  127. if(handler.Filename == "index.html") {
  128. fmt.Fprintf(w, "<p> You cannot pass! I am a servant of the Secret Fire, wielder of the Flame of Anor. The dark fire will not avail you, Go back to the shadow. You cannot pass");
  129. return
  130. }
  131. f, err := os.OpenFile("./apps/" + name + "/" +handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
  132. if err != nil {
  133. fmt.Println(err)
  134. return
  135. }
  136. defer f.Close()
  137. io.Copy(f, file)
  138. fmt.Fprintf(w, "upload ok");
  139. }
  140. }
  141. func home(w http.ResponseWriter, r *http.Request) {
  142. t, _ := template.ParseFiles("home.gtpl")
  143. t.Execute(w, nil)
  144. }
  145. func admin(w http.ResponseWriter, r *http.Request) {
  146. t, _ := template.ParseFiles("admin.gtpl")
  147. t.Execute(w, nil)
  148. }
  149. func remove(w http.ResponseWriter, r *http.Request) {
  150. if r.Method == "GET" {
  151. t, _ := template.ParseFiles("remove.gtpl")
  152. t.Execute(w, nil)
  153. } else {
  154. r.ParseForm()
  155. name := r.PostFormValue("name")
  156. err := os.Remove("apps/" + name)
  157. if err != nil {
  158. fmt.Println(err)
  159. return
  160. }
  161. fmt.Fprintf(w, "remove ok")
  162. }
  163. }
  164. func mkdir(w http.ResponseWriter, r *http.Request) {
  165. if r.Method == "GET" {
  166. t, _ := template.ParseFiles("new.gtpl")
  167. t.Execute(w, nil)
  168. } else {
  169. r.ParseForm()
  170. name := r.PostFormValue("name")
  171. err := os.Mkdir("apps/" + name, 0)
  172. if err != nil {
  173. fmt.Println(err)
  174. return
  175. }
  176. }
  177. fmt.Fprintf(w,"ok")
  178. }
  179. func rename(w http.ResponseWriter, r *http.Request) {
  180. if r.Method == "GET" {
  181. t, _ := template.ParseFiles("rename.gtpl")
  182. t.Execute(w, nil)
  183. } else {
  184. r.ParseForm()
  185. name := r.PostFormValue("name")
  186. newname := r.PostFormValue("new name")
  187. err := os.Rename("apps/" + name, "apps/" + newname)
  188. if err != nil {
  189. fmt.Println(err)
  190. return
  191. }
  192. fmt.Fprintf(w, "rename ok")
  193. }
  194. }
  195. func handleFileServer(dir, prefix string) http.HandlerFunc {
  196. fs := http.FileServer(http.Dir(dir))
  197. realHandler := http.StripPrefix(prefix, fs).ServeHTTP
  198. return func(w http.ResponseWriter, req *http.Request) {
  199. log.Println(req.URL)
  200. realHandler(w, req)
  201. }
  202. }
  203. func main() {
  204. syscall.Chroot(".")
  205. htpasswd := auth.HtpasswdFileProvider("passwd")
  206. admin_htpasswd := auth.HtpasswdFileProvider("passwd")
  207. authenticator := auth.NewBasicAuthenticator("Basic Realm", htpasswd)
  208. admin_authenticator := auth.NewBasicAuthenticator("Basic Realm", admin_htpasswd)
  209. http.HandleFunc("/css/", css) // setting router rule
  210. http.HandleFunc("/", home)
  211. http.HandleFunc("/apps/", auth.JustCheck(authenticator, handleFileServer("apps", "/apps/")))
  212. http.HandleFunc("/remove", auth.JustCheck(admin_authenticator, remove))
  213. http.HandleFunc("/rename", auth.JustCheck(admin_authenticator, rename))
  214. http.HandleFunc("/new", auth.JustCheck(admin_authenticator, mkdir))
  215. http.HandleFunc("/upload", auth.JustCheck(admin_authenticator, uploade))
  216. http.HandleFunc("/search", auth.JustCheck(authenticator, search))
  217. http.HandleFunc("/admin", auth.JustCheck(admin_authenticator, admin))
  218. err := http.ListenAndServe(":80", nil) // setting listening port
  219. if err != nil {
  220. log.Fatal("ListenAndServe: ", err)
  221. }
  222. }