add pkgs from core

This commit is contained in:
ginuerzh
2022-04-04 12:44:35 +08:00
parent 7eb3687e0e
commit a3346ad246
188 changed files with 6084 additions and 283 deletions

View File

@ -7,7 +7,7 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
"github.com/go-gost/x/registry"
"github.com/xtaci/tcpraw"
)

View File

@ -8,8 +8,8 @@ import (
"github.com/go-gost/core/dialer"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
pb "github.com/go-gost/x/internal/util/grpc/proto"
"github.com/go-gost/x/registry"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"

View File

@ -2,6 +2,7 @@ package grpc
import (
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -15,8 +16,8 @@ func (d *grpcDialer) parseMetadata(md mdata.Metadata) (err error) {
host = "host"
)
d.md.insecure = mdata.GetBool(md, insecure)
d.md.host = mdata.GetString(md, host)
d.md.insecure = mdx.GetBool(md, insecure)
d.md.host = mdx.GetString(md, host)
return
}

View File

@ -11,7 +11,8 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
mdx "github.com/go-gost/x/metadata"
"github.com/go-gost/x/registry"
)
func init() {
@ -98,7 +99,7 @@ func (d *http2Dialer) Dial(ctx context.Context, address string, opts ...dialer.D
defer d.clientMutex.Unlock()
delete(d.clients, address)
},
md: md.MapMetadata{"client": client},
md: mdx.NewMetadata(map[string]any{"client": client}),
}
return c, nil

View File

@ -15,7 +15,7 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
"github.com/go-gost/x/registry"
"golang.org/x/net/http2"
)

View File

@ -2,6 +2,7 @@ package h2
import (
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -15,8 +16,8 @@ func (d *h2Dialer) parseMetadata(md mdata.Metadata) (err error) {
path = "path"
)
d.md.host = mdata.GetString(md, host)
d.md.path = mdata.GetString(md, path)
d.md.host = mdx.GetString(md, host)
d.md.path = mdx.GetString(md, path)
return
}

View File

@ -9,8 +9,8 @@ import (
"github.com/go-gost/core/dialer"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
pht_util "github.com/go-gost/x/internal/util/pht"
"github.com/go-gost/x/registry"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3"
)

View File

@ -5,6 +5,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
const (
@ -34,19 +35,19 @@ func (d *http3Dialer) parseMetadata(md mdata.Metadata) (err error) {
host = "host"
)
d.md.authorizePath = mdata.GetString(md, authorizePath)
d.md.authorizePath = mdx.GetString(md, authorizePath)
if !strings.HasPrefix(d.md.authorizePath, "/") {
d.md.authorizePath = defaultAuthorizePath
}
d.md.pushPath = mdata.GetString(md, pushPath)
d.md.pushPath = mdx.GetString(md, pushPath)
if !strings.HasPrefix(d.md.pushPath, "/") {
d.md.pushPath = defaultPushPath
}
d.md.pullPath = mdata.GetString(md, pullPath)
d.md.pullPath = mdx.GetString(md, pullPath)
if !strings.HasPrefix(d.md.pullPath, "/") {
d.md.pullPath = defaultPullPath
}
d.md.host = mdata.GetString(md, host)
d.md.host = mdx.GetString(md, host)
return
}

View File

@ -11,8 +11,8 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
icmp_pkg "github.com/go-gost/x/internal/util/icmp"
"github.com/go-gost/x/registry"
"github.com/lucas-clemente/quic-go"
"golang.org/x/net/icmp"
)

View File

@ -4,6 +4,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -19,11 +20,11 @@ func (d *icmpDialer) parseMetadata(md mdata.Metadata) (err error) {
maxIdleTimeout = "maxIdleTimeout"
)
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
d.md.keepAlive = mdata.GetBool(md, keepAlive)
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.maxIdleTimeout = mdata.GetDuration(md, maxIdleTimeout)
d.md.keepAlive = mdx.GetBool(md, keepAlive)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
d.md.maxIdleTimeout = mdx.GetDuration(md, maxIdleTimeout)
return
}

View File

@ -10,8 +10,8 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
kcp_util "github.com/go-gost/x/internal/util/kcp"
"github.com/go-gost/x/registry"
"github.com/xtaci/kcp-go/v5"
"github.com/xtaci/smux"
"github.com/xtaci/tcpraw"

View File

