Skip to content

Commit

Permalink
Support options for emux
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 18, 2021
1 parent c71f50e commit 4075bc8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/wzshiming/anyproxy v0.5.2
github.com/wzshiming/cmux v0.2.2
github.com/wzshiming/commandproxy v0.2.0
github.com/wzshiming/emux v0.1.3
github.com/wzshiming/emux v0.1.4
github.com/wzshiming/httpproxy v0.4.1
github.com/wzshiming/notify v0.1.0
github.com/wzshiming/schedialer v0.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/wzshiming/cmux v0.2.2 h1:U6IH96kRFxnnjy3duskHvp237o8cK6gILQYIROE3NI4=
github.com/wzshiming/cmux v0.2.2/go.mod h1:Vgdbl+BdSp0X3zHw3P+XW4sxyorR5y9NiKjRGRRnJdo=
github.com/wzshiming/commandproxy v0.2.0 h1:uPVhgIj2YSncRUo6g9smGR6OMzsIg7lwklcMHPPmEeM=
github.com/wzshiming/commandproxy v0.2.0/go.mod h1:wS6+aJ9KMHciqYX3xmDO0W+QVY0zvngeBvmoIFMfq8A=
github.com/wzshiming/emux v0.1.3 h1:afz+iUWEmbeDMiq9CRuO2NySZPlDny7/mF6sNzE8Jp0=
github.com/wzshiming/emux v0.1.3/go.mod h1:VQF6NoR4nfm3+OrKZLx47JuxuDeWemHDc0a4qDNtFtg=
github.com/wzshiming/emux v0.1.4 h1:oZzViIVdFrFuKto3Ii2bqvZT/X4GRqvrrZEYAoYxLhQ=
github.com/wzshiming/emux v0.1.4/go.mod h1:VQF6NoR4nfm3+OrKZLx47JuxuDeWemHDc0a4qDNtFtg=
github.com/wzshiming/httpproxy v0.4.1 h1:6K78q0Vi+/GM4h9KygTGi7nuVn5Xgk5oHqjJyaGLHcQ=
github.com/wzshiming/httpproxy v0.4.1/go.mod h1:drZR+iQLfYsQi7/nETLjyB9Z0f5KDXQo2PEkJBbg3xQ=
github.com/wzshiming/notify v0.1.0 h1:BZND3IfiWkyDseAiaqLb43mTU8vefmmK0gOdnuschPI=
Expand Down
87 changes: 84 additions & 3 deletions protocols/emux/smux.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,104 @@
package emux

import (
"net/url"
"strconv"

"github.com/wzshiming/bridge"
"github.com/wzshiming/bridge/protocols/local"
"github.com/wzshiming/emux"
)

// EMux emux:?handshake=EMUX%20
func EMux(dialer bridge.Dialer, addr string) (bridge.Dialer, error) {
if dialer == nil {
dialer = local.LOCAL
}
handshake, instruction, err := parseConfig(addr)
if err != nil {
return nil, err
}

d := emux.NewDialer(dialer)
d.Instruction = *instruction
if handshake != nil {
if len(handshake) == 0 {
d.Handshake = nil
} else {
d.Handshake = emux.NewHandshake(handshake, true)
}
}
if listenConfig, ok := dialer.(bridge.ListenConfig); ok {
l := emux.NewListenConfig(listenConfig)
l.Instruction = *instruction
if handshake != nil {
if len(handshake) == 0 {
d.Handshake = nil
} else {
l.Handshake = emux.NewHandshake(handshake, false)
}
}
return struct {
bridge.Dialer
bridge.ListenConfig
}{
Dialer: emux.NewDialer(dialer),
ListenConfig: emux.NewListenConfig(listenConfig),
Dialer: d,
ListenConfig: l,
}, nil
}
return emux.NewDialer(dialer), nil
return d, nil
}

func parseConfig(addr string) ([]byte, *emux.Instruction, error) {
u, err := url.Parse(addr)
if err != nil {
return nil, nil, err
}
var handshake []byte
instruction := emux.DefaultInstruction
query := u.Query()
for key := range query {
switch key {
case "handshake":
handshake = []byte(query.Get(key))
case "close":
u, err := strconv.ParseUint(query.Get(key), 0, 8)
if err != nil {
return nil, nil, err
}
instruction.Close = uint8(u)
case "connect":
u, err := strconv.ParseUint(query.Get(key), 0, 8)
if err != nil {
return nil, nil, err
}
instruction.Connect = uint8(u)
case "connected":
u, err := strconv.ParseUint(query.Get(key), 0, 8)
if err != nil {
return nil, nil, err
}
instruction.Connected = uint8(u)
case "disconnect":
u, err := strconv.ParseUint(query.Get(key), 0, 8)
if err != nil {
return nil, nil, err
}
instruction.Disconnect = uint8(u)
case "disconnected":
u, err := strconv.ParseUint(query.Get(key), 0, 8)
if err != nil {
return nil, nil, err
}
instruction.Disconnected = uint8(u)
case "data":
u, err := strconv.ParseUint(query.Get(key), 0, 8)
if err != nil {
return nil, nil, err
}
instruction.Data = uint8(u)
}
}

return handshake, &instruction, nil
}

0 comments on commit 4075bc8

Please sign in to comment.