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.
 
 
 

184 lines
5.5 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 DutchCoders [https://github.com/dutchcoders/]
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. package main
  21. import (
  22. // _ "transfer.sh/app/handlers"
  23. // _ "transfer.sh/app/utils"
  24. "flag"
  25. "fmt"
  26. "log"
  27. "math/rand"
  28. "mime"
  29. "net/http"
  30. "net/url"
  31. "os"
  32. "runtime"
  33. "time"
  34. "github.com/PuerkitoBio/ghost/handlers"
  35. "github.com/gorilla/mux"
  36. )
  37. const SERVER_INFO = "transfer.sh"
  38. // parse request with maximum memory of _24Kilobits
  39. const _24K = (1 << 20) * 24
  40. var config struct {
  41. AWS_ACCESS_KEY string
  42. AWS_SECRET_KEY string
  43. BUCKET string
  44. VIRUSTOTAL_KEY string
  45. CLAMAV_DAEMON_HOST string "/tmp/clamd.socket"
  46. Temp string
  47. }
  48. var storage Storage
  49. func init() {
  50. config.AWS_ACCESS_KEY = os.Getenv("AWS_ACCESS_KEY_ID")
  51. config.AWS_SECRET_KEY = os.Getenv("AWS_SECRET_KEY")
  52. config.BUCKET = os.Getenv("BUCKET")
  53. config.VIRUSTOTAL_KEY = os.Getenv("VIRUSTOTAL_KEY")
  54. if os.Getenv("CLAMAV_DAEMON_HOST") != "" {
  55. config.CLAMAV_DAEMON_HOST = os.Getenv("CLAMAV_DAEMON_HOST")
  56. }
  57. config.Temp = os.TempDir()
  58. }
  59. func main() {
  60. rand.Seed(time.Now().UTC().UnixNano())
  61. nCPU := runtime.NumCPU()
  62. runtime.GOMAXPROCS(nCPU)
  63. fmt.Println("Number of CPUs: ", nCPU)
  64. r := mux.NewRouter()
  65. r.PathPrefix("/scripts/").Methods("GET").Handler(http.FileServer(http.Dir("./static/")))
  66. r.PathPrefix("/styles/").Methods("GET").Handler(http.FileServer(http.Dir("./static/")))
  67. r.PathPrefix("/images/").Methods("GET").Handler(http.FileServer(http.Dir("./static/")))
  68. r.PathPrefix("/fonts/").Methods("GET").Handler(http.FileServer(http.Dir("./static/")))
  69. r.PathPrefix("/ico/").Methods("GET").Handler(http.FileServer(http.Dir("./static/")))
  70. r.PathPrefix("/favicon.ico").Methods("GET").Handler(http.FileServer(http.Dir("./static/")))
  71. r.PathPrefix("/robots.txt").Methods("GET").Handler(http.FileServer(http.Dir("./static/")))
  72. r.HandleFunc("/({files:.*}).zip", zipHandler).Methods("GET")
  73. r.HandleFunc("/({files:.*}).tar", tarHandler).Methods("GET")
  74. r.HandleFunc("/({files:.*}).tar.gz", tarGzHandler).Methods("GET")
  75. r.HandleFunc("/download/{token}/{filename}", getHandler).Methods("GET")
  76. r.HandleFunc("/{token}/{filename}", previewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) (match bool) {
  77. match = false
  78. // The file will show a preview page when opening the link in browser directly or
  79. // from external link. If the referer url path and current path are the same it will be
  80. // downloaded.
  81. if !acceptsHtml(r.Header) {
  82. return false
  83. }
  84. match = (r.Referer() == "")
  85. u, err := url.Parse(r.Referer())
  86. if err != nil {
  87. log.Fatal(err)
  88. return
  89. }
  90. match = match || (u.Path != r.URL.Path)
  91. return
  92. }).Methods("GET")
  93. r.HandleFunc("/{token}/{filename}", getHandler).Methods("GET")
  94. r.HandleFunc("/get/{token}/{filename}", getHandler).Methods("GET")
  95. r.HandleFunc("/{filename}/virustotal", virusTotalHandler).Methods("PUT")
  96. r.HandleFunc("/{filename}/scan", scanHandler).Methods("PUT")
  97. r.HandleFunc("/put/{filename}", putHandler).Methods("PUT")
  98. r.HandleFunc("/upload/{filename}", putHandler).Methods("PUT")
  99. r.HandleFunc("/{filename}", putHandler).Methods("PUT")
  100. r.HandleFunc("/health.html", healthHandler).Methods("GET")
  101. r.HandleFunc("/", postHandler).Methods("POST")
  102. // r.HandleFunc("/{page}", viewHandler).Methods("GET")
  103. r.HandleFunc("/", viewHandler).Methods("GET")
  104. r.NotFoundHandler = http.HandlerFunc(notFoundHandler)
  105. port := flag.String("port", "8080", "port number, default: 8080")
  106. temp := flag.String("temp", config.Temp, "")
  107. basedir := flag.String("basedir", "", "")
  108. logpath := flag.String("log", "", "")
  109. provider := flag.String("provider", "s3", "")
  110. flag.Parse()
  111. if *logpath != "" {
  112. f, err := os.OpenFile(*logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  113. if err != nil {
  114. log.Fatalf("error opening file: %v", err)
  115. }
  116. defer f.Close()
  117. log.SetOutput(f)
  118. }
  119. config.Temp = *temp
  120. var err error
  121. switch *provider {
  122. case "s3":
  123. storage, err = NewS3Storage()
  124. case "local":
  125. if *basedir == "" {
  126. log.Panic("basedir not set")
  127. }
  128. storage, err = NewLocalStorage(*basedir)
  129. }
  130. if err != nil {
  131. log.Panic("Error while creating storage.", err)
  132. }
  133. mime.AddExtensionType(".md", "text/x-markdown")
  134. log.Printf("Transfer.sh server started. :\nlistening on port: %v\nusing temp folder: %s\nusing storage provider: %s", *port, config.Temp, *provider)
  135. log.Printf("---------------------------")
  136. s := &http.Server{
  137. Addr: fmt.Sprintf(":%s", *port),
  138. Handler: handlers.PanicHandler(LoveHandler(RedirectHandler(handlers.LogHandler(r, handlers.NewLogOptions(log.Printf, "_default_")))), nil),
  139. }
  140. log.Panic(s.ListenAndServe())
  141. log.Printf("Server stopped.")
  142. }