From 54dadd0b1aa87088d2be7ccac0f0ca000460be10 Mon Sep 17 00:00:00 2001 From: wweir Date: Sat, 12 Jan 2019 13:22:24 +0800 Subject: [PATCH] Fix shadow conn --- Makefile | 8 +++++-- proxy/client.go | 1 + proxy/server.go | 1 + shadow/shadow.go | 55 ++++++++++++++++-------------------------------- 4 files changed, 26 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 11cdb37..90f012f 100644 --- a/Makefile +++ b/Makefile @@ -18,5 +18,9 @@ server: build kill $(PWD)/sower -n QUIC -logtostderr -v 1 run: build kill - $(PWD)/sower -n QUIC -logtostderr -v 1 & - sudo $(PWD)/sower -f conf/sower.toml -logtostderr + $(PWD)/sower -n QUIC -logtostderr -v 1 & + sudo $(PWD)/sower -f conf/sower.toml -logtostderr & + sleep 1 + @curl 127.0.0.1 + sleep 1 + sudo pkill -9 sower || true diff --git a/proxy/client.go b/proxy/client.go index 7c9efa3..45268b9 100644 --- a/proxy/client.go +++ b/proxy/client.go @@ -25,6 +25,7 @@ func StartClient(netType, server, password string) { case TCP.String(): client = tcp.NewClient() } + glog.Infoln("Client started.") for { conn := <-connCh diff --git a/proxy/server.go b/proxy/server.go index f367cf9..ff87d5c 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -37,6 +37,7 @@ func StartServer(netType, port, password string) { if err != nil { glog.Fatalf("listen %v fail: %s", port, err) } + glog.Infoln("Server started.") for { conn := <-connCh diff --git a/shadow/shadow.go b/shadow/shadow.go index 908fc43..e1421ec 100644 --- a/shadow/shadow.go +++ b/shadow/shadow.go @@ -31,52 +31,33 @@ type conn struct { } func (c *conn) Read(b []byte) (n int, err error) { - bLength := len(b) - offset := 0 - switch c.readOffset { - case -1: - return 0, io.EOF - case 0: // read from conn - default: // read from buffer - offset = copy(b, c.readBuf[c.readOffset:c.readLast]) + // read from buffer + if c.readOffset != 0 { + offset := copy(b, c.readBuf[c.readOffset:c.readLast]) if offset+c.readOffset < c.readLast { c.readOffset += offset - return offset, nil - } - - if c.readLast < c.dataSize { - c.readOffset = -1 - return offset, io.EOF + } else { + c.readOffset = 0 } - c.readOffset = 0 + return offset, nil } // read from conn - for ; offset < bLength; offset += c.readOffset { - _, err = io.ReadFull(c.Conn, c.readBuf) - if err != nil && err != io.EOF { - c.readOffset = 0 - return offset, err - } + if _, err := io.ReadFull(c.Conn, c.readBuf); err != nil { + return 0, err + } - _, e := c.aead.Open(c.readBuf[:0], c.decryptNonce(), c.readBuf, nil) - if e != nil { - c.readOffset = 0 - return offset, e - } + if _, err := c.aead.Open(c.readBuf[:0], c.decryptNonce(), c.readBuf, nil); err != nil { + return 0, err + } - c.readLast = int(c.readBuf[0])<<8 + int(c.readBuf[1]) + 2 - c.readOffset = copy(b[offset:], c.readBuf[2:2+c.readLast]) - if c.readOffset < c.readLast { - c.readOffset += 2 - return len(b), nil - } else if err == io.EOF { - readOffset := c.readOffset - c.readOffset = 0 - return readOffset, err - } + // BigEndian + c.readLast = 2 + int(c.readBuf[0])<<8 + int(c.readBuf[1]) + c.readOffset = 2 + copy(b, c.readBuf[2:c.readLast]) + if c.readOffset < c.readLast { + return len(b), nil } - return len(b), err + return c.readOffset - 2, nil } func (c *conn) Write(b []byte) (n int, err error) {