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.

222 lines
6.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
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "net"
  6. "net/http"
  7. "net/http/fcgi"
  8. "os"
  9. "regexp"
  10. "strconv"
  11. "time"
  12. "github.com/GeertJohan/go.rice"
  13. "github.com/flosch/pongo2"
  14. "github.com/zenazn/goji/graceful"
  15. "github.com/zenazn/goji/web"
  16. "github.com/zenazn/goji/web/middleware"
  17. )
  18. var Config struct {
  19. bind string
  20. filesDir string
  21. metaDir string
  22. siteName string
  23. siteURL string
  24. certFile string
  25. keyFile string
  26. contentSecurityPolicy string
  27. fileContentSecurityPolicy string
  28. xFrameOptions string
  29. maxSize int64
  30. realIp bool
  31. noLogs bool
  32. allowHotlink bool
  33. fastcgi bool
  34. remoteUploads bool
  35. authFile string
  36. remoteAuthFile string
  37. }
  38. var Templates = make(map[string]*pongo2.Template)
  39. var TemplateSet *pongo2.TemplateSet
  40. var staticBox *rice.Box
  41. var timeStarted time.Time
  42. var timeStartedStr string
  43. var remoteAuthKeys []string
  44. func setup() *web.Mux {
  45. mux := web.New()
  46. // middleware
  47. mux.Use(middleware.RequestID)
  48. if Config.realIp {
  49. mux.Use(middleware.RealIP)
  50. }
  51. if !Config.noLogs {
  52. mux.Use(middleware.Logger)
  53. }
  54. mux.Use(middleware.Recoverer)
  55. mux.Use(middleware.AutomaticOptions)
  56. mux.Use(ContentSecurityPolicy(CSPOptions{
  57. policy: Config.contentSecurityPolicy,
  58. frame: Config.xFrameOptions,
  59. }))
  60. if Config.authFile != "" {
  61. mux.Use(UploadAuth(AuthOptions{
  62. AuthFile: Config.authFile,
  63. UnauthMethods: []string{"GET", "HEAD", "OPTIONS", "TRACE"},
  64. }))
  65. }
  66. // make directories if needed
  67. err := os.MkdirAll(Config.filesDir, 0755)
  68. if err != nil {
  69. log.Fatal("Could not create files directory:", err)
  70. }
  71. err = os.MkdirAll(Config.metaDir, 0700)
  72. if err != nil {
  73. log.Fatal("Could not create metadata directory:", err)
  74. }
  75. // ensure siteURL ends wth '/'
  76. if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
  77. Config.siteURL = Config.siteURL + "/"
  78. }
  79. // Template setup
  80. p2l, err := NewPongo2TemplatesLoader()
  81. if err != nil {
  82. log.Fatal("Error: could not load templates", err)
  83. }
  84. TemplateSet := pongo2.NewSet("templates", p2l)
  85. TemplateSet.Globals["sitename"] = Config.siteName
  86. TemplateSet.Globals["siteurl"] = Config.siteURL
  87. TemplateSet.Globals["using_auth"] = Config.authFile != ""
  88. err = populateTemplatesMap(TemplateSet, Templates)
  89. if err != nil {
  90. log.Fatal("Error: could not load templates", err)
  91. }
  92. staticBox = rice.MustFindBox("static")
  93. timeStarted = time.Now()
  94. timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)
  95. // Routing setup
  96. nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
  97. selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
  98. selifIndexRe := regexp.MustCompile(`^/selif/$`)
  99. torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)
  100. if Config.authFile == "" {
  101. mux.Get("/", indexHandler)
  102. mux.Get("/paste/", pasteHandler)
  103. } else {
  104. mux.Get("/", http.RedirectHandler("/API", 303))
  105. mux.Get("/paste/", http.RedirectHandler("/API/", 303))
  106. }
  107. mux.Get("/paste", http.RedirectHandler("/paste/", 301))
  108. mux.Get("/API/", apiDocHandler)
  109. mux.Get("/API", http.RedirectHandler("/API/", 301))
  110. if Config.remoteUploads {
  111. mux.Get("/upload", uploadRemote)
  112. mux.Get("/upload/", uploadRemote)
  113. if Config.remoteAuthFile != "" {
  114. remoteAuthKeys = readAuthKeys(Config.remoteAuthFile)
  115. }
  116. }
  117. mux.Post("/upload", uploadPostHandler)
  118. mux.Post("/upload/", uploadPostHandler)
  119. mux.Put("/upload", uploadPutHandler)
  120. mux.Put("/upload/", uploadPutHandler)
  121. mux.Put("/upload/:name", uploadPutHandler)
  122. mux.Delete("/:name", deleteHandler)
  123. mux.Get("/static/*", staticHandler)
  124. mux.Get("/favicon.ico", staticHandler)
  125. mux.Get("/robots.txt", staticHandler)
  126. mux.Get(nameRe, fileDisplayHandler)
  127. mux.Get(selifRe, fileServeHandler)
  128. mux.Get(selifIndexRe, unauthorizedHandler)
  129. mux.Get(torrentRe, fileTorrentHandler)
  130. mux.NotFound(notFoundHandler)
  131. return mux
  132. }
  133. func main() {
  134. flag.StringVar(&Config.bind, "bind", "127.0.0.1:8080",
  135. "host to bind to (default: 127.0.0.1:8080)")
  136. flag.StringVar(&Config.filesDir, "filespath", "files/",
  137. "path to files directory")
  138. flag.StringVar(&Config.metaDir, "metapath", "meta/",
  139. "path to metadata directory")
  140. flag.BoolVar(&Config.noLogs, "nologs", false,
  141. "remove stdout output for each request")
  142. flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
  143. "Allow hotlinking of files")
  144. flag.StringVar(&Config.siteName, "sitename", "linx",
  145. "name of the site")
  146. flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
  147. "site base url (including trailing slash)")
  148. flag.Int64Var(&Config.maxSize, "maxsize", 4*1024*1024*1024,
  149. "maximum upload file size in bytes (default 4GB)")
  150. flag.StringVar(&Config.certFile, "certfile", "",
  151. "path to ssl certificate (for https)")
  152. flag.StringVar(&Config.keyFile, "keyfile", "",
  153. "path to ssl key (for https)")
  154. flag.BoolVar(&Config.realIp, "realip", false,
  155. "use X-Real-IP/X-Forwarded-For headers as original host")
  156. flag.BoolVar(&Config.fastcgi, "fastcgi", false,
  157. "serve through fastcgi")
  158. flag.BoolVar(&Config.remoteUploads, "remoteuploads", false,
  159. "enable remote uploads")
  160. flag.StringVar(&Config.authFile, "authfile", "",
  161. "path to a file containing newline-separated scrypted auth keys")
  162. flag.StringVar(&Config.remoteAuthFile, "remoteauthfile", "",
  163. "path to a file containing newline-separated scrypted auth keys for remote uploads")
  164. flag.StringVar(&Config.contentSecurityPolicy, "contentsecuritypolicy",
  165. "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; referrer origin;",
  166. "value of default Content-Security-Policy header")
  167. flag.StringVar(&Config.fileContentSecurityPolicy, "filecontentsecuritypolicy",
  168. "default-src 'none'; img-src 'self'; object-src 'self'; media-src 'self'; sandbox; referrer origin;",
  169. "value of Content-Security-Policy header for file access")
  170. flag.StringVar(&Config.xFrameOptions, "xframeoptions", "SAMEORIGIN",
  171. "value of X-Frame-Options header")
  172. flag.Parse()
  173. mux := setup()
  174. if Config.fastcgi {
  175. listener, err := net.Listen("tcp", Config.bind)
  176. if err != nil {
  177. log.Fatal("Could not bind: ", err)
  178. }
  179. log.Printf("Serving over fastcgi, bound on %s, using siteurl %s", Config.bind, Config.siteURL)
  180. fcgi.Serve(listener, mux)
  181. } else if Config.certFile != "" {
  182. log.Printf("Serving over https, bound on %s, using siteurl %s", Config.bind, Config.siteURL)
  183. err := graceful.ListenAndServeTLS(Config.bind, Config.certFile, Config.keyFile, mux)
  184. if err != nil {
  185. log.Fatal(err)
  186. }
  187. } else {
  188. log.Printf("Serving over http, bound on %s, using siteurl %s", Config.bind, Config.siteURL)
  189. err := graceful.ListenAndServe(Config.bind, mux)
  190. if err != nil {
  191. log.Fatal(err)
  192. }
  193. }
  194. }