From eb3c7b8899290c573d7d72d985015d88d9a6a18c Mon Sep 17 00:00:00 2001 From: Voomra Date: Fri, 18 Jul 2025 01:48:18 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + src/main/java/ru/di9/ihc/IhcClient.java | 46 +- src/test/java/ru/di9/ihc/GetDomainsTest.java | 59 ++ src/test/resources/ihc-dns.html | 850 +++++++++++++++++++ 4 files changed, 954 insertions(+), 3 deletions(-) create mode 100644 src/test/java/ru/di9/ihc/GetDomainsTest.java create mode 100644 src/test/resources/ihc-dns.html diff --git a/build.gradle b/build.gradle index d77dcf6..cf21acc 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,9 @@ ext { dependencies { implementation("org.apache.httpcomponents.client5:httpclient5:5.5") + implementation("org.apache.commons:commons-lang3:3.18.0") implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") + implementation("org.jsoup:jsoup:1.21.1") testImplementation(platform("org.junit:junit-bom:$junitVersion")) testImplementation("org.junit.jupiter:junit-jupiter") diff --git a/src/main/java/ru/di9/ihc/IhcClient.java b/src/main/java/ru/di9/ihc/IhcClient.java index b754594..a372cd2 100644 --- a/src/main/java/ru/di9/ihc/IhcClient.java +++ b/src/main/java/ru/di9/ihc/IhcClient.java @@ -1,14 +1,23 @@ package ru.di9.ihc; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.entity.UrlEncodedFormEntity; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.core5.http.message.BasicNameValuePair; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; import java.io.IOException; import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class IhcClient { @@ -16,6 +25,8 @@ public class IhcClient { private final CloseableHttpClient httpClient; private final ObjectMapper mapper; + private boolean isAuth = false; + public IhcClient(String baseUrl) { this.baseUrl = baseUrl; this.httpClient = HttpClientBuilder.create().build(); @@ -34,9 +45,8 @@ public class IhcClient { new BasicNameValuePair("ihccaptcha", "") ))); - boolean authResult; try { - authResult = httpClient.execute(httpPost, resp -> { + isAuth = httpClient.execute(httpPost, resp -> { if (resp.getCode() != 200) { return false; } @@ -48,6 +58,36 @@ public class IhcClient { throw new RuntimeException(e); } - return authResult; + return isAuth; + } + + public List> getDomains() { + if (!isAuth) { + throw new RuntimeException("IS NOT AUTH"); + } + + HttpGet httpGet = new HttpGet(URI.create("%s/dnsZone/list".formatted(baseUrl))); + httpGet.setHeader("Accept", "text/html"); + + try { + return httpClient.execute(httpGet, resp -> { + if (resp.getCode() != 200) { + return Collections.emptyList(); + } + + List> list = new ArrayList<>(); + Document document = Jsoup.parse(resp.getEntity().getContent(), StandardCharsets.UTF_8.name(), baseUrl); + Elements elements = document.select("li[class='zoneList__zone'] a"); + for (Element element : elements) { + Integer id = Integer.valueOf(element.attr("href").substring(1).split("/")[2]); + String domain = element.text(); + list.add(Pair.of(id, domain)); + } + + return list; + }); + } catch (IOException e) { + throw new RuntimeException(e); + } } } diff --git a/src/test/java/ru/di9/ihc/GetDomainsTest.java b/src/test/java/ru/di9/ihc/GetDomainsTest.java new file mode 100644 index 0000000..2609104 --- /dev/null +++ b/src/test/java/ru/di9/ihc/GetDomainsTest.java @@ -0,0 +1,59 @@ +package ru.di9.ihc; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.RandomUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class GetDomainsTest { + static int port; + static WireMockServer wireMockServer; + + @BeforeAll + static void beforeAll() { + port = RandomUtils.nextInt(9000, 9999); + wireMockServer = new WireMockServer(port); + wireMockServer.start(); + } + + @AfterAll + static void afterAll() { + wireMockServer.stop(); + } + + @AfterEach + void after() { + wireMockServer.resetAll(); + } + + @Test + void test() throws IOException { + wireMockServer.stubFor(post("/j_spring_security_check?ajax=true") + .willReturn(WireMock.okJson(""" + {"redirect":{"url":"/"},"alert":{"type":"none","message":""}}"""))); + wireMockServer.stubFor(get("/dnsZone/list") + .willReturn(WireMock.ok(IOUtils.resourceToString("/ihc-dns.html", StandardCharsets.UTF_8)) + .withHeader("Content-Type", "text/html"))); + + var ihc = new IhcClient("http://localhost:%d".formatted(port)); + ihc.auth("user1", "passwd1"); + List> domains = ihc.getDomains(); + + assertEquals(2, domains.size()); + assertEquals(Pair.of(111111, "example-1.ru"), domains.get(0)); + assertEquals(Pair.of(222222, "example-2.ru"), domains.get(1)); + } +} diff --git a/src/test/resources/ihc-dns.html b/src/test/resources/ihc-dns.html new file mode 100644 index 0000000..961e337 --- /dev/null +++ b/src/test/resources/ihc-dns.html @@ -0,0 +1,850 @@ + + + + + + + + + + + + + + + + Интернет Хостинг Центр | Список DNS зон + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + +
 
+ + +
+
Бонусы    + +
+ +
+ 0 руб +
+ + + + +
+ + +
+
Баланс
+ +
+ 0 руб +
+ + + + + + + + + + + +
+
+ + + + + + + + + +
 
+ + + +
+
+ +
+
+ XXXXXXXXXXXX    +
+ +
+ +
+ + +  Выйти  + +   + +
+ + + + +
+
+ +
+ +
+ + + + +
+
+
+
+
+ + + + + + + + + + + + + + + + + + +
+

Список DNS зон + + +

+ +
+ Данный раздел предназначен для управления DNS записями доменов на NS: ns1.ihc.ru, ns2.ihc.ru +
+ + Добавить | + Whois + + +
+ +
+

+ + + +

+
+ +
+ С выбранными: Удалить Смена IP +
+ +
+ +
+ +
+
    +
      +
      + + +
    • + +
    • + +
    • + +
    • + + +
      + +
      + + +
      +
      + + +
      + +
      + + + + + +

      Ваши заказы

      + +

      + Наши услуги +

      + + + + + + Ответы на вопросы + + + + + + Задать вопрос + + + + Запросы + 0 + +
      + + +
      + +
      + +
      +
      + + + + + + + + + + + + + + + + + + +
      +
      + + + +
      +
      + + + + + + + + + + + + + + + + + + + +