Skip to content

Review Proxy Fork and Merge with minor changes #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 32 commits into
base: 15-feature-proxy
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e9e6ef0
[+] add proxy settings strings
grisha765 Sep 16, 2024
4b7f7ac
[+] add proxy settings
grisha765 Sep 16, 2024
56d8935
[u] add socks proxy func for video
grisha765 Sep 16, 2024
85b21b6
[u] add socks proxy func for photo
grisha765 Sep 16, 2024
d39986c
[u] add socks proxy func for text content
grisha765 Sep 16, 2024
cd3d16c
[u] update link checkout
grisha765 Sep 16, 2024
4c98246
[u] update link checkout
grisha765 Sep 16, 2024
961d31a
[f] set release grisha765/Tubular-Proxy
grisha765 Sep 16, 2024
eb90df8
[r] update name and id
grisha765 Sep 16, 2024
668d23e
[r] update apk name
grisha765 Sep 16, 2024
f61ae5d
[-] removed Proxy Help line
grisha765 Sep 17, 2024
af39fb9
[-] removed Proxy Help settings
grisha765 Sep 17, 2024
fad88ba
[u] updated to dynamic strings
grisha765 Sep 17, 2024
6c31d99
[+] add Toast strings
grisha765 Sep 17, 2024
35c1533
[u] add validation
grisha765 Sep 17, 2024
bf45ce0
[u] add dynamic update summary
grisha765 Sep 17, 2024
a311ffc
[u] add Toast notify
grisha765 Sep 17, 2024
219c7d3
[u] update readme
grisha765 Sep 17, 2024
40fc5f4
[+] add proxy icon
grisha765 Sep 17, 2024
db72766
[u] use proxy icon
grisha765 Sep 17, 2024
56731ae
[f] use base fragment
grisha765 Sep 17, 2024
d9f2eba
[f] fix strings
grisha765 Sep 17, 2024
2c4b005
[+] add russian lang
grisha765 Sep 17, 2024
103a33e
[f] fix title
grisha765 Sep 17, 2024
3082c7c
[u] add new strings
grisha765 Nov 15, 2024
93211c7
[u] new parameter proxy type
grisha765 Nov 15, 2024
7ec4519
[u] database implementation for a new parameter
grisha765 Nov 15, 2024
892e6e7
[u] new conditions for the parameter
grisha765 Nov 15, 2024
c304fb1
[u] version code
grisha765 Nov 15, 2024
332e4bf
[f] fix tags
grisha765 Nov 16, 2024
66eb459
Merge remote-tracking branch 'proxy-upstream/master' into 15-feature-…
asandikci Jan 10, 2025
11ed298
remove download link
asandikci Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ graph TD
<!-- <details><summary>Tubular Readme</summary>
<h1 align="center"><b>Tubular</b></h2>
<h4 align="center">A fork of <a href="https://newpipe.net/">NewPipe</a> (<a href="https://github.com/TeamNewPipe/NewPipe/">Github</a>) that implements <a href="https://sponsor.ajay.app/">SponsorBlock</a> (<a href="https://github.com/ajayyy/SponsorBlock/">Github</a>) and <a href="https://www.returnyoutubedislike.com/">ReturnYouTubeDislike</a> (<a href="https://github.com/Anarios/return-youtube-dislike/">Github</a>).</h4>
<p align="center">Download the APK <a href="https://github.com/polymorphicshade/Tubular/releases/latest">here</a>.</p>
<p align="center"><img src="doc/gif/preview_01.gif" width="400"></p>

