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.

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