Rename constants to camel case

This commit is contained in:
Andy Wang 2020-10-21 16:42:24 +01:00
parent 11cfeb4fa3
commit 0f6e0d37b5
19 changed files with 87 additions and 87 deletions

View File

@ -27,7 +27,7 @@ func TestMakeAuthenticationPayload(t *testing.T) {
0x01, 0xd0, 0xb4, 0x87, 0x86, 0x9c, 0x15, 0x9b, 0x01, 0xd0, 0xb4, 0x87, 0x86, 0x9c, 0x15, 0x9b,
0x86, 0x19, 0x53, 0x6e, 0x60, 0xe9, 0x51, 0x42}, 0x86, 0x19, 0x53, 0x6e, 0x60, 0xe9, 0x51, 0x42},
ProxyMethod: "shadowsocks", ProxyMethod: "shadowsocks",
EncryptionMethod: multiplex.E_METHOD_PLAIN, EncryptionMethod: multiplex.EncryptionMethodPlain,
MockDomain: "d2jkinvisak5y9.cloudfront.net", MockDomain: "d2jkinvisak5y9.cloudfront.net",
WorldState: common.WorldState{ WorldState: common.WorldState{
Rand: bytes.NewBuffer([]byte{ Rand: bytes.NewBuffer([]byte{

View File

@ -161,11 +161,11 @@ func (raw *RawConfig) ProcessRawConfig(worldState common.WorldState) (local Loca
// Encryption method // Encryption method
switch strings.ToLower(raw.EncryptionMethod) { switch strings.ToLower(raw.EncryptionMethod) {
case "plain": case "plain":
auth.EncryptionMethod = mux.E_METHOD_PLAIN auth.EncryptionMethod = mux.EncryptionMethodPlain
case "aes-gcm": case "aes-gcm":
auth.EncryptionMethod = mux.E_METHOD_AES_GCM auth.EncryptionMethod = mux.EncryptionMethodAESGCM
case "chacha20-poly1305": case "chacha20-poly1305":
auth.EncryptionMethod = mux.E_METHOD_CHACHA20_POLY1305 auth.EncryptionMethod = mux.EncryptionMethodChaha20Poly1305
default: default:
err = fmt.Errorf("unknown encryption method %v", raw.EncryptionMethod) err = fmt.Errorf("unknown encryption method %v", raw.EncryptionMethod)
return return

View File

@ -122,7 +122,7 @@ func (d *datagramBufferedPipe) Write(f Frame) (toBeClosed bool, err error) {
d.rwCond.Wait() d.rwCond.Wait()
} }
if f.Closing != C_NOOP { if f.Closing != closingNothing {
d.closed = true d.closed = true
d.rwCond.Broadcast() d.rwCond.Broadcast()
return true, nil return true, nil

View File

@ -55,7 +55,7 @@ func TestDatagramBuffer_RW(t *testing.T) {
t.Run("writing closing frame", func(t *testing.T) { t.Run("writing closing frame", func(t *testing.T) {
pipe := NewDatagramBufferedPipe() pipe := NewDatagramBufferedPipe()
toBeClosed, err := pipe.Write(Frame{Closing: C_STREAM}) toBeClosed, err := pipe.Write(Frame{Closing: closingStream})
if !toBeClosed { if !toBeClosed {
t.Error("should be to be closed") t.Error("should be to be closed")
} }

View File

@ -1,9 +1,9 @@
package multiplex package multiplex
const ( const (
C_NOOP = iota closingNothing = iota
C_STREAM closingStream
C_SESSION closingSession
) )
type Frame struct { type Frame struct {

View File

@ -37,7 +37,7 @@ type connPair struct {
func makeSessionPair(numConn int) (*Session, *Session, []*connPair) { 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} 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 sessionId := 1
obfuscator, _ := MakeObfuscator(E_METHOD_CHACHA20_POLY1305, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodChaha20Poly1305, sessionKey)
clientConfig := SessionConfig{ clientConfig := SessionConfig{
Obfuscator: obfuscator, Obfuscator: obfuscator,
Valve: nil, Valve: nil,

View File

@ -19,13 +19,13 @@ var u64 = binary.BigEndian.Uint64
var putU32 = binary.BigEndian.PutUint32 var putU32 = binary.BigEndian.PutUint32
var putU64 = binary.BigEndian.PutUint64 var putU64 = binary.BigEndian.PutUint64
const HEADER_LEN = 14 const frameHeaderLength = 14
const salsa20NonceSize = 8 const salsa20NonceSize = 8
const ( const (
E_METHOD_PLAIN = iota EncryptionMethodPlain = iota
E_METHOD_AES_GCM EncryptionMethodAESGCM
E_METHOD_CHACHA20_POLY1305 EncryptionMethodChaha20Poly1305
) )
// Obfuscator is responsible for serialisation, obfuscation, and optional encryption of data frames. // 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 { if len(buf) < usefulLen {
return 0, errors.New("obfs buffer too small") return 0, errors.New("obfs buffer too small")
} }
// we do as much in-place as possible to save allocation // we do as much in-place as possible to save allocation
payload := buf[HEADER_LEN : HEADER_LEN+payloadLen] payload := buf[frameHeaderLength : frameHeaderLength+payloadLen]
if payloadOffsetInBuf != HEADER_LEN { if payloadOffsetInBuf != frameHeaderLength {
// if payload is not at the correct location in buffer // if payload is not at the correct location in buffer
copy(payload, f.Payload) copy(payload, f.Payload)
} }
header := buf[:HEADER_LEN] header := buf[:frameHeaderLength]
putU32(header[0:4], f.StreamID) putU32(header[0:4], f.StreamID)
putU64(header[4:12], f.Seq) putU64(header[4:12], f.Seq)
header[12] = f.Closing header[12] = f.Closing
@ -134,14 +134,14 @@ func MakeObfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Obfser {
// information and plaintext // information and plaintext
func MakeDeobfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Deobfser { func MakeDeobfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Deobfser {
// frame header length + minimum data size (i.e. nonce size of salsa20) // 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) { deobfs := func(in []byte) (*Frame, error) {
if len(in) < minInputLen { if len(in) < minInputLen {
return nil, fmt.Errorf("input size %v, but it cannot be shorter than %v bytes", 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] header := in[:frameHeaderLength]
pldWithOverHead := in[HEADER_LEN:] // payload + potential overhead pldWithOverHead := in[frameHeaderLength:] // payload + potential overhead
nonce := in[len(in)-salsa20NonceSize:] nonce := in[len(in)-salsa20NonceSize:]
salsa20.XORKeyStream(header, header, nonce, &salsaKey) salsa20.XORKeyStream(header, header, nonce, &salsaKey)
@ -189,10 +189,10 @@ func MakeObfuscator(encryptionMethod byte, sessionKey [32]byte) (obfuscator Obfu
} }
var payloadCipher cipher.AEAD var payloadCipher cipher.AEAD
switch encryptionMethod { switch encryptionMethod {
case E_METHOD_PLAIN: case EncryptionMethodPlain:
payloadCipher = nil payloadCipher = nil
obfuscator.maxOverhead = salsa20NonceSize obfuscator.maxOverhead = salsa20NonceSize
case E_METHOD_AES_GCM: case EncryptionMethodAESGCM:
var c cipher.Block var c cipher.Block
c, err = aes.NewCipher(sessionKey[:]) c, err = aes.NewCipher(sessionKey[:])
if err != nil { if err != nil {
@ -203,7 +203,7 @@ func MakeObfuscator(encryptionMethod byte, sessionKey [32]byte) (obfuscator Obfu
return return
} }
obfuscator.maxOverhead = payloadCipher.Overhead() obfuscator.maxOverhead = payloadCipher.Overhead()
case E_METHOD_CHACHA20_POLY1305: case EncryptionMethodChaha20Poly1305:
payloadCipher, err = chacha20poly1305.New(sessionKey[:]) payloadCipher, err = chacha20poly1305.New(sessionKey[:])
if err != nil { if err != nil {
return return
@ -214,7 +214,7 @@ func MakeObfuscator(encryptionMethod byte, sessionKey [32]byte) (obfuscator Obfu
} }
if payloadCipher != nil { 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") return obfuscator, errors.New("payload AEAD's nonce size cannot be greater than size of frame header")
} }
} }

View File

@ -39,7 +39,7 @@ func TestGenerateObfs(t *testing.T) {
} }
t.Run("plain", func(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 { if err != nil {
t.Errorf("failed to generate obfuscator %v", err) t.Errorf("failed to generate obfuscator %v", err)
} else { } else {
@ -47,7 +47,7 @@ func TestGenerateObfs(t *testing.T) {
} }
}) })
t.Run("aes-gcm", func(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 { if err != nil {
t.Errorf("failed to generate obfuscator %v", err) t.Errorf("failed to generate obfuscator %v", err)
} else { } else {
@ -55,7 +55,7 @@ func TestGenerateObfs(t *testing.T) {
} }
}) })
t.Run("chacha20-poly1305", func(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 { if err != nil {
t.Errorf("failed to generate obfuscator %v", err) t.Errorf("failed to generate obfuscator %v", err)
} else { } else {

View File

@ -108,7 +108,7 @@ func MakeSession(id uint32, config SessionConfig) *Session {
sesh.InactivityTimeout = defaultInactivityTimeout sesh.InactivityTimeout = defaultInactivityTimeout
} }
// todo: validation. this must be smaller than StreamSendBufferSize // 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) sesh.sb = makeSwitchboard(sesh)
go sesh.timeoutAfter(sesh.InactivityTimeout) go sesh.timeoutAfter(sesh.InactivityTimeout)
@ -176,12 +176,12 @@ func (sesh *Session) closeStream(s *Stream, active bool) error {
f := &Frame{ f := &Frame{
StreamID: s.id, StreamID: s.id,
Seq: s.nextSendSeq, Seq: s.nextSendSeq,
Closing: C_STREAM, Closing: closingStream,
Payload: padding, Payload: padding,
} }
s.nextSendSeq++ 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) i, err := sesh.Obfs(f, obfsBuf, 0)
if err != nil { if err != nil {
return err 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) 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") sesh.SetTerminalMsg("Received a closing notification frame")
return sesh.passiveClose() return sesh.passiveClose()
} }
@ -310,10 +310,10 @@ func (sesh *Session) Close() error {
f := &Frame{ f := &Frame{
StreamID: 0xffffffff, StreamID: 0xffffffff,
Seq: 0, Seq: 0,
Closing: C_SESSION, Closing: closingSession,
Payload: pad, 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) i, err := sesh.Obfs(f, obfsBuf, 0)
if err != nil { if err != nil {
return err return err

View File

@ -3,7 +3,7 @@
package multiplex package multiplex
func setupSesh_fuzz(unordered bool) *Session { func setupSesh_fuzz(unordered bool) *Session {
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, [32]byte{}) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, [32]byte{})
seshConfig := SessionConfig{ seshConfig := SessionConfig{
Obfuscator: obfuscator, Obfuscator: obfuscator,

View File

@ -32,7 +32,7 @@ func TestRecvDataFromRemote(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
t.Run("plain ordered", func(t *testing.T) { t.Run("plain ordered", func(t *testing.T) {
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)
@ -59,7 +59,7 @@ func TestRecvDataFromRemote(t *testing.T) {
} }
}) })
t.Run("aes-gcm ordered", func(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 seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)
@ -86,7 +86,7 @@ func TestRecvDataFromRemote(t *testing.T) {
} }
}) })
t.Run("chacha20-poly1305 ordered", func(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 seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)
@ -114,7 +114,7 @@ func TestRecvDataFromRemote(t *testing.T) {
}) })
t.Run("plain unordered", func(t *testing.T) { t.Run("plain unordered", func(t *testing.T) {
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigUnordered.Obfuscator = obfuscator seshConfigUnordered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)
@ -150,14 +150,14 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
f1 := &Frame{ f1 := &Frame{
1, 1,
0, 0,
C_NOOP, closingNothing,
testPayload, testPayload,
} }
// create stream 1 // create stream 1
@ -178,7 +178,7 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) {
f2 := &Frame{ f2 := &Frame{
2, 2,
0, 0,
C_NOOP, closingNothing,
testPayload, testPayload,
} }
n, _ = sesh.Obfs(f2, obfsBuf, 0) n, _ = sesh.Obfs(f2, obfsBuf, 0)
@ -198,7 +198,7 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) {
f1CloseStream := &Frame{ f1CloseStream := &Frame{
1, 1,
1, 1,
C_STREAM, closingStream,
testPayload, testPayload,
} }
n, _ = sesh.Obfs(f1CloseStream, obfsBuf, 0) n, _ = sesh.Obfs(f1CloseStream, obfsBuf, 0)
@ -245,7 +245,7 @@ func TestRecvDataFromRemote_Closing_InOrder(t *testing.T) {
fCloseSession := &Frame{ fCloseSession := &Frame{
StreamID: 0xffffffff, StreamID: 0xffffffff,
Seq: 0, Seq: 0,
Closing: C_SESSION, Closing: closingSession,
Payload: testPayload, Payload: testPayload,
} }
n, _ = sesh.Obfs(fCloseSession, obfsBuf, 0) n, _ = sesh.Obfs(fCloseSession, obfsBuf, 0)
@ -279,7 +279,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
@ -287,7 +287,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) {
f1CloseStream := &Frame{ f1CloseStream := &Frame{
1, 1,
1, 1,
C_STREAM, closingStream,
testPayload, testPayload,
} }
n, _ := sesh.Obfs(f1CloseStream, obfsBuf, 0) n, _ := sesh.Obfs(f1CloseStream, obfsBuf, 0)
@ -307,7 +307,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) {
f1 := &Frame{ f1 := &Frame{
1, 1,
0, 0,
C_NOOP, closingNothing,
testPayload, testPayload,
} }
n, _ = sesh.Obfs(f1, obfsBuf, 0) n, _ = sesh.Obfs(f1, obfsBuf, 0)
@ -334,7 +334,7 @@ func TestRecvDataFromRemote_Closing_OutOfOrder(t *testing.T) {
func TestParallelStreams(t *testing.T) { func TestParallelStreams(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
@ -396,7 +396,7 @@ func TestParallelStreams(t *testing.T) {
func TestStream_SetReadDeadline(t *testing.T) { func TestStream_SetReadDeadline(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
testReadDeadline := func(sesh *Session) { testReadDeadline := func(sesh *Session) {
@ -440,7 +440,7 @@ func TestStream_SetReadDeadline(t *testing.T) {
func TestSession_timeoutAfter(t *testing.T) { func TestSession_timeoutAfter(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
seshConfigOrdered.InactivityTimeout = 100 * time.Millisecond seshConfigOrdered.InactivityTimeout = 100 * time.Millisecond
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
@ -466,7 +466,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
b.Run("plain", func(b *testing.B) { b.Run("plain", func(b *testing.B) {
obfuscator, _ := MakeObfuscator(E_METHOD_PLAIN, sessionKey) obfuscator, _ := MakeObfuscator(EncryptionMethodPlain, sessionKey)
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)
@ -479,7 +479,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
}) })
b.Run("aes-gcm", func(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 seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)
@ -492,7 +492,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {
}) })
b.Run("chacha20-poly1305", func(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 seshConfigOrdered.Obfuscator = obfuscator
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
n, _ := sesh.Obfs(f, obfsBuf, 0) n, _ := sesh.Obfs(f, obfsBuf, 0)

View File

@ -157,7 +157,7 @@ func (s *Stream) Write(in []byte) (n int, err error) {
f := &Frame{ f := &Frame{
StreamID: s.id, StreamID: s.id,
Seq: s.nextSendSeq, Seq: s.nextSendSeq,
Closing: C_NOOP, Closing: closingNothing,
Payload: framePayload, Payload: framePayload,
} }
s.nextSendSeq++ s.nextSendSeq++
@ -184,7 +184,7 @@ func (s *Stream) ReadFrom(r io.Reader) (n int64, err error) {
rder.SetReadDeadline(time.Now().Add(s.readFromTimeout)) 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 { if er != nil {
return n, er return n, er
} }
@ -196,11 +196,11 @@ func (s *Stream) ReadFrom(r io.Reader) (n int64, err error) {
f := &Frame{ f := &Frame{
StreamID: s.id, StreamID: s.id,
Seq: s.nextSendSeq, Seq: s.nextSendSeq,
Closing: C_NOOP, Closing: closingNothing,
Payload: s.obfsBuf[HEADER_LEN : HEADER_LEN+read], Payload: s.obfsBuf[frameHeaderLength : frameHeaderLength+read],
} }
s.nextSendSeq++ s.nextSendSeq++
err = s.obfuscateAndSend(f, HEADER_LEN) err = s.obfuscateAndSend(f, frameHeaderLength)
s.writingM.Unlock() s.writingM.Unlock()
if err != nil { if err != nil {

View File

@ -68,7 +68,7 @@ func (sb *streamBuffer) Write(f Frame) (toBeClosed bool, err error) {
defer sb.recvM.Unlock() defer sb.recvM.Unlock()
// when there'fs no ooo packages in heap and we receive the next package in order // 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 len(sb.sh) == 0 && f.Seq == sb.nextRecvSeq {
if f.Closing != C_NOOP { if f.Closing != closingNothing {
return true, nil return true, nil
} else { } else {
sb.buf.Write(f.Payload) 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 // 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 { for len(sb.sh) > 0 && sb.sh[0].Seq == sb.nextRecvSeq {
f = *heap.Pop(&sb.sh).(*Frame) f = *heap.Pop(&sb.sh).(*Frame)
if f.Closing != C_NOOP { if f.Closing != closingNothing {
return true, nil return true, nil
} else { } else {
sb.buf.Write(f.Payload) sb.buf.Write(f.Payload)

View File

@ -36,9 +36,9 @@ func BenchmarkStream_Write_Ordered(b *testing.B) {
testData := make([]byte, testDataLen) testData := make([]byte, testDataLen)
rand.Read(testData) rand.Read(testData)
eMethods := map[string]byte{ eMethods := map[string]byte{
"plain": E_METHOD_PLAIN, "plain": EncryptionMethodPlain,
"chacha20-poly1305": E_METHOD_CHACHA20_POLY1305, "chacha20-poly1305": EncryptionMethodChaha20Poly1305,
"aes-gcm": E_METHOD_AES_GCM, "aes-gcm": EncryptionMethodAESGCM,
} }
for name, method := range eMethods { for name, method := range eMethods {
@ -59,7 +59,7 @@ func TestStream_Write(t *testing.T) {
hole := connutil.Discard() hole := connutil.Discard()
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) sesh := setupSesh(false, sessionKey, EncryptionMethodPlain)
sesh.AddConnection(hole) sesh.AddConnection(hole)
testData := make([]byte, payloadLen) testData := make([]byte, payloadLen)
rand.Read(testData) rand.Read(testData)
@ -78,8 +78,8 @@ func TestStream_WriteSync(t *testing.T) {
// Close calls made after write MUST have a higher seq // Close calls made after write MUST have a higher seq
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
clientSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) clientSesh := setupSesh(false, sessionKey, EncryptionMethodPlain)
serverSesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) serverSesh := setupSesh(false, sessionKey, EncryptionMethodPlain)
w, r := connutil.AsyncPipe() w, r := connutil.AsyncPipe()
clientSesh.AddConnection(common.NewTLSConn(w)) clientSesh.AddConnection(common.NewTLSConn(w))
serverSesh.AddConnection(common.NewTLSConn(r)) serverSesh.AddConnection(common.NewTLSConn(r))
@ -134,7 +134,7 @@ func TestStream_Close(t *testing.T) {
} }
t.Run("active closing", func(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() rawConn, rawWritingEnd := connutil.AsyncPipe()
sesh.AddConnection(common.NewTLSConn(rawConn)) sesh.AddConnection(common.NewTLSConn(rawConn))
writingEnd := common.NewTLSConn(rawWritingEnd) writingEnd := common.NewTLSConn(rawWritingEnd)
@ -172,7 +172,7 @@ func TestStream_Close(t *testing.T) {
}) })
t.Run("passive closing", func(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() rawConn, rawWritingEnd := connutil.AsyncPipe()
sesh.AddConnection(common.NewTLSConn(rawConn)) sesh.AddConnection(common.NewTLSConn(rawConn))
writingEnd := common.NewTLSConn(rawWritingEnd) writingEnd := common.NewTLSConn(rawWritingEnd)
@ -196,7 +196,7 @@ func TestStream_Close(t *testing.T) {
closingFrame := &Frame{ closingFrame := &Frame{
1, 1,
dataFrame.Seq + 1, dataFrame.Seq + 1,
C_STREAM, closingStream,
testPayload, testPayload,
} }
@ -212,7 +212,7 @@ func TestStream_Close(t *testing.T) {
closingFrameDup := &Frame{ closingFrameDup := &Frame{
1, 1,
dataFrame.Seq + 2, dataFrame.Seq + 2,
C_STREAM, closingStream,
testPayload, testPayload,
} }
@ -260,7 +260,7 @@ func TestStream_Read(t *testing.T) {
obfsBuf := make([]byte, 512) obfsBuf := make([]byte, 512)
for name, unordered := range seshes { for name, unordered := range seshes {
sesh := setupSesh(unordered, emptyKey, E_METHOD_PLAIN) sesh := setupSesh(unordered, emptyKey, EncryptionMethodPlain)
rawConn, rawWritingEnd := connutil.AsyncPipe() rawConn, rawWritingEnd := connutil.AsyncPipe()
sesh.AddConnection(common.NewTLSConn(rawConn)) sesh.AddConnection(common.NewTLSConn(rawConn))
writingEnd := common.NewTLSConn(rawWritingEnd) writingEnd := common.NewTLSConn(rawWritingEnd)
@ -356,8 +356,8 @@ func TestStream_Read(t *testing.T) {
func TestStream_SetWriteToTimeout(t *testing.T) { func TestStream_SetWriteToTimeout(t *testing.T) {
seshes := map[string]*Session{ seshes := map[string]*Session{
"ordered": setupSesh(false, emptyKey, E_METHOD_PLAIN), "ordered": setupSesh(false, emptyKey, EncryptionMethodPlain),
"unordered": setupSesh(true, emptyKey, E_METHOD_PLAIN), "unordered": setupSesh(true, emptyKey, EncryptionMethodPlain),
} }
for name, sesh := range seshes { for name, sesh := range seshes {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
@ -381,8 +381,8 @@ func TestStream_SetWriteToTimeout(t *testing.T) {
func TestStream_SetReadFromTimeout(t *testing.T) { func TestStream_SetReadFromTimeout(t *testing.T) {
seshes := map[string]*Session{ seshes := map[string]*Session{
"ordered": setupSesh(false, emptyKey, E_METHOD_PLAIN), "ordered": setupSesh(false, emptyKey, EncryptionMethodPlain),
"unordered": setupSesh(true, emptyKey, E_METHOD_PLAIN), "unordered": setupSesh(true, emptyKey, EncryptionMethodPlain),
} }
for name, sesh := range seshes { for name, sesh := range seshes {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {

View File

@ -136,7 +136,7 @@ func TestSwitchboard_TxCredit(t *testing.T) {
func TestSwitchboard_CloseOnOneDisconn(t *testing.T) { func TestSwitchboard_CloseOnOneDisconn(t *testing.T) {
var sessionKey [32]byte var sessionKey [32]byte
rand.Read(sessionKey[:]) rand.Read(sessionKey[:])
sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN) sesh := setupSesh(false, sessionKey, EncryptionMethodPlain)
conn0client, conn0server := connutil.AsyncPipe() conn0client, conn0server := connutil.AsyncPipe()
sesh.AddConnection(conn0client) sesh.AddConnection(conn0client)

View File

@ -50,7 +50,7 @@ func decryptClientInfo(fragments authFragments, serverTime time.Time) (info Clie
timestamp := int64(binary.BigEndian.Uint64(plaintext[29:37])) timestamp := int64(binary.BigEndian.Uint64(plaintext[29:37]))
clientTime := time.Unix(timestamp, 0) 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) err = fmt.Errorf("%v: received timestamp %v", ErrTimestampOutOfWindow, timestamp)
return return
} }

View File

@ -66,7 +66,7 @@ func TestTouchStone(t *testing.T) {
return return
} }
nineSixSixOver := time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE + 10) nineSixSixOver := time.Unix(1565998966, 0).Add(timestampTolerance + 10)
_, err = decryptClientInfo(ai, nineSixSixOver) _, err = decryptClientInfo(ai, nineSixSixOver)
if err == nil { if err == nil {
t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err)
@ -82,7 +82,7 @@ func TestTouchStone(t *testing.T) {
return return
} }
nineSixSixUnder := time.Unix(1565998966, 0).Add(TIMESTAMP_TOLERANCE - 10) nineSixSixUnder := time.Unix(1565998966, 0).Add(timestampTolerance - 10)
_, err = decryptClientInfo(ai, nineSixSixUnder) _, err = decryptClientInfo(ai, nineSixSixUnder)
if err == nil { if err == nil {
t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err) t.Errorf("expecting %v, got %v", ErrTimestampOutOfWindow, err)

View File

@ -203,17 +203,17 @@ func (sta *State) IsBypass(UID []byte) bool {
return exist 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() { func (sta *State) UsedRandomCleaner() {
for { for {
time.Sleep(CACHE_CLEAN_INTERVAL) time.Sleep(replayCacheAgeLimit)
sta.usedRandomM.Lock() sta.usedRandomM.Lock()
for key, t := range sta.UsedRandom { 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) delete(sta.UsedRandom, key)
} }
} }

View File

@ -548,9 +548,9 @@ func BenchmarkThroughput(b *testing.B) {
const bufSize = 16 * 1024 const bufSize = 16 * 1024
encryptionMethods := map[string]byte{ encryptionMethods := map[string]byte{
"plain": mux.E_METHOD_PLAIN, "plain": mux.EncryptionMethodPlain,
"chacha20-poly1305": mux.E_METHOD_CHACHA20_POLY1305, "chacha20-poly1305": mux.EncryptionMethodChaha20Poly1305,
"aes-gcm": mux.E_METHOD_AES_GCM, "aes-gcm": mux.EncryptionMethodAESGCM,
} }
for name, method := range encryptionMethods { for name, method := range encryptionMethods {