Skip to content

Commit

Permalink
rawcopy leads mistakes in smux in v2
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Dec 29, 2019
1 parent 34bc48f commit fc57c98
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 126 deletions.
9 changes: 3 additions & 6 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
var VERSION = "SELFBUILD"

// handleClient aggregates connection p1 on mux with 'writeLock'
func handleClient(session *smux.Session, p1 net.Conn, ctrl *generic.CopyControl, quiet bool) {
func handleClient(session *smux.Session, p1 net.Conn, quiet bool) {
logln := func(v ...interface{}) {
if !quiet {
log.Println(v...)
Expand All @@ -52,7 +52,7 @@ func handleClient(session *smux.Session, p1 net.Conn, ctrl *generic.CopyControl,

// start tunnel & wait for tunnel termination
streamCopy := func(dst io.Writer, src io.ReadCloser) {
if _, err := generic.Copy(dst, src, ctrl); err != nil {
if _, err := generic.Copy(dst, src); err != nil {
// report protocol error
if err == smux.ErrInvalidProtocol {
log.Println("smux", err, "in:", p1.RemoteAddr(), "out:", fmt.Sprint(p2.RemoteAddr(), "(", p2.ID(), ")"))
Expand Down Expand Up @@ -425,13 +425,11 @@ func main() {
muxes := make([]struct {
session *smux.Session
ttl time.Time
ctrl *generic.CopyControl // for control of memory in copying
}, numconn)

for k := range muxes {
muxes[k].session = waitConn()
muxes[k].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
muxes[k].ctrl = &generic.CopyControl{Buffer: make([]byte, bufSize)}
}

chScavenger := make(chan *smux.Session, 128)
Expand All @@ -450,10 +448,9 @@ func main() {
chScavenger <- muxes[idx].session
muxes[idx].session = waitConn()
muxes[idx].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
muxes[idx].ctrl = &generic.CopyControl{Buffer: make([]byte, bufSize)}
}

go handleClient(muxes[idx].session, p1, muxes[idx].ctrl, config.Quiet)
go handleClient(muxes[idx].session, p1, config.Quiet)
rr++
}
}
Expand Down
18 changes: 1 addition & 17 deletions generic/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ package generic

import (
"io"
"net"
"sync"
)

const bufSize = 4096

type CopyControl struct {
Buffer []byte // shared buffer for copying controlled by mutex
sync.Mutex
}

// Memory optimized io.Copy function specified for this library
func Copy(dst io.Writer, src io.Reader, ctrl *CopyControl) (written int64, err error) {
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
// If the reader has a WriteTo method, use it to do the copy.
// Avoids an allocation and a copy.
if wt, ok := src.(io.WriterTo); ok {
Expand All @@ -25,15 +18,6 @@ func Copy(dst io.Writer, src io.Reader, ctrl *CopyControl) (written int64, err e
return rt.ReadFrom(src)
}

// if src is net.TCPConn, and dst is a multiplexed connection
// reading can be controlled by writable events of smux
// and make the reading serialized
if tcpconn, ok := src.(*net.TCPConn); ok {
if ctrl != nil {
return rawCopy(dst, tcpconn, ctrl)
}
}

// fallback to standard io.CopyBuffer
buf := make([]byte, bufSize)
return io.CopyBuffer(dst, src, buf)
Expand Down
83 changes: 0 additions & 83 deletions generic/rawcopy_unix.go

This file was deleted.

14 changes: 0 additions & 14 deletions generic/rawcopy_windows.go

This file was deleted.

9 changes: 3 additions & 6 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ func handleMux(conn net.Conn, config *Config) {
}
defer mux.Close()

// copy to stream control
copyControl := &generic.CopyControl{Buffer: make([]byte, bufSize)}

for {
stream, err := mux.AcceptStream()
if err != nil {
Expand All @@ -81,12 +78,12 @@ func handleMux(conn net.Conn, config *Config) {
p1.Close()
return
}
handleClient(p1, p2, copyControl, config.Quiet)
handleClient(p1, p2, config.Quiet)
}(stream)
}
}

func handleClient(p1 *smux.Stream, p2 net.Conn, ctrl *generic.CopyControl, quiet bool) {
func handleClient(p1 *smux.Stream, p2 net.Conn, quiet bool) {
logln := func(v ...interface{}) {
if !quiet {
log.Println(v...)
Expand All @@ -101,7 +98,7 @@ func handleClient(p1 *smux.Stream, p2 net.Conn, ctrl *generic.CopyControl, quiet

// start tunnel & wait for tunnel termination
streamCopy := func(dst io.Writer, src io.ReadCloser) {
if _, err := generic.Copy(dst, src, ctrl); err != nil {
if _, err := generic.Copy(dst, src); err != nil {
if err == smux.ErrInvalidProtocol {
log.Println("smux", err, "in:", fmt.Sprint(p1.RemoteAddr(), "(", p1.ID(), ")"), "out:", p2.RemoteAddr())
}
Expand Down

0 comments on commit fc57c98

Please sign in to comment.