add mtls dialer
This commit is contained in:
parent
965c6846dd
commit
bfe5eae172
@ -21,6 +21,8 @@ import (
|
|||||||
_ "github.com/go-gost/gost/pkg/dialer/obfs/tls"
|
_ "github.com/go-gost/gost/pkg/dialer/obfs/tls"
|
||||||
_ "github.com/go-gost/gost/pkg/dialer/quic"
|
_ "github.com/go-gost/gost/pkg/dialer/quic"
|
||||||
_ "github.com/go-gost/gost/pkg/dialer/tcp"
|
_ "github.com/go-gost/gost/pkg/dialer/tcp"
|
||||||
|
_ "github.com/go-gost/gost/pkg/dialer/tls"
|
||||||
|
_ "github.com/go-gost/gost/pkg/dialer/tls/mux"
|
||||||
_ "github.com/go-gost/gost/pkg/dialer/udp"
|
_ "github.com/go-gost/gost/pkg/dialer/udp"
|
||||||
|
|
||||||
// Register handlers
|
// Register handlers
|
||||||
|
2
go.mod
2
go.mod
@ -44,7 +44,7 @@ require (
|
|||||||
github.com/templexxx/xorsimd v0.4.1 // indirect
|
github.com/templexxx/xorsimd v0.4.1 // indirect
|
||||||
github.com/tjfoc/gmsm v1.3.2 // indirect
|
github.com/tjfoc/gmsm v1.3.2 // indirect
|
||||||
github.com/xtaci/kcp-go/v5 v5.6.1
|
github.com/xtaci/kcp-go/v5 v5.6.1
|
||||||
github.com/xtaci/smux v1.5.15
|
github.com/xtaci/smux v1.5.16
|
||||||
github.com/xtaci/tcpraw v1.2.25
|
github.com/xtaci/tcpraw v1.2.25
|
||||||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
|
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
|
||||||
golang.org/x/mod v0.4.2 // indirect
|
golang.org/x/mod v0.4.2 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -399,6 +399,8 @@ github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae h1:J0GxkO96kL4WF+A
|
|||||||
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae/go.mod h1:gXtu8J62kEgmN++bm9BVICuT/e8yiLI2KFobd/TRFsE=
|
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae/go.mod h1:gXtu8J62kEgmN++bm9BVICuT/e8yiLI2KFobd/TRFsE=
|
||||||
github.com/xtaci/smux v1.5.15 h1:6hMiXswcleXj5oNfcJc+DXS8Vj36XX2LaX98udog6Kc=
|
github.com/xtaci/smux v1.5.15 h1:6hMiXswcleXj5oNfcJc+DXS8Vj36XX2LaX98udog6Kc=
|
||||||
github.com/xtaci/smux v1.5.15/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY=
|
github.com/xtaci/smux v1.5.15/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY=
|
||||||
|
github.com/xtaci/smux v1.5.16 h1:FBPYOkW8ZTjLKUM4LI4xnnuuDC8CQ/dB04HD519WoEk=
|
||||||
|
github.com/xtaci/smux v1.5.16/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY=
|
||||||
github.com/xtaci/tcpraw v1.2.25 h1:VDlqo0op17JeXBM6e2G9ocCNLOJcw9mZbobMbJjo0vk=
|
github.com/xtaci/tcpraw v1.2.25 h1:VDlqo0op17JeXBM6e2G9ocCNLOJcw9mZbobMbJjo0vk=
|
||||||
github.com/xtaci/tcpraw v1.2.25/go.mod h1:dKyZ2V75s0cZ7cbgJYdxPvms7af0joIeOyx1GgJQbLk=
|
github.com/xtaci/tcpraw v1.2.25/go.mod h1:dKyZ2V75s0cZ7cbgJYdxPvms7af0joIeOyx1GgJQbLk=
|
||||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
@ -31,7 +31,7 @@ func (c *Chain) GetRouteFor(network, address string) (r *Route) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if node.transport.IsMultiplex() {
|
if node.transport.Multiplex() {
|
||||||
tr := node.transport.Copy().
|
tr := node.transport.Copy().
|
||||||
WithRoute(r)
|
WithRoute(r)
|
||||||
node = node.Copy().
|
node = node.Copy().
|
||||||
|
@ -75,9 +75,9 @@ func (tr *Transport) Bind(ctx context.Context, conn net.Conn, network, address s
|
|||||||
return nil, connector.ErrBindUnsupported
|
return nil, connector.ErrBindUnsupported
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tr *Transport) IsMultiplex() bool {
|
func (tr *Transport) Multiplex() bool {
|
||||||
if mux, ok := tr.dialer.(dialer.Multiplexer); ok {
|
if mux, ok := tr.dialer.(dialer.Multiplexer); ok {
|
||||||
return mux.IsMultiplex()
|
return mux.Multiplex()
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -18,5 +18,5 @@ type Handshaker interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Multiplexer interface {
|
type Multiplexer interface {
|
||||||
IsMultiplex() bool
|
Multiplex() bool
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,8 @@ func (d *http2Dialer) Init(md md.Metadata) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMultiplex implements dialer.Multiplexer interface.
|
// Multiplex implements dialer.Multiplexer interface.
|
||||||
func (d *http2Dialer) IsMultiplex() bool {
|
func (d *http2Dialer) Multiplex() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,8 +65,8 @@ func (d *h2Dialer) Init(md md.Metadata) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMultiplex implements dialer.Multiplexer interface.
|
// Multiplex implements dialer.Multiplexer interface.
|
||||||
func (d *h2Dialer) IsMultiplex() bool {
|
func (d *h2Dialer) Multiplex() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,8 +50,8 @@ func (d *kcpDialer) Init(md md.Metadata) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMultiplex implements dialer.Multiplexer interface.
|
// Multiplex implements dialer.Multiplexer interface.
|
||||||
func (d *kcpDialer) IsMultiplex() bool {
|
func (d *kcpDialer) Multiplex() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,6 @@ func (d *kcpDialer) Handshake(ctx context.Context, conn net.Conn, options ...dia
|
|||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(opts)
|
option(opts)
|
||||||
}
|
}
|
||||||
config := d.md.config
|
|
||||||
|
|
||||||
d.sessionMutex.Lock()
|
d.sessionMutex.Lock()
|
||||||
defer d.sessionMutex.Unlock()
|
defer d.sessionMutex.Unlock()
|
||||||
@ -120,7 +119,7 @@ func (d *kcpDialer) Handshake(ctx context.Context, conn net.Conn, options ...dia
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !ok || session.session == nil {
|
if !ok || session.session == nil {
|
||||||
s, err := d.initSession(opts.Addr, conn, config)
|
s, err := d.initSession(ctx, opts.Addr, conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logger.Error(err)
|
d.logger.Error(err)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -140,12 +139,14 @@ func (d *kcpDialer) Handshake(ctx context.Context, conn net.Conn, options ...dia
|
|||||||
return cc, nil
|
return cc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *kcpDialer) initSession(addr string, conn net.Conn, config *kcp_util.Config) (*muxSession, error) {
|
func (d *kcpDialer) initSession(ctx context.Context, addr string, conn net.Conn) (*muxSession, error) {
|
||||||
pc, ok := conn.(net.PacketConn)
|
pc, ok := conn.(net.PacketConn)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("kcp: wrong connection type")
|
return nil, errors.New("kcp: wrong connection type")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config := d.md.config
|
||||||
|
|
||||||
kcpconn, err := kcp.NewConn(addr,
|
kcpconn, err := kcp.NewConn(addr,
|
||||||
kcp_util.BlockCrypt(config.Key, config.Crypt, kcp_util.DefaultSalt),
|
kcp_util.BlockCrypt(config.Key, config.Crypt, kcp_util.DefaultSalt),
|
||||||
config.DataShard, config.ParityShard, pc)
|
config.DataShard, config.ParityShard, pc)
|
||||||
|
@ -46,8 +46,8 @@ func (d *quicDialer) Init(md md.Metadata) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMultiplex implements dialer.Multiplexer interface.
|
// Multiplex implements dialer.Multiplexer interface.
|
||||||
func (d *quicDialer) IsMultiplex() bool {
|
func (d *quicDialer) Multiplex() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
67
pkg/dialer/tls/dialer.go
Normal file
67
pkg/dialer/tls/dialer.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registry.RegisterDialer("tls", NewDialer)
|
||||||
|
}
|
||||||
|
|
||||||
|
type tlsDialer struct {
|
||||||
|
md metadata
|
||||||
|
logger logger.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDialer(opts ...dialer.Option) dialer.Dialer {
|
||||||
|
options := &dialer.Options{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &tlsDialer{
|
||||||
|
logger: options.Logger,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *tlsDialer) Init(md md.Metadata) (err error) {
|
||||||
|
return d.parseMetadata(md)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *tlsDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOption) (net.Conn, error) {
|
||||||
|
var options dialer.DialOptions
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
var netd net.Dialer
|
||||||
|
conn, err := netd.DialContext(ctx, "tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
d.logger.Error(err)
|
||||||
|
}
|
||||||
|
return conn, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handshake implements dialer.Handshaker
|
||||||
|
func (d *tlsDialer) Handshake(ctx context.Context, conn net.Conn, options ...dialer.HandshakeOption) (net.Conn, error) {
|
||||||
|
if d.md.handshakeTimeout > 0 {
|
||||||
|
conn.SetDeadline(time.Now().Add(d.md.handshakeTimeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConn := tls.Client(conn, d.md.tlsConfig)
|
||||||
|
if err := tlsConn.HandshakeContext(ctx); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tlsConn, nil
|
||||||
|
}
|
43
pkg/dialer/tls/metadata.go
Normal file
43
pkg/dialer/tls/metadata.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
tls_util "github.com/go-gost/gost/pkg/common/util/tls"
|
||||||
|
md "github.com/go-gost/gost/pkg/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
type metadata struct {
|
||||||
|
tlsConfig *tls.Config
|
||||||
|
handshakeTimeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *tlsDialer) parseMetadata(md md.Metadata) (err error) {
|
||||||
|
const (
|
||||||
|
certFile = "certFile"
|
||||||
|
keyFile = "keyFile"
|
||||||
|
caFile = "caFile"
|
||||||
|
secure = "secure"
|
||||||
|
serverName = "serverName"
|
||||||
|
|
||||||
|
handshakeTimeout = "handshakeTimeout"
|
||||||
|
)
|
||||||
|
|
||||||
|
sn, _, _ := net.SplitHostPort(md.GetString(serverName))
|
||||||
|
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.handshakeTimeout = md.GetDuration(handshakeTimeout)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
38
pkg/dialer/tls/mux/conn.go
Normal file
38
pkg/dialer/tls/mux/conn.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/xtaci/smux"
|
||||||
|
)
|
||||||
|
|
||||||
|
type muxSession struct {
|
||||||
|
conn net.Conn
|
||||||
|
session *smux.Session
|
||||||
|
}
|
||||||
|
|
||||||
|
func (session *muxSession) GetConn() (net.Conn, error) {
|
||||||
|
return session.session.OpenStream()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (session *muxSession) Accept() (net.Conn, error) {
|
||||||
|
return session.session.AcceptStream()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (session *muxSession) Close() error {
|
||||||
|
if session.session == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return session.session.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (session *muxSession) IsClosed() bool {
|
||||||
|
if session.session == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return session.session.IsClosed()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (session *muxSession) NumStreams() int {
|
||||||
|
return session.session.NumStreams()
|
||||||
|
}
|
182
pkg/dialer/tls/mux/dialer.go
Normal file
182
pkg/dialer/tls/mux/dialer.go
Normal file
@ -0,0 +1,182 @@
|
|||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"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"
|
||||||
|
"github.com/xtaci/smux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registry.RegisterDialer("mtls", NewDialer)
|
||||||
|
}
|
||||||
|
|
||||||
|
type mtlsDialer struct {
|
||||||
|
sessions map[string]*muxSession
|
||||||
|
sessionMutex sync.Mutex
|
||||||
|
logger logger.Logger
|
||||||
|
md metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDialer(opts ...dialer.Option) dialer.Dialer {
|
||||||
|
options := &dialer.Options{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &mtlsDialer{
|
||||||
|
sessions: make(map[string]*muxSession),
|
||||||
|
logger: options.Logger,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *mtlsDialer) Init(md md.Metadata) (err error) {
|
||||||
|
if err = d.parseMetadata(md); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiplex implements dialer.Multiplexer interface.
|
||||||
|
func (d *mtlsDialer) Multiplex() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *mtlsDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOption) (conn net.Conn, err error) {
|
||||||
|
var options dialer.DialOptions
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&options)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.sessionMutex.Lock()
|
||||||
|
defer d.sessionMutex.Unlock()
|
||||||
|
|
||||||
|
session, ok := d.sessions[addr]
|
||||||
|
if session != nil && session.IsClosed() {
|
||||||
|
delete(d.sessions, addr) // session is dead
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
conn, err = d.dial(ctx, "tcp", addr, &options)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session = &muxSession{conn: conn}
|
||||||
|
d.sessions[addr] = session
|
||||||
|
}
|
||||||
|
|
||||||
|
return session.conn, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handshake implements dialer.Handshaker
|
||||||
|
func (d *mtlsDialer) Handshake(ctx context.Context, conn net.Conn, options ...dialer.HandshakeOption) (net.Conn, error) {
|
||||||
|
opts := &dialer.HandshakeOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.sessionMutex.Lock()
|
||||||
|
defer d.sessionMutex.Unlock()
|
||||||
|
|
||||||
|
if d.md.handshakeTimeout > 0 {
|
||||||
|
conn.SetDeadline(time.Now().Add(d.md.handshakeTimeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
}
|
||||||
|
|
||||||
|
session, ok := d.sessions[opts.Addr]
|
||||||
|
if session != nil && session.conn != conn {
|
||||||
|
conn.Close()
|
||||||
|
return nil, errors.New("mtls: unrecognized connection")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ok || session.session == nil {
|
||||||
|
s, err := d.initSession(ctx, conn)
|
||||||
|
if err != nil {
|
||||||
|
d.logger.Error(err)
|
||||||
|
conn.Close()
|
||||||
|
delete(d.sessions, opts.Addr)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
session = s
|
||||||
|
d.sessions[opts.Addr] = session
|
||||||
|
}
|
||||||
|
cc, err := session.GetConn()
|
||||||
|
if err != nil {
|
||||||
|
session.Close()
|
||||||
|
delete(d.sessions, opts.Addr)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *mtlsDialer) 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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *mtlsDialer) initSession(ctx context.Context, conn net.Conn) (*muxSession, error) {
|
||||||
|
tlsConn := tls.Client(conn, d.md.tlsConfig)
|
||||||
|
if err := tlsConn.HandshakeContext(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
conn = tlsConn
|
||||||
|
|
||||||
|
// stream multiplex
|
||||||
|
smuxConfig := smux.DefaultConfig()
|
||||||
|
smuxConfig.KeepAliveDisabled = d.md.muxKeepAliveDisabled
|
||||||
|
if d.md.muxKeepAliveInterval > 0 {
|
||||||
|
smuxConfig.KeepAliveInterval = d.md.muxKeepAliveInterval
|
||||||
|
}
|
||||||
|
if d.md.muxKeepAliveTimeout > 0 {
|
||||||
|
smuxConfig.KeepAliveTimeout = d.md.muxKeepAliveTimeout
|
||||||
|
}
|
||||||
|
if d.md.muxMaxFrameSize > 0 {
|
||||||
|
smuxConfig.MaxFrameSize = d.md.muxMaxFrameSize
|
||||||
|
}
|
||||||
|
if d.md.muxMaxReceiveBuffer > 0 {
|
||||||
|
smuxConfig.MaxReceiveBuffer = d.md.muxMaxReceiveBuffer
|
||||||
|
}
|
||||||
|
if d.md.muxMaxStreamBuffer > 0 {
|
||||||
|
smuxConfig.MaxStreamBuffer = d.md.muxMaxStreamBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
session, err := smux.Client(conn, smuxConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &muxSession{conn: conn, session: session}, nil
|
||||||
|
}
|
63
pkg/dialer/tls/mux/metadata.go
Normal file
63
pkg/dialer/tls/mux/metadata.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package mux
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
tls_util "github.com/go-gost/gost/pkg/common/util/tls"
|
||||||
|
md "github.com/go-gost/gost/pkg/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
type metadata struct {
|
||||||
|
tlsConfig *tls.Config
|
||||||
|
handshakeTimeout time.Duration
|
||||||
|
|
||||||
|
muxKeepAliveDisabled bool
|
||||||
|
muxKeepAliveInterval time.Duration
|
||||||
|
muxKeepAliveTimeout time.Duration
|
||||||
|
muxMaxFrameSize int
|
||||||
|
muxMaxReceiveBuffer int
|
||||||
|
muxMaxStreamBuffer int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *mtlsDialer) parseMetadata(md md.Metadata) (err error) {
|
||||||
|
const (
|
||||||
|
certFile = "certFile"
|
||||||
|
keyFile = "keyFile"
|
||||||
|
caFile = "caFile"
|
||||||
|
secure = "secure"
|
||||||
|
serverName = "serverName"
|
||||||
|
|
||||||
|
handshakeTimeout = "handshakeTimeout"
|
||||||
|
|
||||||
|
muxKeepAliveDisabled = "muxKeepAliveDisabled"
|
||||||
|
muxKeepAliveInterval = "muxKeepAliveInterval"
|
||||||
|
muxKeepAliveTimeout = "muxKeepAliveTimeout"
|
||||||
|
muxMaxFrameSize = "muxMaxFrameSize"
|
||||||
|
muxMaxReceiveBuffer = "muxMaxReceiveBuffer"
|
||||||
|
muxMaxStreamBuffer = "muxMaxStreamBuffer"
|
||||||
|
)
|
||||||
|
|
||||||
|
sn, _, _ := net.SplitHostPort(md.GetString(serverName))
|
||||||
|
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.handshakeTimeout = md.GetDuration(handshakeTimeout)
|
||||||
|
|
||||||
|
d.md.muxKeepAliveDisabled = md.GetBool(muxKeepAliveDisabled)
|
||||||
|
d.md.muxKeepAliveInterval = md.GetDuration(muxKeepAliveInterval)
|
||||||
|
d.md.muxKeepAliveTimeout = md.GetDuration(muxKeepAliveTimeout)
|
||||||
|
d.md.muxMaxFrameSize = md.GetInt(muxMaxFrameSize)
|
||||||
|
d.md.muxMaxReceiveBuffer = md.GetInt(muxMaxReceiveBuffer)
|
||||||
|
d.md.muxMaxStreamBuffer = md.GetInt(muxMaxStreamBuffer)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
@ -48,7 +48,3 @@ func (d *udpDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOp
|
|||||||
UDPConn: c,
|
UDPConn: c,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *udpDialer) parseMetadata(md md.Metadata) (err error) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package udp
|
package udp
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
md "github.com/go-gost/gost/pkg/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
dialTimeout = "dialTimeout"
|
dialTimeout = "dialTimeout"
|
||||||
@ -13,3 +17,7 @@ const (
|
|||||||
type metadata struct {
|
type metadata struct {
|
||||||
dialTimeout time.Duration
|
dialTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *udpDialer) parseMetadata(md md.Metadata) (err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/go-gost/gost/pkg/common/util"
|
|
||||||
"github.com/go-gost/gost/pkg/listener"
|
"github.com/go-gost/gost/pkg/listener"
|
||||||
"github.com/go-gost/gost/pkg/logger"
|
"github.com/go-gost/gost/pkg/logger"
|
||||||
md "github.com/go-gost/gost/pkg/metadata"
|
md "github.com/go-gost/gost/pkg/metadata"
|
||||||
@ -17,9 +16,9 @@ func init() {
|
|||||||
|
|
||||||
type tlsListener struct {
|
type tlsListener struct {
|
||||||
addr string
|
addr string
|
||||||
md metadata
|
|
||||||
net.Listener
|
net.Listener
|
||||||
logger logger.Logger
|
logger logger.Logger
|
||||||
|
md metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListener(opts ...listener.Option) listener.Listener {
|
func NewListener(opts ...listener.Option) listener.Listener {
|
||||||
@ -43,14 +42,7 @@ func (l *tlsListener) Init(md md.Metadata) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ln = tls.NewListener(
|
l.Listener = tls.NewListener(ln, l.md.tlsConfig)
|
||||||
&util.TCPKeepAliveListener{
|
|
||||||
TCPListener: ln.(*net.TCPListener),
|
|
||||||
KeepAlivePeriod: l.md.keepAlivePeriod,
|
|
||||||
},
|
|
||||||
l.md.tlsConfig,
|
|
||||||
)
|
|
||||||
|
|
||||||
l.Listener = ln
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package tls
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"time"
|
|
||||||
|
|
||||||
tls_util "github.com/go-gost/gost/pkg/common/util/tls"
|
tls_util "github.com/go-gost/gost/pkg/common/util/tls"
|
||||||
md "github.com/go-gost/gost/pkg/metadata"
|
md "github.com/go-gost/gost/pkg/metadata"
|
||||||
@ -10,7 +9,6 @@ import (
|
|||||||
|
|
||||||
type metadata struct {
|
type metadata struct {
|
||||||
tlsConfig *tls.Config
|
tlsConfig *tls.Config
|
||||||
keepAlivePeriod time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *tlsListener) parseMetadata(md md.Metadata) (err error) {
|
func (l *tlsListener) parseMetadata(md md.Metadata) (err error) {
|
||||||
@ -18,7 +16,6 @@ func (l *tlsListener) parseMetadata(md md.Metadata) (err error) {
|
|||||||
certFile = "certFile"
|
certFile = "certFile"
|
||||||
keyFile = "keyFile"
|
keyFile = "keyFile"
|
||||||
caFile = "caFile"
|
caFile = "caFile"
|
||||||
keepAlivePeriod = "keepAlivePeriod"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
l.md.tlsConfig, err = tls_util.LoadServerConfig(
|
l.md.tlsConfig, err = tls_util.LoadServerConfig(
|
||||||
@ -30,6 +27,5 @@ func (l *tlsListener) parseMetadata(md md.Metadata) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
l.md.keepAlivePeriod = md.GetDuration(keepAlivePeriod)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -17,11 +17,11 @@ func init() {
|
|||||||
|
|
||||||
type mtlsListener struct {
|
type mtlsListener struct {
|
||||||
addr string
|
addr string
|
||||||
md metadata
|
|
||||||
net.Listener
|
net.Listener
|
||||||
connChan chan net.Conn
|
cqueue chan net.Conn
|
||||||
errChan chan error
|
errChan chan error
|
||||||
logger logger.Logger
|
logger logger.Logger
|
||||||
|
md metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListener(opts ...listener.Option) listener.Listener {
|
func NewListener(opts ...listener.Option) listener.Listener {
|
||||||
@ -46,11 +46,7 @@ func (l *mtlsListener) Init(md md.Metadata) (err error) {
|
|||||||
}
|
}
|
||||||
l.Listener = tls.NewListener(ln, l.md.tlsConfig)
|
l.Listener = tls.NewListener(ln, l.md.tlsConfig)
|
||||||
|
|
||||||
queueSize := l.md.connQueueSize
|
l.cqueue = make(chan net.Conn, l.md.backlog)
|
||||||
if queueSize <= 0 {
|
|
||||||
queueSize = defaultQueueSize
|
|
||||||
}
|
|
||||||
l.connChan = make(chan net.Conn, queueSize)
|
|
||||||
l.errChan = make(chan error, 1)
|
l.errChan = make(chan error, 1)
|
||||||
|
|
||||||
go l.listenLoop()
|
go l.listenLoop()
|
||||||
@ -73,8 +69,8 @@ func (l *mtlsListener) listenLoop() {
|
|||||||
func (l *mtlsListener) mux(conn net.Conn) {
|
func (l *mtlsListener) mux(conn net.Conn) {
|
||||||
smuxConfig := smux.DefaultConfig()
|
smuxConfig := smux.DefaultConfig()
|
||||||
smuxConfig.KeepAliveDisabled = l.md.muxKeepAliveDisabled
|
smuxConfig.KeepAliveDisabled = l.md.muxKeepAliveDisabled
|
||||||
if l.md.muxKeepAlivePeriod > 0 {
|
if l.md.muxKeepAliveInterval > 0 {
|
||||||
smuxConfig.KeepAliveInterval = l.md.muxKeepAlivePeriod
|
smuxConfig.KeepAliveInterval = l.md.muxKeepAliveInterval
|
||||||
}
|
}
|
||||||
if l.md.muxKeepAliveTimeout > 0 {
|
if l.md.muxKeepAliveTimeout > 0 {
|
||||||
smuxConfig.KeepAliveTimeout = l.md.muxKeepAliveTimeout
|
smuxConfig.KeepAliveTimeout = l.md.muxKeepAliveTimeout
|
||||||
@ -103,12 +99,12 @@ func (l *mtlsListener) mux(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case l.connChan <- stream:
|
case l.cqueue <- stream:
|
||||||
case <-stream.GetDieCh():
|
case <-stream.GetDieCh():
|
||||||
stream.Close()
|
stream.Close()
|
||||||
default:
|
default:
|
||||||
stream.Close()
|
stream.Close()
|
||||||
l.logger.Error("connection queue is full")
|
l.logger.Warnf("connection queue is full, client %s discarded", stream.RemoteAddr())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +112,7 @@ func (l *mtlsListener) mux(conn net.Conn) {
|
|||||||
func (l *mtlsListener) Accept() (conn net.Conn, err error) {
|
func (l *mtlsListener) Accept() (conn net.Conn, err error) {
|
||||||
var ok bool
|
var ok bool
|
||||||
select {
|
select {
|
||||||
case conn = <-l.connChan:
|
case conn = <-l.cqueue:
|
||||||
case err, ok = <-l.errChan:
|
case err, ok = <-l.errChan:
|
||||||
if !ok {
|
if !ok {
|
||||||
err = listener.ErrClosed
|
err = listener.ErrClosed
|
||||||
|
@ -9,20 +9,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultQueueSize = 128
|
defaultBacklog = 128
|
||||||
)
|
)
|
||||||
|
|
||||||
type metadata struct {
|
type metadata struct {
|
||||||
tlsConfig *tls.Config
|
tlsConfig *tls.Config
|
||||||
|
|
||||||
muxKeepAliveDisabled bool
|
muxKeepAliveDisabled bool
|
||||||
muxKeepAlivePeriod time.Duration
|
muxKeepAliveInterval time.Duration
|
||||||
muxKeepAliveTimeout time.Duration
|
muxKeepAliveTimeout time.Duration
|
||||||
muxMaxFrameSize int
|
muxMaxFrameSize int
|
||||||
muxMaxReceiveBuffer int
|
muxMaxReceiveBuffer int
|
||||||
muxMaxStreamBuffer int
|
muxMaxStreamBuffer int
|
||||||
|
|
||||||
connQueueSize int
|
backlog int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *mtlsListener) parseMetadata(md md.Metadata) (err error) {
|
func (l *mtlsListener) parseMetadata(md md.Metadata) (err error) {
|
||||||
@ -31,8 +31,10 @@ func (l *mtlsListener) parseMetadata(md md.Metadata) (err error) {
|
|||||||
keyFile = "keyFile"
|
keyFile = "keyFile"
|
||||||
caFile = "caFile"
|
caFile = "caFile"
|
||||||
|
|
||||||
|
backlog = "backlog"
|
||||||
|
|
||||||
muxKeepAliveDisabled = "muxKeepAliveDisabled"
|
muxKeepAliveDisabled = "muxKeepAliveDisabled"
|
||||||
muxKeepAlivePeriod = "muxKeepAlivePeriod"
|
muxKeepAliveInterval = "muxKeepAliveInterval"
|
||||||
muxKeepAliveTimeout = "muxKeepAliveTimeout"
|
muxKeepAliveTimeout = "muxKeepAliveTimeout"
|
||||||
muxMaxFrameSize = "muxMaxFrameSize"
|
muxMaxFrameSize = "muxMaxFrameSize"
|
||||||
muxMaxReceiveBuffer = "muxMaxReceiveBuffer"
|
muxMaxReceiveBuffer = "muxMaxReceiveBuffer"
|
||||||
@ -48,5 +50,17 @@ func (l *mtlsListener) parseMetadata(md md.Metadata) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.md.backlog = md.GetInt(backlog)
|
||||||
|
if l.md.backlog <= 0 {
|
||||||
|
l.md.backlog = defaultBacklog
|
||||||
|
}
|
||||||
|
|
||||||
|
l.md.muxKeepAliveDisabled = md.GetBool(muxKeepAliveDisabled)
|
||||||
|
l.md.muxKeepAliveInterval = md.GetDuration(muxKeepAliveInterval)
|
||||||
|
l.md.muxKeepAliveTimeout = md.GetDuration(muxKeepAliveTimeout)
|
||||||
|
l.md.muxMaxFrameSize = md.GetInt(muxMaxFrameSize)
|
||||||
|
l.md.muxMaxReceiveBuffer = md.GetInt(muxMaxReceiveBuffer)
|
||||||
|
l.md.muxMaxStreamBuffer = md.GetInt(muxMaxStreamBuffer)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user