Skip to content

Commit

Permalink
Fix shadow conn
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Jan 12, 2019
1 parent b2282a5 commit 54dadd0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 39 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func StartClient(netType, server, password string) {
case TCP.String():
client = tcp.NewClient()
}
glog.Infoln("Client started.")

for {
conn := <-connCh
Expand Down
1 change: 1 addition & 0 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 18 additions & 37 deletions shadow/shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 54dadd0

Please sign in to comment.