|
| 1 | +//go:build !tinygo |
| 2 | + |
| 3 | +package face |
| 4 | + |
| 5 | +import ( |
| 6 | + "crypto/tls" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | + "net/netip" |
| 12 | + "net/url" |
| 13 | + "strconv" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/named-data/ndnd/fw/core" |
| 17 | + "github.com/quic-go/quic-go" |
| 18 | + "github.com/quic-go/quic-go/http3" |
| 19 | + "github.com/quic-go/webtransport-go" |
| 20 | +) |
| 21 | + |
| 22 | +// HTTP3ListenerConfig contains HTTP/3 WebTransport listener configuration. |
| 23 | +type HTTP3ListenerConfig struct { |
| 24 | + Bind string |
| 25 | + Port uint16 |
| 26 | + TLSCert string |
| 27 | + TLSKey string |
| 28 | +} |
| 29 | + |
| 30 | +func (cfg HTTP3ListenerConfig) addr() string { |
| 31 | + return net.JoinHostPort(cfg.Bind, strconv.FormatUint(uint64(cfg.Port), 10)) |
| 32 | +} |
| 33 | + |
| 34 | +func (cfg HTTP3ListenerConfig) URL() *url.URL { |
| 35 | + u := &url.URL{ |
| 36 | + Scheme: "https", |
| 37 | + Host: cfg.addr(), |
| 38 | + } |
| 39 | + return u |
| 40 | +} |
| 41 | + |
| 42 | +func (cfg HTTP3ListenerConfig) String() string { |
| 43 | + return fmt.Sprintf("http3-listener (url=%s)", cfg.URL()) |
| 44 | +} |
| 45 | + |
| 46 | +// HTTP3Listener listens for incoming HTTP/3 WebTransport sessions. |
| 47 | +type HTTP3Listener struct { |
| 48 | + mux *http.ServeMux |
| 49 | + server *webtransport.Server |
| 50 | +} |
| 51 | + |
| 52 | +func NewHTTP3Listener(cfg HTTP3ListenerConfig) (*HTTP3Listener, error) { |
| 53 | + l := &HTTP3Listener{} |
| 54 | + |
| 55 | + cert, e := tls.LoadX509KeyPair(cfg.TLSCert, cfg.TLSKey) |
| 56 | + if e != nil { |
| 57 | + return nil, fmt.Errorf("tls.LoadX509KeyPair(%s %s): %w", cfg.TLSCert, cfg.TLSKey, e) |
| 58 | + } |
| 59 | + |
| 60 | + l.mux = http.NewServeMux() |
| 61 | + l.mux.HandleFunc("/ndn", l.handler) |
| 62 | + |
| 63 | + l.server = &webtransport.Server{ |
| 64 | + H3: http3.Server{ |
| 65 | + Addr: cfg.addr(), |
| 66 | + TLSConfig: &tls.Config{ |
| 67 | + Certificates: []tls.Certificate{cert}, |
| 68 | + MinVersion: tls.VersionTLS12, |
| 69 | + }, |
| 70 | + QUICConfig: &quic.Config{ |
| 71 | + MaxIdleTimeout: 60 * time.Second, |
| 72 | + KeepAlivePeriod: 30 * time.Second, |
| 73 | + DisablePathMTUDiscovery: true, |
| 74 | + }, |
| 75 | + Handler: l.mux, |
| 76 | + }, |
| 77 | + CheckOrigin: func(r *http.Request) bool { |
| 78 | + return true |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + return l, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (l *HTTP3Listener) String() string { |
| 86 | + return "HTTP/3 listener" |
| 87 | +} |
| 88 | + |
| 89 | +func (l *HTTP3Listener) Run() { |
| 90 | + e := l.server.ListenAndServe() |
| 91 | + if !errors.Is(e, http.ErrServerClosed) { |
| 92 | + core.Log.Fatal(l, "Unable to start listener", "err", e) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (l *HTTP3Listener) handler(rw http.ResponseWriter, r *http.Request) { |
| 97 | + c, e := l.server.Upgrade(rw, r) |
| 98 | + if e != nil { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + remote, e := netip.ParseAddrPort(r.RemoteAddr) |
| 103 | + if e != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + local, e := netip.ParseAddrPort(r.Context().Value(http.LocalAddrContextKey).(net.Addr).String()) |
| 107 | + if e != nil { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + newTransport := NewHTTP3Transport(remote, local, c) |
| 112 | + core.Log.Info(l, "Accepting new HTTP/3 WebTransport face", "remote", r.RemoteAddr) |
| 113 | + |
| 114 | + options := MakeNDNLPLinkServiceOptions() |
| 115 | + options.IsFragmentationEnabled = true |
| 116 | + MakeNDNLPLinkService(newTransport, options).Run(nil) |
| 117 | +} |
0 commit comments