Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
8.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "bitbucket.org/taruti/mimemagic"
  18. "github.com/dchest/uniuri"
  19. "github.com/zenazn/goji/web"
  20. )
  21. var fileBlacklist = map[string]bool{
  22. "favicon.ico": true,
  23. "index.htm": true,
  24. "index.html": true,
  25. "index.php": true,
  26. "robots.txt": true,
  27. "crossdomain.xml": true,
  28. }
  29. // Describes metadata directly from the user request
  30. type UploadRequest struct {
  31. src io.Reader
  32. filename string
  33. expiry time.Duration // Seconds until expiry, 0 = never
  34. randomBarename bool
  35. deletionKey string // Empty string if not defined
  36. }
  37. // Metadata associated with a file as it would actually be stored
  38. type Upload struct {
  39. Filename string // Final filename on disk
  40. Metadata Metadata
  41. }
  42. func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  43. if !strictReferrerCheck(r, Config.siteURL, []string{"Linx-Delete-Key", "Linx-Expiry", "Linx-Randomize", "X-Requested-With"}) {
  44. badRequestHandler(c, w, r)
  45. return
  46. }
  47. upReq := UploadRequest{}
  48. uploadHeaderProcess(r, &upReq)
  49. contentType := r.Header.Get("Content-Type")
  50. if strings.HasPrefix(contentType, "multipart/form-data") {
  51. file, headers, err := r.FormFile("file")
  52. if err != nil {
  53. oopsHandler(c, w, r, RespHTML, "Could not upload file.")
  54. return
  55. }
  56. defer file.Close()
  57. r.ParseForm()
  58. if r.Form.Get("randomize") == "true" {
  59. upReq.randomBarename = true
  60. }
  61. upReq.expiry = parseExpiry(r.Form.Get("expires"))
  62. upReq.src = file
  63. upReq.filename = headers.Filename
  64. } else {
  65. if r.FormValue("content") == "" {
  66. oopsHandler(c, w, r, RespHTML, "Empty file")
  67. return
  68. }
  69. extension := r.FormValue("extension")
  70. if extension == "" {
  71. extension = "txt"
  72. }
  73. upReq.src = strings.NewReader(r.FormValue("content"))
  74. upReq.expiry = parseExpiry(r.FormValue("expires"))
  75. upReq.filename = r.FormValue("filename") + "." + extension
  76. }
  77. upload, err := processUpload(upReq)
  78. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  79. if err != nil {
  80. oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
  81. return
  82. }
  83. js := generateJSONresponse(upload)
  84. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  85. w.Write(js)
  86. } else {
  87. if err != nil {
  88. oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
  89. return
  90. }
  91. http.Redirect(w, r, "/"+upload.Filename, 303)
  92. }
  93. }
  94. func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  95. upReq := UploadRequest{}
  96. uploadHeaderProcess(r, &upReq)
  97. defer r.Body.Close()
  98. upReq.filename = c.URLParams["name"]
  99. upReq.src = r.Body
  100. upload, err := processUpload(upReq)
  101. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  102. if err != nil {
  103. oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
  104. return
  105. }
  106. js := generateJSONresponse(upload)
  107. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  108. w.Write(js)
  109. } else {
  110. if err != nil {
  111. oopsHandler(c, w, r, RespPLAIN, "Could not upload file: "+err.Error())
  112. return
  113. }
  114. fmt.Fprintf(w, Config.siteURL+upload.Filename)
  115. }
  116. }
  117. func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) {
  118. if Config.remoteAuthFile != "" {
  119. result, err := checkAuth(remoteAuthKeys, r.FormValue("key"))
  120. if err != nil || !result {
  121. unauthorizedHandler(c, w, r)
  122. return
  123. }
  124. }
  125. if r.FormValue("url") == "" {
  126. http.Redirect(w, r, "/", 303)
  127. return
  128. }
  129. upReq := UploadRequest{}
  130. grabUrl, _ := url.Parse(r.FormValue("url"))
  131. resp, err := http.Get(grabUrl.String())
  132. if err != nil {
  133. oopsHandler(c, w, r, RespAUTO, "Could not retrieve URL")
  134. return
  135. }
  136. upReq.filename = filepath.Base(grabUrl.Path)
  137. upReq.src = resp.Body
  138. upReq.deletionKey = r.FormValue("deletekey")
  139. upReq.randomBarename = r.FormValue("randomize") == "yes"
  140. upReq.expiry = parseExpiry(r.FormValue("expiry"))
  141. upload, err := processUpload(upReq)
  142. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  143. if err != nil {
  144. oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
  145. return
  146. }
  147. js := generateJSONresponse(upload)
  148. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  149. w.Write(js)
  150. } else {
  151. if err != nil {
  152. oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
  153. return
  154. }
  155. http.Redirect(w, r, "/"+upload.Filename, 303)
  156. }
  157. }
  158. func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) {
  159. if r.Header.Get("Linx-Randomize") == "yes" {
  160. upReq.randomBarename = true
  161. }
  162. upReq.deletionKey = r.Header.Get("Linx-Delete-Key")
  163. // Get seconds until expiry. Non-integer responses never expire.
  164. expStr := r.Header.Get("Linx-Expiry")
  165. upReq.expiry = parseExpiry(expStr)
  166. }
  167. func processUpload(upReq UploadRequest) (upload Upload, err error) {
  168. // Determine the appropriate filename, then write to disk
  169. barename, extension := barePlusExt(upReq.filename)
  170. if upReq.randomBarename || len(barename) == 0 {
  171. barename = generateBarename()
  172. }
  173. var header []byte
  174. if len(extension) == 0 {
  175. // Pull the first 512 bytes off for use in MIME detection
  176. header = make([]byte, 512)
  177. n, _ := upReq.src.Read(header)
  178. if n == 0 {
  179. return upload, errors.New("Empty file")
  180. }
  181. header = header[:n]
  182. // Determine the type of file from header
  183. mimetype := mimemagic.Match("", header)
  184. // If the mime type is in our map, use that
  185. // otherwise just use "ext"
  186. if val, exists := mimeToExtension[mimetype]; exists {
  187. extension = val
  188. } else {
  189. extension = "ext"
  190. }
  191. }
  192. upload.Filename = strings.Join([]string{barename, extension}, ".")
  193. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  194. fileexists := err == nil
  195. // Check if the delete key matches, in which case overwrite
  196. if fileexists {
  197. metad, merr := metadataRead(upload.Filename)
  198. if merr == nil {
  199. if upReq.deletionKey == metad.DeleteKey {
  200. fileexists = false
  201. }
  202. }
  203. }
  204. for fileexists {
  205. counter, err := strconv.Atoi(string(barename[len(barename)-1]))
  206. if err != nil {
  207. barename = barename + "1"
  208. } else {
  209. barename = barename[:len(barename)-1] + strconv.Itoa(counter+1)
  210. }
  211. upload.Filename = strings.Join([]string{barename, extension}, ".")
  212. _, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
  213. fileexists = err == nil
  214. }
  215. if fileBlacklist[strings.ToLower(upload.Filename)] {
  216. return upload, errors.New("Prohibited filename")
  217. }
  218. dst, err := os.Create(path.Join(Config.filesDir, upload.Filename))
  219. if err != nil {
  220. return
  221. }
  222. defer dst.Close()
  223. // Get the rest of the metadata needed for storage
  224. var expiry time.Time
  225. if upReq.expiry == 0 {
  226. expiry = neverExpire
  227. } else {
  228. expiry = time.Now().Add(upReq.expiry)
  229. }
  230. bytes, err := io.Copy(dst, io.MultiReader(bytes.NewReader(header), upReq.src))
  231. if bytes == 0 {
  232. os.Remove(path.Join(Config.filesDir, upload.Filename))
  233. return upload, errors.New("Empty file")
  234. } else if err != nil {
  235. os.Remove(path.Join(Config.filesDir, upload.Filename))
  236. return
  237. } else if bytes > Config.maxSize {
  238. os.Remove(path.Join(Config.filesDir, upload.Filename))
  239. return upload, errors.New("File too large")
  240. }
  241. upload.Metadata, err = generateMetadata(upload.Filename, expiry, upReq.deletionKey)
  242. if err != nil {
  243. os.Remove(path.Join(Config.filesDir, upload.Filename))
  244. os.Remove(path.Join(Config.metaDir, upload.Filename))
  245. return
  246. }
  247. err = metadataWrite(upload.Filename, &upload.Metadata)
  248. if err != nil {
  249. os.Remove(path.Join(Config.filesDir, upload.Filename))
  250. os.Remove(path.Join(Config.metaDir, upload.Filename))
  251. return
  252. }
  253. return
  254. }
  255. func generateBarename() string {
  256. return uniuri.NewLenChars(8, []byte("abcdefghijklmnopqrstuvwxyz0123456789"))
  257. }
  258. func generateJSONresponse(upload Upload) []byte {
  259. js, _ := json.Marshal(map[string]string{
  260. "url": Config.siteURL + upload.Filename,
  261. "filename": upload.Filename,
  262. "delete_key": upload.Metadata.DeleteKey,
  263. "expiry": strconv.FormatInt(upload.Metadata.Expiry.Unix(), 10),
  264. "size": strconv.FormatInt(upload.Metadata.Size, 10),
  265. "mimetype": upload.Metadata.Mimetype,
  266. "sha256sum": upload.Metadata.Sha256sum,
  267. })
  268. return js
  269. }
  270. var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
  271. func barePlusExt(filename string) (barename, extension string) {
  272. filename = strings.TrimSpace(filename)
  273. filename = strings.ToLower(filename)
  274. extension = path.Ext(filename)
  275. barename = filename[:len(filename)-len(extension)]
  276. extension = barePlusRe.ReplaceAllString(extension, "")
  277. barename = barePlusRe.ReplaceAllString(barename, "")
  278. return
  279. }
  280. func parseExpiry(expStr string) time.Duration {
  281. if expStr == "" {
  282. return 0
  283. } else {
  284. expiry, err := strconv.ParseInt(expStr, 10, 64)
  285. if err != nil {
  286. return 0
  287. } else {
  288. return time.Duration(expiry) * time.Second
  289. }
  290. }
  291. }