• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. JAVA
  6. Anti-DDoS Java SDK Demo

Anti-DDoS Java SDK Demo

Querying Optional Anti-DDoS Defense Policies

You can query optional Anti-DDoS defense policies. Based on your service, you can select a policy for Anti-DDoS traffic cleaning.

Code example:

public void listConfigs()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService=antiDDoSServices.antiddos();
        AntiDDoSConfig configs = antiDDoSService.listConfigs();
        LOGGER.info("{}", configs);
    }

Enabling Anti-DDoS

You can enable Anti-DDoS traffic cleaning defense. Successfully invoking this API only means that the service node has received the enabling request. You need to use the task querying API to check the task execution status.

Code example:

public void createAntiDDoS() throws InterruptedException
    {
        AntiDDoS entity = AntiDDoS.builder()
            .enableL7(true)
            .trafficPos(TrafficPos.POS_1)
            .httpRequestPos(HttpRequestPos.POS_1)
            .cleaningAccessPos(CleaningAccessPos.POS_1)
            .appType(AppType.Type_0)
            .build();
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        Task task = antiDDoSService.create(entity, floatingIpId);
        LOGGER.info("{}", task);
        taskId = task.getTaskId();
        waitTaskFinish(taskId);    }
   }
}

Disabling Anti-DDoS

You can disable Anti-DDoS traffic cleaning defense. Successfully invoking this API only means that the service node has received the disabling request. You need to use the task querying API to check the task execution status.

Code example:

public void deleteAntiDDoS()
        throws InterruptedException
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService=antiDDoSServices.antiddos();
        Task task = antiDDoSService.delete(floatingIpId);
        LOGGER.info("{}", task);
        waitTaskFinish(task.getTaskId());
}

Querying Configured Anti-DDoS Defense Policies

You can query the configured Anti-DDoS defense policy of a specified EIP.

Code example:

public void getAntiDDoS()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        AntiDDoS antiDDoS = antiDDoSService.get(floatingIpId);
        LOGGER.info("{}", antiDDoS);
    }

Updating Anti-DDoS Defense Policies

You can update the Anti-DDoS defense policy of a specified EIP. Successfully invoking this API only means that the service node has received the update request. You need to use the task querying API to check the task execution status.

Code example:

public void updateAntiDDoS() throws InterruptedException
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        AntiDDoS entity = antiDDoSService.get(floatingIpId);
        entity = entity.toBuilder().appType(AppType.Type_1).build();
        Task task = osclient.antiDDoS().antiddos().update(entity, floatingIpId);
        LOGGER.info("{}", task);
        waitTaskFinish(task.getTaskId());
    }

Querying Anti-DDoS Tasks

You can query the execution status of a specified Anti-DDoS configuration task.

Code example:

public void getTask()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        Task task = antiDDoSService.getTask(taskId);
        LOGGER.info("{}", task);
}

Querying the List of EIP Defense Statuses

You can query the defense statuses of all EIPs, regardless whether an EIP has been bound to an ECS or not.

Code example:

public void listStatuses()
{
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        AntiDDoSStatus statuses = antiDDoSService.listStatus();
        LOGGER.info("{}", statuses);
}

Querying the Defense Status of a Specified EIP

You can query the defense status of a specified EIP.

Code example:

public void getStatus()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        AntiDDoSStatusDetail status = antiDDoSService.getStatus(floatingIpId);
        LOGGER.info("{}", status);
}

Querying the Traffic of a Specified EIP

You can query the traffic of a specified EIP in the last 24 hours. Traffic is detected at five-minute intervals.

Code example:

public void dailyReport()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        List<? extends AntiDDoSDailyData> dailyReport = antiDDoSService.dailyReport(floatingIpId);
        LOGGER.info("{}", dailyReport);
}

Querying Events of a Specified EIP

You can query events of a specified EIP in the last 24 hours. Events include cleaning and blackhole events, and the query delay is within five minutes.

Code example:

public void listLog()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        List<? extends AntiDDoSLog> logs = antiDDoSService.listLogs(floatingIpId);
        LOGGER.info("{}", logs);
        
        AntiDDoSLogListOptions options = AntiDDoSLogListOptions.create().limit(1).offset(1);
        List<? extends AntiDDoSLog> logs2 = osclient.antiDDoS().antiddos().listLogs(floatingIpId, options);
        LOGGER.info("{}", logs2);
}

Querying Weekly Defense Statistics

You can query weekly defense statistics about all your EIPs, including the number of intercepted DDoS attacks, number of attacks, and ranking by the number of attacks.

Code example:

public void weeklyReport()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSService antiDDoSService =antiDDoSServices.antiddos();
        AntiDDoSWeeklyData weekly = antiDDoSService.weeklyReport();
        LOGGER.info("{}", weekly);
        
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -10);
        AntiDDoSWeeklyData weekly2 = osclient.antiDDoS().antiddos().weeklyReport(cal.getTime());
        LOGGER.info("{}", weekly2);
    }

Querying Alarm Configuration

You can query alarm configuration, such as whether a certain type of alarms will be received, and whether alarms are received through SMS messages or emails.

Code example:

public void queryWarningInfo()
    {
        AntiDDoSServices antiDDoSServices = osclient.antiDDoS();
        AntiDDoSWarn query = antiDDoSServices.warnalert().query();
        LOGGER.info("{}", query);
}