|
|
|
@ -15,21 +15,30 @@ usage example
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
_ "net/http/pprof"
|
|
|
|
|
"net/http/httputil"
|
|
|
|
|
"os"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/fangdingjun/gnutls"
|
|
|
|
|
"github.com/fangdingjun/nghttp2-go"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type clientConn struct {
|
|
|
|
|
host string
|
|
|
|
|
port string
|
|
|
|
|
hostname string
|
|
|
|
|
transport *http2.Transport
|
|
|
|
|
conn *http2.ClientConn
|
|
|
|
|
lock *sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type timeoutConn struct {
|
|
|
|
|
net.Conn
|
|
|
|
|
timeout time.Duration
|
|
|
|
@ -39,91 +48,25 @@ func (tc *timeoutConn) Read(b []byte) (n int, err error) {
|
|
|
|
|
if err = tc.Conn.SetReadDeadline(time.Now().Add(tc.timeout)); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
n, err = tc.Conn.Read(b)
|
|
|
|
|
//log.Printf("read %d bytes from network", n)
|
|
|
|
|
return
|
|
|
|
|
return tc.Conn.Read(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tc *timeoutConn) Write(b []byte) (n int, err error) {
|
|
|
|
|
if err = tc.Conn.SetWriteDeadline(time.Now().Add(tc.timeout)); err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
n, err = tc.Conn.Write(b)
|
|
|
|
|
//log.Printf("write %d bytes to network", n)
|
|
|
|
|
return
|
|
|
|
|
return tc.Conn.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type handler struct {
|
|
|
|
|
h2conn *nghttp2.Conn
|
|
|
|
|
addr string
|
|
|
|
|
hostname string
|
|
|
|
|
insecure bool
|
|
|
|
|
lock *sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *handler) createConnection() (*nghttp2.Conn, error) {
|
|
|
|
|
log.Println("create connection to ", h.addr)
|
|
|
|
|
c, err := net.DialTimeout("tcp", h.addr, 5*time.Second)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
conn, err := gnutls.Client(
|
|
|
|
|
&timeoutConn{c, 20 * time.Second},
|
|
|
|
|
&gnutls.Config{
|
|
|
|
|
ServerName: h.hostname,
|
|
|
|
|
InsecureSkipVerify: h.insecure,
|
|
|
|
|
NextProtos: []string{"h2"},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := conn.Handshake(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
client, err := nghttp2.Client(conn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *handler) getConn() (*nghttp2.Conn, error) {
|
|
|
|
|
h.lock.Lock()
|
|
|
|
|
defer h.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
if h.h2conn != nil {
|
|
|
|
|
if h.h2conn.CanTakeNewRequest() {
|
|
|
|
|
return h.h2conn, nil
|
|
|
|
|
}
|
|
|
|
|
h.h2conn.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
|
h2conn, err := h.createConnection()
|
|
|
|
|
if err == nil {
|
|
|
|
|
h.h2conn = h2conn
|
|
|
|
|
return h2conn, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("create conn failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *handler) checkError() {
|
|
|
|
|
h.lock.Lock()
|
|
|
|
|
defer h.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
if h.h2conn == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := h.h2conn.Error(); err != nil {
|
|
|
|
|
//log.Println("connection has error ", err)
|
|
|
|
|
h.h2conn.Close()
|
|
|
|
|
h.h2conn = nil
|
|
|
|
|
}
|
|
|
|
|
transport *http2.Transport
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
h.handleConnect(w, r)
|
|
|
|
@ -133,38 +76,33 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var err error
|
|
|
|
|
var h2conn *nghttp2.Conn
|
|
|
|
|
var code int
|
|
|
|
|
//var resp *http.Response
|
|
|
|
|
|
|
|
|
|
var cs net.Conn
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
|
h2conn, err = h.getConn()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("connection error ", err)
|
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cs, code, err = h2conn.Connect(r.RequestURI)
|
|
|
|
|
if cs != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
h.checkError()
|
|
|
|
|
}
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
|
|
|
|
|
|
defer pr.Close()
|
|
|
|
|
defer pw.Close()
|
|
|
|
|
|
|
|
|
|
r.Body = ioutil.NopCloser(pr)
|
|
|
|
|
r.URL.Scheme = "https"
|
|
|
|
|
|
|
|
|
|
if err != nil || cs == nil {
|
|
|
|
|
log.Println("send connect error ", err)
|
|
|
|
|
h.checkError()
|
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
|
|
|
r.Header.Del("proxy-connection")
|
|
|
|
|
|
|
|
|
|
resp, err := h.transport.RoundTrip(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("roundtrip: %s", err)
|
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
|
fmt.Fprintf(w, "%s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer cs.Close()
|
|
|
|
|
if code != http.StatusOK {
|
|
|
|
|
log.Println("code", code)
|
|
|
|
|
w.WriteHeader(code)
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
d, _ := httputil.DumpResponse(resp, false)
|
|
|
|
|
log.Printf("%s", string(d))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -182,12 +120,12 @@ func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ch := make(chan struct{}, 2)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
io.Copy(cs, c)
|
|
|
|
|
io.Copy(pw, c)
|
|
|
|
|
ch <- struct{}{}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
io.Copy(c, cs)
|
|
|
|
|
io.Copy(c, resp.Body)
|
|
|
|
|
ch <- struct{}{}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
@ -195,42 +133,19 @@ func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *handler) handleHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var err error
|
|
|
|
|
var resp *http.Response
|
|
|
|
|
var h2conn *nghttp2.Conn
|
|
|
|
|
|
|
|
|
|
if r.RequestURI[0] == '/' {
|
|
|
|
|
http.DefaultServeMux.ServeHTTP(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
|
h2conn, err = h.getConn()
|
|
|
|
|
if err != nil {
|
|
|
|
|
//log.Println("create connection ", err)
|
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp, err = h2conn.RoundTrip(r)
|
|
|
|
|
if resp != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
h.checkError()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil || resp == nil {
|
|
|
|
|
log.Println("create request error ", err)
|
|
|
|
|
h.checkError()
|
|
|
|
|
resp, err := h.transport.RoundTrip(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
|
fmt.Fprintf(w, "%s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if resp.Body != nil {
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if debug {
|
|
|
|
|
d, _ := httputil.DumpResponse(resp, false)
|
|
|
|
|
log.Printf("%s", string(d))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hdr := w.Header()
|
|
|
|
|
for k, v := range resp.Header {
|
|
|
|
@ -243,7 +158,73 @@ func (h *handler) handleHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newClientConn(host string, port string, hostname string, t *http2.Transport) *clientConn {
|
|
|
|
|
return &clientConn{
|
|
|
|
|
host: host,
|
|
|
|
|
port: port,
|
|
|
|
|
hostname: hostname,
|
|
|
|
|
transport: t,
|
|
|
|
|
lock: new(sync.Mutex),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *clientConn) GetClientConn(req *http.Request, addr string) (*http2.ClientConn, error) {
|
|
|
|
|
p.lock.Lock()
|
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
if p.conn != nil && p.conn.CanTakeNewRequest() {
|
|
|
|
|
return p.conn, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
log.Printf("dial to %s:%s", p.host, p.port)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c, err := net.Dial("tcp", net.JoinHostPort(p.host, p.port))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cc := &timeoutConn{c, time.Duration(idleTimeout) * time.Second}
|
|
|
|
|
config := &tls.Config{
|
|
|
|
|
ServerName: p.hostname,
|
|
|
|
|
NextProtos: []string{"h2"},
|
|
|
|
|
InsecureSkipVerify: insecure,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn := tls.Client(cc, config)
|
|
|
|
|
if err := conn.Handshake(); err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http2conn, err := p.transport.NewClientConn(conn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
conn.Close()
|
|
|
|
|
log.Println(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.conn = http2conn
|
|
|
|
|
|
|
|
|
|
return http2conn, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *clientConn) MarkDead(conn *http2.ClientConn) {
|
|
|
|
|
//p.lock.Lock()
|
|
|
|
|
//defer p.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
|
log.Println("mark dead")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//p.conn = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var debug bool
|
|
|
|
|
var insecure bool
|
|
|
|
|
var idleTimeout int
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
var addr string
|
|
|
|
@ -252,7 +233,9 @@ func main() {
|
|
|
|
|
flag.StringVar(&addr, "server", "", "server address")
|
|
|
|
|
flag.StringVar(&hostname, "name", "", "server 's SNI name")
|
|
|
|
|
flag.StringVar(&listen, "listen", ":8080", "listen address")
|
|
|
|
|
flag.BoolVar(&debug, "debug", false, "verbose mode")
|
|
|
|
|
flag.BoolVar(&insecure, "insecure", false, "insecure mode, not verify the server's certificate")
|
|
|
|
|
flag.IntVar(&idleTimeout, "idletime", 30, "idle timeout, close connection when no data transfer")
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
if addr == "" {
|
|
|
|
@ -260,25 +243,31 @@ func main() {
|
|
|
|
|
os.Exit(-1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
host = addr
|
|
|
|
|
addr = fmt.Sprintf("%s:443", addr)
|
|
|
|
|
port = "443"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if hostname == "" {
|
|
|
|
|
hostname = host
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transport := &http2.Transport{
|
|
|
|
|
AllowHTTP: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p := newClientConn(host, port, hostname, transport)
|
|
|
|
|
transport.ConnPool = p
|
|
|
|
|
|
|
|
|
|
log.Printf("listen on %s", listen)
|
|
|
|
|
|
|
|
|
|
hdr := &handler{
|
|
|
|
|
addr: addr,
|
|
|
|
|
hostname: hostname,
|
|
|
|
|
insecure: insecure,
|
|
|
|
|
lock: new(sync.Mutex),
|
|
|
|
|
if debug {
|
|
|
|
|
log.Printf("use parent proxy https://%s:%s/", host, port)
|
|
|
|
|
log.Printf("server SNI name %s", hostname)
|
|
|
|
|
}
|
|
|
|
|
if err := http.ListenAndServe(listen, hdr); err != nil {
|
|
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(listen, &handler{transport}); err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|