-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpf_parser.v
More file actions
1566 lines (1458 loc) · 41.4 KB
/
Copy pathpf_parser.v
File metadata and controls
1566 lines (1458 loc) · 41.4 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module main
// looks_like_macro reports whether a trimmed line is a macro definition,
// i.e. an identifier followed by '=' (with optional surrounding whitespace),
// such as 'ext_if = "em0"' or 'ext_if="em0"'. This avoids misclassifying
// rules that merely contain '=' (e.g. 'pass ... user = 1000').
fn looks_like_macro(trimmed string) bool {
mut i := 0
for i < trimmed.len && (trimmed[i].is_letter() || trimmed[i].is_digit() || trimmed[i] == `_`) {
i++
}
if i == 0 {
return false // must start with an identifier character
}
// Skip whitespace between the name and '='.
mut j := i
for j < trimmed.len && (trimmed[j] == ` ` || trimmed[j] == `\t`) {
j++
}
return j < trimmed.len && trimmed[j] == `=`
}
fn parse_pf_conf_lines(content string) ![]PfLine {
lines := content.split('\n')
mut pf_lines := []PfLine{}
for i, line in lines {
line_num := i + 1
trimmed := line.trim_space()
mut pf_line := PfLine{
line_num: line_num
}
if trimmed == '' {
pf_line.line_type = 'blank'
} else if trimmed.starts_with('#') {
pf_line.line_type = 'comment'
// Store the original line for comments to preserve exact formatting (dashes, spaces, etc.)
pf_line.raw_line = line
} else {
// Try to classify the line type based on content
if looks_like_macro(trimmed) {
pf_line.line_type = 'macro'
// Split on the first '=' (whitespace around it is optional).
eq_pos := line.index('=') or { -1 }
if eq_pos > 0 {
name_trimmed := line[..eq_pos].trim_space()
value_trimmed := line[eq_pos + 1..].trim_space().trim('"')
pf_line.name = name_trimmed
pf_line.value = value_trimmed
// Preserve the original line unless it is exactly the
// canonical 'name = "value"' the generator would emit.
canonical := '${name_trimmed} = "${value_trimmed}"'
if line.trim_space() != canonical {
pf_line.raw_line = line
}
}
} else if trimmed.starts_with('table ') {
pf_line.line_type = 'table'
// Parse table components
table_name, table_values, table_persist := parse_table_line(line)
pf_line.name = table_name
pf_line.values = table_values
pf_line.persist = table_persist
pf_line.raw_line = line // Preserve original line for exact formatting
// Extract inline comment if present
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('nat ') || trimmed.starts_with('rdr ') || trimmed.starts_with('no nat ') || trimmed.starts_with('binat ') {
is_binat := trimmed.starts_with('binat ')
pf_line.line_type = if trimmed.starts_with('rdr ') {
'rdr'
} else if is_binat {
'binat'
} else {
'nat'
}
// Parse NAT/binat/RDR components
if !trimmed.starts_with('rdr ') {
rule_type, direction, quick, log, iface, inet_family, protocol, source, destination, ports, options, label, tag, tagged, dup_to := parse_nat_rule_line(line)
// binat has no nat-style rule_type; let the generator fall back to the line_type
pf_line.rule_type = if is_binat { '' } else { rule_type }
pf_line.direction = direction
pf_line.quick = quick
pf_line.log = log
pf_line.interfaces = parse_interface_array(iface)
pf_line.inet_families = parse_inet_family_array(inet_family)
pf_line.protocols = parse_protocol_array(protocol)
pf_line.sources = parse_source_array(source)
pf_line.destinations = parse_destination_array(destination)
pf_line.ports = parse_port_array(ports)
pf_line.options = options
pf_line.label = label
pf_line.tag = tag
pf_line.tagged = tagged
pf_line.dup_to = dup_to
} else {
// RDR parsing
rule_type, direction, quick, iface, inet_family, protocol, source, destination, ports, options, label, tag, tagged, dup_to := parse_rdr_rule_line(line)
pf_line.rule_type = rule_type
pf_line.direction = direction
pf_line.quick = quick
pf_line.interfaces = parse_interface_array(iface)
pf_line.inet_families = parse_inet_family_array(inet_family)
pf_line.protocols = parse_protocol_array(protocol)
pf_line.sources = parse_source_array(source)
pf_line.destinations = parse_destination_array(destination)
pf_line.ports = parse_port_array(ports)
pf_line.options = options
pf_line.label = label
pf_line.tag = tag
pf_line.tagged = tagged
pf_line.dup_to = dup_to
}
pf_line.raw_line = line // Preserve original line for exact formatting
// Extract inline comment if present
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('set ') {
pf_line.line_type = 'option'
// Parse option components
option_name, option_value := parse_option_line(line)
pf_line.option_name = option_name
pf_line.option_value = option_value
pf_line.raw_line = line // Preserve original line for exact formatting
// Extract inline comment if present
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('antispoof ') {
pf_line.line_type = 'antispoof'
// Parse antispoof components
antispoof_options, antispoof_interface, has_log := parse_antispoof_line(line)
pf_line.antispoof = antispoof_options
pf_line.interfaces = [antispoof_interface]
pf_line.log = has_log
pf_line.raw_line = line // Preserve original line for exact formatting
// Extract inline comment if present
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('scrub ') {
pf_line.line_type = 'scrub'
// Parse scrub components
scrub_direction, scrub_interface, scrub_options := parse_scrub_line(line)
pf_line.direction = scrub_direction
if scrub_interface != '' {
pf_line.interfaces = [scrub_interface]
}
pf_line.options = scrub_options
pf_line.raw_line = line // Preserve original line for exact formatting
// Extract inline comment if present
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('include ') {
pf_line.line_type = 'include'
pf_line.value = parse_include_line(line)
pf_line.raw_line = line // Preserve original line for exact formatting
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('load anchor ') {
pf_line.line_type = 'load'
load_name, load_path := parse_load_anchor_line(line)
pf_line.name = load_name
pf_line.value = load_path
pf_line.raw_line = line // Preserve original line for exact formatting
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('nat-anchor ') || trimmed.starts_with('rdr-anchor ')
|| trimmed.starts_with('binat-anchor ') {
pf_line.line_type = trimmed.split(' ')[0] // nat-anchor / rdr-anchor / binat-anchor
anchor_name, anchor_condition, opens_block := parse_anchor_line(line)
pf_line.name = anchor_name
pf_line.definition = anchor_condition
pf_line.block_open = opens_block
pf_line.raw_line = line // Preserve original line for exact formatting
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('anchor ') {
pf_line.line_type = 'anchor'
anchor_name, anchor_condition, opens_block := parse_anchor_line(line)
pf_line.name = anchor_name
pf_line.definition = anchor_condition
pf_line.block_open = opens_block
pf_line.raw_line = line // Preserve original line for exact formatting
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed == '}' {
pf_line.line_type = 'anchor-close'
pf_line.raw_line = line // Preserve original line for exact formatting
} else if trimmed.starts_with('match ') {
pf_line.line_type = 'match'
pf_line.raw_line = line // Preserve original line for exact formatting
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
// Reuse the filter-rule parser for the shared components
// (action is 'match'; direction/interface/proto/from/to/port/flags).
action, direction, quick, iface, inet_family, protocol, source, destination, port, options, label, tag, tagged, dup_to, flags, icmp_type, icmp6_type := parse_rule_line(line)
pf_line.action = action
pf_line.direction = direction
pf_line.quick = quick
pf_line.inet_families = parse_inet_family_array(inet_family)
pf_line.interfaces = parse_interface_array(iface)
pf_line.protocols = parse_protocol_array(protocol)
pf_line.sources = parse_source_array(source)
pf_line.destinations = parse_destination_array(destination)
pf_line.ports = parse_port_array(port)
pf_line.options = options
pf_line.label = label
pf_line.tag = tag
pf_line.tagged = tagged
pf_line.dup_to = dup_to
pf_line.flags = flags
pf_line.icmp_type = icmp_type
pf_line.icmp6_type = icmp6_type
route_to, reply_to := parse_rule_routing(line)
pf_line.route_to = route_to
pf_line.reply_to = reply_to
mods := parse_rule_modifiers(line)
pf_line.user = mods.user
pf_line.group = mods.group
pf_line.rtable = mods.rtable
pf_line.probability = mods.probability
pf_line.received_on = mods.received_on
pf_line.divert_to = mods.divert_to
pf_line.prio = mods.prio
// Capture the match-specific redirection action, if any.
redirect_kw, redirect_target := parse_match_redirection(line)
pf_line.rule_type = redirect_kw
pf_line.target = redirect_target
} else if trimmed.starts_with('altq ') {
pf_line.line_type = 'altq'
altq_iface, altq_params, altq_queues := parse_altq_line(line)
if altq_iface != '' {
pf_line.interfaces = [altq_iface]
}
pf_line.definition = altq_params
pf_line.values = altq_queues
pf_line.raw_line = line // Preserve original line for exact formatting
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('queue ') {
pf_line.line_type = 'queue'
queue_name, queue_iface, queue_params, queue_subs := parse_queue_line(line)
pf_line.name = queue_name
if queue_iface != '' {
pf_line.interfaces = [queue_iface]
}
pf_line.definition = queue_params
pf_line.values = queue_subs
pf_line.raw_line = line // Preserve original line for exact formatting
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
} else if trimmed.starts_with('pass ') || trimmed.starts_with('block ')
|| trimmed == 'pass' || trimmed == 'block' {
pf_line.line_type = 'rule'
pf_line.raw_line = line // Preserve original line for exact formatting
// Extract inline comment first
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
// Parse rule components
action, direction, quick, iface, inet_family, protocol, source, destination, port, options, label, tag, tagged, dup_to, flags, icmp_type, icmp6_type := parse_rule_line(line)
pf_line.action = action
pf_line.direction = direction
pf_line.quick = quick
pf_line.inet_families = parse_inet_family_array(inet_family)
pf_line.interfaces = parse_interface_array(iface)
pf_line.protocols = parse_protocol_array(protocol)
pf_line.sources = parse_source_array(source)
pf_line.destinations = parse_destination_array(destination)
pf_line.ports = parse_port_array(port)
pf_line.options = options
pf_line.label = label
pf_line.tag = tag
pf_line.tagged = tagged
pf_line.dup_to = dup_to
pf_line.flags = flags
pf_line.icmp_type = icmp_type
pf_line.icmp6_type = icmp6_type
route_to, reply_to := parse_rule_routing(line)
pf_line.route_to = route_to
pf_line.reply_to = reply_to
mods := parse_rule_modifiers(line)
pf_line.user = mods.user
pf_line.group = mods.group
pf_line.rtable = mods.rtable
pf_line.probability = mods.probability
pf_line.received_on = mods.received_on
pf_line.divert_to = mods.divert_to
pf_line.prio = mods.prio
} else {
pf_line.line_type = 'unknown'
pf_line.raw_line = line // Preserve original line for exact formatting
// Extract inline comment if present
_, inline_comment := extract_inline_comment(line)
if inline_comment != '' {
pf_line.comment = inline_comment
}
}
}
pf_lines << pf_line
}
return pf_lines
}
// Extract inline comment from a line
fn extract_inline_comment(line string) (string, string) {
// Find the last '#' that's not inside quotes
mut in_quotes := false
mut quote_char := `"`
for i := line.len - 1; i >= 0; i-- {
ch := line[i]
if ch == `"` || ch == `'` {
if !in_quotes {
in_quotes = true
quote_char = ch
} else if ch == quote_char {
in_quotes = false
}
} else if ch == `#` && !in_quotes {
// Found inline comment
rule_part := line[..i].trim_right(' \t')
comment_part := line[i + 1..].trim_space()
return rule_part, comment_part
}
}
// No inline comment found
return line, ''
}
fn parse_rule_line(line string) (string, string, bool, string, string, string, string, string, string, []string, string, string, string, string, string, string, string) {
// Remove inline comment first
rule_part, _ := extract_inline_comment(line)
// Split into tokens (simple space-based for now)
parts := rule_part.trim_space().split(' ')
if parts.len == 0 {
return '', '', false, '', '', '', '', '', '', []string{}, '', '', '', '', '', '', ''
}
mut action := ''
mut direction := ''
mut quick := false
mut iface := ''
mut inet_family := ''
mut protocol := ''
mut source := ''
mut destination := ''
mut port := ''
mut options := []string{}
mut label := ''
mut tag := ''
mut tagged := ''
mut dup_to := ''
mut flags := ''
mut icmp_type := ''
mut icmp6_type := ''
mut i := 0
// Parse action (pass/block)
if i < parts.len {
action = parts[i]
i++
}
// Parse remaining components
for i < parts.len {
part := parts[i]
match part {
'in', 'out' {
direction = part
i++
}
'quick' {
quick = true
i++
}
'on' {
if i + 1 < parts.len {
i++
iface = parts[i]
i++
} else {
i++
}
}
'proto' {
if i + 1 < parts.len {
i++
// Handle complex protocols like { tcp, udp }
if parts[i].starts_with('{') {
mut proto_parts := []string{}
for i < parts.len && !parts[i].ends_with('}') {
proto_parts << parts[i]
i++
}
if i < parts.len {
proto_parts << parts[i] // include the closing brace
i++
}
protocol = proto_parts.join(' ')
} else {
protocol = parts[i]
i++
}
} else {
i++
}
}
'inet' {
inet_family = 'inet'
i++
}
'inet6' {
inet_family = 'inet6'
i++
}
'from' {
if i + 1 < parts.len {
i++
// Handle complex sources like { <vpn-trusted> } or { addr1, addr2 }
if parts[i].starts_with('{') || parts[i].starts_with('<') {
mut src_parts := []string{}
for i < parts.len && !parts[i].ends_with('}') && !parts[i].ends_with('>') {
src_parts << parts[i]
i++
}
if i < parts.len {
src_parts << parts[i] // include the closing brace/bracket
i++
}
source = src_parts.join(' ')
} else {
source = parts[i]
i++
}
} else {
i++
}
}
'to' {
// "to port N" has no destination address; leave 'port' for the
// port handler rather than consuming it as the destination.
if i + 1 < parts.len && parts[i + 1] != 'port' {
i++
// Handle complex destinations
if parts[i].starts_with('{') || parts[i].starts_with('<') {
mut dst_parts := []string{}
for i < parts.len && !parts[i].ends_with('}') && !parts[i].ends_with('>') {
dst_parts << parts[i]
i++
}
if i < parts.len {
dst_parts << parts[i] // include the closing brace/bracket
i++
}
destination = dst_parts.join(' ')
} else {
destination = parts[i]
i++
}
} else {
i++
}
}
'port' {
if i + 1 < parts.len {
i++
// Handle complex ports like { 80, 443 }
if parts[i].starts_with('{') {
mut port_parts := []string{}
for i < parts.len && !parts[i].ends_with('}') {
port_parts << parts[i]
i++
}
if i < parts.len {
port_parts << parts[i] // include the closing brace
i++
}
port = port_parts.join(' ')
} else {
port = parts[i]
i++
}
} else {
i++
}
}
'keep' {
if i + 1 < parts.len && parts[i + 1] == 'state' {
options << 'keep state'
i += 2
} else {
i++
}
}
'log' {
options << 'log'
i++
}
'flags' {
if i + 1 < parts.len {
i++
flags = parts[i]
i++
} else {
i++
}
}
'label' {
if i + 1 < parts.len {
i++
label = parts[i]
i++
} else {
i++
}
}
'tag' {
if i + 1 < parts.len {
i++
tag = parts[i]
i++
} else {
i++
}
}
'tagged' {
if i + 1 < parts.len {
i++
tagged = parts[i]
i++
} else {
i++
}
}
'dup-to' {
if i + 1 < parts.len {
i++
// Handle dup-to destinations like ($ext_if 192.168.1.1)
if parts[i].starts_with('(') {
mut dup_parts := []string{}
for i < parts.len && !parts[i].ends_with(')') {
dup_parts << parts[i]
i++
}
if i < parts.len {
dup_parts << parts[i] // include the closing paren
i++
}
dup_to = dup_parts.join(' ')
} else {
dup_to = parts[i]
i++
}
} else {
i++
}
}
'icmp-type' {
if i + 1 < parts.len {
i++
// Handle braced expressions like "{ echoreq, echorep }"
if parts[i].starts_with('{') {
mut icmp_parts := []string{}
for i < parts.len {
icmp_parts << parts[i]
if parts[i].ends_with('}') {
break
}
i++
}
icmp_type = icmp_parts.join(' ')
} else {
icmp_type = parts[i]
}
i++
} else {
i++
}
}
'icmp6-type' {
if i + 1 < parts.len {
i++
// Handle braced expressions like "{ neighbrsol, neighbradv }"
if parts[i].starts_with('{') {
mut icmp6_parts := []string{}
for i < parts.len {
icmp6_parts << parts[i]
if parts[i].ends_with('}') {
break
}
i++
}
icmp6_type = icmp6_parts.join(' ')
} else {
icmp6_type = parts[i]
}
i++
} else {
i++
}
}
else {
// Skip unknown tokens
i++
}
}
}
return action, direction, quick, iface, inet_family, protocol, source, destination, port, options, label, tag, tagged, dup_to, flags, icmp_type, icmp6_type
}
// Parse NAT rule line into components (e.g., "nat pass log on eth0 from { 192.168.1.0/24 } to { any } -> (eth0)")
fn parse_nat_rule_line(line string) (string, string, bool, bool, string, string, string, string, string, string, []string, string, string, string, string) {
mut action := 'nat'
mut direction := ''
mut quick := false
mut log := false
mut iface := ''
mut inet_family := ''
mut protocol := ''
mut source := ''
mut destination := ''
mut port := ''
mut options := []string{}
mut label := ''
mut tag := ''
mut tagged := ''
mut dup_to := ''
// Parse the initial NAT rule type and options
if line.starts_with('no nat') {
action = 'no nat'
} else if line.contains('nat pass log') {
action = 'pass'
log = true
} else if line.contains('nat log') {
action = 'nat'
log = true
} else if line.contains('nat pass') {
action = 'pass'
} else {
action = 'nat'
}
// Extract components using regex-like parsing
parts := line.split(' ')
mut i := 0
for i < parts.len {
match parts[i] {
'on' {
if i + 1 < parts.len {
i++
iface = parts[i]
}
}
'from' {
if i + 1 < parts.len {
i++
// Handle complex source specifications like { $net_dmz }
mut source_parts := []string{}
if parts[i].starts_with('{') {
// Collect all parts until we find the closing }
for i < parts.len && !parts[i].ends_with('}') {
source_parts << parts[i]
i++
}
if i < parts.len {
source_parts << parts[i] // Add the closing part
}
source_raw := source_parts.join(' ')
// Keep the braced format for array parsing
source = source_raw
} else {
source = parts[i]
}
}
}
'to' {
if i + 1 < parts.len {
i++
// Handle complex destination specifications like { 192.168.178.4 }
mut dest_parts := []string{}
if parts[i].starts_with('{') {
// Collect all parts until we find the closing }
for i < parts.len && !parts[i].ends_with('}') {
dest_parts << parts[i]
i++
}
if i < parts.len {
dest_parts << parts[i] // Add the closing part
}
dest_raw := dest_parts.join(' ')
// Keep the braced format for array parsing
destination = dest_raw
} else {
// Handle destination until -> or end
for i < parts.len && parts[i] != '->' {
dest_parts << parts[i]
i++
}
if dest_parts.len > 0 {
destination = dest_parts.join(' ')
}
i-- // Adjust for the outer loop increment
}
}
}
else {}
}
i++
}
return action, direction, quick, log, iface, inet_family, protocol, source, destination, port, options, label, tag, tagged, dup_to
}
// Parse RDR rule line into components (e.g., "rdr on eth0 proto tcp from any to 192.168.1.1 port 80 -> 10.0.0.1 port 8080")
fn parse_rdr_rule_line(line string) (string, string, bool, string, string, string, string, string, string, []string, string, string, string, string) {
mut action := '' // Start with empty action
mut direction := ''
mut quick := false
mut iface := ''
mut inet_family := ''
mut protocol := ''
mut source := ''
mut destination := ''
mut port := ''
mut options := []string{}
mut label := ''
mut tag := ''
mut tagged := ''
mut dup_to := ''
// Detect "pass" action in RDR rules
if line.contains('rdr pass ') {
action = 'pass'
}
// If no pass is found, action remains empty (will be omitted due to @[omitempty])
// Extract components using regex-like parsing
parts := line.split(' ')
mut i := 0
for i < parts.len {
match parts[i] {
'on' {
if i + 1 < parts.len {
i++
iface = parts[i]
}
}
'proto' {
if i + 1 < parts.len {
i++
protocol = parts[i]
}
}
'from' {
if i + 1 < parts.len {
i++
// Handle complex source specifications like { 172.16.0.65 }
mut source_parts := []string{}
if parts[i].starts_with('{') {
// Collect all parts until we find the closing }
for i < parts.len && !parts[i].ends_with('}') {
source_parts << parts[i]
i++
}
if i < parts.len {
source_parts << parts[i] // Add the closing part
}
source = source_parts.join(' ')
} else {
source = parts[i]
}
}
}
'to' {
if i + 1 < parts.len {
i++
// Handle destination until 'port' or '->'
mut dest_parts := []string{}
for i < parts.len && parts[i] != 'port' && parts[i] != '->' {
dest_parts << parts[i]
i++
}
if dest_parts.len > 0 {
destination = dest_parts.join(' ')
}
i-- // Adjust for the outer loop increment
}
}
'port' {
if i + 1 < parts.len {
i++
// Check if this is source port or destination port
if parts[i-2] == 'to' || (i > 2 && parts[i-3] == 'to') {
// Handle complex port specifications like { ldap, ldaps }
mut port_parts := []string{}
if parts[i].starts_with('{') {
// Collect all parts until we find the closing }
for i < parts.len && !parts[i].ends_with('}') {
port_parts << parts[i]
i++
}
if i < parts.len {
port_parts << parts[i] // Add the closing part
}
port = port_parts.join(' ')
} else {
port = parts[i]
}
}
}
}
else {}
}
i++
}
return action, direction, quick, iface, inet_family, protocol, source, destination, port, options, label, tag, tagged, dup_to
}
// Parse protocol string into array (e.g., "{ tcp, udp }" -> ["tcp", "udp"])
fn parse_protocol_array(protocol_str string) []string {
if protocol_str == '' {
return []
}
trimmed := protocol_str.trim_space()
// Handle protocol lists like "{ tcp, udp }"
if trimmed.starts_with('{') && trimmed.ends_with('}') {
inner := trimmed[1..trimmed.len-1].trim_space()
parts := inner.split(',')
mut protocols := []string{}
for part in parts {
cleaned := part.trim_space()
if cleaned != '' {
protocols << cleaned
}
}
return protocols
} else {
// Single protocol
return [trimmed]
}
}
// Parse port string into array (e.g., "{ 80, 443 }" -> ["80", "443"])
fn parse_port_array(port_str string) []string {
if port_str == '' {
return []
}
trimmed := port_str.trim_space()
// Handle port lists like "{ 80, 443 }" or "{ http, https }"
if trimmed.starts_with('{') && trimmed.ends_with('}') {
inner := trimmed[1..trimmed.len-1].trim_space()
parts := inner.split(',')
mut ports := []string{}
for part in parts {
cleaned := part.trim_space()
if cleaned != '' {
ports << cleaned
}
}
return ports
} else {
// Single port or port range
return [trimmed]
}
}
// Parse inet family string into array (handles "inet", "inet6", or empty)
fn parse_inet_family_array(inet_family_str string) []string {
if inet_family_str == '' {
return []
}
trimmed := inet_family_str.trim_space()
return [trimmed]
}
// Parse source string into array (e.g., "{ addr1, addr2 }" -> ["addr1", "addr2"])
fn parse_source_array(source_str string) []string {
if source_str == '' {
return []
}
trimmed := source_str.trim_space()
// Handle source lists like "{ addr1, addr2 }" or "{ !addr1, !addr2 }"
if trimmed.starts_with('{') && trimmed.ends_with('}') {
inner := trimmed[1..trimmed.len-1].trim_space()
parts := inner.split(',')
mut sources := []string{}
for part in parts {
cleaned := part.trim_space()
if cleaned != '' {
sources << cleaned
}
}
return sources
} else {
// Single source
return [trimmed]
}
}
// Parse interface string into array (e.g., "{ eth0, eth1 }" -> ["eth0", "eth1"])
fn parse_interface_array(interface_str string) []string {
if interface_str == '' {
return []
}
trimmed := interface_str.trim_space()
// Handle interface lists like "{ eth0, eth1 }"
if trimmed.starts_with('{') && trimmed.ends_with('}') {
inner := trimmed[1..trimmed.len-1].trim_space()
parts := inner.split(',')
mut interfaces := []string{}
for part in parts {
cleaned := part.trim_space()
if cleaned != '' {
interfaces << cleaned
}
}
return interfaces
} else {
// Single interface
return [trimmed]
}
}
// Parse destination string into array (e.g., "{ addr1, addr2 }" -> ["addr1", "addr2"])
fn parse_destination_array(destination_str string) []string {
if destination_str == '' {
return []
}
trimmed := destination_str.trim_space()
// Handle destination lists like "{ addr1, addr2 }" or "{ !addr1, !addr2 }"
if trimmed.starts_with('{') && trimmed.ends_with('}') {
inner := trimmed[1..trimmed.len-1].trim_space()
parts := inner.split(',')
mut destinations := []string{}
for part in parts {
cleaned := part.trim_space()
if cleaned != '' {
destinations << cleaned
}
}
return destinations
} else {
// Single destination