## To Do
Expand Down
48 changes: 43 additions & 5 deletions app/src/main/java/org/schabi/newpipe/DownloaderImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.schabi.newpipe;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -22,6 +24,8 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.net.Proxy;
import java.net.InetSocketAddress;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
Expand All @@ -40,11 +44,45 @@ public final class DownloaderImpl extends Downloader {
private final OkHttpClient client;

private DownloaderImpl(final OkHttpClient.Builder builder) {
this.client = builder
.readTimeout(30, TimeUnit.SECONDS)
// .cache(new Cache(new File(context.getExternalCacheDir(), "okhttp"),
// 16 * 1024 * 1024))
.build();
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
App.getApp()
);
final boolean isProxyEnabled = sharedPreferences.getBoolean(
App.getApp().getString(R.string.proxy_enabled_key), false);
Log.d("DownloaderImpl_ProxySettings", "Read proxy_enabled_key: " + isProxyEnabled);
if (isProxyEnabled) {
Log.d("DownloaderImpl_ProxySettings",
"Update called. proxy_enabled_key on: " + isProxyEnabled);
final String proxyAddress = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_address_key), "192.168.1.1");
final String proxyPortStr = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_port_key), "1080");
final String proxyTypeStr = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_type_key), "socks");
final Proxy.Type proxyType = "http".equalsIgnoreCase(proxyTypeStr)
? Proxy.Type.HTTP
: Proxy.Type.SOCKS;
final int proxyPort = Integer.parseInt(proxyPortStr);
Log.d("DownloaderImpl_ProxySettings",
"Proxy enabled with address: " + proxyAddress
+ " and port: " + proxyPort + ", type: " + proxyType);
final Proxy proxy = new Proxy(proxyType,
new InetSocketAddress(proxyAddress, proxyPort));
this.client = builder
.proxy(proxy)
.readTimeout(30, TimeUnit.SECONDS)
// .cache(new Cache(new File(context.getExternalCacheDir(), "okhttp"),
// 16 * 1024 * 1024))
.build();
} else {
Log.d("DownloaderImpl_ProxySettings",
"Update called. proxy_enabled_key off: " + isProxyEnabled);
this.client = builder
.readTimeout(30, TimeUnit.SECONDS)
// .cache(new Cache(new File(context.getExternalCacheDir(), "okhttp"),
// 16 * 1024 * 1024))
.build();
}
this.mCookies = new HashMap<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.content.SharedPreferences;

import androidx.preference.PreferenceManager;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.upstream.BaseDataSource;
Expand All @@ -44,6 +48,8 @@
import com.google.common.collect.Sets;
import com.google.common.net.HttpHeaders;

import org.schabi.newpipe.App;
import org.schabi.newpipe.R;
import org.schabi.newpipe.DownloaderImpl;

import java.io.IOException;
Expand All @@ -61,6 +67,9 @@
import java.util.Objects;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.net.Proxy;
import java.net.InetSocketAddress;


/**
* An {@link HttpDataSource} that uses Android's {@link HttpURLConnection}, based on
Expand Down Expand Up @@ -713,7 +722,38 @@ private HttpURLConnection makeConnection(
* @return an {@link HttpURLConnection} created with the {@code url}
*/
private HttpURLConnection openConnection(@NonNull final URL url) throws IOException {
return (HttpURLConnection) url.openConnection();
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
App.getApp()
);
final boolean isProxyEnabled = sharedPreferences.getBoolean(
App.getApp().getString(R.string.proxy_enabled_key), false);
android.util.Log.d("YoutubeHttpDataSource_ProxySettings",
"Read proxy_enabled_key: " + isProxyEnabled);
if (isProxyEnabled) {
android.util.Log.d("YoutubeHttpDataSource_ProxySettings",
"Update called. proxy_enabled_key on: " + isProxyEnabled);
final String proxyAddress = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_address_key), "192.168.1.1");
final String proxyPortStr = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_port_key), "1080");
final String proxyTypeStr = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_type_key), "socks");
final Proxy.Type proxyType = "http".equalsIgnoreCase(proxyTypeStr)
? Proxy.Type.HTTP
: Proxy.Type.SOCKS;
final int proxyPort = Integer.parseInt(proxyPortStr);
android.util.Log.d("YoutubeHttpDataSource_ProxySettings",
"Proxy enabled with address: "
+ proxyAddress + " and port: " + proxyPort + ", type: " + proxyType);
final Proxy proxy = new Proxy(proxyType,
new InetSocketAddress(proxyAddress, proxyPort));
connection = (HttpURLConnection) url.openConnection(proxy);
} else {
android.util.Log.d("YoutubeHttpDataSource_ProxySettings",
"Update called. proxy_enabled_key off: " + isProxyEnabled);
connection = (HttpURLConnection) url.openConnection();
}
return connection;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package org.schabi.newpipe.settings;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.widget.Toast;
import androidx.preference.Preference;
import androidx.preference.ListPreference;
import androidx.preference.SwitchPreferenceCompat;
import androidx.preference.PreferenceManager;

