antiflood handleClientRequest

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

adifong
Posts: 20
Joined: 10 Aug 2016, 06:50

antiflood handleClientRequest

Postby adifong » 11 Jun 2018, 06:19

Hi,

i have problem with handleClientRequest, is there any solution to block spam or block if user already call handleclientrequest with the same request id, i just only need to run 1 time?

i already try to block with get the latest time and request id, but now it call at the same time. so i cannot block it.

thank you.

Code: Select all

public void handleClientRequest(User player, ISFSObject params) {
   String requestId = params.getUtfString(SFSExtension.MULTIHANDLER_REQUEST_ID);
      
      trace("-------action : "+ requestId);
      long timeNow = System.currentTimeMillis();
      trace("-------enddd "+ timeNow );
      
      String lastRequest = "";
      Long lastTimeRequest = (long) 0;
      try {
         lastRequest = (String) player.getProperty("lastRequest");
         lastTimeRequest = (Long) player.getProperty("lastTimeRequest");
         
         if(lastRequest.equals("act") ){
            trace("lastRequest : "+lastRequest);
            trace("lastTimeRequest : "+lastTimeRequest + " Now : "+  timeNow);
            if(timeNow - lastTimeRequest < 200){
               trace("action cancel!");
               return;
            }
            
         }
         
         player.setProperty("lastRequest",requestId);
         player.setProperty("lastTimeRequest",timeNow);
         
      } catch (Exception e) {
         player.setProperty("lastRequest",requestId);
         player.setProperty("lastTimeRequest",timeNow);
         trace("-------action error : "+ e);
         // TODO: handle exception
      }
      
      switch(requestId){
      .
      .
      .
      


this is my log
11 Jun 2018 | 12:14:07,631 | INFO | SFSWorker:Ext:2 | Extensions | | {txh_game}: -------action : act
11 Jun 2018 | 12:14:07,631 | INFO | SFSWorker:Ext:3 | Extensions | | {txh_game}: -------action : act
11 Jun 2018 | 12:14:07,633 | INFO | SFSWorker:Ext:2 | Extensions | | {txh_game}: -------enddd 1528694047633
11 Jun 2018 | 12:14:07,633 | INFO | SFSWorker:Ext:3 | Extensions | | {txh_game}: -------enddd 1528694047633
11 Jun 2018 | 12:14:07,633 | INFO | SFSWorker:Ext:3 | Extensions | | {txh_game}: lastRequest : act
11 Jun 2018 | 12:14:07,633 | INFO | SFSWorker:Ext:2 | Extensions | | {txh_game}: lastRequest : act
11 Jun 2018 | 12:14:07,633 | INFO | SFSWorker:Ext:3 | Extensions | | {txh_game}: lastTimeRequest : 1528694008216 Now : 1528694047633
11 Jun 2018 | 12:14:07,633 | INFO | SFSWorker:Ext:2 | Extensions | | {txh_game}: lastTimeRequest : 1528694008216 Now : 1528694047633
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: antiflood handleClientRequest

Postby Lapo » 11 Jun 2018, 08:12

Hi,
if the action can be called only one time by the user you can attach a counter for that action to the User object to verify how many times it was invoked.

You can synchronize the call on the User object itself so that concurrent calls handled by different threads will not cause multiple invocations. To give you an example:

Code: Select all

public class MyHandler extends BaseClientRequestHandler
{
   public void handleClientRequest(User sender, ISFSObject params)
   {
      synchronized (sender)
      {
         if (sender.containsProperty("counter"))
         {
            sender.setProperty("counter", 0);
         }
         
         int counter = sender.getProperty("counter");
         
         if (counter > 0)
         {
            // Action refused
            return;
         }
         
         else
         {
            // Perform action...
            counter++;
            sender.setProperty("counter", counter);
         }
      }
   }
}


The code checks if the User has a counter active, if not it initializes one set to zero. Then based on the counter's value it decides whether or not the request should be executed or not.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
adifong
Posts: 20
Joined: 10 Aug 2016, 06:50

Re: antiflood handleClientRequest

Postby adifong » 12 Jun 2018, 01:12

Hi lapo,

ok i will try this code..

thank you

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 53 guests