2020-06-29 21:13:52 +02:00
|
|
|
package http3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
2020-07-01 20:10:32 +02:00
|
|
|
"fmt"
|
2020-06-29 21:13:52 +02:00
|
|
|
"math/big"
|
|
|
|
"net/http"
|
2021-01-09 05:08:20 +01:00
|
|
|
"strconv"
|
2021-04-06 20:28:29 +02:00
|
|
|
"sync"
|
2020-07-01 00:14:51 +02:00
|
|
|
"time"
|
2020-06-29 21:13:52 +02:00
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/internal/metrics"
|
|
|
|
"github.com/lbryio/reflector.go/store"
|
2020-06-30 01:14:52 +02:00
|
|
|
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
2020-09-01 20:15:34 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/stop"
|
2020-06-30 01:14:52 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2020-06-29 21:13:52 +02:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"github.com/lucas-clemente/quic-go/http3"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server is an instance of a peer server that houses the listener and store.
|
|
|
|
type Server struct {
|
|
|
|
store store.BlobStore
|
2020-06-30 01:14:52 +02:00
|
|
|
grp *stop.Group
|
2020-06-29 21:13:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns an initialized Server pointer.
|
2021-05-20 23:17:18 +02:00
|
|
|
func NewServer(store store.BlobStore, requestQueueSize int) *Server {
|
2020-06-29 21:13:52 +02:00
|
|
|
return &Server{
|
|
|
|
store: store,
|
|
|
|
grp: stop.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown gracefully shuts down the peer server.
|
|
|
|
func (s *Server) Shutdown() {
|
2020-07-01 20:42:23 +02:00
|
|
|
log.Debug("shutting down http3 peer server")
|
2020-06-29 21:13:52 +02:00
|
|
|
s.grp.StopAndWait()
|
2020-07-01 20:42:23 +02:00
|
|
|
log.Debug("http3 peer server stopped")
|
2020-06-29 21:13:52 +02:00
|
|
|
}
|
2020-06-30 01:14:52 +02:00
|
|
|
|
2020-06-29 21:13:52 +02:00
|
|
|
func (s *Server) logError(e error) {
|
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shouldLog := metrics.TrackError(metrics.DirectionDownload, e)
|
|
|
|
if shouldLog {
|
|
|
|
log.Errorln(errors.FullTrace(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type availabilityResponse struct {
|
|
|
|
LbrycrdAddress string `json:"lbrycrd_address"`
|
|
|
|
IsAvailable bool `json:"is_available"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the server listener to handle connections.
|
|
|
|
func (s *Server) Start(address string) error {
|
|
|
|
log.Println("HTTP3 peer listening on " + address)
|
2021-05-06 22:53:18 +02:00
|
|
|
window500M := 500 * 1 << 20
|
|
|
|
|
2020-07-10 15:19:57 +02:00
|
|
|
quicConf := &quic.Config{
|
2021-05-06 22:53:18 +02:00
|
|
|
MaxStreamReceiveWindow: uint64(window500M),
|
|
|
|
MaxConnectionReceiveWindow: uint64(window500M),
|
|
|
|
EnableDatagrams: true,
|
|
|
|
HandshakeIdleTimeout: 4 * time.Second,
|
|
|
|
MaxIdleTimeout: 20 * time.Second,
|
2020-07-10 15:19:57 +02:00
|
|
|
}
|
2020-06-29 21:13:52 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/get/{hash}", func(w http.ResponseWriter, r *http.Request) {
|
2021-04-06 20:28:29 +02:00
|
|
|
waiter := &sync.WaitGroup{}
|
|
|
|
waiter.Add(1)
|
2021-04-06 20:21:05 +02:00
|
|
|
enqueue(&blobRequest{request: r, reply: w, finished: waiter})
|
2021-04-06 20:28:29 +02:00
|
|
|
waiter.Wait()
|
2020-06-29 21:13:52 +02:00
|
|
|
})
|
|
|
|
r.HandleFunc("/has/{hash}", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
requestedBlob := vars["hash"]
|
|
|
|
blobExists, err := s.store.Has(requestedBlob)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
s.logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !blobExists {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
// LbrycrdAddress to be used when paying for data. Not implemented yet.
|
|
|
|
const LbrycrdAddress = "bJxKvpD96kaJLriqVajZ7SaQTsWWyrGQct"
|
|
|
|
resp, err := json.Marshal(availabilityResponse{
|
|
|
|
LbrycrdAddress: LbrycrdAddress,
|
|
|
|
IsAvailable: blobExists,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
s.logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = w.Write(resp)
|
|
|
|
if err != nil {
|
|
|
|
s.logError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
server := http3.Server{
|
|
|
|
Server: &http.Server{
|
|
|
|
Handler: r,
|
|
|
|
Addr: address,
|
|
|
|
TLSConfig: generateTLSConfig(),
|
|
|
|
},
|
|
|
|
QuicConfig: quicConf,
|
|
|
|
}
|
2021-04-12 23:05:50 +02:00
|
|
|
go InitWorkers(s, 200)
|
2020-06-29 21:13:52 +02:00
|
|
|
go s.listenForShutdown(&server)
|
|
|
|
s.grp.Add(1)
|
|
|
|
go func() {
|
|
|
|
s.listenAndServe(&server)
|
|
|
|
s.grp.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a bare-bones TLS config for the server
|
|
|
|
func generateTLSConfig() *tls.Config {
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
template := x509.Certificate{SerialNumber: big.NewInt(1)}
|
|
|
|
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
|
|
|
|
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
|
|
|
|
|
|
|
|
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{tlsCert},
|
2020-06-30 01:14:52 +02:00
|
|
|
NextProtos: []string{"http3-reflector-server"},
|
2020-06-29 21:13:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) listenAndServe(server *http3.Server) {
|
|
|
|
err := server.ListenAndServe()
|
2020-10-28 20:15:35 +01:00
|
|
|
if err != nil && err.Error() != "server closed" {
|
2020-06-29 21:13:52 +02:00
|
|
|
log.Errorln(errors.FullTrace(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) listenForShutdown(listener *http3.Server) {
|
|
|
|
<-s.grp.Ch()
|
|
|
|
err := listener.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error closing listener for peer server - ", err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 20:00:36 +02:00
|
|
|
|
|
|
|
func (s *Server) HandleGetBlob(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
requestedBlob := vars["hash"]
|
|
|
|
traceParam := r.URL.Query().Get("trace")
|
|
|
|
var err error
|
|
|
|
wantsTrace := false
|
|
|
|
if traceParam != "" {
|
|
|
|
wantsTrace, err = strconv.ParseBool(traceParam)
|
|
|
|
if err != nil {
|
|
|
|
wantsTrace = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blob, trace, err := s.store.Get(requestedBlob)
|
|
|
|
|
|
|
|
if wantsTrace {
|
|
|
|
serialized, err := trace.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Add("Via", serialized)
|
|
|
|
log.Debug(trace.String())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, store.ErrBlobNotFound) {
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("%s: %s", requestedBlob, errors.FullTrace(err))
|
|
|
|
s.logError(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(blob)
|
|
|
|
if err != nil {
|
|
|
|
s.logError(err)
|
|
|
|
}
|
|
|
|
metrics.MtrOutBytesUdp.Add(float64(len(blob)))
|
|
|
|
metrics.BlobDownloadCount.Inc()
|
|
|
|
metrics.Http3DownloadCount.Inc()
|
|
|
|
}
|