@ -6,6 +6,7 @@ import (
mdata "github.com/go-gost/core/metadata"
kcp_util "github.com/go-gost/x/internal/util/kcp"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -19,7 +20,7 @@ func (d *kcpDialer) parseMetadata(md mdata.Metadata) (err error) {
handshakeTimeout = "handshakeTimeout"
)
if m := mdata.GetStringMap(md, config); len(m) > 0 {
if m := mdx.GetStringMap(md, config); len(m) > 0 {
b, err := json.Marshal(m)
if err != nil {
return err
@ -34,6 +35,6 @@ func (d *kcpDialer) parseMetadata(md mdata.Metadata) (err error) {
d.md.config = kcp_util.DefaultConfig
}
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
return
}

View File

@ -11,7 +11,7 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
"github.com/go-gost/x/registry"
"github.com/xtaci/smux"
)

View File

@ -4,6 +4,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -29,14 +30,14 @@ func (d *mtlsDialer) parseMetadata(md mdata.Metadata) (err error) {
muxMaxStreamBuffer = "muxMaxStreamBuffer"
)
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
d.md.muxKeepAliveDisabled = mdata.GetBool(md, muxKeepAliveDisabled)
d.md.muxKeepAliveInterval = mdata.GetDuration(md, muxKeepAliveInterval)
d.md.muxKeepAliveTimeout = mdata.GetDuration(md, muxKeepAliveTimeout)
d.md.muxMaxFrameSize = mdata.GetInt(md, muxMaxFrameSize)
d.md.muxMaxReceiveBuffer = mdata.GetInt(md, muxMaxReceiveBuffer)
d.md.muxMaxStreamBuffer = mdata.GetInt(md, muxMaxStreamBuffer)
d.md.muxKeepAliveDisabled = mdx.GetBool(md, muxKeepAliveDisabled)
d.md.muxKeepAliveInterval = mdx.GetDuration(md, muxKeepAliveInterval)
d.md.muxKeepAliveTimeout = mdx.GetDuration(md, muxKeepAliveTimeout)
d.md.muxMaxFrameSize = mdx.GetInt(md, muxMaxFrameSize)
d.md.muxMaxReceiveBuffer = mdx.GetInt(md, muxMaxReceiveBuffer)
d.md.muxMaxStreamBuffer = mdx.GetInt(md, muxMaxStreamBuffer)
return
}

View File

@ -10,8 +10,8 @@ import (
"github.com/go-gost/core/dialer"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
ws_util "github.com/go-gost/x/internal/util/ws"
"github.com/go-gost/x/registry"
"github.com/gorilla/websocket"
"github.com/xtaci/smux"
)

View File

@ -5,6 +5,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
const (
@ -54,34 +55,34 @@ func (d *mwsDialer) parseMetadata(md mdata.Metadata) (err error) {
muxMaxStreamBuffer = "muxMaxStreamBuffer"
)
d.md.host = mdata.GetString(md, host)
d.md.host = mdx.GetString(md, host)
d.md.path = mdata.GetString(md, path)
d.md.path = mdx.GetString(md, path)
if d.md.path == "" {
d.md.path = defaultPath
}
d.md.muxKeepAliveDisabled = mdata.GetBool(md, muxKeepAliveDisabled)
d.md.muxKeepAliveInterval = mdata.GetDuration(md, muxKeepAliveInterval)
d.md.muxKeepAliveTimeout = mdata.GetDuration(md, muxKeepAliveTimeout)
d.md.muxMaxFrameSize = mdata.GetInt(md, muxMaxFrameSize)
d.md.muxMaxReceiveBuffer = mdata.GetInt(md, muxMaxReceiveBuffer)
d.md.muxMaxStreamBuffer = mdata.GetInt(md, muxMaxStreamBuffer)
d.md.muxKeepAliveDisabled = mdx.GetBool(md, muxKeepAliveDisabled)
d.md.muxKeepAliveInterval = mdx.GetDuration(md, muxKeepAliveInterval)
d.md.muxKeepAliveTimeout = mdx.GetDuration(md, muxKeepAliveTimeout)
d.md.muxMaxFrameSize = mdx.GetInt(md, muxMaxFrameSize)
d.md.muxMaxReceiveBuffer = mdx.GetInt(md, muxMaxReceiveBuffer)
d.md.muxMaxStreamBuffer = mdx.GetInt(md, muxMaxStreamBuffer)
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.readHeaderTimeout = mdata.GetDuration(md, readHeaderTimeout)
d.md.readBufferSize = mdata.GetInt(md, readBufferSize)
d.md.writeBufferSize = mdata.GetInt(md, writeBufferSize)
d.md.enableCompression = mdata.GetBool(md, enableCompression)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
d.md.readHeaderTimeout = mdx.GetDuration(md, readHeaderTimeout)
d.md.readBufferSize = mdx.GetInt(md, readBufferSize)
d.md.writeBufferSize = mdx.GetInt(md, writeBufferSize)
d.md.enableCompression = mdx.GetBool(md, enableCompression)
if m := mdata.GetStringMapString(md, header); len(m) > 0 {
if m := mdx.GetStringMapString(md, header); len(m) > 0 {
h := http.Header{}
for k, v := range m {
h.Add(k, v)
}
d.md.header = h
}
d.md.keepAlive = mdata.GetDuration(md, keepAlive)
d.md.keepAlive = mdx.GetDuration(md, keepAlive)
return
}

View File

@ -7,7 +7,7 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
"github.com/go-gost/x/registry"
)
func init() {

View File

@ -4,6 +4,7 @@ import (
"net/http"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -17,13 +18,13 @@ func (d *obfsHTTPDialer) parseMetadata(md mdata.Metadata) (err error) {
host = "host"
)
if m := mdata.GetStringMapString(md, header); len(m) > 0 {
if m := mdx.GetStringMapString(md, header); len(m) > 0 {
h := http.Header{}
for k, v := range m {
h.Add(k, v)
}
d.md.header = h
}
d.md.host = mdata.GetString(md, host)
d.md.host = mdx.GetString(md, host)
return
}

View File

@ -7,7 +7,7 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
"github.com/go-gost/x/registry"
)
func init() {

View File

@ -2,6 +2,7 @@ package tls
import (
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -13,6 +14,6 @@ func (d *obfsTLSDialer) parseMetadata(md mdata.Metadata) (err error) {
host = "host"
)
d.md.host = mdata.GetString(md, host)
d.md.host = mdx.GetString(md, host)
return
}

View File

@ -10,8 +10,8 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
pht_util "github.com/go-gost/x/internal/util/pht"
"github.com/go-gost/x/registry"
)
func init() {

View File

@ -5,6 +5,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
const (
@ -34,19 +35,19 @@ func (d *phtDialer) parseMetadata(md mdata.Metadata) (err error) {
host = "host"
)
d.md.authorizePath = mdata.GetString(md, authorizePath)
d.md.authorizePath = mdx.GetString(md, authorizePath)
if !strings.HasPrefix(d.md.authorizePath, "/") {
d.md.authorizePath = defaultAuthorizePath
}
d.md.pushPath = mdata.GetString(md, pushPath)
d.md.pushPath = mdx.GetString(md, pushPath)
if !strings.HasPrefix(d.md.pushPath, "/") {
d.md.pushPath = defaultPushPath
}
d.md.pullPath = mdata.GetString(md, pullPath)
d.md.pullPath = mdx.GetString(md, pullPath)
if !strings.HasPrefix(d.md.pullPath, "/") {
d.md.pullPath = defaultPullPath
}
d.md.host = mdata.GetString(md, host)
d.md.host = mdx.GetString(md, host)
return
}

View File

@ -9,8 +9,8 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
quic_util "github.com/go-gost/x/internal/util/quic"
"github.com/go-gost/x/registry"
"github.com/lucas-clemente/quic-go"
)

View File

@ -4,6 +4,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
@ -25,16 +26,16 @@ func (d *quicDialer) parseMetadata(md mdata.Metadata) (err error) {
host = "host"
)
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
if key := mdata.GetString(md, cipherKey); key != "" {
if key := mdx.GetString(md, cipherKey); key != "" {
d.md.cipherKey = []byte(key)
}
d.md.keepAlive = mdata.GetBool(md, keepAlive)
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.maxIdleTimeout = mdata.GetDuration(md, maxIdleTimeout)
d.md.keepAlive = mdx.GetBool(md, keepAlive)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
d.md.maxIdleTimeout = mdx.GetDuration(md, maxIdleTimeout)
d.md.host = mdata.GetString(md, host)
d.md.host = mdx.GetString(md, host)
return
}

View File

@ -10,8 +10,8 @@ import (
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
ssh_util "github.com/go-gost/x/internal/util/ssh"
"github.com/go-gost/x/registry"
"golang.org/x/crypto/ssh"
)

View File

@ -7,6 +7,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
"golang.org/x/crypto/ssh"
)
@ -24,7 +25,7 @@ func (d *sshDialer) parseMetadata(md mdata.Metadata) (err error) {
passphrase = "passphrase"
)
if v := mdata.GetString(md, user); v != "" {
if v := mdx.GetString(md, user); v != "" {
ss := strings.SplitN(v, ":", 2)
if len(ss) == 1 {
d.md.user = url.User(ss[0])
@ -33,13 +34,13 @@ func (d *sshDialer) parseMetadata(md mdata.Metadata) (err error) {
}
}
if key := mdata.GetString(md, privateKeyFile); key != "" {
if key := mdx.GetString(md, privateKeyFile); key != "" {
data, err := ioutil.ReadFile(key)
if err != nil {
return err
}
pp := mdata.GetString(md, passphrase)
pp := mdx.GetString(md, passphrase)
if pp == "" {
d.md.signer, err = ssh.ParsePrivateKey(data)
} else {
@ -50,7 +51,7 @@ func (d *sshDialer) parseMetadata(md mdata.Metadata) (err error) {
}
}
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
return
}

View File

@ -9,8 +9,8 @@ import (
"github.com/go-gost/core/dialer"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
ssh_util "github.com/go-gost/x/internal/util/ssh"
"github.com/go-gost/x/registry"
"golang.org/x/crypto/ssh"
)

View File

@ -5,6 +5,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
"golang.org/x/crypto/ssh"
)
@ -20,13 +21,13 @@ func (d *sshdDialer) parseMetadata(md mdata.Metadata) (err error) {
passphrase = "passphrase"
)
if key := mdata.GetString(md, privateKeyFile); key != "" {
if key := mdx.GetString(md, privateKeyFile); key != "" {
data, err := ioutil.ReadFile(key)
if err != nil {
return err
}
pp := mdata.GetString(md, passphrase)
pp := mdx.GetString(md, passphrase)
if pp == "" {
d.md.signer, err = ssh.ParsePrivateKey(data)
} else {
@ -37,7 +38,7 @@ func (d *sshdDialer) parseMetadata(md mdata.Metadata) (err error) {
}
}
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
return
}

48
dialer/tcp/dialer.go Normal file
View File

@ -0,0 +1,48 @@
package tcp
import (
"context"
"net"
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/x/registry"
)
func init() {
registry.DialerRegistry().Register("tcp", NewDialer)
}
type tcpDialer struct {
md metadata
logger logger.Logger
}
func NewDialer(opts ...dialer.Option) dialer.Dialer {
options := &dialer.Options{}
for _, opt := range opts {
opt(options)
}
return &tcpDialer{
logger: options.Logger,
}
}
func (d *tcpDialer) Init(md md.Metadata) (err error) {
return d.parseMetadata(md)
}
func (d *tcpDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOption) (net.Conn, error) {
var options dialer.DialOptions
for _, opt := range opts {
opt(&options)
}
conn, err := options.NetDialer.Dial(ctx, "tcp", addr)
if err != nil {
d.logger.Error(err)
}
return conn, err
}

23
dialer/tcp/metadata.go Normal file
View File

@ -0,0 +1,23 @@
package tcp
import (
"time"
md "github.com/go-gost/core/metadata"
)
const (
dialTimeout = "dialTimeout"
)
const (
defaultDialTimeout = 5 * time.Second
)
type metadata struct {
dialTimeout time.Duration
}
func (d *tcpDialer) parseMetadata(md md.Metadata) (err error) {
return
}

68
dialer/tls/dialer.go Normal file
View File

@ -0,0 +1,68 @@
package tls
import (
"context"
"crypto/tls"
"net"
"time"
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/x/registry"
)
func init() {
registry.DialerRegistry().Register("tls", NewDialer)
}
type tlsDialer struct {
md metadata
logger logger.Logger
options dialer.Options
}
func NewDialer(opts ...dialer.Option) dialer.Dialer {
options := dialer.Options{}
for _, opt := range opts {
opt(&options)
}
return &tlsDialer{
logger: options.Logger,
options: options,
}
}
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)
}
conn, err := options.NetDialer.Dial(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.options.TLSConfig)
if err := tlsConn.HandshakeContext(ctx); err != nil {
conn.Close()
return nil, err
}
return tlsConn, nil
}

22
dialer/tls/metadata.go Normal file
View File

@ -0,0 +1,22 @@
package tls
import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
type metadata struct {
handshakeTimeout time.Duration
}
func (d *tlsDialer) parseMetadata(md mdata.Metadata) (err error) {
const (
handshakeTimeout = "handshakeTimeout"
)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
return
}

17
dialer/udp/conn.go Normal file
View File

@ -0,0 +1,17 @@
package udp
import "net"
type conn struct {
*net.UDPConn
}
func (c *conn) WriteTo(b []byte, addr net.Addr) (int, error) {
return c.UDPConn.Write(b)
}
func (c *conn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
n, err = c.UDPConn.Read(b)
addr = c.RemoteAddr()
return
}

50
dialer/udp/dialer.go Normal file
View File

@ -0,0 +1,50 @@
package udp
import (
"context"
"net"
"github.com/go-gost/core/dialer"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/x/registry"
)
func init() {
registry.DialerRegistry().Register("udp", NewDialer)
}
type udpDialer struct {
md metadata
logger logger.Logger
}
func NewDialer(opts ...dialer.Option) dialer.Dialer {
options := &dialer.Options{}
for _, opt := range opts {
opt(options)
}
return &udpDialer{
logger: options.Logger,
}
}
func (d *udpDialer) Init(md md.Metadata) (err error) {
return d.parseMetadata(md)
}
func (d *udpDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOption) (net.Conn, error) {
var options dialer.DialOptions
for _, opt := range opts {
opt(&options)
}
c, err := options.NetDialer.Dial(ctx, "udp", addr)
if err != nil {
return nil, err
}
return &conn{
UDPConn: c.(*net.UDPConn),
}, nil
}

23
dialer/udp/metadata.go Normal file
View File

@ -0,0 +1,23 @@
package udp
import (
"time"
md "github.com/go-gost/core/metadata"
)
const (
dialTimeout = "dialTimeout"
)
const (
defaultDialTimeout = 5 * time.Second
)
type metadata struct {
dialTimeout time.Duration
}
func (d *udpDialer) parseMetadata(md md.Metadata) (err error) {
return
}

View File

@ -8,8 +8,8 @@ import (
"github.com/go-gost/core/dialer"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/registry"
ws_util "github.com/go-gost/x/internal/util/ws"
"github.com/go-gost/x/registry"
"github.com/gorilla/websocket"
)

View File

@ -5,6 +5,7 @@ import (
"time"
mdata "github.com/go-gost/core/metadata"
mdx "github.com/go-gost/x/metadata"
)
const (
@ -40,27 +41,27 @@ func (d *wsDialer) parseMetadata(md mdata.Metadata) (err error) {
keepAlive = "keepAlive"
)
d.md.host = mdata.GetString(md, host)
d.md.host = mdx.GetString(md, host)
d.md.path = mdata.GetString(md, path)
d.md.path = mdx.GetString(md, path)
if d.md.path == "" {
d.md.path = defaultPath
}
d.md.handshakeTimeout = mdata.GetDuration(md, handshakeTimeout)
d.md.readHeaderTimeout = mdata.GetDuration(md, readHeaderTimeout)
d.md.readBufferSize = mdata.GetInt(md, readBufferSize)
d.md.writeBufferSize = mdata.GetInt(md, writeBufferSize)
d.md.enableCompression = mdata.GetBool(md, enableCompression)
d.md.handshakeTimeout = mdx.GetDuration(md, handshakeTimeout)
d.md.readHeaderTimeout = mdx.GetDuration(md, readHeaderTimeout)
d.md.readBufferSize = mdx.GetInt(md, readBufferSize)
d.md.writeBufferSize = mdx.GetInt(md, writeBufferSize)
d.md.enableCompression = mdx.GetBool(md, enableCompression)
if m := mdata.GetStringMapString(md, header); len(m) > 0 {
if m := mdx.GetStringMapString(md, header); len(m) > 0 {
h := http.Header{}
for k, v := range m {
h.Add(k, v)
}
d.md.header = h
}
d.md.keepAlive = mdata.GetDuration(md, keepAlive)
d.md.keepAlive = mdx.GetDuration(md, keepAlive)
return
}