import org.schabi.newpipe.App;
import org.schabi.newpipe.R;

public class ProxySettingsFragment extends BasePreferenceFragment {

@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
// Подключаем preferences файл для Proxy
addPreferencesFromResource(R.xml.proxy_settings);

// Получаем SharedPreferences
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
App.getApp()
);

// Настройка включения прокси
final SwitchPreferenceCompat proxyEnablePref = findPreference(
App.getApp().getString(R.string.proxy_enabled_key)
);
assert proxyEnablePref != null;

proxyEnablePref.setOnPreferenceChangeListener((preference, newValue) -> {
final boolean isEnabled = (Boolean) newValue;
Log.d("ProxySettings", "Read proxy_enabled_key: " + isEnabled);
// Сохраняем новое значение proxy_enabled_key
sharedPreferences.edit().putBoolean(
App.getApp().getString(R.string.proxy_enabled_key),
isEnabled).apply();
Log.d("ProxySettings",
"Saved proxy_enabled_key: " + sharedPreferences.getBoolean(
App.getApp().getString(R.string.proxy_enabled_key), false));
// Сообщаем пользователю, что требуется перезапуск
Toast.makeText(getContext(),
getString(R.string.proxy_restart_required), Toast.LENGTH_LONG).show();
return true;
});

// Настройка адреса прокси
final Preference proxyAddressPref = findPreference(
App.getApp().getString(R.string.proxy_address_key)
);
assert proxyAddressPref != null;

proxyAddressPref.setOnPreferenceChangeListener((preference, newValue) -> {
// Валидация IP-адреса
if (!isValidIpAddress(newValue.toString())) {
Toast.makeText(getContext(),
getString(R.string.invalid_ip_address), Toast.LENGTH_SHORT).show();
return false; // Не сохраняем изменение
}
Log.d("ProxySettings", "Read proxy_address_key: " + newValue);
// Сохраняем новое значение IP-адреса
sharedPreferences.edit().putString(
App.getApp().getString(R.string.proxy_address_key),
newValue.toString()).apply();
// Обновляем summary с новым адресом
proxyAddressPref.setSummary(
getString(R.string.proxy_address_summary, newValue)
);
// Сообщаем пользователю, что требуется перезапуск
Toast.makeText(getContext(),
getString(R.string.proxy_restart_required), Toast.LENGTH_LONG).show();
return true;
});
// Устанавливаем текущее значение в summary при загрузке настроек
final String currentAddress = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_address_key), "192.168.1.1"
);
proxyAddressPref.setSummary(
getString(R.string.proxy_address_summary, currentAddress)
);

// Настройка порта прокси
final Preference proxyPortPref = findPreference(
App.getApp().getString(R.string.proxy_port_key)
);
assert proxyPortPref != null;

proxyPortPref.setOnPreferenceChangeListener((preference, newValue) -> {
// Валидация порта
if (!isValidPort(newValue.toString())) {
Toast.makeText(getContext(),
getString(R.string.invalid_port), Toast.LENGTH_SHORT).show();
return false; // Не сохраняем изменение
}
Log.d("ProxySettings", "Read proxy_port_key: " + newValue);
// Сохраняем новое значение порта
sharedPreferences.edit().putString(
App.getApp().getString(R.string.proxy_port_key),
newValue.toString()).apply();
// Обновляем summary с новым портом
proxyPortPref.setSummary(
getString(R.string.proxy_port_summary, newValue)
);
// Сообщаем пользователю, что требуется перезапуск
Toast.makeText(getContext(),
getString(R.string.proxy_restart_required), Toast.LENGTH_LONG).show();
return true;
});
// Устанавливаем текущее значение в summary при загрузке настроек
final String currentPort = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_port_key), "1080"
);
proxyPortPref.setSummary(
getString(R.string.proxy_port_summary, currentPort)
);
// Настройка типа прокси
final ListPreference proxyTypePref = findPreference(
App.getApp().getString(R.string.proxy_type_key)
);
assert proxyTypePref != null;

