main.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package main
  2. import (
  3. "crypto/md5"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "mime/multipart"
  11. "net/http"
  12. "path"
  13. "runtime"
  14. "strings"
  15. "sync"
  16. _ "net/http/pprof"
  17. quic "github.com/lucas-clemente/quic-go"
  18. "github.com/lucas-clemente/quic-go/h2quic"
  19. "github.com/lucas-clemente/quic-go/internal/protocol"
  20. "github.com/lucas-clemente/quic-go/internal/utils"
  21. )
  22. type binds []string
  23. func (b binds) String() string {
  24. return strings.Join(b, ",")
  25. }
  26. func (b *binds) Set(v string) error {
  27. *b = strings.Split(v, ",")
  28. return nil
  29. }
  30. // Size is needed by the /demo/upload handler to determine the size of the uploaded file
  31. type Size interface {
  32. Size() int64
  33. }
  34. func init() {
  35. http.HandleFunc("/demo/tile", func(w http.ResponseWriter, r *http.Request) {
  36. // Small 40x40 png
  37. w.Write([]byte{
  38. 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
  39. 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28,
  40. 0x01, 0x03, 0x00, 0x00, 0x00, 0xb6, 0x30, 0x2a, 0x2e, 0x00, 0x00, 0x00,
  41. 0x03, 0x50, 0x4c, 0x54, 0x45, 0x5a, 0xc3, 0x5a, 0xad, 0x38, 0xaa, 0xdb,
  42. 0x00, 0x00, 0x00, 0x0b, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x63, 0x18,
  43. 0x61, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x01, 0xe2, 0xb8, 0x75, 0x22, 0x00,
  44. 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
  45. })
  46. })
  47. http.HandleFunc("/demo/tiles", func(w http.ResponseWriter, r *http.Request) {
  48. io.WriteString(w, "<html><head><style>img{width:40px;height:40px;}</style></head><body>")
  49. for i := 0; i < 200; i++ {
  50. fmt.Fprintf(w, `<img src="/demo/tile?cachebust=%d">`, i)
  51. }
  52. io.WriteString(w, "</body></html>")
  53. })
  54. http.HandleFunc("/demo/echo", func(w http.ResponseWriter, r *http.Request) {
  55. body, err := ioutil.ReadAll(r.Body)
  56. if err != nil {
  57. fmt.Printf("error reading body while handling /echo: %s\n", err.Error())
  58. }
  59. w.Write(body)
  60. })
  61. // accept file uploads and return the MD5 of the uploaded file
  62. // maximum accepted file size is 1 GB
  63. http.HandleFunc("/demo/upload", func(w http.ResponseWriter, r *http.Request) {
  64. if r.Method == http.MethodPost {
  65. err := r.ParseMultipartForm(1 << 30) // 1 GB
  66. if err == nil {
  67. var file multipart.File
  68. file, _, err = r.FormFile("uploadfile")
  69. if err == nil {
  70. var size int64
  71. if sizeInterface, ok := file.(Size); ok {
  72. size = sizeInterface.Size()
  73. b := make([]byte, size)
  74. file.Read(b)
  75. md5 := md5.Sum(b)
  76. fmt.Fprintf(w, "%x", md5)
  77. return
  78. }
  79. err = errors.New("couldn't get uploaded file size")
  80. }
  81. }
  82. if err != nil {
  83. utils.DefaultLogger.Infof("Error receiving upload: %#v", err)
  84. }
  85. }
  86. io.WriteString(w, `<html><body><form action="/demo/upload" method="post" enctype="multipart/form-data">
  87. <input type="file" name="uploadfile"><br>
  88. <input type="submit">
  89. </form></body></html>`)
  90. })
  91. }
  92. func getBuildDir() string {
  93. _, filename, _, ok := runtime.Caller(0)
  94. if !ok {
  95. panic("Failed to get current frame")
  96. }
  97. return path.Dir(filename)
  98. }
  99. func main() {
  100. // defer profile.Start().Stop()
  101. go func() {
  102. log.Println(http.ListenAndServe("localhost:6060", nil))
  103. }()
  104. // runtime.SetBlockProfileRate(1)
  105. verbose := flag.Bool("v", false, "verbose")
  106. bs := binds{}
  107. flag.Var(&bs, "bind", "bind to")
  108. certPath := flag.String("certpath", getBuildDir(), "certificate directory")
  109. www := flag.String("www", "/var/www", "www data")
  110. tcp := flag.Bool("tcp", false, "also listen on TCP")
  111. tls := flag.Bool("tls", false, "activate support for IETF QUIC (work in progress)")
  112. flag.Parse()
  113. logger := utils.DefaultLogger
  114. if *verbose {
  115. logger.SetLogLevel(utils.LogLevelDebug)
  116. } else {
  117. logger.SetLogLevel(utils.LogLevelInfo)
  118. }
  119. logger.SetLogTimeFormat("")
  120. versions := protocol.SupportedVersions
  121. if *tls {
  122. versions = append([]protocol.VersionNumber{protocol.VersionTLS}, versions...)
  123. }
  124. certFile := *certPath + "/fullchain.pem"
  125. keyFile := *certPath + "/privkey.pem"
  126. http.Handle("/", http.FileServer(http.Dir(*www)))
  127. if len(bs) == 0 {
  128. bs = binds{"localhost:6121"}
  129. }
  130. var wg sync.WaitGroup
  131. wg.Add(len(bs))
  132. for _, b := range bs {
  133. bCap := b
  134. go func() {
  135. var err error
  136. if *tcp {
  137. err = h2quic.ListenAndServe(bCap, certFile, keyFile, nil)
  138. } else {
  139. server := h2quic.Server{
  140. Server: &http.Server{Addr: bCap},
  141. QuicConfig: &quic.Config{Versions: versions},
  142. }
  143. err = server.ListenAndServeTLS(certFile, keyFile)
  144. }
  145. if err != nil {
  146. fmt.Println(err)
  147. }
  148. wg.Done()
  149. }()
  150. }
  151. wg.Wait()
  152. }