Skip to content

Commit

Permalink
add ws dialer in NewMWSSTransporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jul 14, 2020
1 parent 218aaeb commit 8d88ec4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/ehco/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var PprofPort string
func main() {
app := cli.NewApp()
app.Name = "ehco"
app.Version = "0.1.4"
app.Version = "0.1.5"
app.Usage = "ehco is a network relay tool and a typo :)"
app.Flags = []cli.Flag{
&cli.StringFlag{
Expand Down
15 changes: 9 additions & 6 deletions internal/relay/mwss.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
type mwssTransporter struct {
sessions map[string][]*muxSession
sessionMutex sync.Mutex
dialer ws.Dialer
}

func NewMWSSTransporter() *mwssTransporter {
return &mwssTransporter{
sessions: make(map[string][]*muxSession),
dialer: ws.Dialer{TLSConfig: DefaultTLSConfig, Timeout: DialTimeOut},
}
}

Expand Down Expand Up @@ -61,17 +63,18 @@ func (tr *mwssTransporter) Dial(addr string) (conn net.Conn, err error) {
session.Close()
return nil, err
}
// close last not used session
if lastSession := sessions[len(sessions)-1]; lastSession.NumStreams() == 0 {
lastSession.Close()
if len(sessions) > 1 {
// close last not used session, but we keep one conn in session pool
if lastSession := sessions[len(sessions)-1]; lastSession.NumStreams() == 0 {
lastSession.Close()
}
}
tr.sessions[addr] = sessions
return cc, nil
}

func (tr *mwssTransporter) initSession(addr string) (*muxSession, error) {
d := ws.Dialer{TLSConfig: DefaultTLSConfig, Timeout: DialTimeOut}
rc, _, _, err := d.Dial(context.TODO(), addr)
rc, _, _, err := tr.dialer.Dial(context.TODO(), addr)
if err != nil {
return nil, err
}
Expand All @@ -81,7 +84,7 @@ func (tr *mwssTransporter) initSession(addr string) (*muxSession, error) {
if err != nil {
return nil, err
}
Logger.Infof("[mwss] Init new session %s", session.RemoteAddr())
Logger.Infof("[mwss] Init new session: %v ", session)
return &muxSession{conn: rc, session: session, maxStreamCnt: MaxMWSSStreamCnt}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

const (
MaxMWSSStreamCnt = 10
DialTimeOut = 10 * time.Second
DialTimeOut = 3 * time.Second

Listen_RAW = "raw"
Listen_WS = "ws"
Expand Down

0 comments on commit 8d88ec4

Please sign in to comment.