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.
 
 
 

437 lines
10 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2017 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 server
  21. import (
  22. "errors"
  23. "log"
  24. "math/rand"
  25. "mime"
  26. "net/http"
  27. "net/url"
  28. "os"
  29. "os/signal"
  30. "strings"
  31. "sync"
  32. "syscall"
  33. "time"
  34. context "golang.org/x/net/context"
  35. "github.com/PuerkitoBio/ghost/handlers"
  36. "github.com/VojtechVitek/ratelimit"
  37. "github.com/VojtechVitek/ratelimit/memory"
  38. "github.com/gorilla/mux"
  39. _ "net/http/pprof"
  40. "crypto/tls"
  41. web "github.com/dutchcoders/transfer.sh-web"
  42. assetfs "github.com/elazarl/go-bindata-assetfs"
  43. autocert "golang.org/x/crypto/acme/autocert"
  44. "path/filepath"
  45. )
  46. const SERVER_INFO = "transfer.sh"
  47. // parse request with maximum memory of _24Kilobits
  48. const _24K = (1 << 3) * 24
  49. // parse request with maximum memory of _5Megabytes
  50. const _5M = (1 << 20) * 5
  51. type OptionFn func(*Server)
  52. func ClamavHost(s string) OptionFn {
  53. return func(srvr *Server) {
  54. srvr.ClamAVDaemonHost = s
  55. }
  56. }
  57. func VirustotalKey(s string) OptionFn {
  58. return func(srvr *Server) {
  59. srvr.VirusTotalKey = s
  60. }
  61. }
  62. func Listener(s string) OptionFn {
  63. return func(srvr *Server) {
  64. srvr.ListenerString = s
  65. }
  66. }
  67. func GoogleAnalytics(gaKey string) OptionFn {
  68. return func(srvr *Server) {
  69. srvr.gaKey = gaKey
  70. }
  71. }
  72. func UserVoice(userVoiceKey string) OptionFn {
  73. return func(srvr *Server) {
  74. srvr.userVoiceKey = userVoiceKey
  75. }
  76. }
  77. func TLSListener(s string, t bool) OptionFn {
  78. return func(srvr *Server) {
  79. srvr.TLSListenerString = s
  80. srvr.TLSListenerOnly = t
  81. }
  82. }
  83. func ProfileListener(s string) OptionFn {
  84. return func(srvr *Server) {
  85. srvr.ProfileListenerString = s
  86. }
  87. }
  88. func WebPath(s string) OptionFn {
  89. return func(srvr *Server) {
  90. if s[len(s)-1:] != "/" {
  91. s = s + string(filepath.Separator)
  92. }
  93. srvr.webPath = s
  94. }
  95. }
  96. func TempPath(s string) OptionFn {
  97. return func(srvr *Server) {
  98. if s[len(s)-1:] != "/" {
  99. s = s + string(filepath.Separator)
  100. }
  101. srvr.tempPath = s
  102. }
  103. }
  104. func LogFile(logger *log.Logger, s string) OptionFn {
  105. return func(srvr *Server) {
  106. f, err := os.OpenFile(s, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  107. if err != nil {
  108. log.Fatalf("error opening file: %v", err)
  109. }
  110. logger.SetOutput(f)
  111. srvr.logger = logger
  112. }
  113. }
  114. func Logger(logger *log.Logger) OptionFn {
  115. return func(srvr *Server) {
  116. srvr.logger = logger
  117. }
  118. }
  119. func RateLimit(requests int) OptionFn {
  120. return func(srvr *Server) {
  121. srvr.rateLimitRequests = requests
  122. }
  123. }
  124. func ForceHTTPs() OptionFn {
  125. return func(srvr *Server) {
  126. srvr.forceHTTPs = true
  127. }
  128. }
  129. func EnableProfiler() OptionFn {
  130. return func(srvr *Server) {
  131. srvr.profilerEnabled = true
  132. }
  133. }
  134. func UseStorage(s Storage) OptionFn {
  135. return func(srvr *Server) {
  136. srvr.storage = s
  137. }
  138. }
  139. func UseLetsEncrypt(hosts []string) OptionFn {
  140. return func(srvr *Server) {
  141. cacheDir := "./cache/"
  142. m := autocert.Manager{
  143. Prompt: autocert.AcceptTOS,
  144. Cache: autocert.DirCache(cacheDir),
  145. HostPolicy: func(_ context.Context, host string) error {
  146. found := false
  147. for _, h := range hosts {
  148. found = found || strings.HasSuffix(host, h)
  149. }
  150. if !found {
  151. return errors.New("acme/autocert: host not configured")
  152. }
  153. return nil
  154. },
  155. }
  156. srvr.tlsConfig = &tls.Config{
  157. GetCertificate: m.GetCertificate,
  158. }
  159. }
  160. }
  161. func TLSConfig(cert, pk string) OptionFn {
  162. certificate, err := tls.LoadX509KeyPair(cert, pk)
  163. return func(srvr *Server) {
  164. srvr.tlsConfig = &tls.Config{
  165. GetCertificate: func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
  166. return &certificate, err
  167. },
  168. }
  169. }
  170. }
  171. func HttpAuthCredentials(user string, pass string) OptionFn {
  172. return func(srvr *Server) {
  173. srvr.AuthUser = user
  174. srvr.AuthPass = pass
  175. }
  176. }
  177. type Server struct {
  178. AuthUser string
  179. AuthPass string
  180. logger *log.Logger
  181. tlsConfig *tls.Config
  182. profilerEnabled bool
  183. locks map[string]*sync.Mutex
  184. rateLimitRequests int
  185. storage Storage
  186. forceHTTPs bool
  187. VirusTotalKey string
  188. ClamAVDaemonHost string
  189. tempPath string
  190. webPath string
  191. gaKey string
  192. userVoiceKey string
  193. TLSListenerOnly bool
  194. ListenerString string
  195. TLSListenerString string
  196. ProfileListenerString string
  197. Certificate string
  198. LetsEncryptCache string
  199. }
  200. func New(options ...OptionFn) (*Server, error) {
  201. s := &Server{
  202. locks: map[string]*sync.Mutex{},
  203. }
  204. for _, optionFn := range options {
  205. optionFn(s)
  206. }
  207. return s, nil
  208. }
  209. func init() {
  210. rand.Seed(time.Now().UTC().UnixNano())
  211. }
  212. func (s *Server) Run() {
  213. listening := false
  214. if s.profilerEnabled {
  215. listening = true
  216. go func() {
  217. s.logger.Println("Profiled listening at: :6060")
  218. http.ListenAndServe(":6060", nil)
  219. }()
  220. }
  221. r := mux.NewRouter()
  222. var fs http.FileSystem
  223. if s.webPath != "" {
  224. s.logger.Println("Using static file path: ", s.webPath)
  225. fs = http.Dir(s.webPath)
  226. htmlTemplates, _ = htmlTemplates.ParseGlob(s.webPath + "*.html")
  227. textTemplates, _ = textTemplates.ParseGlob(s.webPath + "*.txt")
  228. } else {
  229. fs = &assetfs.AssetFS{
  230. Asset: web.Asset,
  231. AssetDir: web.AssetDir,
  232. AssetInfo: func(path string) (os.FileInfo, error) {
  233. return os.Stat(path)
  234. },
  235. Prefix: web.Prefix,
  236. }
  237. for _, path := range web.AssetNames() {
  238. bytes, err := web.Asset(path)
  239. if err != nil {
  240. s.logger.Panicf("Unable to parse: path=%s, err=%s", path, err)
  241. }
  242. htmlTemplates.New(stripPrefix(path)).Parse(string(bytes))
  243. textTemplates.New(stripPrefix(path)).Parse(string(bytes))
  244. }
  245. }
  246. staticHandler := http.FileServer(fs)
  247. r.PathPrefix("/images/").Handler(staticHandler).Methods("GET")
  248. r.PathPrefix("/styles/").Handler(staticHandler).Methods("GET")
  249. r.PathPrefix("/scripts/").Handler(staticHandler).Methods("GET")
  250. r.PathPrefix("/fonts/").Handler(staticHandler).Methods("GET")
  251. r.PathPrefix("/ico/").Handler(staticHandler).Methods("GET")
  252. r.HandleFunc("/favicon.ico", staticHandler.ServeHTTP).Methods("GET")
  253. r.HandleFunc("/robots.txt", staticHandler.ServeHTTP).Methods("GET")
  254. r.HandleFunc("/{filename:(?:favicon\\.ico|robots\\.txt|health\\.html)}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
  255. r.HandleFunc("/health.html", healthHandler).Methods("GET")
  256. r.HandleFunc("/", s.viewHandler).Methods("GET")
  257. r.HandleFunc("/({files:.*}).zip", s.zipHandler).Methods("GET")
  258. r.HandleFunc("/({files:.*}).tar", s.tarHandler).Methods("GET")
  259. r.HandleFunc("/({files:.*}).tar.gz", s.tarGzHandler).Methods("GET")
  260. r.HandleFunc("/{token}/{filename}", s.headHandler).Methods("HEAD")
  261. r.HandleFunc("/{action:(?:download|get|inline)}/{token}/{filename}", s.headHandler).Methods("HEAD")
  262. r.HandleFunc("/{token}/{filename}", s.previewHandler).MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) (match bool) {
  263. return false
  264. match = false
  265. // The file will show a preview page when opening the link in browser directly or
  266. // from external link. If the referer url path and current path are the same it will be
  267. // downloaded.
  268. if !acceptsHTML(r.Header) {
  269. return false
  270. }
  271. match = (r.Referer() == "")
  272. u, err := url.Parse(r.Referer())
  273. if err != nil {
  274. s.logger.Fatal(err)
  275. return
  276. }
  277. match = match || (u.Path != r.URL.Path)
  278. return
  279. }).Methods("GET")
  280. getHandlerFn := s.getHandler
  281. if s.rateLimitRequests > 0 {
  282. getHandlerFn = ratelimit.Request(ratelimit.IP).Rate(s.rateLimitRequests, 60*time.Second).LimitBy(memory.New())(http.HandlerFunc(getHandlerFn)).ServeHTTP
  283. }
  284. r.HandleFunc("/{token}/{filename}", getHandlerFn).Methods("GET")
  285. r.HandleFunc("/{action:(?:download|get|inline)}/{token}/{filename}", getHandlerFn).Methods("GET")
  286. r.HandleFunc("/{filename}/virustotal", s.virusTotalHandler).Methods("PUT")
  287. r.HandleFunc("/{filename}/scan", s.scanHandler).Methods("PUT")
  288. r.HandleFunc("/put/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
  289. r.HandleFunc("/upload/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
  290. r.HandleFunc("/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
  291. r.HandleFunc("/", s.BasicAuthHandler(http.HandlerFunc(s.postHandler))).Methods("POST")
  292. // r.HandleFunc("/{page}", viewHandler).Methods("GET")
  293. r.HandleFunc("/{token}/{filename}/{deletionToken}", s.deleteHandler).Methods("DELETE")
  294. r.NotFoundHandler = http.HandlerFunc(s.notFoundHandler)
  295. mime.AddExtensionType(".md", "text/x-markdown")
  296. s.logger.Printf("Transfer.sh server started.\nusing temp folder: %s\nusing storage provider: %s", s.tempPath, s.storage.Type())
  297. h := handlers.PanicHandler(handlers.LogHandler(LoveHandler(s.RedirectHandler(r)), handlers.NewLogOptions(s.logger.Printf, "_default_")), nil)
  298. if !s.TLSListenerOnly {
  299. srvr := &http.Server{
  300. Addr: s.ListenerString,
  301. Handler: h,
  302. }
  303. listening = true
  304. s.logger.Printf("listening on port: %v\n", s.ListenerString)
  305. go func() {
  306. srvr.ListenAndServe()
  307. }()
  308. }
  309. if s.TLSListenerString != "" {
  310. listening = true
  311. s.logger.Printf("listening on port: %v\n", s.TLSListenerString)
  312. go func() {
  313. s := &http.Server{
  314. Addr: s.TLSListenerString,
  315. Handler: h,
  316. TLSConfig: s.tlsConfig,
  317. }
  318. if err := s.ListenAndServeTLS("", ""); err != nil {
  319. panic(err)
  320. }
  321. }()
  322. }
  323. s.logger.Printf("---------------------------")
  324. term := make(chan os.Signal, 1)
  325. signal.Notify(term, os.Interrupt)
  326. signal.Notify(term, syscall.SIGTERM)
  327. if listening {
  328. <-term
  329. } else {
  330. s.logger.Printf("No listener active.")
  331. }
  332. s.logger.Printf("Server stopped.")
  333. }