Skip to content

Commit e51448f

Browse files
committed
ipn/amnezia: add logs + disable amnezia
1 parent bb1a4c9 commit e51448f

1 file changed

Lines changed: 31 additions & 21 deletions

File tree

intra/ipn/wg/amnezia.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import (
1818
// https://github.com/amnezia-vpn/amneziawg-go/pull/2/files
1919

2020
const (
21-
sNoop = 0 // no-op size
21+
// TODO: re-enable after figuring out how to account for
22+
// changing the message header values for cookies mac1 & mac2
23+
// here: github.com/amnezia-vpn/amneziawg-go/blob/27e661d68e/device/send.go#L167
24+
disableAmenzia = true
25+
sNoop = 0 // no-op size
2226
)
2327

2428
// Jc (Junk packet count) - number of packets with random data that are sent before the start of the session
@@ -49,6 +53,9 @@ func (a *Amnezia) String() string {
4953
if a == nil {
5054
return "<nil>"
5155
}
56+
if disableAmenzia {
57+
return "<disabled>"
58+
}
5259
if !a.Set() {
5360
return "<unset>"
5461
}
@@ -57,7 +64,7 @@ func (a *Amnezia) String() string {
5764
}
5865

5966
func (a *Amnezia) Set() bool {
60-
if a == nil {
67+
if a == nil || disableAmenzia {
6168
return false
6269
}
6370

@@ -93,9 +100,10 @@ func (a *Amnezia) send(pktptr *[]byte) (ok bool) {
93100

94101
typ := binary.LittleEndian.Uint32(pkt)
95102

96-
a.logIfNeeded("send", typ, n)
97-
98103
*pktptr, _ = a.instate(pkt)
104+
105+
a.logIfNeeded("send", typ, n, len(*pktptr))
106+
99107
return true
100108
}
101109

@@ -107,13 +115,15 @@ func (a *Amnezia) recv(pktptr *[]byte) (ok bool) {
107115
var typ uint32
108116
pkt := *pktptr
109117

110-
if len(pkt) < device.MinMessageSize {
118+
recvLen := len(pkt)
119+
if recvLen < device.MinMessageSize {
111120
return
112121
}
113122
// h := uint16(device.MessageTransportOffsetReceiver)
114123

115124
pkt, typ = a.strip(pkt)
116125

126+
stripLen := len(pkt)
117127
switch typ {
118128
case device.MessageInitiationType, a.H1:
119129
typ = device.MessageInitiationType
@@ -129,7 +139,7 @@ func (a *Amnezia) recv(pktptr *[]byte) (ok bool) {
129139
binary.LittleEndian.PutUint32(pkt, device.MessageTransportType)
130140
}
131141

132-
a.logIfNeeded("recv", typ, len(pkt))
142+
a.logIfNeeded("recv", typ, recvLen, stripLen)
133143

134144
*pktptr = pkt
135145
return true
@@ -176,17 +186,17 @@ func (a *Amnezia) instate(pkt []byte) ([]byte, uint32) {
176186
log.VV("wg: %s: amnezia: instate: msg size: %d, msg typ: (d: %d, o: %d), pad? %t, s1/s2: %d/%d, do? %t",
177187
a.id, n, defaultType, obsType, pad > 0, a.S1, a.S2, maybeInstate)
178188

179-
if obsType > 0 {
189+
if defaultType != obsType {
180190
binary.LittleEndian.PutUint32(pkt, obsType)
181191
}
182192
// pad may be 0
183193
if random, err := blob(pad); err != nil { // unlikely
184-
log.E("wg: %s: amnezia: instate: %v", a.id, err)
194+
log.E("wg: %s: amnezia: instate: pad err %v", a.id, err)
185195
} else if len(random) > 0 && len(random) == int(pad) {
186196
pkt = append(random, pkt...)
187197
}
188198

189-
if obsType > 0 {
199+
if defaultType != obsType {
190200
return pkt, obsType
191201
}
192202
return pkt, defaultType
@@ -229,27 +239,27 @@ func (a *Amnezia) strip(pkt []byte) ([]byte, uint32) {
229239
return pkt, uint32(defaultType)
230240
}
231241

232-
func (a *Amnezia) logIfNeeded(dir string, typ uint32, n int) {
242+
func (a *Amnezia) logIfNeeded(dir string, typ uint32, n int, newn int) {
233243
switch typ {
234244
case device.MessageInitiationType:
235245
notok := n != device.MessageInitiationSize
236-
logif(notok)("wg: %s: amnezia: %s: err initiation %d != %d",
237-
a.id, dir, n, device.MessageInitiationSize)
246+
logif(notok)("wg: %s: amnezia: %s: err initiation %d != %d (=> %d)",
247+
a.id, dir, n, device.MessageInitiationSize, newn)
238248
case device.MessageResponseType:
239249
notok := n != device.MessageResponseSize
240-
logif(notok)("wg: %s: amnezia: %s: err response %d != %d",
241-
a.id, dir, n, device.MessageResponseSize)
250+
logif(notok)("wg: %s: amnezia: %s: err response %d != %d (=> %d)",
251+
a.id, dir, n, device.MessageResponseSize, newn)
242252
case device.MessageCookieReplyType:
243253
notok := n != device.MessageCookieReplySize
244-
logif(notok)("wg: %s: amnezia: %s: err cookie %d != %d",
245-
a.id, dir, n, device.MessageCookieReplySize)
254+
logif(notok)("wg: %s: amnezia: %s: err cookie %d != %d (=> %d)",
255+
a.id, dir, n, device.MessageCookieReplySize, newn)
246256
case device.MessageTransportType:
247257
notok := n < device.MinMessageSize
248-
logif(notok)("wg: %s: amnezia: %s: err data %d < %d",
249-
a.id, dir, n, device.MinMessageSize)
258+
logif(notok)("wg: %s: amnezia: %s: err data %d < %d (=> %d)",
259+
a.id, dir, n, device.MinMessageSize, newn)
250260
default:
251-
log.W("wg: %s: amnezia: %s: unexpected type %d; sz(pkt): %d",
252-
a.id, dir, typ, n)
261+
log.W("wg: %s: amnezia: %s: unexpected type %d; sz(pkt): %d => %d",
262+
a.id, dir, typ, n, newn)
253263
}
254264
}
255265

@@ -268,5 +278,5 @@ func logif(cond bool) log.LogFn {
268278
if cond {
269279
return log.D
270280
}
271-
return log.VV
281+
return log.N
272282
}

0 commit comments

Comments
 (0)