File Coverage

lib/Wireguard/WGmeta/ValidAttributes.pm
Criterion Covered Total %
statement 54 54 100.0
branch 2 2 100.0
condition n/a
subroutine 16 16 100.0
pod 1 1 100.0
total 73 73 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             WGmeta::ValidAttributes - Supported attribute configurations
4              
5             =head1 DESCRIPTION
6              
7             In this module all supported attributes are configured (and as well their possible validation function). Attributes configured
8             here affect how the parser stores them
9              
10             =head1 SYNOPSIS
11              
12             use Wireguard::WGmeta::ValidAttributes;
13              
14             my $attr_type = get_attr_type($attr_name);
15              
16             if ($attr_type == ATTR_TYPE_IS_WG_META){
17             print "Yiii-haa";
18             }
19              
20              
21             =cut
22              
23             package Wireguard::WGmeta::ValidAttributes;
24 5     5   44 use strict;
  5         10  
  5         163  
25 5     5   25 use warnings FATAL => 'all';
  5         11  
  5         160  
26 5     5   25 use experimental 'signatures';
  5         10  
  5         30  
27              
28 5     5   2610 use Wireguard::WGmeta::Validator;
  5         13  
  5         369  
29              
30             our $VERSION = "0.3.3";
31              
32             =head1 ATTRIBUTE TYPES
33              
34             =cut
35              
36             =head3 ATTR_TYPE_IS_WG_META
37              
38             (Default) - wg-meta attributes
39              
40             =cut
41 5     5   38 use constant ATTR_TYPE_IS_WG_META => 10;
  5         8  
  5         261  
42              
43             =head3 ATTR_TYPE_IS_WG_QUICK
44              
45             wg-quick attribute
46              
47             =cut
48 5     5   26 use constant ATTR_TYPE_IS_WG_QUICK => 12;
  5         11  
  5         218  
49             =head3 ATTR_TYPE_IS_WG_ORIG_INTERFACE
50              
51             Original Wireguard attribute, valid for C<[Interface]> sections.
52              
53             =cut
54 5     5   26 use constant ATTR_TYPE_IS_WG_ORIG_INTERFACE => 13;
  5         9  
  5         247  
55             =head3 ATTR_TYPE_IS_WG_ORIG_PEER
56              
57             Original Wireguard attribute, valid for C<[Peer]> sections.
58              
59             =cut
60 5     5   27 use constant ATTR_TYPE_IS_WG_ORIG_PEER => 14;
  5         10  
  5         224  
61             =head3 ATTR_TYPE_IS_UNKNOWN
62              
63             Any unknown attribute types
64              
65             =cut
66 5     5   27 use constant ATTR_TYPE_IS_UNKNOWN => 15;
  5         18  
  5         264  
67              
68 5     5   27 use constant TRUE => 1;
  5         14  
  5         272  
69 5     5   43 use constant FALSE => 0;
  5         12  
  5         244  
70              
71 5     5   30 use base 'Exporter';
  5         9  
  5         2257  
