From 77c03f0da5e6bc67f0fefcf8fad6b0668909fc81 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 3 Nov 2018 13:03:02 +0100 Subject: [PATCH] move back to serial --- common/mux/frame.go | 6 +++--- common/mux/reader.go | 4 ++-- common/mux/writer.go | 4 ++-- common/protocol/address.go | 4 ++-- common/{vio => serial}/serial.go | 2 +- common/{vio => serial}/serial_test.go | 6 +++--- proxy/shadowsocks/ota.go | 4 ++-- proxy/socks/protocol.go | 4 ++-- proxy/vmess/encoding/client.go | 12 ++++++------ proxy/vmess/encoding/commands.go | 6 +++--- proxy/vmess/validator.go | 5 ++--- proxy/vmess/validator_test.go | 6 +++--- 12 files changed, 31 insertions(+), 32 deletions(-) rename common/{vio => serial}/serial.go (97%) rename common/{vio => serial}/serial_test.go (81%) diff --git a/common/mux/frame.go b/common/mux/frame.go index 495e39b79..5bab6586d 100644 --- a/common/mux/frame.go +++ b/common/mux/frame.go @@ -9,7 +9,7 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" - "v2ray.com/core/common/vio" + "v2ray.com/core/common/serial" ) type SessionStatus byte @@ -65,7 +65,7 @@ func (f FrameMetadata) WriteTo(b *buf.Buffer) error { lenBytes := b.Bytes() len0 := b.Len() - if _, err := vio.WriteUint16(b, f.SessionID); err != nil { + if _, err := serial.WriteUint16(b, f.SessionID); err != nil { return err } @@ -91,7 +91,7 @@ func (f FrameMetadata) WriteTo(b *buf.Buffer) error { // Unmarshal reads FrameMetadata from the given reader. func (f *FrameMetadata) Unmarshal(reader io.Reader) error { - metaLen, err := vio.ReadUint16(reader) + metaLen, err := serial.ReadUint16(reader) if err != nil { return err } diff --git a/common/mux/reader.go b/common/mux/reader.go index b6def6892..d9ace3dbf 100644 --- a/common/mux/reader.go +++ b/common/mux/reader.go @@ -5,7 +5,7 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/crypto" - "v2ray.com/core/common/vio" + "v2ray.com/core/common/serial" ) // PacketReader is an io.Reader that reads whole chunk of Mux frames every time. @@ -28,7 +28,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return nil, io.EOF } - size, err := vio.ReadUint16(r.reader) + size, err := serial.ReadUint16(r.reader) if err != nil { return nil, err } diff --git a/common/mux/writer.go b/common/mux/writer.go index d879cb1fa..482d3362a 100644 --- a/common/mux/writer.go +++ b/common/mux/writer.go @@ -5,7 +5,7 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" - "v2ray.com/core/common/vio" + "v2ray.com/core/common/serial" ) type Writer struct { @@ -66,7 +66,7 @@ func writeMetaWithFrame(writer buf.Writer, meta FrameMetadata, data buf.MultiBuf if err := meta.WriteTo(frame); err != nil { return err } - if _, err := vio.WriteUint16(frame, uint16(data.Len())); err != nil { + if _, err := serial.WriteUint16(frame, uint16(data.Len())); err != nil { return err } diff --git a/common/protocol/address.go b/common/protocol/address.go index 70028469d..c7058865a 100644 --- a/common/protocol/address.go +++ b/common/protocol/address.go @@ -6,8 +6,8 @@ import ( "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/net" + "v2ray.com/core/common/serial" "v2ray.com/core/common/task" - "v2ray.com/core/common/vio" ) type AddressOption func(*AddressParser) @@ -167,7 +167,7 @@ func (p *AddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (ne } func (p *AddressParser) writePort(writer io.Writer, port net.Port) error { - return common.Error2(vio.WriteUint16(writer, port.Value())) + return common.Error2(serial.WriteUint16(writer, port.Value())) } func (p *AddressParser) writeAddress(writer io.Writer, address net.Address) error { diff --git a/common/vio/serial.go b/common/serial/serial.go similarity index 97% rename from common/vio/serial.go rename to common/serial/serial.go index cb2b56e5e..e58b3a36f 100644 --- a/common/vio/serial.go +++ b/common/serial/serial.go @@ -1,4 +1,4 @@ -package vio +package serial import ( "encoding/binary" diff --git a/common/vio/serial_test.go b/common/serial/serial_test.go similarity index 81% rename from common/vio/serial_test.go rename to common/serial/serial_test.go index ebbab130b..f0c3d3a9d 100644 --- a/common/vio/serial_test.go +++ b/common/serial/serial_test.go @@ -1,4 +1,4 @@ -package vio_test +package serial_test import ( "testing" @@ -6,14 +6,14 @@ import ( "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/compare" - "v2ray.com/core/common/vio" + "v2ray.com/core/common/serial" ) func TestUint32Serial(t *testing.T) { b := buf.New() defer b.Release() - n, err := vio.WriteUint32(b, 10) + n, err := serial.WriteUint32(b, 10) common.Must(err) if n != 4 { t.Error("expect 4 bytes writtng, but actually ", n) diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index 3df85a9f5..106e13111 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -10,7 +10,7 @@ import ( "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/bytespool" - "v2ray.com/core/common/vio" + "v2ray.com/core/common/serial" ) const ( @@ -70,7 +70,7 @@ func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader { } func (v *ChunkReader) ReadMultiBuffer() (buf.MultiBuffer, error) { - size, err := vio.ReadUint16(v.reader) + size, err := serial.ReadUint16(v.reader) if err != nil { return nil, newError("failed to read size") } diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index a7411e9ad..5c1f23a43 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -7,7 +7,7 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" - "v2ray.com/core/common/vio" + "v2ray.com/core/common/serial" ) const ( @@ -260,7 +260,7 @@ func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, po defer buffer.Release() common.Must2(buffer.WriteBytes(0x00, errCode)) - common.Must2(vio.WriteUint16(buffer, port.Value())) + common.Must2(serial.WriteUint16(buffer, port.Value())) common.Must2(buffer.Write(address.IP())) return buf.WriteAllBytes(writer, buffer.Bytes()) } diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index 12356c4e2..6050bfabb 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -16,15 +16,15 @@ import ( "v2ray.com/core/common/crypto" "v2ray.com/core/common/dice" "v2ray.com/core/common/protocol" - "v2ray.com/core/common/vio" + "v2ray.com/core/common/serial" "v2ray.com/core/proxy/vmess" ) func hashTimestamp(h hash.Hash, t protocol.Timestamp) []byte { - vio.WriteUint64(h, uint64(t)) - vio.WriteUint64(h, uint64(t)) - vio.WriteUint64(h, uint64(t)) - vio.WriteUint64(h, uint64(t)) + serial.WriteUint64(h, uint64(t)) + serial.WriteUint64(h, uint64(t)) + serial.WriteUint64(h, uint64(t)) + serial.WriteUint64(h, uint64(t)) return h.Sum(nil) } @@ -59,7 +59,7 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)() account := header.User.Account.(*vmess.MemoryAccount) idHash := c.idHash(account.AnyValidID().Bytes()) - common.Must2(vio.WriteUint64(idHash, uint64(timestamp))) + common.Must2(serial.WriteUint64(idHash, uint64(timestamp))) common.Must2(writer.Write(idHash.Sum(nil))) buffer := buf.New() diff --git a/proxy/vmess/encoding/commands.go b/proxy/vmess/encoding/commands.go index 0be8741a9..4b0bc4479 100644 --- a/proxy/vmess/encoding/commands.go +++ b/proxy/vmess/encoding/commands.go @@ -8,8 +8,8 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" + "v2ray.com/core/common/serial" "v2ray.com/core/common/uuid" - "v2ray.com/core/common/vio" ) var ( @@ -96,11 +96,11 @@ func (f *CommandSwitchAccountFactory) Marshal(command interface{}, writer io.Wri common.Must2(writer.Write([]byte(hostStr))) } - common.Must2(vio.WriteUint16(writer, cmd.Port.Value())) + common.Must2(serial.WriteUint16(writer, cmd.Port.Value())) idBytes := cmd.ID.Bytes() common.Must2(writer.Write(idBytes)) - common.Must2(vio.WriteUint16(writer, cmd.AlterIds)) + common.Must2(serial.WriteUint16(writer, cmd.AlterIds)) common.Must2(writer.Write([]byte{byte(cmd.Level)})) common.Must2(writer.Write([]byte{cmd.ValidMin})) diff --git a/proxy/vmess/validator.go b/proxy/vmess/validator.go index ea06ed5c9..2f9ec7b0a 100644 --- a/proxy/vmess/validator.go +++ b/proxy/vmess/validator.go @@ -5,10 +5,9 @@ import ( "sync" "time" - "v2ray.com/core/common/vio" - "v2ray.com/core/common" "v2ray.com/core/common/protocol" + "v2ray.com/core/common/serial" "v2ray.com/core/common/task" ) @@ -66,7 +65,7 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user * genBeginSec = nowSec - cacheDurationSec } for ts := genBeginSec; ts <= genEndSec; ts++ { - common.Must2(vio.WriteUint64(idHash, uint64(ts))) + common.Must2(serial.WriteUint64(idHash, uint64(ts))) idHash.Sum(hashValue[:0]) idHash.Reset() diff --git a/proxy/vmess/validator_test.go b/proxy/vmess/validator_test.go index a82ae1efe..7e0efc96b 100644 --- a/proxy/vmess/validator_test.go +++ b/proxy/vmess/validator_test.go @@ -6,8 +6,8 @@ import ( "v2ray.com/core/common" "v2ray.com/core/common/protocol" + "v2ray.com/core/common/serial" "v2ray.com/core/common/uuid" - "v2ray.com/core/common/vio" . "v2ray.com/core/proxy/vmess" . "v2ray.com/ext/assert" ) @@ -39,7 +39,7 @@ func TestUserValidator(t *testing.T) { testSmallLag := func(lag time.Duration) { ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix()) idHash := hasher(id.Bytes()) - common.Must2(vio.WriteUint64(idHash, uint64(ts))) + common.Must2(serial.WriteUint64(idHash, uint64(ts))) userHash := idHash.Sum(nil) euser, ets, found := v.Get(userHash) @@ -61,7 +61,7 @@ func TestUserValidator(t *testing.T) { testBigLag := func(lag time.Duration) { ts := protocol.Timestamp(time.Now().Add(time.Second * lag).Unix()) idHash := hasher(id.Bytes()) - common.Must2(vio.WriteUint64(idHash, uint64(ts))) + common.Must2(serial.WriteUint64(idHash, uint64(ts))) userHash := idHash.Sum(nil) euser, _, found := v.Get(userHash)