add chain
This commit is contained in:
19
pkg/components/dialer/dialer.go
Normal file
19
pkg/components/dialer/dialer.go
Normal file
@ -0,0 +1,19 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
// Transporter is responsible for dialing to the proxy server.
|
||||
type Dialer interface {
|
||||
Dial(ctx context.Context, addr string, opts ...DialOption) (net.Conn, error)
|
||||
}
|
||||
|
||||
type Handshaker interface {
|
||||
Handshake(ctx context.Context, conn net.Conn) (net.Conn, error)
|
||||
}
|
||||
|
||||
type Multiplexer interface {
|
||||
IsMultiplex() bool
|
||||
}
|
3
pkg/components/dialer/metadata.go
Normal file
3
pkg/components/dialer/metadata.go
Normal file
@ -0,0 +1,3 @@
|
||||
package dialer
|
||||
|
||||
type Metadata map[string]string
|
32
pkg/components/dialer/option.go
Normal file
32
pkg/components/dialer/option.go
Normal file
@ -0,0 +1,32 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type Option func(opts *Options)
|
||||
|
||||
func LoggerOption(logger logger.Logger) Option {
|
||||
return func(opts *Options) {
|
||||
opts.Logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
type DialOptions struct {
|
||||
DialFunc func(ctx context.Context, addr string) (net.Conn, error)
|
||||
}
|
||||
|
||||
type DialOption func(opts *DialOptions)
|
||||
|
||||
func DialFuncDialOption(dialf func(ctx context.Context, addr string) (net.Conn, error)) DialOption {
|
||||
return func(opts *DialOptions) {
|
||||
opts.DialFunc = dialf
|
||||
}
|
||||
}
|
56
pkg/components/dialer/tcp/dialer.go
Normal file
56
pkg/components/dialer/tcp/dialer.go
Normal file
@ -0,0 +1,56 @@
|
||||
package tcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/components/dialer"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
_ dialer.Dialer = (*Dialer)(nil)
|
||||
)
|
||||
|
||||
type Dialer struct {
|
||||
md metadata
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func NewDialer(opts ...dialer.Option) *Dialer {
|
||||
options := &dialer.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
return &Dialer{
|
||||
logger: options.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Dialer) Init(md dialer.Metadata) (err error) {
|
||||
d.md, err = d.parseMetadata(md)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOption) (net.Conn, error) {
|
||||
var options dialer.DialOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
dial := options.DialFunc
|
||||
if dial != nil {
|
||||
return dial(ctx, addr)
|
||||
}
|
||||
|
||||
var netd net.Dialer
|
||||
return netd.DialContext(ctx, "tcp", addr)
|
||||
}
|
||||
|
||||
func (d *Dialer) parseMetadata(md dialer.Metadata) (m metadata, err error) {
|
||||
return
|
||||
}
|
15
pkg/components/dialer/tcp/metadata.go
Normal file
15
pkg/components/dialer/tcp/metadata.go
Normal file
@ -0,0 +1,15 @@
|
||||
package tcp
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
dialTimeout = "dialTimeout"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultDialTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
dialTimeout time.Duration
|
||||
}
|
Reference in New Issue
Block a user