Skip to content

Commit 7ab36b2

Browse files
committed
Refactor sendToDiscard().
1 parent 5381cf9 commit 7ab36b2

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

service/udp_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ func (m *natTestMetrics) AddUDPCipherSearch(accessKeyFound bool, timeToCipher ti
124124

125125
// Takes a validation policy, and returns the metrics it
126126
// generates when localhost access is attempted
127-
func sendToDiscard(payloads [][]byte, validator onet.TargetIPValidator, useValidCipher bool) *natTestMetrics {
128-
ciphers, _ := MakeTestCiphers([]string{"asdf"})
129-
cipher := ciphers.SnapshotForClientIP(netip.Addr{})[0].Value.(*CipherEntry).CryptoKey
127+
func sendToDiscard(ciphers CipherList, payloads [][]byte, cipher *shadowsocks.EncryptionKey, validator onet.TargetIPValidator) *natTestMetrics {
130128
clientConn := makePacketConn()
131129
metrics := &natTestMetrics{}
132130
handler := NewPacketHandler(timeout, ciphers, metrics)
@@ -141,12 +139,7 @@ func sendToDiscard(payloads [][]byte, validator onet.TargetIPValidator, useValid
141139
targetAddr := socks.ParseAddr("127.0.0.1:9")
142140
for _, payload := range payloads {
143141
plaintext := append(targetAddr, payload...)
144-
var ciphertext []byte
145-
if useValidCipher {
146-
ciphertext = make([]byte, cipher.SaltSize()+len(plaintext)+cipher.TagSize())
147-
} else {
148-
ciphertext = []byte("invalid cipher")
149-
}
142+
ciphertext := make([]byte, cipher.SaltSize()+len(plaintext)+cipher.TagSize())
150143
shadowsocks.Pack(ciphertext, plaintext, cipher)
151144
clientConn.recv <- packet{
152145
addr: &net.UDPAddr{
@@ -163,11 +156,15 @@ func sendToDiscard(payloads [][]byte, validator onet.TargetIPValidator, useValid
163156
}
164157

165158
func sendToDiscardWithValidCipher(payloads [][]byte, validator onet.TargetIPValidator) *natTestMetrics {
166-
return sendToDiscard(payloads, validator, true)
159+
ciphers, _ := MakeTestCiphers([]string{"asdf"})
160+
cipher := ciphers.SnapshotForClientIP(netip.Addr{})[0].Value.(*CipherEntry).CryptoKey
161+
return sendToDiscard(ciphers, payloads, cipher, validator)
167162
}
168163

169164
func sendToDiscardWithInValidCipher(payloads [][]byte, validator onet.TargetIPValidator) *natTestMetrics {
170-
return sendToDiscard(payloads, validator, false)
165+
ciphers, _ := MakeTestCiphers([]string{"asdf"})
166+
cipher, _ := shadowsocks.NewEncryptionKey(shadowsocks.CHACHA20IETFPOLY1305, "invalid cipher")
167+
return sendToDiscard(ciphers, payloads, cipher, validator)
171168
}
172169

173170
func TestIPFilter(t *testing.T) {

0 commit comments

Comments
 (0)