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