Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: h2 server support #96

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type bufferedReader struct {
buffer bytes.Buffer
bufferRead int
bufferSize int
stagePoint int
sniffing bool
lastErr error
}
Expand Down Expand Up @@ -65,3 +66,11 @@ func (s *bufferedReader) reset(snif bool) {
s.bufferRead = 0
s.bufferSize = s.buffer.Len()
}

func (s *bufferedReader) newStage() {
s.stagePoint = s.buffer.Len()
}

func (s *bufferedReader) discard() {
s.buffer.Truncate(s.stagePoint)
}
52 changes: 50 additions & 2 deletions cmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmux

import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -189,7 +191,7 @@ func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) {
}
for _, sl := range m.sls {
for _, s := range sl.ss {
matched := s(muc.Conn, muc.startSniffing())
matched := s(muc.getConn(), muc.startSniffing())
if matched {
muc.doneSniffing()
if m.readTimeout > noTimeout {
Expand Down Expand Up @@ -276,7 +278,13 @@ type MuxConn struct {
buf bufferedReader
}

func newMuxConn(c net.Conn) *MuxConn {
func newMuxConn(c net.Conn) muxConn {
if tlsconn, ok := c.(TLSConn); ok {
return &MuxTLSConn{
TLSConn: tlsconn,
buf: bufferedReader{source: c},
}
}
return &MuxConn{
Conn: c,
buf: bufferedReader{source: c},
Expand Down Expand Up @@ -305,3 +313,43 @@ func (m *MuxConn) startSniffing() io.Reader {
func (m *MuxConn) doneSniffing() {
m.buf.reset(false)
}

func (m *MuxConn) getConn() net.Conn { return m.Conn }

type muxConn interface {
net.Conn
startSniffing() io.Reader
doneSniffing()
getConn() net.Conn
}

type TLSConn interface {
net.Conn
CloseWrite() error
ConnectionState() tls.ConnectionState
Handshake() error
HandshakeContext(ctx context.Context) error
NetConn() net.Conn
OCSPResponse() []byte
VerifyHostname(host string) error
}

var _ TLSConn = (*tls.Conn)(nil)

type MuxTLSConn struct {
TLSConn
buf bufferedReader
}

func (m *MuxTLSConn) Read(p []byte) (int, error) { return m.buf.Read(p) }

func (m *MuxTLSConn) startSniffing() io.Reader {
m.buf.reset(true)
return &m.buf
}

func (m *MuxTLSConn) doneSniffing() {
m.buf.reset(false)
}

func (m *MuxTLSConn) getConn() net.Conn { return m.TLSConn }
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/soheilhy/cmux

go 1.11
go 1.18

require golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb
require golang.org/x/net v0.0.0-20220927171203-f486391704dc

require golang.org/x/text v0.3.7 // indirect
16 changes: 4 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/net v0.0.0-20220927171203-f486391704dc h1:FxpXZdoBqT8RjqTy6i1E8nXHhW21wK7ptQ/EPIGxzPQ=
golang.org/x/net v0.0.0-20220927171203-f486391704dc/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Loading