fix websocket for tunnel
This commit is contained in:
parent
0a565120df
commit
a9f0dda805
@ -3,20 +3,20 @@ package mtls
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/xtaci/smux"
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
type muxSession struct {
|
type muxSession struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
session *smux.Session
|
session *mux.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *muxSession) GetConn() (net.Conn, error) {
|
func (session *muxSession) GetConn() (net.Conn, error) {
|
||||||
return session.session.OpenStream()
|
return session.session.GetConn()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *muxSession) Accept() (net.Conn, error) {
|
func (session *muxSession) Accept() (net.Conn, error) {
|
||||||
return session.session.AcceptStream()
|
return session.session.Accept()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *muxSession) Close() error {
|
func (session *muxSession) Close() error {
|
||||||
|
@ -11,8 +11,8 @@ import (
|
|||||||
"github.com/go-gost/core/dialer"
|
"github.com/go-gost/core/dialer"
|
||||||
"github.com/go-gost/core/logger"
|
"github.com/go-gost/core/logger"
|
||||||
md "github.com/go-gost/core/metadata"
|
md "github.com/go-gost/core/metadata"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
"github.com/go-gost/x/registry"
|
"github.com/go-gost/x/registry"
|
||||||
"github.com/xtaci/smux"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -130,25 +130,7 @@ func (d *mtlsDialer) initSession(ctx context.Context, conn net.Conn) (*muxSessio
|
|||||||
conn = tlsConn
|
conn = tlsConn
|
||||||
|
|
||||||
// stream multiplex
|
// stream multiplex
|
||||||
smuxConfig := smux.DefaultConfig()
|
session, err := mux.ClientSession(conn, d.md.muxCfg)
|
||||||
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -5,39 +5,25 @@ import (
|
|||||||
|
|
||||||
mdata "github.com/go-gost/core/metadata"
|
mdata "github.com/go-gost/core/metadata"
|
||||||
mdutil "github.com/go-gost/core/metadata/util"
|
mdutil "github.com/go-gost/core/metadata/util"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
type metadata struct {
|
type metadata struct {
|
||||||
handshakeTimeout time.Duration
|
handshakeTimeout time.Duration
|
||||||
|
muxCfg *mux.Config
|
||||||
muxKeepAliveDisabled bool
|
|
||||||
muxKeepAliveInterval time.Duration
|
|
||||||
muxKeepAliveTimeout time.Duration
|
|
||||||
muxMaxFrameSize int
|
|
||||||
muxMaxReceiveBuffer int
|
|
||||||
muxMaxStreamBuffer int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *mtlsDialer) parseMetadata(md mdata.Metadata) (err error) {
|
func (d *mtlsDialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||||
const (
|
d.md.handshakeTimeout = mdutil.GetDuration(md, "handshakeTimeout")
|
||||||
handshakeTimeout = "handshakeTimeout"
|
|
||||||
|
|
||||||
muxKeepAliveDisabled = "muxKeepAliveDisabled"
|
|
||||||
muxKeepAliveInterval = "muxKeepAliveInterval"
|
|
||||||
muxKeepAliveTimeout = "muxKeepAliveTimeout"
|
|
||||||
muxMaxFrameSize = "muxMaxFrameSize"
|
|
||||||
muxMaxReceiveBuffer = "muxMaxReceiveBuffer"
|
|
||||||
muxMaxStreamBuffer = "muxMaxStreamBuffer"
|
|
||||||
)
|
|
||||||
|
|
||||||
d.md.handshakeTimeout = mdutil.GetDuration(md, handshakeTimeout)
|
|
||||||
|
|
||||||
d.md.muxKeepAliveDisabled = mdutil.GetBool(md, muxKeepAliveDisabled)
|
|
||||||
d.md.muxKeepAliveInterval = mdutil.GetDuration(md, muxKeepAliveInterval)
|
|
||||||
d.md.muxKeepAliveTimeout = mdutil.GetDuration(md, muxKeepAliveTimeout)
|
|
||||||
d.md.muxMaxFrameSize = mdutil.GetInt(md, muxMaxFrameSize)
|
|
||||||
d.md.muxMaxReceiveBuffer = mdutil.GetInt(md, muxMaxReceiveBuffer)
|
|
||||||
d.md.muxMaxStreamBuffer = mdutil.GetInt(md, muxMaxStreamBuffer)
|
|
||||||
|
|
||||||
|
d.md.muxCfg = &mux.Config{
|
||||||
|
Version: mdutil.GetInt(md, "mux.version"),
|
||||||
|
KeepAliveInterval: mdutil.GetDuration(md, "mux.keepaliveInterval"),
|
||||||
|
KeepAliveDisabled: mdutil.GetBool(md, "mux.keepaliveDisabled"),
|
||||||
|
KeepAliveTimeout: mdutil.GetDuration(md, "mux.keepaliveTimeout"),
|
||||||
|
MaxFrameSize: mdutil.GetInt(md, "mux.maxFrameSize"),
|
||||||
|
MaxReceiveBuffer: mdutil.GetInt(md, "mux.maxReceiveBuffer"),
|
||||||
|
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,20 @@ package mws
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/xtaci/smux"
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
type muxSession struct {
|
type muxSession struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
session *smux.Session
|
session *mux.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *muxSession) GetConn() (net.Conn, error) {
|
func (session *muxSession) GetConn() (net.Conn, error) {
|
||||||
return session.session.OpenStream()
|
return session.session.GetConn()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *muxSession) Accept() (net.Conn, error) {
|
func (session *muxSession) Accept() (net.Conn, error) {
|
||||||
return session.session.AcceptStream()
|
return session.session.Accept()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *muxSession) Close() error {
|
func (session *muxSession) Close() error {
|
||||||
|
@ -10,10 +10,10 @@ import (
|
|||||||
|
|
||||||
"github.com/go-gost/core/dialer"
|
"github.com/go-gost/core/dialer"
|
||||||
md "github.com/go-gost/core/metadata"
|
md "github.com/go-gost/core/metadata"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
ws_util "github.com/go-gost/x/internal/util/ws"
|
ws_util "github.com/go-gost/x/internal/util/ws"
|
||||||
"github.com/go-gost/x/registry"
|
"github.com/go-gost/x/registry"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/xtaci/smux"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -178,25 +178,7 @@ func (d *mwsDialer) initSession(ctx context.Context, host string, conn net.Conn)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stream multiplex
|
// stream multiplex
|
||||||
smuxConfig := smux.DefaultConfig()
|
session, err := mux.ClientSession(conn, d.md.muxCfg)
|
||||||
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(cc, smuxConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
mdata "github.com/go-gost/core/metadata"
|
mdata "github.com/go-gost/core/metadata"
|
||||||
mdutil "github.com/go-gost/core/metadata/util"
|
mdutil "github.com/go-gost/core/metadata/util"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -23,15 +24,9 @@ type metadata struct {
|
|||||||
writeBufferSize int
|
writeBufferSize int
|
||||||
enableCompression bool
|
enableCompression bool
|
||||||
|
|
||||||
muxKeepAliveDisabled bool
|
|
||||||
muxKeepAliveInterval time.Duration
|
|
||||||
muxKeepAliveTimeout time.Duration
|
|
||||||
muxMaxFrameSize int
|
|
||||||
muxMaxReceiveBuffer int
|
|
||||||
muxMaxStreamBuffer int
|
|
||||||
|
|
||||||
header http.Header
|
header http.Header
|
||||||
keepaliveInterval time.Duration
|
keepaliveInterval time.Duration
|
||||||
|
muxCfg *mux.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *mwsDialer) parseMetadata(md mdata.Metadata) (err error) {
|
func (d *mwsDialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||||
@ -46,13 +41,6 @@ func (d *mwsDialer) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
enableCompression = "enableCompression"
|
enableCompression = "enableCompression"
|
||||||
|
|
||||||
header = "header"
|
header = "header"
|
||||||
|
|
||||||
muxKeepAliveDisabled = "muxKeepAliveDisabled"
|
|
||||||
muxKeepAliveInterval = "muxKeepAliveInterval"
|
|
||||||
muxKeepAliveTimeout = "muxKeepAliveTimeout"
|
|
||||||
muxMaxFrameSize = "muxMaxFrameSize"
|
|
||||||
muxMaxReceiveBuffer = "muxMaxReceiveBuffer"
|
|
||||||
muxMaxStreamBuffer = "muxMaxStreamBuffer"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
d.md.host = mdutil.GetString(md, host)
|
d.md.host = mdutil.GetString(md, host)
|
||||||
@ -62,12 +50,15 @@ func (d *mwsDialer) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
d.md.path = defaultPath
|
d.md.path = defaultPath
|
||||||
}
|
}
|
||||||
|
|
||||||
d.md.muxKeepAliveDisabled = mdutil.GetBool(md, muxKeepAliveDisabled)
|
d.md.muxCfg = &mux.Config{
|
||||||
d.md.muxKeepAliveInterval = mdutil.GetDuration(md, muxKeepAliveInterval)
|
Version: mdutil.GetInt(md, "mux.version"),
|
||||||
d.md.muxKeepAliveTimeout = mdutil.GetDuration(md, muxKeepAliveTimeout)
|
KeepAliveInterval: mdutil.GetDuration(md, "mux.keepaliveInterval"),
|
||||||
d.md.muxMaxFrameSize = mdutil.GetInt(md, muxMaxFrameSize)
|
KeepAliveDisabled: mdutil.GetBool(md, "mux.keepaliveDisabled"),
|
||||||
d.md.muxMaxReceiveBuffer = mdutil.GetInt(md, muxMaxReceiveBuffer)
|
KeepAliveTimeout: mdutil.GetDuration(md, "mux.keepaliveTimeout"),
|
||||||
d.md.muxMaxStreamBuffer = mdutil.GetInt(md, muxMaxStreamBuffer)
|
MaxFrameSize: mdutil.GetInt(md, "mux.maxFrameSize"),
|
||||||
|
MaxReceiveBuffer: mdutil.GetInt(md, "mux.maxReceiveBuffer"),
|
||||||
|
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
|
||||||
|
}
|
||||||
|
|
||||||
d.md.handshakeTimeout = mdutil.GetDuration(md, handshakeTimeout)
|
d.md.handshakeTimeout = mdutil.GetDuration(md, handshakeTimeout)
|
||||||
d.md.readHeaderTimeout = mdutil.GetDuration(md, readHeaderTimeout)
|
d.md.readHeaderTimeout = mdutil.GetDuration(md, readHeaderTimeout)
|
||||||
|
@ -271,6 +271,14 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
|||||||
return resp.Write(rw)
|
return resp.Write(rw)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Header.Get("Upgrade") == "websocket" {
|
||||||
|
err := xnet.CopyBuffer(cc, br, 8192)
|
||||||
|
if err == nil {
|
||||||
|
err = io.EOF
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
res, err := http.ReadResponse(bufio.NewReader(cc), req)
|
res, err := http.ReadResponse(bufio.NewReader(cc), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("read response from node %s(%s): %v", target.Name, target.Addr, err)
|
log.Warnf("read response from node %s(%s): %v", target.Name, target.Addr, err)
|
||||||
|
@ -276,6 +276,14 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, remot
|
|||||||
return resp.Write(rw)
|
return resp.Write(rw)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if req.Header.Get("Upgrade") == "websocket" {
|
||||||
|
err := xnet.CopyBuffer(cc, br, 8192)
|
||||||
|
if err == nil {
|
||||||
|
err = io.EOF
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
res, err := http.ReadResponse(bufio.NewReader(cc), req)
|
res, err := http.ReadResponse(bufio.NewReader(cc), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("read response from node %s(%s): %v", target.Name, target.Addr, err)
|
log.Warnf("read response from node %s(%s): %v", target.Name, target.Addr, err)
|
||||||
|
@ -38,10 +38,6 @@ func Sniffing(ctx context.Context, rdw io.ReadWriter) (rw io.ReadWriter, host st
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
host = r.Host
|
host = r.Host
|
||||||
protocol = ProtoHTTP
|
protocol = ProtoHTTP
|
||||||
|
|
||||||
if r.Header.Get("Upgrade") == "websocket" {
|
|
||||||
protocol = ProtoWebsocket
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,7 +90,6 @@ func isHTTP(s string) bool {
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
ProtoHTTP = "http"
|
ProtoHTTP = "http"
|
||||||
ProtoWebsocket = "ws"
|
|
||||||
ProtoTLS = "tls"
|
ProtoTLS = "tls"
|
||||||
ProtoSSHv2 = "SSH-2"
|
ProtoSSHv2 = "SSH-2"
|
||||||
)
|
)
|
||||||
|
@ -12,11 +12,11 @@ import (
|
|||||||
admission "github.com/go-gost/x/admission/wrapper"
|
admission "github.com/go-gost/x/admission/wrapper"
|
||||||
xnet "github.com/go-gost/x/internal/net"
|
xnet "github.com/go-gost/x/internal/net"
|
||||||
"github.com/go-gost/x/internal/net/proxyproto"
|
"github.com/go-gost/x/internal/net/proxyproto"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
climiter "github.com/go-gost/x/limiter/conn/wrapper"
|
climiter "github.com/go-gost/x/limiter/conn/wrapper"
|
||||||
limiter "github.com/go-gost/x/limiter/traffic/wrapper"
|
limiter "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||||
metrics "github.com/go-gost/x/metrics/wrapper"
|
metrics "github.com/go-gost/x/metrics/wrapper"
|
||||||
"github.com/go-gost/x/registry"
|
"github.com/go-gost/x/registry"
|
||||||
"github.com/xtaci/smux"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -105,24 +105,7 @@ func (l *mtlsListener) listenLoop() {
|
|||||||
func (l *mtlsListener) mux(conn net.Conn) {
|
func (l *mtlsListener) mux(conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
smuxConfig := smux.DefaultConfig()
|
session, err := mux.ServerSession(conn, l.md.muxCfg)
|
||||||
smuxConfig.KeepAliveDisabled = l.md.muxKeepAliveDisabled
|
|
||||||
if l.md.muxKeepAliveInterval > 0 {
|
|
||||||
smuxConfig.KeepAliveInterval = l.md.muxKeepAliveInterval
|
|
||||||
}
|
|
||||||
if l.md.muxKeepAliveTimeout > 0 {
|
|
||||||
smuxConfig.KeepAliveTimeout = l.md.muxKeepAliveTimeout
|
|
||||||
}
|
|
||||||
if l.md.muxMaxFrameSize > 0 {
|
|
||||||
smuxConfig.MaxFrameSize = l.md.muxMaxFrameSize
|
|
||||||
}
|
|
||||||
if l.md.muxMaxReceiveBuffer > 0 {
|
|
||||||
smuxConfig.MaxReceiveBuffer = l.md.muxMaxReceiveBuffer
|
|
||||||
}
|
|
||||||
if l.md.muxMaxStreamBuffer > 0 {
|
|
||||||
smuxConfig.MaxStreamBuffer = l.md.muxMaxStreamBuffer
|
|
||||||
}
|
|
||||||
session, err := smux.Server(conn, smuxConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logger.Error(err)
|
l.logger.Error(err)
|
||||||
return
|
return
|
||||||
@ -130,7 +113,7 @@ func (l *mtlsListener) mux(conn net.Conn) {
|
|||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
stream, err := session.AcceptStream()
|
stream, err := session.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logger.Error("accept stream: ", err)
|
l.logger.Error("accept stream: ", err)
|
||||||
return
|
return
|
||||||
@ -138,8 +121,6 @@ func (l *mtlsListener) mux(conn net.Conn) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case l.cqueue <- stream:
|
case l.cqueue <- stream:
|
||||||
case <-stream.GetDieCh():
|
|
||||||
stream.Close()
|
|
||||||
default:
|
default:
|
||||||
stream.Close()
|
stream.Close()
|
||||||
l.logger.Warnf("connection queue is full, client %s discarded", stream.RemoteAddr())
|
l.logger.Warnf("connection queue is full, client %s discarded", stream.RemoteAddr())
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
package mtls
|
package mtls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
mdata "github.com/go-gost/core/metadata"
|
mdata "github.com/go-gost/core/metadata"
|
||||||
mdutil "github.com/go-gost/core/metadata/util"
|
mdutil "github.com/go-gost/core/metadata/util"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -12,41 +11,26 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type metadata struct {
|
type metadata struct {
|
||||||
muxKeepAliveDisabled bool
|
muxCfg *mux.Config
|
||||||
muxKeepAliveInterval time.Duration
|
|
||||||
muxKeepAliveTimeout time.Duration
|
|
||||||
muxMaxFrameSize int
|
|
||||||
muxMaxReceiveBuffer int
|
|
||||||
muxMaxStreamBuffer int
|
|
||||||
|
|
||||||
backlog int
|
backlog int
|
||||||
mptcp bool
|
mptcp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *mtlsListener) parseMetadata(md mdata.Metadata) (err error) {
|
func (l *mtlsListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||||
const (
|
l.md.backlog = mdutil.GetInt(md, "backlog")
|
||||||
backlog = "backlog"
|
|
||||||
|
|
||||||
muxKeepAliveDisabled = "muxKeepAliveDisabled"
|
|
||||||
muxKeepAliveInterval = "muxKeepAliveInterval"
|
|
||||||
muxKeepAliveTimeout = "muxKeepAliveTimeout"
|
|
||||||
muxMaxFrameSize = "muxMaxFrameSize"
|
|
||||||
muxMaxReceiveBuffer = "muxMaxReceiveBuffer"
|
|
||||||
muxMaxStreamBuffer = "muxMaxStreamBuffer"
|
|
||||||
)
|
|
||||||
|
|
||||||
l.md.backlog = mdutil.GetInt(md, backlog)
|
|
||||||
if l.md.backlog <= 0 {
|
if l.md.backlog <= 0 {
|
||||||
l.md.backlog = defaultBacklog
|
l.md.backlog = defaultBacklog
|
||||||
}
|
}
|
||||||
|
|
||||||
l.md.muxKeepAliveDisabled = mdutil.GetBool(md, muxKeepAliveDisabled)
|
l.md.muxCfg = &mux.Config{
|
||||||
l.md.muxKeepAliveInterval = mdutil.GetDuration(md, muxKeepAliveInterval)
|
Version: mdutil.GetInt(md, "mux.version"),
|
||||||
l.md.muxKeepAliveTimeout = mdutil.GetDuration(md, muxKeepAliveTimeout)
|
KeepAliveInterval: mdutil.GetDuration(md, "mux.keepaliveInterval"),
|
||||||
l.md.muxMaxFrameSize = mdutil.GetInt(md, muxMaxFrameSize)
|
KeepAliveDisabled: mdutil.GetBool(md, "mux.keepaliveDisabled"),
|
||||||
l.md.muxMaxReceiveBuffer = mdutil.GetInt(md, muxMaxReceiveBuffer)
|
KeepAliveTimeout: mdutil.GetDuration(md, "mux.keepaliveTimeout"),
|
||||||
l.md.muxMaxStreamBuffer = mdutil.GetInt(md, muxMaxStreamBuffer)
|
MaxFrameSize: mdutil.GetInt(md, "mux.maxFrameSize"),
|
||||||
|
MaxReceiveBuffer: mdutil.GetInt(md, "mux.maxReceiveBuffer"),
|
||||||
|
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
|
||||||
|
}
|
||||||
l.md.mptcp = mdutil.GetBool(md, "mptcp")
|
l.md.mptcp = mdutil.GetBool(md, "mptcp")
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -14,13 +14,13 @@ import (
|
|||||||
admission "github.com/go-gost/x/admission/wrapper"
|
admission "github.com/go-gost/x/admission/wrapper"
|
||||||
xnet "github.com/go-gost/x/internal/net"
|
xnet "github.com/go-gost/x/internal/net"
|
||||||
"github.com/go-gost/x/internal/net/proxyproto"
|
"github.com/go-gost/x/internal/net/proxyproto"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
ws_util "github.com/go-gost/x/internal/util/ws"
|
ws_util "github.com/go-gost/x/internal/util/ws"
|
||||||
climiter "github.com/go-gost/x/limiter/conn/wrapper"
|
climiter "github.com/go-gost/x/limiter/conn/wrapper"
|
||||||
limiter "github.com/go-gost/x/limiter/traffic/wrapper"
|
limiter "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||||
metrics "github.com/go-gost/x/metrics/wrapper"
|
metrics "github.com/go-gost/x/metrics/wrapper"
|
||||||
"github.com/go-gost/x/registry"
|
"github.com/go-gost/x/registry"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/xtaci/smux"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -170,24 +170,7 @@ func (l *mwsListener) upgrade(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (l *mwsListener) mux(conn net.Conn) {
|
func (l *mwsListener) mux(conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
smuxConfig := smux.DefaultConfig()
|
session, err := mux.ServerSession(conn, l.md.muxCfg)
|
||||||
smuxConfig.KeepAliveDisabled = l.md.muxKeepAliveDisabled
|
|
||||||
if l.md.muxKeepAliveInterval > 0 {
|
|
||||||
smuxConfig.KeepAliveInterval = l.md.muxKeepAliveInterval
|
|
||||||
}
|
|
||||||
if l.md.muxKeepAliveTimeout > 0 {
|
|
||||||
smuxConfig.KeepAliveTimeout = l.md.muxKeepAliveTimeout
|
|
||||||
}
|
|
||||||
if l.md.muxMaxFrameSize > 0 {
|
|
||||||
smuxConfig.MaxFrameSize = l.md.muxMaxFrameSize
|
|
||||||
}
|
|
||||||
if l.md.muxMaxReceiveBuffer > 0 {
|
|
||||||
smuxConfig.MaxReceiveBuffer = l.md.muxMaxReceiveBuffer
|
|
||||||
}
|
|
||||||
if l.md.muxMaxStreamBuffer > 0 {
|
|
||||||
smuxConfig.MaxStreamBuffer = l.md.muxMaxStreamBuffer
|
|
||||||
}
|
|
||||||
session, err := smux.Server(conn, smuxConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logger.Error(err)
|
l.logger.Error(err)
|
||||||
return
|
return
|
||||||
@ -195,7 +178,7 @@ func (l *mwsListener) mux(conn net.Conn) {
|
|||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
stream, err := session.AcceptStream()
|
stream, err := session.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.logger.Error("accept stream: ", err)
|
l.logger.Error("accept stream: ", err)
|
||||||
return
|
return
|
||||||
@ -203,8 +186,6 @@ func (l *mwsListener) mux(conn net.Conn) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case l.cqueue <- stream:
|
case l.cqueue <- stream:
|
||||||
case <-stream.GetDieCh():
|
|
||||||
stream.Close()
|
|
||||||
default:
|
default:
|
||||||
stream.Close()
|
stream.Close()
|
||||||
l.logger.Warnf("connection queue is full, client %s discarded", stream.RemoteAddr())
|
l.logger.Warnf("connection queue is full, client %s discarded", stream.RemoteAddr())
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
mdata "github.com/go-gost/core/metadata"
|
mdata "github.com/go-gost/core/metadata"
|
||||||
mdutil "github.com/go-gost/core/metadata/util"
|
mdutil "github.com/go-gost/core/metadata/util"
|
||||||
|
"github.com/go-gost/x/internal/util/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -24,12 +25,7 @@ type metadata struct {
|
|||||||
writeBufferSize int
|
writeBufferSize int
|
||||||
enableCompression bool
|
enableCompression bool
|
||||||
|
|
||||||
muxKeepAliveDisabled bool
|
muxCfg *mux.Config
|
||||||
muxKeepAliveInterval time.Duration
|
|
||||||
muxKeepAliveTimeout time.Duration
|
|
||||||
muxMaxFrameSize int
|
|
||||||
muxMaxReceiveBuffer int
|
|
||||||
muxMaxStreamBuffer int
|
|
||||||
|
|
||||||
mptcp bool
|
mptcp bool
|
||||||
}
|
}
|
||||||
@ -70,12 +66,15 @@ func (l *mwsListener) parseMetadata(md mdata.Metadata) (err error) {
|
|||||||
l.md.writeBufferSize = mdutil.GetInt(md, writeBufferSize)
|
l.md.writeBufferSize = mdutil.GetInt(md, writeBufferSize)
|
||||||
l.md.enableCompression = mdutil.GetBool(md, enableCompression)
|
l.md.enableCompression = mdutil.GetBool(md, enableCompression)
|
||||||
|
|
||||||
l.md.muxKeepAliveDisabled = mdutil.GetBool(md, muxKeepAliveDisabled)
|
l.md.muxCfg = &mux.Config{
|
||||||
l.md.muxKeepAliveInterval = mdutil.GetDuration(md, muxKeepAliveInterval)
|
Version: mdutil.GetInt(md, "mux.version"),
|
||||||
l.md.muxKeepAliveTimeout = mdutil.GetDuration(md, muxKeepAliveTimeout)
|
KeepAliveInterval: mdutil.GetDuration(md, "mux.keepaliveInterval"),
|
||||||
l.md.muxMaxFrameSize = mdutil.GetInt(md, muxMaxFrameSize)
|
KeepAliveDisabled: mdutil.GetBool(md, "mux.keepaliveDisabled"),
|
||||||
l.md.muxMaxReceiveBuffer = mdutil.GetInt(md, muxMaxReceiveBuffer)
|
KeepAliveTimeout: mdutil.GetDuration(md, "mux.keepaliveTimeout"),
|
||||||
l.md.muxMaxStreamBuffer = mdutil.GetInt(md, muxMaxStreamBuffer)
|
MaxFrameSize: mdutil.GetInt(md, "mux.maxFrameSize"),
|
||||||
|
MaxReceiveBuffer: mdutil.GetInt(md, "mux.maxReceiveBuffer"),
|
||||||
|
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
|
||||||
|
}
|
||||||
|
|
||||||
if mm := mdutil.GetStringMapString(md, header); len(mm) > 0 {
|
if mm := mdutil.GetStringMapString(md, header); len(mm) > 0 {
|
||||||
hd := http.Header{}
|
hd := http.Header{}
|
||||||
|
Loading…
Reference in New Issue
Block a user