Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

167 Zeilen
4.9 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. "github.com/PuerkitoBio/ghost/handlers"
  27. "github.com/gorilla/mux"
  28. "log"
  29. "math/rand"
  30. "net/http"
  31. "net/url"
  32. "os"
  33. "time"
  34. )
  35. const SERVER_INFO = "transfer.sh"
  36. // parse request with maximum memory of _24Kilobits
  37. const _24K = (1 << 20) * 24
  38. var config struct {
  39. AWS_ACCESS_KEY string
  40. AWS_SECRET_KEY string
  41. BUCKET string
  42. VIRUSTOTAL_KEY string
  43. Temp string
  44. }
  45. var storage Storage
  46. func init() {
  47. config.AWS_ACCESS_KEY = os.Getenv("AWS_ACCESS_KEY")
  48. config.AWS_SECRET_KEY = os.Getenv("AWS_SECRET_KEY")
  49. config.BUCKET = os.Getenv("BUCKET")
  50. config.VIRUSTOTAL_KEY = os.Getenv("VIRUSTOTAL_KEY")
  51. config.Temp = ""
  52. }
  53. func main() {
  54. rand.Seed(time.Now().UTC().UnixNano())
  55. r := mux.NewRouter()
  56. r.PathPrefix("/scripts/").Handler(http.FileServer(http.Dir("./static/")))
  57. r.PathPrefix("/styles/").Handler(http.FileServer(http.Dir("./static/")))
  58. r.PathPrefix("/images/").Handler(http.FileServer(http.Dir("./static/")))
  59. r.PathPrefix("/fonts/").Handler(http.FileServer(http.Dir("./static/")))
  60. r.PathPrefix("/ico/").Handler(http.FileServer(http.Dir("./static/")))
  61. r.PathPrefix("/favicon.ico").Handler(http.FileServer(http.Dir("./static/")))
  62. r.PathPrefix("/robots.txt").Handler(http.FileServer(http.Dir("./static/")))
  63. r.HandleFunc("/({files:.*}).zip", zipHandler).Methods("GET")
  64. r.HandleFunc("/({files:.*}).tar", tarHandler).Methods("GET")
  65. r.HandleFunc("/({files:.*}).tar.gz", tarGzHandler).Methods("GET")
  66. r.HandleFunc("/download/{token}/{filename}", getHandler).Methods("GET")
  67. r.HandleFunc("/{token}/{filename}", previewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
  68. if !acceptsHtml(r.Header) {
  69. return false
  70. }
  71. match := (r.Referer() == "")
  72. u, err := url.Parse(r.Referer())
  73. if err != nil {
  74. log.Fatal(err)
  75. return false
  76. }
  77. match = match || (u.Host == "transfer.sh")
  78. match = match || (u.Host == "127.0.0.1")
  79. log.Printf("%s %s match %s", r.Referer(), u.Host, match)
  80. return match
  81. }).Methods("GET")
  82. r.HandleFunc("/{token}/{filename}", getHandler).Methods("GET")
  83. r.HandleFunc("/get/{token}/{filename}", getHandler).Methods("GET")
  84. r.HandleFunc("/{filename}/virustotal", virusTotalHandler).Methods("PUT")
  85. r.HandleFunc("/{filename}/scan", scanHandler).Methods("PUT")
  86. r.HandleFunc("/put/{filename}", putHandler).Methods("PUT")
  87. r.HandleFunc("/upload/{filename}", putHandler).Methods("PUT")
  88. r.HandleFunc("/{filename}", putHandler).Methods("PUT")
  89. r.HandleFunc("/health.html", healthHandler).Methods("GET")
  90. r.HandleFunc("/", postHandler).Methods("POST")
  91. // r.HandleFunc("/{page}", viewHandler).Methods("GET")
  92. r.HandleFunc("/", viewHandler).Methods("GET")
  93. r.NotFoundHandler = http.HandlerFunc(notFoundHandler)
  94. port := flag.String("port", "8080", "port number, default: 8080")
  95. temp := flag.String("temp", "", "")
  96. basedir := flag.String("basedir", "", "")
  97. logpath := flag.String("log", "", "")
  98. provider := flag.String("provider", "s3", "")
  99. flag.Parse()
  100. if *logpath != "" {
  101. f, err := os.OpenFile(*logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  102. if err != nil {
  103. log.Fatalf("error opening file: %v", err)
  104. }
  105. defer f.Close()
  106. log.SetOutput(f)
  107. }
  108. config.Temp = *temp
  109. var err error
  110. switch *provider {
  111. case "s3":
  112. storage, err = NewS3Storage()
  113. case "local":
  114. if *basedir == "" {
  115. log.Panic("basedir not set")
  116. }
  117. storage, err = NewLocalStorage(*basedir)
  118. }
  119. if err != nil {
  120. log.Panic("Error while creating storage.")
  121. }
  122. log.Printf("Transfer.sh server started. :%v using temp folder: %s", *port, config.Temp)
  123. log.Printf("---------------------------")
  124. s := &http.Server{
  125. Addr: fmt.Sprintf(":%s", *port),
  126. Handler: handlers.PanicHandler(LoveHandler(RedirectHandler(handlers.LogHandler(r, handlers.NewLogOptions(log.Printf, "_default_")))), nil),
  127. }
  128. log.Panic(s.ListenAndServe())
  129. log.Printf("Server stopped.")
  130. }