|
|
@ -24,6 +24,7 @@ import (
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"net/http/httputil"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -42,6 +43,11 @@ type handler struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
|
|
|
req, _ := httputil.DumpRequest(r, false)
|
|
|
|
|
|
|
|
log.Printf("%s", string(req))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if r.Method == http.MethodConnect {
|
|
|
|
if r.Method == http.MethodConnect {
|
|
|
|
h.handleConnect(w, r)
|
|
|
|
h.handleConnect(w, r)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -70,6 +76,11 @@ func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
|
|
|
d, _ := httputil.DumpResponse(resp, false)
|
|
|
|
|
|
|
|
log.Printf("%s", string(d))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -110,7 +121,10 @@ func (h *handler) handleHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
resp.Header.Del("connection")
|
|
|
|
if debug {
|
|
|
|
|
|
|
|
d, _ := httputil.DumpResponse(resp, false)
|
|
|
|
|
|
|
|
log.Printf("%s", string(d))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hdr := w.Header()
|
|
|
|
hdr := w.Header()
|
|
|
|
for k, v := range resp.Header {
|
|
|
|
for k, v := range resp.Header {
|
|
|
@ -136,37 +150,51 @@ func newClientConn(host string, port string, hostname string, t *http2.Transport
|
|
|
|
func (p *clientConn) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) {
|
|
|
|
func (p *clientConn) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) {
|
|
|
|
p.lock.Lock()
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
if p.conn != nil {
|
|
|
|
if p.conn != nil {
|
|
|
|
return p.conn, nil
|
|
|
|
return p.conn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
config := &tls.Config{
|
|
|
|
config := &tls.Config{
|
|
|
|
ServerName: p.hostname,
|
|
|
|
ServerName: p.hostname,
|
|
|
|
NextProtos: []string{"h2"},
|
|
|
|
NextProtos: []string{"h2"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
|
|
|
log.Printf("dial to %s:%s", p.host, p.port)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
conn, err := tls.Dial("tcp", net.JoinHostPort(p.host, p.port), config)
|
|
|
|
conn, err := tls.Dial("tcp", net.JoinHostPort(p.host, p.port), config)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
log.Println(err)
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
http2conn, err := p.transport.NewClientConn(conn)
|
|
|
|
http2conn, err := p.transport.NewClientConn(conn)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
conn.Close()
|
|
|
|
log.Println(err)
|
|
|
|
log.Println(err)
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.conn = http2conn
|
|
|
|
p.conn = http2conn
|
|
|
|
|
|
|
|
|
|
|
|
return http2conn, err
|
|
|
|
return http2conn, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *clientConn) MarkDead(conn *http2.ClientConn) {
|
|
|
|
func (p *clientConn) MarkDead(conn *http2.ClientConn) {
|
|
|
|
p.lock.Lock()
|
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
log.Println("mark dead")
|
|
|
|
log.Println("mark dead")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.conn = nil
|
|
|
|
p.conn = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var debug bool
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
var addr string
|
|
|
|
var addr string
|
|
|
|
var hostname string
|
|
|
|
var hostname string
|
|
|
@ -174,6 +202,7 @@ func main() {
|
|
|
|
flag.StringVar(&addr, "server", "", "server address")
|
|
|
|
flag.StringVar(&addr, "server", "", "server address")
|
|
|
|
flag.StringVar(&hostname, "name", "", "server 's SNI name")
|
|
|
|
flag.StringVar(&hostname, "name", "", "server 's SNI name")
|
|
|
|
flag.StringVar(&listen, "listen", ":8080", "listen address")
|
|
|
|
flag.StringVar(&listen, "listen", ":8080", "listen address")
|
|
|
|
|
|
|
|
flag.BoolVar(&debug, "debug", false, "verbose mode")
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
if addr == "" {
|
|
|
|
if addr == "" {
|
|
|
@ -199,8 +228,11 @@ func main() {
|
|
|
|
transport.ConnPool = p
|
|
|
|
transport.ConnPool = p
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("listen on %s", listen)
|
|
|
|
log.Printf("listen on %s", listen)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
log.Printf("use parent proxy https://%s:%s/", host, port)
|
|
|
|
log.Printf("use parent proxy https://%s:%s/", host, port)
|
|
|
|
log.Printf("server SNI name %s", hostname)
|
|
|
|
log.Printf("server SNI name %s", hostname)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(listen, &handler{transport}); err != nil {
|
|
|
|
if err := http.ListenAndServe(listen, &handler{transport}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Fatal(err)
|
|
|
|