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.
 
 
 

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