72             our @EXPORT = qw(
73             ATTR_TYPE_IS_WG_META
74             ATTR_TYPE_IS_WG_QUICK
75             ATTR_TYPE_IS_WG_ORIG_INTERFACE
76             ATTR_TYPE_IS_WG_ORIG_PEER
77             ATTR_TYPE_IS_UNKNOWN
78             NAME_2_KEYS_MAPPING
79             KNOWN_ATTRIBUTES
80             get_attr_type
81             );
82              
83              
84             =head3 KNOWN_ATTRIBUTES
85              
86             Mapping of all known attributes
87              
88             =cut
89              
90 5         974 use constant KNOWN_ATTRIBUTES => {
91             'alias' => {
92             'type' => ATTR_TYPE_IS_WG_META,
93             'in_config_name' => 'Alias',
94             'validator' => \&accept_any
95             },
96             'disabled' => {
97             'type' => ATTR_TYPE_IS_WG_META,
98             'in_config_name' => 'Disabled',
99             'validator' => \&accept_any
100             },
101             'checksum' => {
102             'type' => ATTR_TYPE_IS_WG_META,
103             'in_config_name' => 'Checksum',
104             'validator' => \&accept_any
105             },
106             'address' => {
107             'in_config_name' => 'Address',
108             'type' => ATTR_TYPE_IS_WG_QUICK,
109             'validator' => \&accept_any
110             },
111             'dns' => {
112             'in_config_name' => 'DNS',
113             'type' => ATTR_TYPE_IS_WG_QUICK,
114             'validator' => \&accept_any
115             },
116             'mtu' => {
117             'in_config_name' => 'MTU',
118             'type' => ATTR_TYPE_IS_WG_QUICK,
119             'validator' => \&accept_any
120             },
121             'table' => {
122             'in_config_name' => 'Table',
123             'type' => ATTR_TYPE_IS_WG_QUICK,
124             'validator' => \&accept_any
125             },
126             'pre-up' => {
127             'in_config_name' => 'PreUp',
128             'type' => ATTR_TYPE_IS_WG_QUICK,
129             'validator' => \&accept_any
130             },
131             'post-up' => {
132             'in_config_name' => 'PostUP',
133             'type' => ATTR_TYPE_IS_WG_QUICK,
134             'validator' => \&accept_any
135             },
136             'pre-down' => {
137             'in_config_name' => 'PreDown',
138             'type' => ATTR_TYPE_IS_WG_QUICK,
139             'validator' => \&accept_any
140             },
141             'post-down' => {
142             'in_config_name' => 'PostDown',
143             'type' => ATTR_TYPE_IS_WG_QUICK,
144             'validator' => \&accept_any
145             },
146             'save-config' => {
147             'in_config_name' => 'SaveConfig',
148             'type' => ATTR_TYPE_IS_WG_QUICK,
149             'validator' => \&accept_any
150             },
151             'listen-port' => {
152             'in_config_name' => 'ListenPort',
153             'type' => ATTR_TYPE_IS_WG_ORIG_INTERFACE,
154             'validator' => \&is_number
155             },
156             'fwmark' => {
157             'in_config_name' => 'Fwmark',
158             'type' => ATTR_TYPE_IS_WG_ORIG_INTERFACE,
159             'validator' => \&accept_any
160             },
161             'private-key' => {
162             'in_config_name' => 'PrivateKey',
163             'type' => ATTR_TYPE_IS_WG_ORIG_INTERFACE,
164             'validator' => \&accept_any
165             },
166             'public-key' => {
167             'in_config_name' => 'PublicKey',
168             'type' => ATTR_TYPE_IS_WG_ORIG_PEER,
169             'validator' => \&accept_any
170             },
171             'preshared-key' => {
172             'in_config_name' => 'PresharedKey',
173             'type' => ATTR_TYPE_IS_WG_ORIG_PEER,
174             'validator' => \&accept_any
175             },
176             'endpoint' => {
177             'in_config_name' => 'Endpoint',
178             'type' => ATTR_TYPE_IS_WG_ORIG_PEER,
179             'validator' => \&accept_any
180             },
181             'persistent-keepalive' => {
182             'in_config_name' => 'PersistentKeepalive',
183             'type' => ATTR_TYPE_IS_WG_ORIG_PEER,
184             'validator' => \&accept_any
185             },
186             'allowed-ips' => {
187             'in_config_name' => 'AllowedIPs',
188             'type' => ATTR_TYPE_IS_WG_ORIG_PEER,
189             'validator' => \&accept_any
190             },
191 5     5   37 };
  5         10  
192              
193 5     5   11 sub _create_inconfig_name_mapping() {
  5         8  
194 5         9 my $names2key = {};
195 5         10 map {$names2key->{KNOWN_ATTRIBUTES->{$_}{in_config_name}} = $_;} (keys %{+KNOWN_ATTRIBUTES});
  100         250  
  5         58  
196 5         627 return $names2key;
197             }
198              
199              
200             =head3 NAME_2_KEYS_MAPPING
201              
202             [Generated] Static mapping from I to I.
203              
204             =cut
205 5     5   36 use constant NAME_2_KEYS_MAPPING => _create_inconfig_name_mapping;
  5         10  
  5         12  
206              
207              
208             =head3 get_attr_type($attr_name)
209              
210             Shorthand for getting the attribute type
211              
212             =cut
213              
214 42     42 1 63 sub get_attr_type($attr_name) {
  42         70  
  42         61  
215 42 100       158 return KNOWN_ATTRIBUTES->{$attr_name}{type} if exists KNOWN_ATTRIBUTES->{$attr_name};
216 12         34 return ATTR_TYPE_IS_UNKNOWN;
217             }
218              
219             1;