|
1 | 1 | package fr.sandro642.github; |
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
3 | 5 | import fr.sandro642.github.enums.LangType; |
4 | 6 | import fr.sandro642.github.enums.lang.CategoriesType; |
5 | 7 | import fr.sandro642.github.hook.HookManager; |
6 | 8 | import fr.sandro642.github.hook.LangSupport; |
7 | 9 | import fr.sandro642.github.hook.MCSupport; |
| 10 | +import fr.sandro642.github.log.Logger; |
| 11 | +import fr.sandro642.github.log.Logs; |
8 | 12 | import fr.sandro642.github.misc.*; |
9 | 13 | import fr.sandro642.github.jobs.JobGetInfos; |
10 | 14 | import fr.sandro642.github.enums.ResourceType; |
11 | 15 | import fr.sandro642.github.spring.Application; |
12 | 16 | import fr.sandro642.github.update.RetrieveLastVersion; |
13 | 17 |
|
| 18 | +import java.net.URI; |
| 19 | +import java.net.http.HttpClient; |
| 20 | +import java.net.http.HttpRequest; |
| 21 | +import java.net.http.HttpResponse; |
14 | 22 | import java.util.*; |
15 | 23 |
|
16 | 24 | /** |
@@ -147,6 +155,53 @@ public void webServices(int port, String nameDashboard) { |
147 | 155 | SpringApp().startApplication().subscribe(); |
148 | 156 | } |
149 | 157 |
|
| 158 | + /** |
| 159 | + * Implement WAN connection to retrieve the port from the server. |
| 160 | + * @param urlServ the server URL |
| 161 | + * @param codeWan the WAN code |
| 162 | + * @return the port as a String |
| 163 | + */ |
| 164 | + public void wanImplement(String urlServ) { |
| 165 | + try { |
| 166 | + String codeWan = ""; |
| 167 | + |
| 168 | + HttpClient client1 = HttpClient.newHttpClient(); |
| 169 | + HttpRequest request1 = HttpRequest.newBuilder() |
| 170 | + .uri(URI.create(urlServ)) // remplacer par l'URL qui |
| 171 | + .build(); |
| 172 | + |
| 173 | + HttpResponse<String> response1 = client1.send(request1, HttpResponse.BodyHandlers.ofString()); |
| 174 | + |
| 175 | + Gson gson1 = new Gson(); |
| 176 | + JsonObject root1 = gson1.fromJson(response1.body(), JsonObject.class); |
| 177 | + |
| 178 | + if (root1 != null) { |
| 179 | + codeWan = root1.get("code").getAsString(); |
| 180 | + } |
| 181 | + |
| 182 | + HttpClient client = HttpClient.newHttpClient(); |
| 183 | + HttpRequest request = HttpRequest.newBuilder() |
| 184 | + .uri(URI.create(urlServ + "/connect/" + codeWan)) // remplacer par l'URL qui renvoie le JSON |
| 185 | + .build(); |
| 186 | + |
| 187 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 188 | + |
| 189 | + Gson gson = new Gson(); |
| 190 | + JsonObject root = gson.fromJson(response.body(), JsonObject.class); |
| 191 | + |
| 192 | + if (root != null && root.has("user")) { |
| 193 | + JsonObject user = root.getAsJsonObject("user"); |
| 194 | + if (user != null && user.has("port") && !user.get("port").isJsonNull()) { |
| 195 | + StoreAndRetrieve().put(StoreAndRetrieve().DYNAMIC_PORT, user.get("port").getAsString()); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + SpringApp().startApplication().subscribe(); |
| 200 | + } catch (Exception e) { |
| 201 | + e.printStackTrace(); |
| 202 | + } |
| 203 | + } |
| 204 | + |
150 | 205 | /** |
151 | 206 | * Check if the ConnectLib is initialized. |
152 | 207 | * @return true if initialized, false otherwise |
|
0 commit comments