Skip to content

Commit

Permalink
fix type assert
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Feb 28, 2020
1 parent e203d77 commit ee8b5d5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
17 changes: 17 additions & 0 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,20 @@ func (c *relayConn) Read(b []byte) (n int, err error) {
resp := new(relay.Response)
_, err = resp.ReadFrom(c.Conn)
if err != nil {
log.Log("[relay] read:", err)
return
}
if resp.Version != relay.Version1 {
err = relay.ErrBadVersion
log.Log("[relay] read:", err)
return
}
if resp.Status != relay.StatusOK {
err = fmt.Errorf("status %d", resp.Status)
log.Log("[relay] read:", err)
return
}
log.Log("[relay] read response OK")
})

if !c.udp {
Expand All @@ -292,6 +296,7 @@ func (c *relayConn) Read(b []byte) (n int, err error) {
return
}
dlen := int(binary.BigEndian.Uint16(bb[:]))
log.Log("[relay] read udp", dlen)
if len(b) >= dlen {
return io.ReadFull(c.Conn, b[:dlen])
}
Expand All @@ -301,6 +306,12 @@ func (c *relayConn) Read(b []byte) (n int, err error) {
return
}

func (c *relayConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
n, err = c.Read(b)
addr = c.Conn.RemoteAddr()
return
}

func (c *relayConn) Write(b []byte) (n int, err error) {
if len(b) > 0xFFFF {
err = errors.New("write: data maximum exceeded")
Expand All @@ -314,6 +325,7 @@ func (c *relayConn) Write(b []byte) (n int, err error) {
c.wbuf.Write(bb[:])
}
c.wbuf.Write(b) // append the data to the cached header
log.Log("[relay] write wbuf", len(b))
// _, err = c.Conn.Write(c.wbuf.Bytes())
// c.wbuf.Reset()
_, err = c.wbuf.WriteTo(c.Conn)
Expand All @@ -327,5 +339,10 @@ func (c *relayConn) Write(b []byte) (n int, err error) {
binary.BigEndian.PutUint16(buf[:2], uint16(len(b)))
n = copy(buf[2:], b)
_, err = c.Conn.Write(buf)
log.Log("[relay] write", n)
return
}

func (c *relayConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
return c.Write(b)
}
8 changes: 7 additions & 1 deletion ss.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,13 @@ func (h *shadowUDPHandler) Handle(conn net.Conn) {
log.Logf("[ssu] %s: %s", conn.LocalAddr(), err)
return
}
cc = c.(net.PacketConn)
var ok bool
cc, ok = c.(net.PacketConn)
if !ok {
log.Logf("[ssu] %s: not a packet connection", conn.LocalAddr())
return
}

defer cc.Close()

pc, ok := conn.(net.PacketConn)
Expand Down
8 changes: 7 additions & 1 deletion tuntap.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ func (h *tunHandler) Handle(conn net.Conn) {
if err != nil {
return err
}
pc = cc.(net.PacketConn)
var ok bool
pc, ok = cc.(net.PacketConn)
if !ok {
err = errors.New("not a packet connection")
log.Logf("[tun] %s - %s: %s", conn.LocalAddr(), raddr, err)
return err
}
} else {
if h.options.TCPMode {
if raddr != nil {
Expand Down

0 comments on commit ee8b5d5

Please sign in to comment.