Skip to content

Commit

Permalink
fix issues with http auth headers not working when watching a video a…
Browse files Browse the repository at this point in the history
…s well as download
lamarios committed Sep 22, 2024
1 parent a040b3b commit 759d159
Showing 7 changed files with 56 additions and 29 deletions.
11 changes: 9 additions & 2 deletions lib/downloads/states/download_manager.dart
Original file line number Diff line number Diff line change
@@ -113,6 +113,9 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
return false;
} else {
Video vid = await service.getVideo(videoId);

final server = await db.getCurrentlySelectedServer();

var downloadedVideo = DownloadedVideo(
videoId: vid.videoId,
title: vid.title,
@@ -157,7 +160,9 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
//download thumbnail
var thumbnailPath = await downloadedVideo.thumbnailPath;
log.fine("Downloading thumbnail to: $thumbnailPath");
await dio.download(thumbUrl, thumbnailPath, cancelToken: cancelToken);
await dio.download(thumbUrl, thumbnailPath,
cancelToken: cancelToken,
options: Options(headers: server.headersForUrl(thumbUrl)));
}

// download video
@@ -176,7 +181,8 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
count, total, downloadedVideo,
step: 1, totalSteps: audioOnly ? 2 : 3),
cancelToken: cancelToken,
deleteOnError: true)
deleteOnError: true,
options: Options(headers: server.headersForUrl(audioUrl)))
.catchError((err) {
onDownloadError(err, downloadedVideo);
return Response<void>(requestOptions: RequestOptions());
@@ -193,6 +199,7 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
totalSteps: 3,
),
cancelToken: cancelToken,
options: Options(headers: server.headersForUrl(videoUrl)),
deleteOnError: true)
.catchError((err) {
onDownloadError(err, downloadedVideo);
7 changes: 5 additions & 2 deletions lib/player/states/audio_player.dart
Original file line number Diff line number Diff line change
@@ -113,13 +113,15 @@ class AudioPlayerCubit extends MediaPlayerCubit<AudioPlayerState> {
player.setEvent(const MediaEvent(state: MediaState.loading));
try {
AudioSource? source;
final server = await db.getCurrentlySelectedServer();

if (!offline) {
if (service.useProxy()) {
// audio only streams don't seem to work when using proxy mode, using formatted streams when proxy is enabled
var formatStream = state
.video!.formatStreams[state.video!.formatStreams.length - 1];
source = AudioSource.uri(Uri.parse(formatStream.url));
source = AudioSource.uri(Uri.parse(formatStream.url),
headers: server.headersForUrl(formatStream.url));
} else {
AdaptiveFormat? audio = state.video?.adaptiveFormats
.where((element) => element.type.contains("audio"))
@@ -135,7 +137,8 @@ class AudioPlayerCubit extends MediaPlayerCubit<AudioPlayerState> {
}
emit(state);

source = AudioSource.uri(Uri.parse(audio.url));
source = AudioSource.uri(Uri.parse(audio.url),
headers: server.headersForUrl(audio.url));
}
}
} else {
16 changes: 10 additions & 6 deletions lib/player/states/video_player.dart
Original file line number Diff line number Diff line change
@@ -255,7 +255,8 @@ class VideoPlayerCubit extends MediaPlayerCubit<VideoPlayerState> {
liveStream: false,
);
} else {
String baseUrl = (await db.getCurrentlySelectedServer()).url;
var server = await db.getCurrentlySelectedServer();
String baseUrl = (server).url;

Map<String, String> resolutions = {};

@@ -265,21 +266,23 @@ class VideoPlayerCubit extends MediaPlayerCubit<VideoPlayerState> {
? null
: newState
.video!.formatStreams[newState.video!.formatStreams.length - 1];

var useProxy = service.useProxy();

String videoUrl = isHls
? '${newState.video!.hlsUrl!}${service.useProxy() ? '?local=true' : ''}'
? '${newState.video!.hlsUrl!}${useProxy ? '?local=true' : ''}'
: isUsingDash()
? '${newState.video!.dashUrl}${service.useProxy() ? '?local=true' : ''}'
? '${newState.video!.dashUrl}${useProxy ? '?local=true' : ''}'
: formatStream?.url ?? '';
if (!isUsingDash() && formatStream != null) {
newState =
newState.copyWith(selectedNonDashTrack: formatStream.resolution);
}

// somehow invidious is sending google url even when using local proxy when not using dash
if (!isUsingDash() && service.useProxy()) {
if (!isUsingDash() && useProxy) {
// we replace google's cdn by invidious server url.
videoUrl = videoUrl.replaceFirst(_googleCdnRegex,
'${(await db.getCurrentlySelectedServer()).url}/');
videoUrl = videoUrl.replaceFirst(_googleCdnRegex, '${(server).url}/');
}

log.info(
@@ -305,6 +308,7 @@ class VideoPlayerCubit extends MediaPlayerCubit<VideoPlayerState> {
betterPlayerDataSource = BetterPlayerDataSource(
BetterPlayerDataSourceType.network,
videoUrl,
headers: !offline ? server.headersForUrl(videoUrl) : {},
videoFormat: format,
liveStream: newState.video!.liveNow,
subtitles: newState.video!.captions
12 changes: 12 additions & 0 deletions lib/settings/models/db/server.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:logging/logging.dart';

part 'server.freezed.dart';

part 'server.g.dart';

final _log = Logger('Server');

@freezed
class Server with _$Server {
const factory Server(
@@ -15,5 +19,13 @@ class Server with _$Server {
@Default({}) Map<String, String> customHeaders,
@Default(false) bool inUse}) = _Server;

const Server._();

Map<String, String>? headersForUrl(String url) {
var useHeaders = url.startsWith(this.url);
_log.fine('need header for $url ? $useHeaders');
return useHeaders ? customHeaders : null;
}

factory Server.fromJson(Map<String, Object?> json) => _$ServerFromJson(json);
}
8 changes: 5 additions & 3 deletions lib/settings/models/db/server.freezed.dart
Original file line number Diff line number Diff line change
@@ -197,7 +197,7 @@ class __$$ServerImplCopyWithImpl<$Res>

/// @nodoc
@JsonSerializable()
class _$ServerImpl implements _Server {
class _$ServerImpl extends _Server {
const _$ServerImpl(
{required this.url,
this.authToken,
@@ -207,7 +207,8 @@ class _$ServerImpl implements _Server {
@JsonKey(includeFromJson: false, includeToJson: false) this.region,
final Map<String, String> customHeaders = const {},
this.inUse = false})
: _customHeaders = customHeaders;
: _customHeaders = customHeaders,
super._();

factory _$ServerImpl.fromJson(Map<String, dynamic> json) =>
_$$ServerImplFromJson(json);
@@ -284,7 +285,7 @@ class _$ServerImpl implements _Server {
}
}

abstract class _Server implements Server {
abstract class _Server extends Server {
const factory _Server(
{required final String url,
final String? authToken,
@@ -296,6 +297,7 @@ abstract class _Server implements Server {
final String? region,
final Map<String, String> customHeaders,
final bool inUse}) = _$ServerImpl;
const _Server._() : super._();

factory _Server.fromJson(Map<String, dynamic> json) = _$ServerImpl.fromJson;

15 changes: 7 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -556,10 +556,10 @@ packages:
dependency: "direct dev"
description:
name: flutter_launcher_icons
sha256: "526faf84284b86a4cb36d20a5e45147747b7563d921373d4ee0559c54fcdbcea"
sha256: a38f2f1b3c373d42bf08bd17d60e20d3c73abce7727607b4d085ec7d5acaa294
url: "https://pub.dev"
source: hosted
version: "0.13.1"
version: "0.14.0"
flutter_linkify:
dependency: "direct main"
description:
@@ -839,12 +839,11 @@ packages:
locale_names:
dependency: "direct main"
description:
path: "."
ref: master
resolved-ref: cea057c220f4ee7e09e8f1fc7036110245770948
url: "https://github.com/lamarios/locale_names.git"
source: git
version: "0.0.1"
name: locale_names
sha256: "7a89ca54072f4f13d0f5df5a9ba69337554bf2fd057d1dd2a238898f3f159374"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
logger:
dependency: transitive
description:
16 changes: 8 additions & 8 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: invidious
version: 1.21.2+4061
version: 1.21.3+4062
publish_to: none
description: A new Flutter project.
description: Client for invidious.
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
@@ -38,6 +38,10 @@ dependencies:
intl: 0.19.0
json_annotation: 4.9.0
just_audio: 0.9.40
locale_names: 1.1.1
# git:
# url: https://github.com/lamarios/locale_names.git
# ref: master
logging: 1.2.0
package_info_plus: 8.0.2
path: any
@@ -56,11 +60,7 @@ dependencies:
sdk: flutter
flutter_localizations:
sdk: flutter
locale_names:
git:
url: https://github.com/lamarios/locale_names.git
ref: master
river_player:
river_player:
git:
url: https://github.com/lamarios/river_player.git
ref: master
@@ -72,7 +72,7 @@ dev_dependencies:
auto_route_generator: any
build_runner: ^2.4.10
copy_with_extension_gen: 5.0.4
flutter_launcher_icons: ^0.13.1
flutter_launcher_icons: ^0.14.0
flutter_lints: ^4.0.0
freezed: ^2.5.7
json_serializable: ^6.7.1

0 comments on commit 759d159

Please sign in to comment.