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.

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