fix: Update domains to streamtp10.com and implement robust DNS fallback
- Update all channel URLs and event endpoint to streamtp10.com - Create NetworkUtils for centralized OkHttpClient configuration - Implement DNS fallback: Google (Primary) -> AdGuard (Secondary) -> System (Tertiary) - Migrate EventRepository to use NetworkUtils client instead of HttpURLConnection - Fix Referer header in StreamUrlResolver
This commit is contained in:
@@ -7,12 +7,7 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
@@ -24,6 +19,9 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class EventRepository {
|
||||
|
||||
private static final String PREFS_NAME = "events_cache";
|
||||
@@ -76,98 +74,38 @@ public class EventRepository {
|
||||
}
|
||||
|
||||
private String downloadJson(Context context) throws IOException {
|
||||
return downloadFromUrl(EVENTS_URL);
|
||||
}
|
||||
Request request = new Request.Builder()
|
||||
.url(EVENTS_URL)
|
||||
.header("User-Agent", NetworkUtils.getUserAgent())
|
||||
.header("Accept", "application/json")
|
||||
.build();
|
||||
|
||||
private String downloadFromUrl(String urlString) throws IOException {
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(15000);
|
||||
connection.setReadTimeout(15000);
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
||||
|
||||
// Habilitar seguimiento de redirecciones automáticamente
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
|
||||
String currentUrl = urlString;
|
||||
int redirectCount = 0;
|
||||
final int MAX_REDIRECTS = 5;
|
||||
|
||||
try {
|
||||
int responseCode = connection.getResponseCode();
|
||||
|
||||
// Seguir redirecciones manualmente si es necesario
|
||||
while (isRedirect(responseCode) && redirectCount < MAX_REDIRECTS) {
|
||||
redirectCount++;
|
||||
String newUrl = connection.getHeaderField("Location");
|
||||
|
||||
if (newUrl == null) {
|
||||
throw new IOException("Redirección sin cabecera Location");
|
||||
}
|
||||
|
||||
// Manejar URLs relativas
|
||||
if (newUrl.startsWith("/")) {
|
||||
newUrl = url.getProtocol() + "://" + url.getHost() + newUrl;
|
||||
} else if (!newUrl.startsWith("http")) {
|
||||
newUrl = url.getProtocol() + "://" + url.getHost() +
|
||||
(url.getPort() > 0 ? ":" + url.getPort() : "") + "/" + newUrl;
|
||||
}
|
||||
|
||||
currentUrl = newUrl;
|
||||
url = new URL(currentUrl);
|
||||
connection.disconnect();
|
||||
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(15000);
|
||||
connection.setReadTimeout(15000);
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
connection.setRequestProperty("User-Agent", "StreamPlayer/1.0");
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
|
||||
responseCode = connection.getResponseCode();
|
||||
try (Response response = NetworkUtils.getClient().newCall(request).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("Error HTTP " + response.code() + ": " + response.message());
|
||||
}
|
||||
|
||||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||
throw new IOException("Error HTTP " + responseCode + ": " + connection.getResponseMessage());
|
||||
if (response.body() == null) {
|
||||
throw new IOException("Respuesta vacía del servidor");
|
||||
}
|
||||
|
||||
String contentType = connection.getContentType();
|
||||
String contentType = response.header("Content-Type");
|
||||
// Permitir json o text/plain (Raw de Gitea a veces es text/plain)
|
||||
if (contentType != null && !contentType.contains("json") && !contentType.contains("text/plain")) {
|
||||
throw new IOException("El servidor devolvió " + contentType + " en lugar de JSON. Verifica que la URL sea correcta.");
|
||||
// Aceptamos text/plain también por flexibilidad
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
String response = builder.toString();
|
||||
String responseBody = response.body().string();
|
||||
|
||||
// Validar que no sea HTML
|
||||
if (response.trim().startsWith("<!") || response.trim().startsWith("<html")) {
|
||||
throw new IOException("El servidor devolvió HTML en lugar de JSON. La URL del endpoint puede estar incorrecta o el servidor tiene problemas.");
|
||||
}
|
||||
|
||||
return response;
|
||||
// Validar que no sea HTML
|
||||
if (responseBody.trim().startsWith("<!") || responseBody.trim().startsWith("<html")) {
|
||||
throw new IOException("El servidor devolvió HTML en lugar de JSON. La URL del endpoint puede estar incorrecta o el servidor tiene problemas.");
|
||||
}
|
||||
} finally {
|
||||
connection.disconnect();
|
||||
|
||||
return responseBody;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRedirect(int statusCode) {
|
||||
return statusCode == HttpURLConnection.HTTP_MOVED_PERM ||
|
||||
statusCode == HttpURLConnection.HTTP_MOVED_TEMP ||
|
||||
statusCode == HttpURLConnection.HTTP_SEE_OTHER ||
|
||||
statusCode == 307 || // Temporary Redirect
|
||||
statusCode == 308; // Permanent Redirect
|
||||
}
|
||||
|
||||
private List<EventItem> parseEvents(String json) throws JSONException {
|
||||
if (json == null || json.trim().isEmpty()) {
|
||||
throw new JSONException("La respuesta está vacía");
|
||||
@@ -213,7 +151,9 @@ public class EventRepository {
|
||||
if (link == null) {
|
||||
return "";
|
||||
}
|
||||
String updated = link.replace("streamtpmedia.com", "streamtpcloud.com");
|
||||
// Actualizado a streamtp10.com
|
||||
String updated = link.replace("streamtpmedia.com", "streamtp10.com")
|
||||
.replace("streamtpcloud.com", "streamtp10.com");
|
||||
return updated.replace("global1.php", "global2.php");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user