proxyTypePref.setOnPreferenceChangeListener((preference, newValue) -> {
final String proxyType = newValue.toString();
Log.d("ProxySettings", "Read proxy_type_key: " + proxyType);
// Сохраняем новое значение типа прокси
sharedPreferences.edit().putString(
App.getApp().getString(R.string.proxy_type_key),
proxyType).apply();
// Обновляем summary
proxyTypePref.setSummary(
getString(R.string.proxy_type_summary) + ": " + proxyType.toUpperCase()
);
// Сообщаем пользователю, что требуется перезапуск
Toast.makeText(getContext(),
getString(R.string.proxy_restart_required), Toast.LENGTH_LONG).show();
return true;
});
// Устанавливаем текущее значение в summary при загрузке настроек
final String currentProxyType = sharedPreferences.getString(
App.getApp().getString(R.string.proxy_type_key), "socks"
);
proxyTypePref.setSummary(
getString(R.string.proxy_type_summary) + ": " + currentProxyType.toUpperCase()
);
}
// Метод для валидации IP-адреса
public boolean isValidIpAddress(final String ipAddress) {
return Patterns.IP_ADDRESS.matcher(ipAddress).matches();
}
// Метод для валидации порта
public boolean isValidPort(final String port) {
try {
final int portNumber = Integer.parseInt(port);
return portNumber >= 1 && portNumber <= 65535;
} catch (final NumberFormatException e) {
return false;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;

import androidx.preference.PreferenceManager;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -28,6 +31,8 @@
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.net.Proxy;
import java.net.InetSocketAddress;

import okhttp3.OkHttpClient;

Expand All @@ -45,17 +50,45 @@ private PicassoHelper() {
// suppress because terminate() is called in App.onTerminate(), preventing leaks
@SuppressLint("StaticFieldLeak")
private static Picasso picassoInstance;
private static Proxy proxySet;


public static void init(final Context context) {
picassoCache = new LruCache(10 * 1024 * 1024);
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(
context
);
final boolean isProxyEnabled = sharedPreferences.getBoolean(
context.getString(R.string.proxy_enabled_key), false);
Log.d("PicassoHelper_ProxySettings", "Read proxy_enabled_key: " + isProxyEnabled);
if (isProxyEnabled) {
Log.d("PicassoHelper_ProxySettings",
"Update called. proxy_enabled_key on: " + isProxyEnabled);
final String proxyAddress = sharedPreferences.getString(
context.getString(R.string.proxy_address_key), "192.168.1.1");
final String proxyPortStr = sharedPreferences.getString(
context.getString(R.string.proxy_port_key), "1080");
final String proxyTypeStr = sharedPreferences.getString(
context.getString(R.string.proxy_type_key), "socks");
final Proxy.Type proxyType = "http".equalsIgnoreCase(proxyTypeStr)
? Proxy.Type.HTTP
: Proxy.Type.SOCKS;
final int proxyPort = Integer.parseInt(proxyPortStr);
Log.d("PicassoHelper_ProxySettings",
"Proxy enabled with address: "
+ proxyAddress + " and port: " + proxyPort + ", type: " + proxyType);
proxySet = new Proxy(proxyType, new InetSocketAddress(proxyAddress, proxyPort));
} else {
Log.d("PicassoHelper_ProxySettings",
"Update called. proxy_enabled_key off: " + isProxyEnabled);
}
picassoDownloaderClient = new OkHttpClient.Builder()
.proxy(proxySet)
.cache(new okhttp3.Cache(new File(context.getExternalCacheDir(), "picasso"),
50L * 1024L * 1024L))
// this should already be the default timeout in OkHttp3, but just to be sure...
.callTimeout(15, TimeUnit.SECONDS)
.build();

picassoInstance = new Picasso.Builder(context)
.memoryCache(picassoCache) // memory cache
.downloader(new OkHttp3Downloader(picassoDownloaderClient)) // disk cache
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_key.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/defaultIconTint"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="#FF000000"
android:pathData="M7 14C5.9 14 5 13.1 5 12S5.9 10 7 10 9 10.9 9 12 8.1 14 7 14M12.6 10C11.8 7.7 9.6 6 7 6C3.7 6 1 8.7 1 12S3.7 18 7 18C9.6 18 11.8 16.3 12.6 14H16V18H20V14H23V10H12.6Z" />
</vector>
Loading
Loading