14
14
import com .wireguard .util .NonNullForAll ;
15
15
16
16
import java .net .InetAddress ;
17
+ import java .util .ArrayList ;
17
18
import java .util .Collection ;
18
19
import java .util .Collections ;
19
20
import java .util .LinkedHashSet ;
@@ -45,6 +46,10 @@ public final class Interface {
45
46
private final KeyPair keyPair ;
46
47
private final Optional <Integer > listenPort ;
47
48
private final Optional <Integer > mtu ;
49
+ private final List <String > preUp ;
50
+ private final List <String > postUp ;
51
+ private final List <String > preDown ;
52
+ private final List <String > postDown ;
48
53
49
54
private Interface (final Builder builder ) {
50
55
// Defensively copy to ensure immutability even if the Builder is reused.
@@ -55,6 +60,10 @@ private Interface(final Builder builder) {
55
60
keyPair = Objects .requireNonNull (builder .keyPair , "Interfaces must have a private key" );
56
61
listenPort = builder .listenPort ;
57
62
mtu = builder .mtu ;
63
+ preUp = Collections .unmodifiableList (new ArrayList <>(builder .preUp ));
64
+ postUp = Collections .unmodifiableList (new ArrayList <>(builder .postUp ));
65
+ preDown = Collections .unmodifiableList (new ArrayList <>(builder .preDown ));
66
+ postDown = Collections .unmodifiableList (new ArrayList <>(builder .postDown ));
58
67
}
59
68
60
69
/**
@@ -93,6 +102,18 @@ public static Interface parse(final Iterable<? extends CharSequence> lines)
93
102
case "privatekey" :
94
103
builder .parsePrivateKey (attribute .getValue ());
95
104
break ;
105
+ case "preup" :
106
+ builder .parsePreUp (attribute .getValue ());
107
+ break ;
108
+ case "postup" :
109
+ builder .parsePostUp (attribute .getValue ());
110
+ break ;
111
+ case "predown" :
112
+ builder .parsePreDown (attribute .getValue ());
113
+ break ;
114
+ case "postdown" :
115
+ builder .parsePostDown (attribute .getValue ());
116
+ break ;
96
117
default :
97
118
throw new BadConfigException (Section .INTERFACE , Location .TOP_LEVEL ,
98
119
Reason .UNKNOWN_ATTRIBUTE , attribute .getKey ());
@@ -112,7 +133,12 @@ public boolean equals(final Object obj) {
112
133
&& includedApplications .equals (other .includedApplications )
113
134
&& keyPair .equals (other .keyPair )
114
135
&& listenPort .equals (other .listenPort )
115
- && mtu .equals (other .mtu );
136
+ && mtu .equals (other .mtu )
137
+ && preUp .equals (other .preUp )
138
+ && postUp .equals (other .postUp )
139
+ && preDown .equals (other .preDown )
140
+ && postDown .equals (other .postDown );
141
+
116
142
}
117
143
118
144
/**
@@ -182,6 +208,22 @@ public Optional<Integer> getMtu() {
182
208
return mtu ;
183
209
}
184
210
211
+ public List <String > getPreUp () {
212
+ return preUp ;
213
+ }
214
+
215
+ public List <String > getPostUp () {
216
+ return postUp ;
217
+ }
218
+
219
+ public List <String > getPreDown () {
220
+ return preDown ;
221
+ }
222
+
223
+ public List <String > getPostDown () {
224
+ return postDown ;
225
+ }
226
+
185
227
@ Override
186
228
public int hashCode () {
187
229
int hash = 1 ;
@@ -192,6 +234,10 @@ public int hashCode() {
192
234
hash = 31 * hash + keyPair .hashCode ();
193
235
hash = 31 * hash + listenPort .hashCode ();
194
236
hash = 31 * hash + mtu .hashCode ();
237
+ hash = 31 * hash + preUp .hashCode ();
238
+ hash = 31 * hash + postUp .hashCode ();
239
+ hash = 31 * hash + preDown .hashCode ();
240
+ hash = 31 * hash + postDown .hashCode ();
195
241
return hash ;
196
242
}
197
243
@@ -231,6 +277,14 @@ public String toWgQuickString() {
231
277
listenPort .ifPresent (lp -> sb .append ("ListenPort = " ).append (lp ).append ('\n' ));
232
278
mtu .ifPresent (m -> sb .append ("MTU = " ).append (m ).append ('\n' ));
233
279
sb .append ("PrivateKey = " ).append (keyPair .getPrivateKey ().toBase64 ()).append ('\n' );
280
+ for (final String script : preUp )
281
+ sb .append ("PreUp = " ).append (script ).append ('\n' );
282
+ for (final String script : postUp )
283
+ sb .append ("PostUp = " ).append (script ).append ('\n' );
284
+ for (final String script : preDown )
285
+ sb .append ("PreDown = " ).append (script ).append ('\n' );
286
+ for (final String script : postDown )
287
+ sb .append ("PostDown = " ).append (script ).append ('\n' );
234
288
return sb .toString ();
235
289
}
236
290
@@ -263,6 +317,14 @@ public static final class Builder {
263
317
private Optional <Integer > listenPort = Optional .empty ();
264
318
// Defaults to not present.
265
319
private Optional <Integer > mtu = Optional .empty ();
320
+ // Defaults to empty list
321
+ private List <String > preUp = new ArrayList <>();
322
+ // Defaults to empty list
323
+ private List <String > postUp = new ArrayList <>();
324
+ // Defaults to empty list
325
+ private List <String > preDown = new ArrayList <>();
326
+ // Defaults to empty list
327
+ private List <String > postDown = new ArrayList <>();
266
328
267
329
public Builder addAddress (final InetNetwork address ) {
268
330
addresses .add (address );
@@ -366,6 +428,26 @@ public Builder parsePrivateKey(final String privateKey) throws BadConfigExceptio
366
428
}
367
429
}
368
430
431
+ public Builder parsePreUp (final String script ) throws BadConfigException {
432
+ preUp .add (script );
433
+ return this ;
434
+ }
435
+
436
+ public Builder parsePostUp (final String script ) throws BadConfigException {
437
+ postUp .add (script );
438
+ return this ;
439
+ }
440
+
441
+ public Builder parsePreDown (final String script ) throws BadConfigException {
442
+ preDown .add (script );
443
+ return this ;
444
+ }
445
+
446
+ public Builder parsePostDown (final String script ) throws BadConfigException {
447
+ postDown .add (script );
448
+ return this ;
449
+ }
450
+
369
451
public Builder setKeyPair (final KeyPair keyPair ) {
370
452
this .keyPair = keyPair ;
371
453
return this ;
0 commit comments