mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-12 20:26:37 +08:00
init
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/dialer"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
ws_util "github.com/go-gost/x/internal/util/ws"
|
||||
"github.com/go-gost/x/registry"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.DialerRegistry().Register("ws", NewDialer)
|
||||
registry.DialerRegistry().Register("wss", NewTLSDialer)
|
||||
}
|
||||
|
||||
type wsDialer struct {
|
||||
tlsEnabled bool
|
||||
md metadata
|
||||
options dialer.Options
|
||||
}
|
||||
|
||||
func NewDialer(opts ...dialer.Option) dialer.Dialer {
|
||||
options := dialer.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &wsDialer{
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTLSDialer(opts ...dialer.Option) dialer.Dialer {
|
||||
options := dialer.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &wsDialer{
|
||||
tlsEnabled: true,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *wsDialer) Init(md md.Metadata) (err error) {
|
||||
return d.parseMetadata(md)
|
||||
}
|
||||
|
||||
func (d *wsDialer) 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.Dialer.Dial(ctx, "tcp", addr)
|
||||
if err != nil {
|
||||
d.options.Logger.Error(err)
|
||||
}
|
||||
return conn, err
|
||||
}
|
||||
|
||||
// Handshake implements dialer.Handshaker
|
||||
func (d *wsDialer) Handshake(ctx context.Context, conn net.Conn, options ...dialer.HandshakeOption) (net.Conn, error) {
|
||||
opts := &dialer.HandshakeOptions{}
|
||||
for _, option := range options {
|
||||
option(opts)
|
||||
}
|
||||
|
||||
if d.md.handshakeTimeout > 0 {
|
||||
conn.SetReadDeadline(time.Now().Add(d.md.handshakeTimeout))
|
||||
defer conn.SetReadDeadline(time.Time{})
|
||||
}
|
||||
|
||||
host := d.md.host
|
||||
if host == "" {
|
||||
host = opts.Addr
|
||||
}
|
||||
|
||||
dialer := websocket.Dialer{
|
||||
HandshakeTimeout: d.md.handshakeTimeout,
|
||||
ReadBufferSize: d.md.readBufferSize,
|
||||
WriteBufferSize: d.md.writeBufferSize,
|
||||
EnableCompression: d.md.enableCompression,
|
||||
NetDial: func(net, addr string) (net.Conn, error) {
|
||||
return conn, nil
|
||||
},
|
||||
}
|
||||
|
||||
url := url.URL{Scheme: "ws", Host: host, Path: d.md.path}
|
||||
if d.tlsEnabled {
|
||||
url.Scheme = "wss"
|
||||
dialer.TLSClientConfig = d.options.TLSConfig
|
||||
}
|
||||
|
||||
c, resp, err := dialer.DialContext(ctx, url.String(), d.md.header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
cc := ws_util.Conn(c)
|
||||
|
||||
if d.md.keepaliveInterval > 0 {
|
||||
d.options.Logger.Debugf("keepalive is enabled, ttl: %v", d.md.keepaliveInterval)
|
||||
c.SetReadDeadline(time.Now().Add(d.md.keepaliveInterval * 2))
|
||||
c.SetPongHandler(func(string) error {
|
||||
c.SetReadDeadline(time.Now().Add(d.md.keepaliveInterval * 2))
|
||||
d.options.Logger.Debugf("pong: set read deadline: %v", d.md.keepaliveInterval*2)
|
||||
return nil
|
||||
})
|
||||
go d.keepalive(cc)
|
||||
}
|
||||
|
||||
return cc, nil
|
||||
}
|
||||
|
||||
func (d *wsDialer) keepalive(conn ws_util.WebsocketConn) {
|
||||
ticker := time.NewTicker(d.md.keepaliveInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
d.options.Logger.Debug("send ping")
|
||||
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
|
||||
if err := conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||
// d.options.Logger.Error(err)
|
||||
return
|
||||
}
|
||||
conn.SetWriteDeadline(time.Time{})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/x/metadata/util"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPath = "/ws"
|
||||
defaultKeepalivePeriod = 15 * time.Second
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
host string
|
||||
path string
|
||||
|
||||
handshakeTimeout time.Duration
|
||||
readHeaderTimeout time.Duration
|
||||
readBufferSize int
|
||||
writeBufferSize int
|
||||
enableCompression bool
|
||||
|
||||
header http.Header
|
||||
keepaliveInterval time.Duration
|
||||
}
|
||||
|
||||
func (d *wsDialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||
d.md.host = mdutil.GetString(md, "ws.host", "host")
|
||||
|
||||
d.md.path = mdutil.GetString(md, "ws.path", "path")
|
||||
if d.md.path == "" {
|
||||
d.md.path = defaultPath
|
||||
}
|
||||
|
||||
d.md.handshakeTimeout = mdutil.GetDuration(md, "ws.handshakeTimeout", "handshakeTimeout")
|
||||
d.md.readHeaderTimeout = mdutil.GetDuration(md, "ws.readHeaderTimeout", "readHeaderTimeout")
|
||||
d.md.readBufferSize = mdutil.GetInt(md, "ws.readBufferSize", "readBufferSize")
|
||||
d.md.writeBufferSize = mdutil.GetInt(md, "ws.writeBufferSize", "writeBufferSize")
|
||||
d.md.enableCompression = mdutil.GetBool(md, "ws.enableCompression", "enableCompression")
|
||||
|
||||
if m := mdutil.GetStringMapString(md, "ws.header", "header"); len(m) > 0 {
|
||||
h := http.Header{}
|
||||
for k, v := range m {
|
||||
h.Add(k, v)
|
||||
}
|
||||
d.md.header = h
|
||||
}
|
||||
|
||||
if mdutil.GetBool(md, "ws.keepalive", "keepalive") {
|
||||
d.md.keepaliveInterval = mdutil.GetDuration(md, "ttl", "keepalive.interval")
|
||||
if d.md.keepaliveInterval <= 0 {
|
||||
d.md.keepaliveInterval = defaultKeepalivePeriod
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user