mirror of
https://gitlab.com/vyla-entertainment/TV.git
synced 2026-11-16 13:07:21 -05:00
ported sdk and this works
This commit is contained in:
@@ -24,7 +24,9 @@
|
||||
android:theme="@style/AppTheme.NoActionBarLaunch"
|
||||
android:banner="@drawable/tv_banner"
|
||||
android:launchMode="singleTask"
|
||||
android:exported="true">
|
||||
android:exported="true"
|
||||
android:screenOrientation="landscape"
|
||||
android:hardwareAccelerated="true">
|
||||
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
233
android/app/src/main/java/tv/vyla/app/LocalProxy.java
Normal file
233
android/app/src/main/java/tv/vyla/app/LocalProxy.java
Normal file
@@ -0,0 +1,233 @@
|
||||
package tv.vyla.app;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class LocalProxy extends NanoHTTPD {
|
||||
private static final Pattern STRIP = Pattern.compile("tiktokcdn\\.com|ibyteimg\\.com|seg\\.html|enproxy|letsgocdn\\d+\\.shop", Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern SEGSTRIP = Pattern.compile("seg\\.html|enproxy|tiktokcdn|ibyteimg", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private static class Code implements Response.IStatus {
|
||||
private final int code;
|
||||
|
||||
Code(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequestStatus() {
|
||||
return code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return code + " Status";
|
||||
}
|
||||
}
|
||||
|
||||
public LocalProxy(int port) {
|
||||
super("127.0.0.1", port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response serve(IHTTPSession session) {
|
||||
Response r;
|
||||
if (session.getMethod() == Method.OPTIONS) r = newFixedLengthResponse(new Code(204), "text/plain", "");
|
||||
else if (!"/api/proxy".equals(session.getUri())) r = text(404, "Not found");
|
||||
else r = proxy(session);
|
||||
r.addHeader("Access-Control-Allow-Origin", "*");
|
||||
r.addHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS");
|
||||
r.addHeader("Access-Control-Allow-Headers", "*");
|
||||
r.addHeader("Access-Control-Expose-Headers", "Content-Length, Content-Range, Accept-Ranges");
|
||||
return r;
|
||||
}
|
||||
|
||||
private Response text(int code, String msg) {
|
||||
return newFixedLengthResponse(new Code(code), "text/plain; charset=utf-8", msg == null ? "" : msg);
|
||||
}
|
||||
|
||||
private static boolean nap(long ms) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
return true;
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static String enc(String s) {
|
||||
try {
|
||||
return URLEncoder.encode(s, "UTF-8");
|
||||
} catch (Exception e) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
private static String origin(String u) {
|
||||
HttpUrl h = HttpUrl.parse(u);
|
||||
if (h == null) return "";
|
||||
return h.scheme() + "://" + h.host() + (h.port() == HttpUrl.defaultPort(h.scheme()) ? "" : ":" + h.port());
|
||||
}
|
||||
|
||||
private static String abs(String u, String base, String origin) {
|
||||
return u.startsWith("http") ? u : u.startsWith("/") ? origin + u : base + u;
|
||||
}
|
||||
|
||||
private Response proxy(IHTTPSession session) {
|
||||
Map<String, String> p = session.getParms();
|
||||
final String target = p.get("url");
|
||||
if (target == null || target.isEmpty()) return text(400, "Missing url parameter");
|
||||
|
||||
JSONObject ch = new JSONObject();
|
||||
try {
|
||||
if (p.get("headers") != null) ch = new JSONObject(p.get("headers"));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
ch.remove("Host");
|
||||
ch.remove("host");
|
||||
|
||||
final boolean strip = "1".equals(p.get("strip")) || "1".equals(p.get("tt")) || STRIP.matcher(target).find();
|
||||
|
||||
Request.Builder rb = new Request.Builder();
|
||||
try {
|
||||
rb.url(target);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return text(400, "Bad url");
|
||||
}
|
||||
rb.header("User-Agent", VylaPlugin.UA).header("Accept", "*/*").header("Accept-Language", "en-US,en;q=0.9");
|
||||
Iterator<String> it = ch.keys();
|
||||
while (it.hasNext()) {
|
||||
String k = it.next();
|
||||
if (k.equalsIgnoreCase("accept-encoding")) continue;
|
||||
try {
|
||||
rb.header(k, ch.optString(k));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
String range = session.getHeaders().get("range");
|
||||
if (range != null) rb.header("Range", range);
|
||||
if (session.getMethod() == Method.HEAD) rb.head();
|
||||
Request req = rb.build();
|
||||
|
||||
okhttp3.Response up = null;
|
||||
IOException last = null;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (up != null) {
|
||||
up.close();
|
||||
up = null;
|
||||
}
|
||||
try {
|
||||
up = VylaPlugin.CLIENT.newCall(req).execute();
|
||||
int c = up.code();
|
||||
if (up.isSuccessful() || c == 404) break;
|
||||
if (c == 403) {
|
||||
if (i == 2) break;
|
||||
if (!nap(1000)) break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
last = e;
|
||||
if (i == 2 || !nap(500L * (i + 1))) break;
|
||||
}
|
||||
}
|
||||
if (up == null) return text(502, last != null ? last.getMessage() : "Bad Gateway");
|
||||
|
||||
final okhttp3.Response upf = up;
|
||||
int code = up.code();
|
||||
if (!up.isSuccessful()) {
|
||||
up.close();
|
||||
return text(code, "Upstream error: " + code);
|
||||
}
|
||||
|
||||
try {
|
||||
String ctRaw = up.header("Content-Type", "");
|
||||
String ct = ctRaw.toLowerCase();
|
||||
boolean m3u8 = target.contains(".m3u8") || ct.contains("mpegurl") || ct.contains("m3u8");
|
||||
ResponseBody body = up.body();
|
||||
|
||||
if (target.contains(".srt") || ct.contains("subrip")) {
|
||||
String raw = body == null ? "" : body.string();
|
||||
String vtt = "WEBVTT\n\n" + raw.replaceAll("\r\n|\r", "\n").replaceAll("(\\d{2}:\\d{2}:\\d{2}),(\\d{3})", "$1.$2");
|
||||
up.close();
|
||||
return newFixedLengthResponse(Response.Status.OK, "text/vtt; charset=utf-8", vtt);
|
||||
}
|
||||
|
||||
if (m3u8) {
|
||||
String txt = body == null ? "" : body.string();
|
||||
up.close();
|
||||
String base = target.substring(0, target.lastIndexOf('/') + 1);
|
||||
String origin = origin(target);
|
||||
String hp = ch.length() > 0 ? "&headers=" + enc(ch.toString()) : "";
|
||||
String[] lines = txt.split("\n", -1);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
String raw = lines[i];
|
||||
String t = raw.trim();
|
||||
String out = raw;
|
||||
if (t.startsWith("#EXT-X-KEY") || t.startsWith("#EXT-X-MAP") || t.startsWith("#EXT-X-MEDIA")) {
|
||||
int u = t.indexOf("URI=\"");
|
||||
if (u != -1) {
|
||||
int end = t.indexOf('"', u + 5);
|
||||
if (end != -1) {
|
||||
String uri = t.substring(u + 5, end);
|
||||
out = t.substring(0, u + 5) + "/api/proxy?url=" + enc(abs(uri, base, origin)) + hp + "\"" + t.substring(end + 1);
|
||||
}
|
||||
}
|
||||
} else if (!t.isEmpty() && !t.startsWith("#")) {
|
||||
String a = abs(t, base, origin);
|
||||
String sp = strip || SEGSTRIP.matcher(a).find() ? "&strip=1" : "";
|
||||
out = "/api/proxy?url=" + enc(a) + hp + sp;
|
||||
}
|
||||
sb.append(out);
|
||||
if (i < lines.length - 1) sb.append('\n');
|
||||
}
|
||||
return newFixedLengthResponse(Response.Status.OK, "application/vnd.apple.mpegurl", sb.toString());
|
||||
}
|
||||
|
||||
if (strip) {
|
||||
byte[] b = body == null ? new byte[0] : body.bytes();
|
||||
up.close();
|
||||
if (b.length > 120 && (b[0] == (byte) 0x89 || b[0] == (byte) 0xFF || b[0] == 0x00)) b = Arrays.copyOfRange(b, 120, b.length);
|
||||
return newFixedLengthResponse(Response.Status.OK, "video/MP2T", new ByteArrayInputStream(b), b.length);
|
||||
}
|
||||
|
||||
if (body == null) {
|
||||
up.close();
|
||||
return text(code, "");
|
||||
}
|
||||
long n = body.contentLength();
|
||||
String mime = ctRaw.isEmpty() ? "application/octet-stream" : ctRaw;
|
||||
InputStream in = new FilterInputStream(body.byteStream()) {
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
try {
|
||||
super.close();
|
||||
} finally {
|
||||
upf.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
Response r = n >= 0 ? newFixedLengthResponse(new Code(code), mime, in, n) : newChunkedResponse(new Code(code), mime, in);
|
||||
String cr = up.header("Content-Range");
|
||||
String ar = up.header("Accept-Ranges");
|
||||
if (cr != null) r.addHeader("Content-Range", cr);
|
||||
if (ar != null) r.addHeader("Accept-Ranges", ar);
|
||||
return r;
|
||||
} catch (IOException e) {
|
||||
up.close();
|
||||
return text(502, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,30 @@
|
||||
package tv.vyla.app;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.WindowManager;
|
||||
import com.getcapacitor.BridgeActivity;
|
||||
|
||||
public class MainActivity extends BridgeActivity {}
|
||||
public class MainActivity extends BridgeActivity {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
registerPlugin(VylaPlugin.class);
|
||||
super.onCreate(savedInstanceState);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
getBridge().getWebView().getSettings().setMediaPlaybackRequiresUserGesture(false);
|
||||
getBridge().getWebView().requestFocus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean dispatchKeyEvent(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.KEYCODE_BACK) {
|
||||
if (e.getAction() == KeyEvent.ACTION_UP) {
|
||||
getBridge().getWebView().evaluateJavascript("window.vylaBack?window.vylaBack():false", v -> {
|
||||
if (!"true".equals(v)) finish();
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return super.dispatchKeyEvent(e);
|
||||
}
|
||||
}
|
||||
138
android/app/src/main/java/tv/vyla/app/VylaPlugin.java
Normal file
138
android/app/src/main/java/tv/vyla/app/VylaPlugin.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package tv.vyla.app;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import com.getcapacitor.JSObject;
|
||||
import com.getcapacitor.Plugin;
|
||||
import com.getcapacitor.PluginCall;
|
||||
import com.getcapacitor.PluginMethod;
|
||||
import com.getcapacitor.annotation.CapacitorPlugin;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.Dispatcher;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
@CapacitorPlugin(name = "VylaNative")
|
||||
public class VylaPlugin extends Plugin {
|
||||
static final String UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
|
||||
static final OkHttpClient CLIENT = buildClient();
|
||||
private LocalProxy proxy;
|
||||
|
||||
private static OkHttpClient buildClient() {
|
||||
Dispatcher d = new Dispatcher();
|
||||
d.setMaxRequests(128);
|
||||
d.setMaxRequestsPerHost(32);
|
||||
return new OkHttpClient.Builder()
|
||||
.dispatcher(d)
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
try {
|
||||
proxy = new LocalProxy(38417);
|
||||
proxy.start(5000, true);
|
||||
} catch (IOException e) {
|
||||
proxy = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleOnDestroy() {
|
||||
if (proxy != null) proxy.stop();
|
||||
}
|
||||
|
||||
private static byte[] read(Response r, int max) throws IOException {
|
||||
if (r.body() == null) return new byte[0];
|
||||
InputStream in = r.body().byteStream();
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[16384];
|
||||
int n;
|
||||
while ((n = in.read(buf)) != -1) {
|
||||
if (max > 0 && bo.size() + n >= max) {
|
||||
bo.write(buf, 0, Math.min(n, max - bo.size()));
|
||||
break;
|
||||
}
|
||||
bo.write(buf, 0, n);
|
||||
}
|
||||
return bo.toByteArray();
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
public void fetch(final PluginCall call) {
|
||||
final String url = call.getString("url");
|
||||
if (url == null) {
|
||||
call.reject("Missing url");
|
||||
return;
|
||||
}
|
||||
final int max = call.getInt("maxBytes", 0);
|
||||
final int timeout = call.getInt("timeout", 30000);
|
||||
final boolean follow = !"manual".equals(call.getString("redirect", "follow"));
|
||||
String method = call.getString("method", "GET").toUpperCase();
|
||||
JSObject hd = call.getObject("headers", new JSObject());
|
||||
Request.Builder rb;
|
||||
try {
|
||||
rb = new Request.Builder().url(url);
|
||||
String ctype = null;
|
||||
Iterator<String> it = hd.keys();
|
||||
while (it.hasNext()) {
|
||||
String k = it.next();
|
||||
if (k.equalsIgnoreCase("accept-encoding")) continue;
|
||||
String v = hd.optString(k, null);
|
||||
if (v == null) continue;
|
||||
rb.header(k, v);
|
||||
if (k.equalsIgnoreCase("content-type")) ctype = v;
|
||||
}
|
||||
if (!hd.has("user-agent")) rb.header("User-Agent", UA);
|
||||
RequestBody rbody = null;
|
||||
if (!method.equals("GET") && !method.equals("HEAD")) {
|
||||
String body = call.getString("body");
|
||||
rbody = RequestBody.create(body == null ? "" : body, MediaType.parse(ctype == null ? "text/plain; charset=utf-8" : ctype));
|
||||
}
|
||||
rb.method(method, rbody);
|
||||
} catch (Exception e) {
|
||||
call.reject(String.valueOf(e.getMessage()));
|
||||
return;
|
||||
}
|
||||
OkHttpClient c = CLIENT.newBuilder()
|
||||
.callTimeout(timeout, TimeUnit.MILLISECONDS)
|
||||
.followRedirects(follow)
|
||||
.followSslRedirects(follow)
|
||||
.build();
|
||||
c.newCall(rb.build()).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call c, IOException e) {
|
||||
call.reject(String.valueOf(e.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call c, Response r) {
|
||||
try {
|
||||
JSObject out = new JSObject();
|
||||
out.put("status", r.code());
|
||||
out.put("url", r.request().url().toString());
|
||||
JSObject h = new JSObject();
|
||||
for (String n : r.headers().names()) h.put(n.toLowerCase(), TextUtils.join(", ", r.headers(n)));
|
||||
out.put("headers", h);
|
||||
out.put("body", Base64.encodeToString(read(r, max), Base64.NO_WRAP));
|
||||
call.resolve(out);
|
||||
} catch (Exception e) {
|
||||
call.reject(String.valueOf(e.getMessage()));
|
||||
} finally {
|
||||
r.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
<base-config cleartextTrafficPermitted="true">
|
||||
<trust-anchors>
|
||||
<certificates src="system" />
|
||||
<certificates src="user" />
|
||||
</trust-anchors>
|
||||
</base-config>
|
||||
</network-security-config>
|
||||
Reference in New Issue
Block a user