2424import java .util .ArrayList ;
2525import java .util .Base64 ;
2626import java .util .List ;
27- import java .util .concurrent .CompletableFuture ;
2827import java .util .concurrent .TimeUnit ;
2928
3029import de .marhali .json5 .Json5Array ;
@@ -54,8 +53,8 @@ public OriginBlacklist(final IOriginBlacklistPlugin plugin) {
5453 this .plugin = plugin ;
5554 this .config = new OriginBlacklistConfig (plugin );
5655 plugin .scheduleRepeat (() -> {
57- this .checkForUpdate ();
58- }, 60 , TimeUnit .MINUTES );
56+ this .checkForUpdates ();
57+ }, this . config . getInteger ( "update_checker.check_timer" ) , TimeUnit .SECONDS );
5958 }
6059
6160 public final void init () {
@@ -109,11 +108,11 @@ public final void handleMOTD(final OriginBlacklistMOTDEvent event) {
109108 }
110109
111110 public final boolean isDebugEnabled () {
112- return this .config .get ("debug" ). getAsBoolean ( );
111+ return this .config .getBoolean ("debug" );
113112 }
114113
115114 public final boolean isMetricsEnabled () {
116- return this .config .get ("bStats" ). getAsBoolean ( );
115+ return this .config .getBoolean ("bStats" );
117116 }
118117
119118 public final OriginBlacklistConfig getConfig () {
@@ -135,65 +134,141 @@ public final void setEaglerMOTD(final Component comp, final OriginBlacklistMOTDE
135134 conn .disconnect ();
136135 }
137136
137+ public final void checkForUpdates (Runnable action1 , Runnable action2 ) {
138+ if (this .config .getBoolean ("update_checker.enabled" )) {
139+ this .plugin .runAsync (() -> {
140+ this .updateURL = UpdateChecker .checkForUpdates (PLUGIN_REPO , this .plugin .getPluginVersion (),
141+ this .config .getBoolean ("update_checker.allow_snapshots" ));
142+ if (isNonNull ((this .updateURL ))) {
143+ action1 .run ();
144+ return ;
145+ }
146+ action2 .run ();
147+ });
148+ } else {
149+ action2 .run ();
150+ }
151+ }
152+
153+ public final void updatePlugin (Runnable action1 , Runnable action2 ) {
154+ try {
155+ final URL url = new URL (this .updateURL );
156+ final Path jar = this .plugin .getPluginJarPath ();
157+ final Path bak = jar .resolveSibling (jar .getFileName ().toString () + ".bak" );
158+ final Path upd = jar .resolveSibling (Paths .get (URLDecoder .decode (url .getPath (), StandardCharsets .UTF_8 )).getFileName ());
159+
160+ try {
161+ Files .copy (jar , bak , StandardCopyOption .REPLACE_EXISTING );
162+ } catch (final Throwable t ) {
163+ t .printStackTrace ();
164+ }
165+
166+ try {
167+ final HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
168+ conn .setRequestMethod ("GET" );
169+ conn .setConnectTimeout (15000 );
170+ conn .setReadTimeout (15000 );
171+ conn .setRequestProperty ("User-Agent" , OriginBlacklist .getUserAgent ());
172+ conn .connect ();
173+ try (final InputStream in = conn .getInputStream ()) {
174+ Files .copy (in , upd , StandardCopyOption .REPLACE_EXISTING );
175+ } finally {
176+ conn .disconnect ();
177+ }
178+ Files .delete (jar );
179+ Files .delete (bak );
180+ action1 .run ();
181+ return ;
182+ } catch (final Throwable t ) {
183+ t .printStackTrace ();
184+ Files .move (bak , jar , StandardCopyOption .REPLACE_EXISTING );
185+ }
186+ } catch (final Throwable t ) {
187+ t .printStackTrace ();
188+ }
189+ action2 .run ();
190+ }
191+
192+ public final void updatePlugin () {
193+ this .updatePlugin (() -> {}, () -> {});
194+ }
195+
138196 private final EnumBlacklistType testBlacklist (final OPlayer player ) {
139197 final String name = player .getName ();
140198 final String addr = player .getAddr ();
141199 final String origin = player .getOrigin ();
142200 final String brand = player .getBrand ();
143201
202+ final boolean whitelist = this .config .getBoolean ("blacklist_to_whitelist" );
203+ EnumBlacklistType type = EnumBlacklistType .NONE ;
204+
144205 if (isNonNull (origin )) {
145- for (final Json5Element element : this .config .get ("blacklist.origins" ).getAsJson5Array ()) {
206+ if (whitelist && !type .isBlacklisted ()) type = EnumBlacklistType .ORIGIN ;
207+ for (final Json5Element element : this .config .getArray ("blacklist.origins" ).getAsJson5Array ()) {
146208 if (origin .matches (element .getAsString ())) {
147- return EnumBlacklistType .ORIGIN ;
209+ if (whitelist ) type = EnumBlacklistType .NONE ;
210+ else if (!type .isBlacklisted ()) type = EnumBlacklistType .ORIGIN ;
211+ break ;
148212 }
149213 }
214+ } else if (this .config .getBoolean ("block_undefined_origin" )) {
215+ return whitelist ? EnumBlacklistType .NONE : EnumBlacklistType .ORIGIN ;
150216 }
151217
152218 if (isNonNull (brand )) {
153- for (final Json5Element element : this .config .get ("blacklist.brands" ).getAsJson5Array ()) {
219+ if (whitelist && !type .isBlacklisted ()) type = EnumBlacklistType .BRAND ;
220+ for (final Json5Element element : this .config .getArray ("blacklist.brands" )) {
154221 if (brand .matches (element .getAsString ())) {
155- return EnumBlacklistType .BRAND ;
222+ if (whitelist ) type = EnumBlacklistType .NONE ;
223+ else if (!type .isBlacklisted ()) type = EnumBlacklistType .BRAND ;
224+ break ;
156225 }
157226 }
158227 }
159228
160229 if (isNonNull (name )) {
161- for (final Json5Element element : this .config .get ("blacklist.player_names" ).getAsJson5Array ()) {
230+ if (whitelist && !type .isBlacklisted ()) type = EnumBlacklistType .NAME ;
231+ for (final Json5Element element : this .config .getArray ("blacklist.player_names" )) {
162232 this .plugin .log (EnumLogLevel .DEBUG , element .getAsString ());
163233 if (name .matches (element .getAsString ())) {
164- return EnumBlacklistType .NAME ;
234+ if (whitelist ) type = EnumBlacklistType .NONE ;
235+ else if (!type .isBlacklisted ()) type = EnumBlacklistType .NAME ;
236+ break ;
165237 }
166238 }
167239 }
168240
169241 if (isNonNull (addr )) {
170- for (final Json5Element element : this .config .get ("blacklist.ip_addresses" ).getAsJson5Array ()) {
242+ if (whitelist && !type .isBlacklisted ()) type = EnumBlacklistType .ADDR ;
243+ for (final Json5Element element : this .config .getArray ("blacklist.ip_addresses" )) {
171244 try {
172245 if ((new IPAddressString (element .getAsString ()).toAddress ())
173246 .contains ((new IPAddressString (addr )).toAddress ())) {
174- return EnumBlacklistType .ADDR ;
247+ if (whitelist ) type = EnumBlacklistType .NONE ;
248+ else if (!type .isBlacklisted ()) type = EnumBlacklistType .ADDR ;
249+ break ;
175250 }
176251 } catch (final AddressStringException exception ) {
177- // exception.printStackTrace();
252+ if ( this . isDebugEnabled ()) exception .printStackTrace ();
178253 }
179254 }
180255 }
181256
182- return EnumBlacklistType . NONE ;
257+ return type ;
183258 }
184259
185260 private final Component getBlacklistedComponent (final String type , final String id , final String blockType ,
186261 final String blockTypeAlt , final String notAllowed , final String notAllowedAlt , final String blockValue ,
187262 final String action ) {
188- final Json5Array arr = this .config .get ("messages." + type ). getAsJson5Array ( );
263+ final Json5Array arr = this .config .getArray ("messages." + type );
189264 final StringBuilder sb = new StringBuilder ();
190265 for (int i = 0 ; i < arr .size (); i ++) {
191266 if (i > 0 )
192267 sb .append ("\n " );
193268 sb .append (arr .get (i ).getAsString ());
194269 }
195270 final String str = sb .toString ()
196- .replaceAll ("%action%" , this .config .get ("messages.actions." + action ). getAsString ( ))
271+ .replaceAll ("%action%" , this .config .getString ("messages.actions." + action ))
197272 .replaceAll ("%block_type%" , blockType )
198273 .replaceAll ("%block_type%" , blockType )
199274 .replaceAll ("%not_allowed%" , notAllowed )
@@ -203,8 +278,7 @@ private final Component getBlacklistedComponent(final String type, final String
203278 }
204279
205280 private final void sendWebhooks (final OriginBlacklistLoginEvent event , final EnumBlacklistType type ) {
206- Json5Element element = this .config .get ("discord.enabled" );
207- if (element .getAsBoolean ()) {
281+ if (this .config .getBoolean ("discord.enabled" )) {
208282 final OPlayer player = event .getPlayer ();
209283 final EnumConnectionType connType = event .getConnectionType ();
210284 final String userAgent ;
@@ -252,7 +326,7 @@ private final void sendWebhooks(final OriginBlacklistLoginEvent event, final Enu
252326 player .getName ().replaceAll ("_" , "\\ _" ),
253327 player .getOrigin (),
254328 player .getBrand (),
255- this .config .get ("discord.send_ips" ). getAsBoolean ( ) ? player .getAddr () : "*\\ *CENSORED\\ **" ,
329+ this .config .getBoolean ("discord.send_ips" ) ? player .getAddr () : "*\\ *CENSORED\\ **" ,
256330 player .getPVN (),
257331 userAgent ,
258332 player .isRewind () ? "YES" : "NO" ,
@@ -261,10 +335,10 @@ private final void sendWebhooks(final OriginBlacklistLoginEvent event, final Enu
261335 PLUGIN_REPO ,
262336 PLUGIN_REPO
263337 ).getBytes ();
264- element = this .config .get ("discord.webhook_urls" );
338+ final Json5Element element = this .config .get ("discord.webhook_urls" );
265339 if (element instanceof Json5Array ) {
266340 for (final Json5Element _element : element .getAsJson5Array ()) {
267- CompletableFuture .runAsync (() -> {
341+ this . plugin .runAsync (() -> {
268342 try {
269343 final URL url = new URL (_element .getAsString ());
270344 final HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
@@ -293,56 +367,14 @@ private final void sendWebhooks(final OriginBlacklistLoginEvent event, final Enu
293367 }
294368 }
295369
296- private final void checkForUpdate () {
297- if (this .config .get ("update_checker.enabled" ).getAsBoolean ()) {
298- CompletableFuture .runAsync (() -> {
299- this .updateURL = UpdateChecker .checkForUpdate (PLUGIN_REPO , this .plugin .getPluginVersion (),
300- this .config .get ("update_checker.allow_snapshots" ).getAsBoolean ());
301- if (isNonNull ((this .updateURL ))) {
302- if (!this .config .get ("update_checker.auto_update" ).getAsBoolean ()) {
303- this .plugin .log (EnumLogLevel .INFO , "An update is available! Download it at " + this .updateURL );
304- } else {
305- this .updatePlugin ();
306- }
307- }
308- });
309- }
310- }
311-
312- private final void updatePlugin () {
313- try {
314- final URL url = new URL (this .updateURL );
315- final Path jar = this .plugin .getPluginJarPath ();
316- final Path bak = jar .resolveSibling (jar .getFileName ().toString () + ".bak" );
317- final Path upd = jar .resolveSibling (Paths .get (URLDecoder .decode (url .getPath (), StandardCharsets .UTF_8 )).getFileName ());
318-
319- try {
320- Files .copy (jar , bak , StandardCopyOption .REPLACE_EXISTING );
321- } catch (final Throwable t ) {
322- t .printStackTrace ();
323- }
324-
325- try {
326- final HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
327- conn .setRequestMethod ("GET" );
328- conn .setConnectTimeout (15000 );
329- conn .setReadTimeout (15000 );
330- conn .setRequestProperty ("User-Agent" , OriginBlacklist .getUserAgent ());
331- conn .connect ();
332- try (final InputStream in = conn .getInputStream ()) {
333- Files .copy (in , upd , StandardCopyOption .REPLACE_EXISTING );
334- } finally {
335- conn .disconnect ();
336- }
337- Files .delete (jar );
338- Files .delete (bak );
339- } catch (final Throwable t ) {
340- t .printStackTrace ();
341- Files .move (bak , jar , StandardCopyOption .REPLACE_EXISTING );
370+ private final void checkForUpdates () {
371+ this .checkForUpdates (() -> {
372+ if (!this .config .getBoolean ("update_checker.auto_update" )) {
373+ this .plugin .log (EnumLogLevel .INFO , "An update is available! Download it at " + this .updateURL );
374+ } else {
375+ this .updatePlugin ();
342376 }
343- } catch (final Throwable t ) {
344- t .printStackTrace ();
345- }
377+ }, () -> {});
346378 }
347379
348380 public static final String getComponentString (final Component comp ) {
0 commit comments