33
44import Foundation
55
6- public extension TunnelConfiguration {
6+ extension TunnelConfiguration {
77
88 enum ParserState {
99 case inInterfaceSection
@@ -22,6 +22,7 @@ public extension TunnelConfiguration {
2222 case interfaceHasInvalidDNS( String )
2323 case interfaceHasInvalidMTU( String )
2424 case interfaceHasUnrecognizedKey( String )
25+ case interfaceHasInvalidCustomParam( String )
2526 case peerHasNoPublicKey
2627 case peerHasInvalidPublicKey( String )
2728 case peerHasInvalidPreSharedKey( String )
@@ -71,7 +72,33 @@ public extension TunnelConfiguration {
7172 } else {
7273 attributes [ key] = value
7374 }
74- let interfaceSectionKeys : Set < String > = [ " privatekey " , " listenport " , " address " , " dns " , " mtu " ]
75+ let interfaceSectionKeys : Set < String > = [
76+ " privatekey " ,
77+ " listenport " ,
78+ " address " ,
79+ " dns " ,
80+ " mtu " ,
81+ " jc " ,
82+ " jmin " ,
83+ " jmax " ,
84+ " s1 " ,
85+ " s2 " ,
86+ " s3 " ,
87+ " s4 " ,
88+ " h1 " ,
89+ " h2 " ,
90+ " h3 " ,
91+ " h4 " ,
92+ " i1 " ,
93+ " i2 " ,
94+ " i3 " ,
95+ " i4 " ,
96+ " i5 " ,
97+ " j1 " ,
98+ " j2 " ,
99+ " j3 " ,
100+ " itime " ,
101+ ]
75102 let peerSectionKeys : Set < String > = [ " publickey " , " presharedkey " , " allowedips " , " endpoint " , " persistentkeepalive " ]
76103 if parserState == . inInterfaceSection {
77104 guard interfaceSectionKeys. contains ( key) else {
@@ -129,6 +156,67 @@ public extension TunnelConfiguration {
129156 if let listenPort = interface. listenPort {
130157 output. append ( " ListenPort = \( listenPort) \n " )
131158 }
159+
160+ if let junkPacketCount = interface. junkPacketCount {
161+ output. append ( " Jc = \( junkPacketCount) \n " )
162+ }
163+ if let junkPacketMinSize = interface. junkPacketMinSize {
164+ output. append ( " Jmin = \( junkPacketMinSize) \n " )
165+ }
166+ if let junkPacketMaxSize = interface. junkPacketMaxSize {
167+ output. append ( " Jmax = \( junkPacketMaxSize) \n " )
168+ }
169+ if let initPacketJunkSize = interface. initPacketJunkSize {
170+ output. append ( " S1 = \( initPacketJunkSize) \n " )
171+ }
172+ if let responsePacketJunkSize = interface. responsePacketJunkSize {
173+ output. append ( " S2 = \( responsePacketJunkSize) \n " )
174+ }
175+ if let cookieReplyPacketJunkSize = interface. cookieReplyPacketJunkSize {
176+ output. append ( " S3 = \( cookieReplyPacketJunkSize) \n " )
177+ }
178+ if let transportPacketJunkSize = interface. transportPacketJunkSize {
179+ output. append ( " S4 = \( transportPacketJunkSize) \n " )
180+ }
181+ if let initPacketMagicHeader = interface. initPacketMagicHeader {
182+ output. append ( " H1 = \( initPacketMagicHeader) \n " )
183+ }
184+ if let responsePacketMagicHeader = interface. responsePacketMagicHeader {
185+ output. append ( " H2 = \( responsePacketMagicHeader) \n " )
186+ }
187+ if let underloadPacketMagicHeader = interface. underloadPacketMagicHeader {
188+ output. append ( " H3 = \( underloadPacketMagicHeader) \n " )
189+ }
190+ if let transportPacketMagicHeader = interface. transportPacketMagicHeader {
191+ output. append ( " H4 = \( transportPacketMagicHeader) \n " )
192+ }
193+ if let specialJunk1 = interface. specialJunk1 {
194+ output. append ( " I1 = \( specialJunk1) \n " )
195+ }
196+ if let specialJunk2 = interface. specialJunk2 {
197+ output. append ( " I2 = \( specialJunk2) \n " )
198+ }
199+ if let specialJunk3 = interface. specialJunk3 {
200+ output. append ( " I3 = \( specialJunk3) \n " )
201+ }
202+ if let specialJunk4 = interface. specialJunk4 {
203+ output. append ( " I4 = \( specialJunk4) \n " )
204+ }
205+ if let specialJunk5 = interface. specialJunk5 {
206+ output. append ( " I5 = \( specialJunk5) \n " )
207+ }
208+ if let controlledJunk1 = interface. controlledJunk1 {
209+ output. append ( " J1 = \( controlledJunk1) \n " )
210+ }
211+ if let controlledJunk2 = interface. controlledJunk2 {
212+ output. append ( " J2 = \( controlledJunk2) \n " )
213+ }
214+ if let controlledJunk3 = interface. controlledJunk3 {
215+ output. append ( " J3 = \( controlledJunk3) \n " )
216+ }
217+ if let specialHandshakeTimeout = interface. specialHandshakeTimeout {
218+ output. append ( " Itime = \( specialHandshakeTimeout) \n " )
219+ }
132220 if !interface. addresses. isEmpty {
133221 let addressString = interface. addresses. map { $0. stringRepresentation } . joined ( separator: " , " )
134222 output. append ( " Address = \( addressString) \n " )
@@ -207,6 +295,102 @@ public extension TunnelConfiguration {
207295 }
208296 interface. mtu = mtu
209297 }
298+ if let junkPacketCountString = attributes [ " jc " ] {
299+ guard let junkPacketCount = UInt16 ( junkPacketCountString) else {
300+ throw ParseError . interfaceHasInvalidCustomParam ( junkPacketCountString)
301+ }
302+ interface. junkPacketCount = junkPacketCount
303+ }
304+ if let junkPacketMinSizeString = attributes [ " jmin " ] {
305+ guard let junkPacketMinSize = UInt16 ( junkPacketMinSizeString) else {
306+ throw ParseError . interfaceHasInvalidCustomParam ( junkPacketMinSizeString)
307+ }
308+ interface. junkPacketMinSize = junkPacketMinSize
309+ }
310+ if let junkPacketMaxSizeString = attributes [ " jmax " ] {
311+ guard let junkPacketMaxSize = UInt16 ( junkPacketMaxSizeString) else {
312+ throw ParseError . interfaceHasInvalidCustomParam ( junkPacketMaxSizeString)
313+ }
314+ interface. junkPacketMaxSize = junkPacketMaxSize
315+ }
316+ if let initPacketJunkSizeString = attributes [ " s1 " ] {
317+ guard let initPacketJunkSize = UInt16 ( initPacketJunkSizeString) else {
318+ throw ParseError . interfaceHasInvalidCustomParam ( initPacketJunkSizeString)
319+ }
320+ interface. initPacketJunkSize = initPacketJunkSize
321+ }
322+ if let responsePacketJunkSizeString = attributes [ " s2 " ] {
323+ guard let responsePacketJunkSize = UInt16 ( responsePacketJunkSizeString) else {
324+ throw ParseError . interfaceHasInvalidCustomParam ( responsePacketJunkSizeString)
325+ }
326+ interface. responsePacketJunkSize = responsePacketJunkSize
327+ }
328+ if let cookieReplyPacketJunkSizeString = attributes [ " s3 " ] {
329+ guard let cookieReplyPacketJunkSize = UInt16 ( cookieReplyPacketJunkSizeString) else {
330+ throw ParseError . interfaceHasInvalidCustomParam ( cookieReplyPacketJunkSizeString)
331+ }
332+ interface. cookieReplyPacketJunkSize = cookieReplyPacketJunkSize
333+ }
334+ if let transportPacketJunkSizeString = attributes [ " s4 " ] {
335+ guard let transportPacketJunkSize = UInt16 ( transportPacketJunkSizeString) else {
336+ throw ParseError . interfaceHasInvalidCustomParam ( transportPacketJunkSizeString)
337+ }
338+ interface. transportPacketJunkSize = transportPacketJunkSize
339+ }
340+ if let initPacketMagicHeaderString = attributes [ " h1 " ] {
341+ guard let initPacketMagicHeader = UInt32 ( initPacketMagicHeaderString) else {
342+ throw ParseError . interfaceHasInvalidCustomParam ( initPacketMagicHeaderString)
343+ }
344+ interface. initPacketMagicHeader = initPacketMagicHeader
345+ }
346+ if let responsePacketMagicHeaderString = attributes [ " h2 " ] {
347+ guard let responsePacketMagicHeader = UInt32 ( responsePacketMagicHeaderString) else {
348+ throw ParseError . interfaceHasInvalidCustomParam ( responsePacketMagicHeaderString)
349+ }
350+ interface. responsePacketMagicHeader = responsePacketMagicHeader
351+ }
352+ if let underloadPacketMagicHeaderString = attributes [ " h3 " ] {
353+ guard let underloadPacketMagicHeader = UInt32 ( underloadPacketMagicHeaderString) else {
354+ throw ParseError . interfaceHasInvalidCustomParam ( underloadPacketMagicHeaderString)
355+ }
356+ interface. underloadPacketMagicHeader = underloadPacketMagicHeader
357+ }
358+ if let transportPacketMagicHeaderString = attributes [ " h4 " ] {
359+ guard let transportPacketMagicHeader = UInt32 ( transportPacketMagicHeaderString) else {
360+ throw ParseError . interfaceHasInvalidCustomParam ( transportPacketMagicHeaderString)
361+ }
362+ interface. transportPacketMagicHeader = transportPacketMagicHeader
363+ }
364+ if let specialJunk1String = attributes [ " i1 " ] {
365+ interface. specialJunk1 = specialJunk1String
366+ }
367+ if let specialJunk2String = attributes [ " i2 " ] {
368+ interface. specialJunk2 = specialJunk2String
369+ }
370+ if let specialJunk3String = attributes [ " i3 " ] {
371+ interface. specialJunk3 = specialJunk3String
372+ }
373+ if let specialJunk4String = attributes [ " i4 " ] {
374+ interface. specialJunk4 = specialJunk4String
375+ }
376+ if let specialJunk5String = attributes [ " i5 " ] {
377+ interface. specialJunk5 = specialJunk5String
378+ }
379+ if let controlledJunk1String = attributes [ " j1 " ] {
380+ interface. controlledJunk1 = controlledJunk1String
381+ }
382+ if let controlledJunk2String = attributes [ " j2 " ] {
383+ interface. controlledJunk2 = controlledJunk2String
384+ }
385+ if let controlledJunk3String = attributes [ " j3 " ] {
386+ interface. controlledJunk3 = controlledJunk3String
387+ }
388+ if let specialHandshakeTimeoutString = attributes [ " itime " ] {
389+ guard let specialHandshakeTimeout = Int ( specialHandshakeTimeoutString) else {
390+ throw ParseError . interfaceHasInvalidCustomParam ( specialHandshakeTimeoutString)
391+ }
392+ interface. specialHandshakeTimeout = specialHandshakeTimeout
393+ }
210394 return interface
211395 }
212396
0 commit comments