mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-26 03:25:35 +00:00
1db46919ab
Due to a bug in Go (golang/go#51778), cmd/go doesn't warn about your Go version being older than the go.mod's declared Go version in that case that package loading fails before the build starts, such as when you use packages that are only in the current version of Go, like our use of net/netip. This change works around that Go bug by adding build tags and a pre-Go1.18-only file that will cause Go 1.17 and earlier to fail like: $ ~/sdk/go1.17/bin/go install ./cmd/tailscaled # tailscale.com/cmd/tailscaled ./required_version.go:11:2: undefined: you_need_Go_1_18_to_compile_Tailscale note: module requires Go 1.18 Change-Id: I39f5820de646703e19dde448dd86a7022252f75c Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build go1.18
|
|
// +build go1.18
|
|
|
|
// HTTP proxy code
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"strings"
|
|
)
|
|
|
|
// httpProxyHandler returns an HTTP proxy http.Handler using the
|
|
// provided backend dialer.
|
|
func httpProxyHandler(dialer func(ctx context.Context, netw, addr string) (net.Conn, error)) http.Handler {
|
|
rp := &httputil.ReverseProxy{
|
|
Director: func(r *http.Request) {}, // no change
|
|
Transport: &http.Transport{
|
|
DialContext: dialer,
|
|
},
|
|
}
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "CONNECT" {
|
|
backURL := r.RequestURI
|
|
if strings.HasPrefix(backURL, "/") || backURL == "*" {
|
|
http.Error(w, "bogus RequestURI; must be absolute URL or CONNECT", 400)
|
|
return
|
|
}
|
|
rp.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
// CONNECT support:
|
|
|
|
dst := r.RequestURI
|
|
c, err := dialer(r.Context(), "tcp", dst)
|
|
if err != nil {
|
|
w.Header().Set("Tailscale-Connect-Error", err.Error())
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
defer c.Close()
|
|
|
|
cc, ccbuf, err := w.(http.Hijacker).Hijack()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
defer cc.Close()
|
|
|
|
io.WriteString(cc, "HTTP/1.1 200 OK\r\n\r\n")
|
|
|
|
var clientSrc io.Reader = ccbuf
|
|
if ccbuf.Reader.Buffered() == 0 {
|
|
// In the common case (with no
|
|
// buffered data), read directly from
|
|
// the underlying client connection to
|
|
// save some memory, letting the
|
|
// bufio.Reader/Writer get GC'ed.
|
|
clientSrc = cc
|
|
}
|
|
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
_, err := io.Copy(cc, c)
|
|
errc <- err
|
|
}()
|
|
go func() {
|
|
_, err := io.Copy(c, clientSrc)
|
|
errc <- err
|
|
}()
|
|
<-errc
|
|
})
|
|
}
|