From 0f6e0d37b5571052532b104c54998d9d8a4d62fc Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Wed, 21 Oct 2020 16:42:24 +0100 Subject: [PATCH] Rename constants to camel case --- internal/client/auth_test.go | 2 +- internal/client/state.go | 6 ++-- internal/multiplex/datagramBufferedPipe.go | 2 +- .../multiplex/datagramBufferedPipe_test.go | 2 +- internal/multiplex/frame.go | 6 ++-- internal/multiplex/mux_test.go | 2 +- internal/multiplex/obfs.go | 30 ++++++++-------- internal/multiplex/obfs_test.go | 6 ++-- internal/multiplex/session.go | 12 +++---- internal/multiplex/session_fuzz.go | 2 +- internal/multiplex/session_test.go | 36 +++++++++---------- internal/multiplex/stream.go | 10 +++--- internal/multiplex/streamBuffer.go | 4 +-- internal/multiplex/stream_test.go | 30 ++++++++-------- internal/multiplex/switchboard_test.go | 2 +- internal/server/auth.go | 2 +- internal/server/auth_test.go | 4 +-- internal/server/state.go | 10 +++--- internal/test/integration_test.go | 6 ++-- 19 files changed, 87 insertions(+), 87 deletions(-) diff --git a/internal/client/auth_test.go b/internal/client/auth_test.go index 006bca9..eda0c2c 100644 --- a/internal/client/auth_test.go +++ b/internal/client/auth_test.go @@ -27,7 +27,7 @@ func TestMakeAuthenticationPayload(t *testing.T) { 0x01, 0xd0, 0xb4, 0x87, 0x86, 0x9c, 0x15, 0x9b, 0x86, 0x19, 0x53, 0x6e, 0x60, 0xe9, 0x51, 0x42}, ProxyMethod: "shadowsocks", - EncryptionMethod: multiplex.E_METHOD_PLAIN, + EncryptionMethod: multiplex.EncryptionMethodPlain, MockDomain: "d2jkinvisak5y9.cloudfront.net", WorldState: common.WorldState{ Rand: bytes.NewBuffer([]byte{ diff --git a/internal/client/state.go b/internal/client/state.go index 2d6af8f..458b205 100644 --- a/internal/client/state.go +++ b/internal/client/state.go @@ -161,11 +161,11 @@ func (raw *RawConfig) ProcessRawConfig(worldState common.WorldState) (local Loca // Encryption method switch strings.ToLower(raw.EncryptionMethod) { case "plain": - auth.EncryptionMethod = mux.E_METHOD_PLAIN + auth.EncryptionMethod = mux.EncryptionMethodPlain case "aes-gcm": - auth.EncryptionMethod = mux.E_METHOD_AES_GCM + auth.EncryptionMethod = mux.EncryptionMethodAESGCM case "chacha20-poly1305": - auth.EncryptionMethod = mux.E_METHOD_CHACHA20_POLY1305 + auth.EncryptionMethod = mux.EncryptionMethodChaha20Poly1305 default: err = fmt.Errorf("unknown encryption method %v", raw.EncryptionMethod) return diff --git a/internal/multiplex/datagramBufferedPipe.go b/internal/multiplex/datagramBufferedPipe.go index 51a8fe7..863992b 100644 --- a/internal/multiplex/datagramBufferedPipe.go +++ b/internal/multiplex/datagramBufferedPipe.go @@ -122,7 +122,7 @@ func (d *datagramBufferedPipe) Write(f Frame) (toBeClosed bool, err error) { d.rwCond.Wait() } - if f.Closing != C_NOOP { + if f.Closing != closingNothing { d.closed = true d.rwCond.Broadcast() return true, nil diff --git a/internal/multiplex/datagramBufferedPipe_test.go b/internal/multiplex/datagramBufferedPipe_test.go index 4f7e906..f03cff0 100644 --- a/internal/multiplex/datagramBufferedPipe_test.go +++ b/internal/multiplex/datagramBufferedPipe_test.go @@ -55,7 +55,7 @@ func TestDatagramBuffer_RW(t *testing.T) { t.Run("writing closing frame", func(t *testing.T) { pipe := NewDatagramBufferedPipe() - toBeClosed, err := pipe.Write(Frame{Closing: C_STREAM}) + toBeClosed, err := pipe.Write(Frame{Closing: closingStream}) if !toBeClosed { t.Error("should be to be closed") } diff --git a/internal/multiplex/frame.go b/internal/multiplex/frame.go index 776efc5..cefbab4 100644 --- a/internal/multiplex/frame.go +++ b/internal/multiplex/frame.go @@ -1,9 +1,9 @@ package multiplex const ( - C_NOOP = iota - C_STREAM - C_SESSION + closingNothing = iota + closingStream + closingSession ) type Frame struct { diff --git a/internal/multiplex/mux_test.go b/internal/multiplex/mux_test.go index 5930583..e23d8a1 100644 --- a/internal/multiplex/mux_test.go +++ b/internal/multiplex/mux_test.go @@ -37,7 +37,7 @@ type connPair struct { func makeSessionPair(numConn int) (*Session, *Session, []*connPair) { sessionKey := [32]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} sessionId := 1 - obfuscator, _ := MakeObfuscator(E_METHOD_CHACHA20_POLY1305, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodChaha20Poly1305, sessionKey) clientConfig := SessionConfig{ Obfuscator: obfuscator, Valve: nil, diff --git a/internal/multiplex/obfs.go b/internal/multiplex/obfs.go index 68452bd..0c1f8c6 100644 --- a/internal/multiplex/obfs.go +++ b/internal/multiplex/obfs.go @@ -19,13 +19,13 @@ var u64 = binary.BigEndian.Uint64 var putU32 = binary.BigEndian.PutUint32 var putU64 = binary.BigEndian.PutUint64 -const HEADER_LEN = 14 +const frameHeaderLength = 14 const salsa20NonceSize = 8 const ( - E_METHOD_PLAIN = iota - E_METHOD_AES_GCM - E_METHOD_CHACHA20_POLY1305 + EncryptionMethodPlain = iota + EncryptionMethodAESGCM + EncryptionMethodChaha20Poly1305 ) // Obfuscator is responsible for serialisation, obfuscation, and optional encryption of data frames. @@ -95,18 +95,18 @@ func MakeObfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Obfser { } } - usefulLen := HEADER_LEN + payloadLen + extraLen + usefulLen := frameHeaderLength + payloadLen + extraLen if len(buf) < usefulLen { return 0, errors.New("obfs buffer too small") } // we do as much in-place as possible to save allocation - payload := buf[HEADER_LEN : HEADER_LEN+payloadLen] - if payloadOffsetInBuf != HEADER_LEN { + payload := buf[frameHeaderLength : frameHeaderLength+payloadLen] + if payloadOffsetInBuf != frameHeaderLength { // if payload is not at the correct location in buffer copy(payload, f.Payload) } - header := buf[:HEADER_LEN] + header := buf[:frameHeaderLength] putU32(header[0:4], f.StreamID) putU64(header[4:12], f.Seq) header[12] = f.Closing @@ -134,14 +134,14 @@ func MakeObfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Obfser { // information and plaintext func MakeDeobfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Deobfser { // frame header length + minimum data size (i.e. nonce size of salsa20) - const minInputLen = HEADER_LEN + salsa20NonceSize + const minInputLen = frameHeaderLength + salsa20NonceSize deobfs := func(in []byte) (*Frame, error) { if len(in) < minInputLen { return nil, fmt.Errorf("input size %v, but it cannot be shorter than %v bytes", len(in), minInputLen) } - header := in[:HEADER_LEN] - pldWithOverHead := in[HEADER_LEN:] // payload + potential overhead + header := in[:frameHeaderLength] + pldWithOverHead := in[frameHeaderLength:] // payload + potential overhead nonce := in[len(in)-salsa20NonceSize:] salsa20.XORKeyStream(header, header, nonce, &salsaKey) @@ -189,10 +189,10 @@ func MakeObfuscator(encryptionMethod byte, sessionKey [32]byte) (obfuscator Obfu } var payloadCipher cipher.AEAD switch encryptionMethod { - case E_METHOD_PLAIN: + case EncryptionMethodPlain: payloadCipher = nil obfuscator.maxOverhead = salsa20NonceSize - case E_METHOD_AES_GCM: + case EncryptionMethodAESGCM: var c cipher.Block c, err = aes.NewCipher(sessionKey[:]) if err != nil { @@ -203,7 +203,7 @@ func MakeObfuscator(encryptionMethod byte, sessionKey [32]byte) (obfuscator Obfu return } obfuscator.maxOverhead = payloadCipher.Overhead() - case E_METHOD_CHACHA20_POLY1305: + case EncryptionMethodChaha20Poly1305: payloadCipher, err = chacha20poly1305.New(sessionKey[:]) if err != nil { return @@ -214,7 +214,7 @@ func MakeObfuscator(encryptionMethod byte, sessionKey [32]byte) (obfuscator Obfu } if payloadCipher != nil { - if payloadCipher.NonceSize() > HEADER_LEN { + if payloadCipher.NonceSize() > frameHeaderLength { return obfuscator, errors.New("payload AEAD's nonce size cannot be greater than size of frame header") } } diff --git a/internal/multiplex/obfs_test.go b/internal/multiplex/obfs_test.go index b95a314..6cbbb5b 100644 --- a/internal/multiplex/obfs_test.go +++ b/internal/multiplex/obfs_test.go @@ -39,7 +39,7 @@ func TestGenerateObfs(t *testing.T) { } t.Run("plain", func(t *testing.T) { - obfuscator, err := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, err := MakeObfuscator(EncryptionMethodPlain, sessionKey) if err != nil { t.Errorf("failed to generate obfuscator %v", err) } else { @@ -47,7 +47,7 @@ func TestGenerateObfs(t *testing.T) { } }) t.Run("aes-gcm", func(t *testing.T) { - obfuscator, err := MakeObfuscator(E_METHOD_AES_GCM, sessionKey) + obfuscator, err := MakeObfuscator(EncryptionMethodAESGCM, sessionKey) if err != nil { t.Errorf("failed to generate obfuscator %v", err) } else { @@ -55,7 +55,7 @@ func TestGenerateObfs(t *testing.T) { } }) t.Run("chacha20-poly1305", func(t *testing.T) { - obfuscator, err := MakeObfuscator(E_METHOD_CHACHA20_POLY1305, sessionKey) + obfuscator, err := MakeObfuscator(EncryptionMethodChaha20Poly1305, sessionKey) if err != nil { t.Errorf("failed to generate obfuscator %v", err) } else { diff --git a/internal/multiplex/session.go b/internal/multiplex/session.go index ea12f44..541279a 100644 --- a/internal/multiplex/session.go +++ b/internal/multiplex/session.go @@ -108,7 +108,7 @@ func MakeSession(id uint32, config SessionConfig) *Session { sesh.InactivityTimeout = defaultInactivityTimeout } // todo: validation. this must be smaller than StreamSendBufferSize - sesh.maxStreamUnitWrite = sesh.MsgOnWireSizeLimit - HEADER_LEN - sesh.Obfuscator.maxOverhead + sesh.maxStreamUnitWrite = sesh.MsgOnWireSizeLimit - frameHeaderLength - sesh.Obfuscator.maxOverhead sesh.sb = makeSwitchboard(sesh) go sesh.timeoutAfter(sesh.InactivityTimeout) @@ -176,12 +176,12 @@ func (sesh *Session) closeStream(s *Stream, active bool) error { f := &Frame{ StreamID: s.id, Seq: s.nextSendSeq, - Closing: C_STREAM, + Closing: closingStream, Payload: padding, } s.nextSendSeq++ - obfsBuf := make([]byte, len(padding)+HEADER_LEN+sesh.Obfuscator.maxOverhead) + obfsBuf := make([]byte, len(padding)+frameHeaderLength+sesh.Obfuscator.maxOverhead) i, err := sesh.Obfs(f, obfsBuf, 0) if err != nil { return err @@ -217,7 +217,7 @@ func (sesh *Session) recvDataFromRemote(data []byte) error { return fmt.Errorf("Failed to decrypt a frame for session %v: %v", sesh.id, err) } - if frame.Closing == C_SESSION { + if frame.Closing == closingSession { sesh.SetTerminalMsg("Received a closing notification frame") return sesh.passiveClose() } @@ -310,10 +310,10 @@ func (sesh *Session) Close() error { f := &Frame{ StreamID: 0xffffffff, Seq: 0, - Closing: C_SESSION, + Closing: closingSession, Payload: pad, } - obfsBuf := make([]byte, len(pad)+HEADER_LEN+sesh.Obfuscator.maxOverhead) + obfsBuf := make([]byte, len(pad)+frameHeaderLength+sesh.Obfuscator.maxOverhead) i, err := sesh.Obfs(f, obfsBuf, 0) if err != nil { return err diff --git a/internal/multiplex/session_fuzz.go b/internal/multiplex/session_fuzz.go index 2d083b0..1eb6932 100644 --- a/internal/multiplex/session_fuzz.go +++ b/internal/multiplex/session_fuzz.go @@ -3,7 +3,7 @@ package multiplex func setupSesh_fuzz(unordered bool) *Session { - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, [32]byte{}) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, [32]byte{}) seshConfig := SessionConfig{ Obfuscator: obfuscator, diff --git a/internal/multiplex/session_test.go b/internal/multiplex/session_test.go index 8f4b207..f2de88c 100644 --- a/internal/multiplex/session_test.go +++ b/internal/multiplex/session_test.go @@ -32,7 +32,7 @@ func TestRecvDataFromRemote(t *testing.T) { var sessionKey [32]byte rand.Read(sessionKey[:]) t.Run("plain ordered", func(t *testing.T) { - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf, 0) @@ -59,7 +59,7 @@ func TestRecvDataFromRemote(t *testing.T) { } }) t.Run("aes-gcm ordered", func(t *testing.T) { - obfuscator, _ := MakeObfuscator(E_METHOD_AES_GCM, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodAESGCM, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf, 0) @@ -86,7 +86,7 @@ func TestRecvDataFromRemote(t *testing.T) { } }) t.Run("chacha20-poly1305 ordered", func(t *testing.T) { - obfuscator, _ := MakeObfuscator(E_METHOD_CHACHA20_POLY1305, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodChaha20Poly1305, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf, 0) @@ -114,7 +114,7 @@ func TestRecvDataFromRemote(t *testing.T) { }) t.Run("plain unordered", func(t *testing.T) { - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigUnordered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf, 0) @@ -150,14 +150,14 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) { var sessionKey [32]byte rand.Read(sessionKey[:]) - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) f1 := &Frame{ 1, 0, - C_NOOP, + closingNothing, testPayload, } // create stream 1 @@ -178,7 +178,7 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) { f2 := &Frame{ 2, 0, - C_NOOP, + closingNothing, testPayload, } n, _ = sesh.Obfs(f2, obfsBuf, 0) @@ -198,7 +198,7 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) { f1CloseStream := &Frame{ 1, 1, - C_STREAM, + closingStream, testPayload, } n, _ = sesh.Obfs(f1CloseStream, obfsBuf, 0) @@ -245,7 +245,7 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) { fCloseSession := &Frame{ StreamID: 0xffffffff, Seq: 0, - Closing: C_SESSION, + Closing: closingSession, Payload: testPayload, } n, _ = sesh.Obfs(fCloseSession, obfsBuf, 0) @@ -279,7 +279,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) { var sessionKey [32]byte rand.Read(sessionKey[:]) - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) @@ -287,7 +287,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) { f1CloseStream := &Frame{ 1, 1, - C_STREAM, + closingStream, testPayload, } n, _ := sesh.Obfs(f1CloseStream, obfsBuf, 0) @@ -307,7 +307,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) { f1 := &Frame{ 1, 0, - C_NOOP, + closingNothing, testPayload, } n, _ = sesh.Obfs(f1, obfsBuf, 0) @@ -334,7 +334,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) { func TestParallelStreams(t *testing.T) { var sessionKey [32]byte rand.Read(sessionKey[:]) - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) @@ -396,7 +396,7 @@ func TestParallelStreams(t *testing.T) { func TestStream_SetReadDeadline(t *testing.T) { var sessionKey [32]byte rand.Read(sessionKey[:]) - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigOrdered.Obfuscator = obfuscator testReadDeadline := func(sesh *Session) { @@ -440,7 +440,7 @@ func TestStream_SetReadDeadline(t *testing.T) { func TestSession_timeoutAfter(t *testing.T) { var sessionKey [32]byte rand.Read(sessionKey[:]) - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.InactivityTimeout = 100 * time.Millisecond sesh := MakeSession(0, seshConfigOrdered) @@ -466,7 +466,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) { rand.Read(sessionKey[:]) b.Run("plain", func(b *testing.B) { - obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf, 0) @@ -479,7 +479,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) { }) b.Run("aes-gcm", func(b *testing.B) { - obfuscator, _ := MakeObfuscator(E_METHOD_AES_GCM, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodAESGCM, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf, 0) @@ -492,7 +492,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) { }) b.Run("chacha20-poly1305", func(b *testing.B) { - obfuscator, _ := MakeObfuscator(E_METHOD_CHACHA20_POLY1305, sessionKey) + obfuscator, _ := MakeObfuscator(EncryptionMethodChaha20Poly1305, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf, 0) diff --git a/internal/multiplex/stream.go b/internal/multiplex/stream.go index c712f95..beee2b8 100644 --- a/internal/multiplex/stream.go +++ b/internal/multiplex/stream.go @@ -157,7 +157,7 @@ func (s *Stream) Write(in []byte) (n int, err error) { f := &Frame{ StreamID: s.id, Seq: s.nextSendSeq, - Closing: C_NOOP, + Closing: closingNothing, Payload: framePayload, } s.nextSendSeq++ @@ -184,7 +184,7 @@ func (s *Stream) ReadFrom(r io.Reader) (n int64, err error) { rder.SetReadDeadline(time.Now().Add(s.readFromTimeout)) } } - read, er := r.Read(s.obfsBuf[HEADER_LEN : HEADER_LEN+s.session.maxStreamUnitWrite]) + read, er := r.Read(s.obfsBuf[frameHeaderLength : frameHeaderLength+s.session.maxStreamUnitWrite]) if er != nil { return n, er } @@ -196,11 +196,11 @@ func (s *Stream) ReadFrom(r io.Reader) (n int64, err error) { f := &Frame{ StreamID: s.id, Seq: s.nextSendSeq, - Closing: C_NOOP, - Payload: s.obfsBuf[HEADER_LEN : HEADER_LEN+read], + Closing: closingNothing, + Payload: s.obfsBuf[frameHeaderLength : frameHeaderLength+read], } s.nextSendSeq++ - err = s.obfuscateAndSend(f, HEADER_LEN) + err = s.obfuscateAndSend(f, frameHeaderLength) s.writingM.Unlock() if err != nil { diff --git a/internal/multiplex/streamBuffer.go b/internal/multiplex/streamBuffer.go index 9e5f53c..4adfae2 100644 --- a/internal/multiplex/streamBuffer.go +++ b/internal/multiplex/streamBuffer.go @@ -68,7 +68,7 @@ func (sb *streamBuffer) Write(f Frame) (toBeClosed bool, err error) { defer sb.recvM.Unlock() // when there'fs no ooo packages in heap and we receive the next package in order if len(sb.sh) == 0 && f.Seq == sb.nextRecvSeq { - if f.Closing != C_NOOP { + if f.Closing != closingNothing { return true, nil } else { sb.buf.Write(f.Payload) @@ -85,7 +85,7 @@ func (sb *streamBuffer) Write(f Frame) (toBeClosed bool, err error) { // Keep popping from the heap until empty or to the point that the wanted seq was not received for len(sb.sh) > 0 && sb.sh[0].Seq == sb.nextRecvSeq { f = *heap.Pop(&sb.sh).(*Frame) - if f.Closing != C_NOOP { + if f.Closing != closingNothing { return true, nil } else { sb.buf.Write(f.Payload) diff --git a/internal/multiplex/stream_test.go b/internal/multiplex/stream_test.go index 59fc11f..278082b 100644 --- a/internal/multiplex/stream_test.go +++ b/internal/multiplex/stream_test.go @@ -36,9 +36,9 @@ func BenchmarkStream_Write_Ordered(b *testing.B) { testData := make([]byte, testDataLen) rand.Read(testData) eMethods := map[string]byte{ - "plain": E_METHOD_PLAIN, - "chacha20-poly1305": E_METHOD_CHACHA20_POLY1305, - "aes-gcm": E_METHOD_AES_GCM, + "plain": EncryptionMethodPlain, + "chacha20-poly1305": EncryptionMethodChaha20Poly1305, + "aes-gcm": EncryptionMethodAESGCM, } for name, method := range eMethods { @@ -59,7 +59,7 @@ func TestStream_Write(t *testing.T) { hole := connutil.Discard() var sessionKey [32]byte rand.Read(sessionKey[:]) - sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) + sesh := setupSesh(false, sessionKey, EncryptionMethodPlain) sesh.AddConnection(hole) testData := make([]byte, payloadLen) rand.Read(testData) @@ -78,8 +78,8 @@ func TestStream_WriteSync(t *testing.T) { // Close calls made after write MUST have a higher seq var sessionKey [32]byte rand.Read(sessionKey[:]) - clientSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) - serverSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) + clientSesh := setupSesh(false, sessionKey, EncryptionMethodPlain) + serverSesh := setupSesh(false, sessionKey, EncryptionMethodPlain) w, r := connutil.AsyncPipe() clientSesh.AddConnection(common.NewTLSConn(w)) serverSesh.AddConnection(common.NewTLSConn(r)) @@ -134,7 +134,7 @@ func TestStream_Close(t *testing.T) { } t.Run("active closing", func(t *testing.T) { - sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) + sesh := setupSesh(false, sessionKey, EncryptionMethodPlain) rawConn, rawWritingEnd := connutil.AsyncPipe() sesh.AddConnection(common.NewTLSConn(rawConn)) writingEnd := common.NewTLSConn(rawWritingEnd) @@ -172,7 +172,7 @@ func TestStream_Close(t *testing.T) { }) t.Run("passive closing", func(t *testing.T) { - sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) + sesh := setupSesh(false, sessionKey, EncryptionMethodPlain) rawConn, rawWritingEnd := connutil.AsyncPipe() sesh.AddConnection(common.NewTLSConn(rawConn)) writingEnd := common.NewTLSConn(rawWritingEnd) @@ -196,7 +196,7 @@ func TestStream_Close(t *testing.T) { closingFrame := &Frame{ 1, dataFrame.Seq + 1, - C_STREAM, + closingStream, testPayload, } @@ -212,7 +212,7 @@ func TestStream_Close(t *testing.T) { closingFrameDup := &Frame{ 1, dataFrame.Seq + 2, - C_STREAM, + closingStream, testPayload, } @@ -260,7 +260,7 @@ func TestStream_Read(t *testing.T) { obfsBuf := make([]byte, 512) for name, unordered := range seshes { - sesh := setupSesh(unordered, emptyKey, E_METHOD_PLAIN) + sesh := setupSesh(unordered, emptyKey, EncryptionMethodPlain) rawConn, rawWritingEnd := connutil.AsyncPipe() sesh.AddConnection(common.NewTLSConn(rawConn)) writingEnd := common.NewTLSConn(rawWritingEnd) @@ -356,8 +356,8 @@ func TestStream_Read(t *testing.T) { func TestStream_SetWriteToTimeout(t *testing.T) { seshes := map[string]*Session{ - "ordered": setupSesh(false, emptyKey, E_METHOD_PLAIN), - "unordered": setupSesh(true, emptyKey, E_METHOD_PLAIN), + "ordered": setupSesh(false, emptyKey, EncryptionMethodPlain), + "unordered": setupSesh(true, emptyKey, EncryptionMethodPlain), } for name, sesh := range seshes { t.Run(name, func(t *testing.T) { @@ -381,8 +381,8 @@ func TestStream_SetWriteToTimeout(t *testing.T) { func TestStream_SetReadFromTimeout(t *testing.T) { seshes := map[string]*Session{ - "ordered": setupSesh(false, emptyKey, E_METHOD_PLAIN), - "unordered": setupSesh(true, emptyKey, E_METHOD_PLAIN), + "ordered": setupSesh(false, emptyKey, EncryptionMethodPlain), + "unordered": setupSesh(true, emptyKey, EncryptionMethodPlain), } for name, sesh := range seshes { t.Run(name, func(t *testing.T) { diff --git a/internal/multiplex/switchboard_test.go b/internal/multiplex/switchboard_test.go index 6d6e55f..e052b55 100644 --- a/internal/multiplex/switchboard_test.go +++ b/internal/multiplex/switchboard_test.go @@ -136,7 +136,7 @@ func TestSwitchboard_TxCredit(t *testing.T) { func TestSwitchboard_CloseOnOneDisconn(t *testing.T) { var sessionKey [32]byte rand.Read(sessionKey[:]) - sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) + sesh := setupSesh(false, sessionKey, EncryptionMethodPlain) conn0client, conn0server := connutil.AsyncPipe() sesh.AddConnection(conn0client) diff --git a/internal/server/auth.go b/internal/server/auth.go index fcaa2cf..66d2fb5 100644 --- a/internal/server/auth.go +++ b/internal/server/auth.go @@ -50,7 +50,7 @@ func decryptClientInfo(fragments authFragments, serverTime time.Time) (info Clie timestamp := int64(binary.BigEndian.Uint64(plaintext[29:37])) clientTime := time.Unix(timestamp, 0) - if !(clientTime.After(serverTime.Truncate(TIMESTAMP_TOLERANCE)) && clientTime.Before(serverTime.Add(TIMESTAMP_TOLERANCE))) { + if !(clientTime.After(serverTime.Truncate(timestampTolerance)) && clientTime.Before(serverTime.Add(timestampTolerance))) { err = fmt.Errorf("%v: received timestamp %v", ErrTimestampOutOfWindow, timestamp) return } diff --git a/internal/server/auth_test.go b/internal/server/auth_test.go index 66738a0..e971758 100644 --- a/internal/server/auth_test.go +++ b/internal/server/auth_test.go @@ -66,7 +66,7 @@ func TestTouchStone(t *testing.T) { return } - nineSixSixOver := time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE + 10) + nineSixSixOver := time.Unix(1565998966, 0).Add(timestampTolerance + 10) _, err = decryptClientInfo(ai, nineSixSixOver) if err == nil { t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) @@ -82,7 +82,7 @@ func TestTouchStone(t *testing.T) { return } - nineSixSixUnder := time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE - 10) + nineSixSixUnder := time.Unix(1565998966, 0).Add(timestampTolerance - 10) _, err = decryptClientInfo(ai, nineSixSixUnder) if err == nil { t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) diff --git a/internal/server/state.go b/internal/server/state.go index cbd7661..b364783 100644 --- a/internal/server/state.go +++ b/internal/server/state.go @@ -203,17 +203,17 @@ func (sta *State) IsBypass(UID []byte) bool { return exist } -const TIMESTAMP_TOLERANCE = 180 * time.Second +const timestampTolerance = 180 * time.Second -const CACHE_CLEAN_INTERVAL = 12 * time.Hour +const replayCacheAgeLimit = 12 * time.Hour -// UsedRandomCleaner clears the cache of used random fields every CACHE_CLEAN_INTERVAL +// UsedRandomCleaner clears the cache of used random fields every replayCacheAgeLimit func (sta *State) UsedRandomCleaner() { for { - time.Sleep(CACHE_CLEAN_INTERVAL) + time.Sleep(replayCacheAgeLimit) sta.usedRandomM.Lock() for key, t := range sta.UsedRandom { - if time.Unix(t, 0).Before(sta.WorldState.Now().Add(TIMESTAMP_TOLERANCE)) { + if time.Unix(t, 0).Before(sta.WorldState.Now().Add(timestampTolerance)) { delete(sta.UsedRandom, key) } } diff --git a/internal/test/integration_test.go b/internal/test/integration_test.go index 0e410c4..211092a 100644 --- a/internal/test/integration_test.go +++ b/internal/test/integration_test.go @@ -548,9 +548,9 @@ func BenchmarkThroughput(b *testing.B) { const bufSize = 16 * 1024 encryptionMethods := map[string]byte{ - "plain": mux.E_METHOD_PLAIN, - "chacha20-poly1305": mux.E_METHOD_CHACHA20_POLY1305, - "aes-gcm": mux.E_METHOD_AES_GCM, + "plain": mux.EncryptionMethodPlain, + "chacha20-poly1305": mux.EncryptionMethodChaha20Poly1305, + "aes-gcm": mux.EncryptionMethodAESGCM, } for name, method := range encryptionMethods {