add http2 tunnel
This commit is contained in:
@ -80,20 +80,6 @@ func (d *http2Dialer) Dial(ctx context.Context, address string, opts ...dialer.D
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
},
|
||||
}
|
||||
/*
|
||||
client = &http.Client{
|
||||
Transport: &http2.Transport{
|
||||
TLSClientConfig: d.md.tlsConfig,
|
||||
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
|
||||
conn, err := d.dial(ctx, network, addr, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tls_util.WrapTLSClient(conn, cfg, time.Duration(0))
|
||||
},
|
||||
},
|
||||
}
|
||||
*/
|
||||
d.clients[address] = client
|
||||
}
|
||||
|
||||
|
54
pkg/dialer/http2/h2/conn.go
Normal file
54
pkg/dialer/http2/h2/conn.go
Normal file
@ -0,0 +1,54 @@
|
||||
package h2
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HTTP2 connection, wrapped up just like a net.Conn.
|
||||
type http2Conn struct {
|
||||
r io.Reader
|
||||
w io.Writer
|
||||
remoteAddr net.Addr
|
||||
localAddr net.Addr
|
||||
}
|
||||
|
||||
func (c *http2Conn) Read(b []byte) (n int, err error) {
|
||||
return c.r.Read(b)
|
||||
}
|
||||
|
||||
func (c *http2Conn) Write(b []byte) (n int, err error) {
|
||||
return c.w.Write(b)
|
||||
}
|
||||
|
||||
func (c *http2Conn) Close() (err error) {
|
||||
if r, ok := c.r.(io.Closer); ok {
|
||||
err = r.Close()
|
||||
}
|
||||
if w, ok := c.w.(io.Closer); ok {
|
||||
err = w.Close()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *http2Conn) LocalAddr() net.Addr {
|
||||
return c.localAddr
|
||||
}
|
||||
|
||||
func (c *http2Conn) RemoteAddr() net.Addr {
|
||||
return c.remoteAddr
|
||||
}
|
||||
|
||||
func (c *http2Conn) SetDeadline(t time.Time) error {
|
||||
return &net.OpError{Op: "set", Net: "h2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||
}
|
||||
|
||||
func (c *http2Conn) SetReadDeadline(t time.Time) error {
|
||||
return &net.OpError{Op: "set", Net: "h2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||
}
|
||||
|
||||
func (c *http2Conn) SetWriteDeadline(t time.Time) error {
|
||||
return &net.OpError{Op: "set", Net: "h2", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||
}
|
190
pkg/dialer/http2/h2/dialer.go
Normal file
190
pkg/dialer/http2/h2/dialer.go
Normal file
@ -0,0 +1,190 @@
|
||||
package h2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/dialer"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.RegisterDialer("h2", NewTLSDialer)
|
||||
registry.RegisterDialer("h2c", NewDialer)
|
||||
}
|
||||
|
||||
type h2Dialer struct {
|
||||
clients map[string]*http.Client
|
||||
clientMutex sync.Mutex
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
h2c bool
|
||||
}
|
||||
|
||||
func NewDialer(opts ...dialer.Option) dialer.Dialer {
|
||||
options := &dialer.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
return &h2Dialer{
|
||||
clients: make(map[string]*http.Client),
|
||||
logger: options.Logger,
|
||||
h2c: true,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTLSDialer(opts ...dialer.Option) dialer.Dialer {
|
||||
options := &dialer.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
return &h2Dialer{
|
||||
clients: make(map[string]*http.Client),
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *h2Dialer) Init(md md.Metadata) (err error) {
|
||||
if err = d.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsMultiplex implements dialer.Multiplexer interface.
|
||||
func (d *h2Dialer) IsMultiplex() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *h2Dialer) Dial(ctx context.Context, address string, opts ...dialer.DialOption) (net.Conn, error) {
|
||||
raddr, err := net.ResolveTCPAddr("tcp", address)
|
||||
if err != nil {
|
||||
d.logger.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d.clientMutex.Lock()
|
||||
|
||||
client, ok := d.clients[address]
|
||||
if !ok {
|
||||
options := &dialer.DialOptions{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
client = &http.Client{}
|
||||
if d.h2c {
|
||||
client.Transport = &http2.Transport{
|
||||
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
|
||||
return d.dial(ctx, network, addr, options)
|
||||
},
|
||||
}
|
||||
} else {
|
||||
client.Transport = &http.Transport{
|
||||
TLSClientConfig: d.md.tlsConfig,
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return d.dial(ctx, network, addr, options)
|
||||
},
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
d.clients[address] = client
|
||||
}
|
||||
d.clientMutex.Unlock()
|
||||
|
||||
host := d.md.host
|
||||
if host == "" {
|
||||
host = address
|
||||
}
|
||||
|
||||
pr, pw := io.Pipe()
|
||||
req := &http.Request{
|
||||
Method: http.MethodConnect,
|
||||
URL: &url.URL{Scheme: "https", Host: host},
|
||||
Header: make(http.Header),
|
||||
ProtoMajor: 2,
|
||||
ProtoMinor: 0,
|
||||
Body: pr,
|
||||
Host: host,
|
||||
// ContentLength: -1,
|
||||
}
|
||||
if d.md.path != "" {
|
||||
req.Method = http.MethodGet
|
||||
req.URL.Path = d.md.path
|
||||
}
|
||||
|
||||
if d.logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
dump, _ := httputil.DumpRequest(req, false)
|
||||
d.logger.Debug(string(dump))
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if d.logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
dump, _ := httputil.DumpResponse(resp, false)
|
||||
d.logger.Debug(string(dump))
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
resp.Body.Close()
|
||||
return nil, errors.New(resp.Status)
|
||||
}
|
||||
|
||||
conn := &http2Conn{
|
||||
r: resp.Body,
|
||||
w: pw,
|
||||
remoteAddr: raddr,
|
||||
localAddr: &net.TCPAddr{IP: net.IPv4zero, Port: 0},
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (d *h2Dialer) dial(ctx context.Context, network, addr string, opts *dialer.DialOptions) (net.Conn, error) {
|
||||
dial := opts.DialFunc
|
||||
if dial != nil {
|
||||
conn, err := dial(ctx, addr)
|
||||
if err != nil {
|
||||
d.logger.Error(err)
|
||||
} else {
|
||||
d.logger.WithFields(map[string]interface{}{
|
||||
"src": conn.LocalAddr().String(),
|
||||
"dst": addr,
|
||||
}).Debug("dial with dial func")
|
||||
}
|
||||
return conn, err
|
||||
}
|
||||
|
||||
var netd net.Dialer
|
||||
conn, err := netd.DialContext(ctx, network, addr)
|
||||
if err != nil {
|
||||
d.logger.Error(err)
|
||||
} else {
|
||||
d.logger.WithFields(map[string]interface{}{
|
||||
"src": conn.LocalAddr().String(),
|
||||
"dst": addr,
|
||||
}).Debugf("dial direct %s/%s", addr, network)
|
||||
}
|
||||
return conn, err
|
||||
}
|
43
pkg/dialer/http2/h2/metadata.go
Normal file
43
pkg/dialer/http2/h2/metadata.go
Normal file
@ -0,0 +1,43 @@
|
||||
package h2
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
|
||||
tls_util "github.com/go-gost/gost/pkg/common/util/tls"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
path string
|
||||
host string
|
||||
tlsConfig *tls.Config
|
||||
}
|
||||
|
||||
func (d *h2Dialer) parseMetadata(md md.Metadata) (err error) {
|
||||
const (
|
||||
certFile = "certFile"
|
||||
keyFile = "keyFile"
|
||||
caFile = "caFile"
|
||||
secure = "secure"
|
||||
serverName = "serverName"
|
||||
path = "path"
|
||||
)
|
||||
|
||||
d.md.host = md.GetString(serverName)
|
||||
sn, _, _ := net.SplitHostPort(d.md.host)
|
||||
if sn == "" {
|
||||
sn = "localhost"
|
||||
}
|
||||
d.md.tlsConfig, err = tls_util.LoadClientConfig(
|
||||
md.GetString(certFile),
|
||||
md.GetString(keyFile),
|
||||
md.GetString(caFile),
|
||||
md.GetBool(secure),
|
||||
sn,
|
||||
)
|
||||
|
||||
d.md.path = md.GetString(path)
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user