1
// Copyright Istio Authors
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
24
"golang.org/x/net/http2"
26
"istio.io/istio/pkg/h2c"
29
func NewServer() *http.Server {
30
// Need to set this to allow timeout on the read header
31
h1 := &http.Transport{
32
ExpectContinueTimeout: 3 * time.Second,
34
h2, _ := http2.ConfigureTransports(h1)
35
h2.ReadIdleTimeout = 10 * time.Minute // TODO: much larger to support long-lived connections
37
h2Server := &http2.Server{}
38
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39
if r.Method == http.MethodConnect {
40
if handleConnect(w, r) {
44
log.Errorf("non-CONNECT: %v", r.Method)
45
w.WriteHeader(http.StatusMethodNotAllowed)
49
Handler: h2c.NewHandler(handler, h2Server),
54
func handleConnect(w http.ResponseWriter, r *http.Request) bool {
56
log.WithLabels("host", r.Host, "source", r.RemoteAddr).Info("Received CONNECT")
57
// Send headers back immediately so we can start getting the body
58
w.(http.Flusher).Flush()
59
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
62
dst, err := (&net.Dialer{}).DialContext(ctx, "tcp", r.Host)
64
w.WriteHeader(http.StatusServiceUnavailable)
65
log.Errorf("failed to dial upstream: %v", err)
68
log.Infof("Connected to %v", r.Host)
69
w.WriteHeader(http.StatusOK)
71
wg := sync.WaitGroup{}
74
// downstream (hbone client) <-- upstream (app)
75
copyBuffered(w, dst, log.WithLabels("name", "dst to w"))
78
log.Infof("connection to hbone client is not closed: %v", err)
82
// downstream (hbone client) --> upstream (app)
83
copyBuffered(dst, r.Body, log.WithLabels("name", "body to dst"))
85
log.Infof("connection closed in %v", time.Since(t0))