Skip to content

Commit

Permalink
Remove MMKV Cookie Storage (#896)
Browse files Browse the repository at this point in the history
* fix retry button

* remove cookie storage

* add cookie storage

* fix import

* remove all references of mmkv cookies
  • Loading branch information
nguyd1 authored Jan 14, 2024
1 parent 36cb7fc commit 02b058d
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 119 deletions.
4 changes: 1 addition & 3 deletions src/components/ListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StyleSheet, View, Text, Pressable } from 'react-native';
import FastImage from 'react-native-fast-image';
import { coverPlaceholderColor } from '../theme/colors';

import { getSourceStorage } from '@hooks/useSourceStorage';
import { defaultUserAgentString } from '@utils/fetch/fetch';

import color from 'color';
Expand All @@ -19,7 +18,6 @@ const ListView = ({
isSelected,
onLongPress,
}) => {
const { cookies = '' } = getSourceStorage(item.sourceId);
return (
<Pressable
android_ripple={{ color: theme.rippleColor }}
Expand All @@ -35,7 +33,7 @@ const ListView = ({
<FastImage
source={{
uri: item.novelCover,
headers: { Cookie: cookies, 'User-Agent': defaultUserAgentString },
headers: { 'User-Agent': defaultUserAgentString },
}}
style={[styles.extensionIcon, inLibraryBadge && { opacity: 0.5 }]}
/>
Expand Down
5 changes: 1 addition & 4 deletions src/components/NovelCover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { SourceNovelItem } from 'src/sources/types';
import { ThemeColors } from '@theme/types';
import SourceScreenSkeletonLoading from '@screens/browse/loadingAnimation/SourceScreenSkeletonLoading';

import { getSourceStorage } from '@hooks/useSourceStorage';
import { defaultUserAgentString } from '@utils/fetch/fetch';

interface NovelCoverProps {
Expand Down Expand Up @@ -68,8 +67,6 @@ const NovelCover: React.FC<NovelCoverProps> = ({

const uri = item.novelCover;

const { cookies = '' } = getSourceStorage(item.sourceId);

return item.sourceId < 0 ? (
<SourceScreenSkeletonLoading theme={theme} completeRow={item.sourceId} />
) : displayMode !== DisplayModes.List ? (
Expand Down Expand Up @@ -115,7 +112,7 @@ const NovelCover: React.FC<NovelCoverProps> = ({
<FastImage
source={{
uri,
headers: { Cookie: cookies, 'User-Agent': defaultUserAgentString },
headers: { 'User-Agent': defaultUserAgentString },
}}
style={[
{
Expand Down
64 changes: 0 additions & 64 deletions src/hooks/useSourceStorage.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SourceNovelItem } from '../../../sources/types';
import { ThemeColors } from '../../../theme/types';
import { getString } from '@strings/translations';

import { getSourceStorage } from '@hooks/useSourceStorage';
import { defaultUserAgentString } from '@utils/fetch/fetch';

interface Props {
Expand All @@ -35,8 +34,6 @@ const GlobalSearchNovelItem: React.FC<Props> = ({
[inLibrary],
);

const { cookies = '' } = getSourceStorage(novel.sourceId);

return (
<View style={styles.novelItem}>
<Pressable
Expand All @@ -48,7 +45,7 @@ const GlobalSearchNovelItem: React.FC<Props> = ({
<FastImage
source={{
uri: novel.novelCover,
headers: { Cookie: cookies, 'User-Agent': defaultUserAgentString },
headers: { 'User-Agent': defaultUserAgentString },
}}
style={[styles.novelCover, { ...novelItemDimensions }]}
/>
Expand Down
19 changes: 6 additions & 13 deletions src/screens/WebviewScreen/WebviewScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useEffect } from 'react';
import React from 'react';
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
import WebView from 'react-native-webview';
import CookieManager from '@react-native-cookies/cookies';

import { Appbar } from '@components';
import { useTheme } from '@hooks/useTheme';
import useSourceStorage from '@hooks/useSourceStorage';
import { defaultUserAgentString } from '@utils/fetch/fetch';

type ReaderScreenRouteProps = RouteProp<{
Expand All @@ -21,19 +20,12 @@ const WebviewScreen = () => {
const { goBack } = useNavigation();

const {
params: { name, sourceId, url },
params: { name, url },
} = useRoute<ReaderScreenRouteProps>();
const { setSourceStorage } = useSourceStorage({ sourceId });

useEffect(() => {
CookieManager.get(url, true).then(cookies => {
const cloudflareCookie = cookies?.cf_clearance;
if (cloudflareCookie) {
const cloudflareCookieString = `${cloudflareCookie.name}=${cloudflareCookie.value}`;
setSourceStorage('cookies', cloudflareCookieString);
}
});
}, []);
const syncCookies = () => {
CookieManager.flush();
};

return (
<>
Expand All @@ -42,6 +34,7 @@ const WebviewScreen = () => {
startInLoadingState
userAgent={defaultUserAgentString}
source={{ uri: url }}
onNavigationStateChange={syncCookies}
/>
</>
);
Expand Down
4 changes: 0 additions & 4 deletions src/screens/history/components/HistoryCard/HistoryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
NovelScreenRouteParams,
} from '@utils/NavigationUtils';

import { getSourceStorage } from '@hooks/useSourceStorage';
import { defaultUserAgentString } from '@utils/fetch/fetch';

interface HistoryCardProps {
Expand Down Expand Up @@ -59,8 +58,6 @@ const HistoryCard: React.FC<HistoryCardProps> = ({
[chapterName, historyTimeRead],
);

const { cookies = '' } = getSourceStorage(sourceId);

return (
<Pressable
style={styles.container}
Expand Down Expand Up @@ -94,7 +91,6 @@ const HistoryCard: React.FC<HistoryCardProps> = ({
source={{
uri: novelCover,
headers: {
Cookie: cookies,
'User-Agent': defaultUserAgentString,
},
}}
Expand Down
5 changes: 0 additions & 5 deletions src/screens/novel/components/Info/NovelInfoHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { followNovelAction } from '../../../../redux/novel/novel.actions';
import { useSettings } from '../../../../hooks/reduxHooks';
import { showToast } from '../../../../hooks/showToast';

import { getSourceStorage } from '@hooks/useSourceStorage';
import { defaultUserAgentString } from '@utils/fetch/fetch';

import {
Expand Down Expand Up @@ -57,15 +56,12 @@ const NovelInfoHeader = ({
[],
);

const { cookies = '' } = getSourceStorage(novel.sourceId);

return (
<>
<CoverImage
source={{
uri: novel.novelCover,
headers: {
Cookie: cookies,
'User-Agent': defaultUserAgentString,
},
}}
Expand All @@ -77,7 +73,6 @@ const NovelInfoHeader = ({
source={{
uri: novel.novelCover,
headers: {
Cookie: cookies,
'User-Agent': defaultUserAgentString,
},
}}
Expand Down
7 changes: 5 additions & 2 deletions src/screens/settings/SettingsAdvancedScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import { openDirectory } from '../../native/epubParser';

import { Appbar, Button, List } from '@components';
import { ScreenContainer } from '@components/Common';
import useSourceStorage from '@hooks/useSourceStorage';
import CookieManager from '@react-native-cookies/cookies';

const AdvancedSettings = ({ navigation }) => {
const theme = useTheme();
const { clearCookies } = useSourceStorage({});
const clearCookies = () => {
CookieManager.clearAll();
showToast('Cookies cleared');
};

/**
* Confirm Clear Database Dialog
Expand Down
6 changes: 2 additions & 4 deletions src/screens/updates/components/UpdateNovelCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ import { useAppDispatch } from '@redux/hooks';
import { useTheme } from '@hooks/useTheme';
import { noop } from 'lodash-es';

import { getSourceStorage } from '@hooks/useSourceStorage';
import { defaultUserAgentString } from '@utils/fetch/fetch';

const NovelCover = ({
uri,
navigateToNovel,
sourceId,
}: {
uri: string;
navigateToNovel: () => void;
sourceId: number;
}) => {
const { cookies = '' } = getSourceStorage(sourceId);
return (
<Pressable
onPress={navigateToNovel}
Expand All @@ -41,7 +39,7 @@ const NovelCover = ({
}}
>
<FastImage
source={{ uri, headers: { 'cookie': cookies } }}
source={{ uri, headers: { 'User-Agent': defaultUserAgentString } }}
style={styles.cover}
/>
</Pressable>
Expand Down
5 changes: 1 addition & 4 deletions src/sources/helpers/cloudflareImagesBypass.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { getSourceStorage } from '@hooks/useSourceStorage';
import { defaultUserAgentString, fetchApi } from '@utils/fetch/fetch';
import { Cheerio, CheerioAPI, Element } from 'cheerio';

export const bypassImages = async (
loadedCheerio: CheerioAPI,
element: Cheerio<Element>,
sourceId: number,
): Promise<string> => {
const { cookies = '' } = getSourceStorage(sourceId);
let promises: Promise<number>[] = [];

element.find('noscript').each(function () {
Expand All @@ -28,7 +25,7 @@ export const bypassImages = async (
const response = await fetchApi({
url: attr.value,
init: {
headers: { Cookie: cookies, 'User-Agent': defaultUserAgentString },
headers: { 'User-Agent': defaultUserAgentString },
},
});

Expand Down
12 changes: 0 additions & 12 deletions src/utils/fetch/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { getSourceStorage } from '@hooks/useSourceStorage';

export const defaultUserAgentString =
'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36';

interface FetchParams {
url: string; // URL of request
init?: RequestInit; // Variable for passing headers and other information
sourceId?: number; // ID number of source for cookies
}

// Checks if we bypassed cloudflare. If we failed to bypass, throw error.
Expand All @@ -27,7 +24,6 @@ export const cloudflareCheck = (text: string) => {
export const fetchApi = async ({
url,
init,
sourceId,
}: FetchParams): Promise<Response> => {
let headers = new Headers({
'User-Agent': defaultUserAgentString,
Expand All @@ -38,14 +34,6 @@ export const fetchApi = async ({
// You can have NO user agent by doing this:
// init: { headers: { 'User-Agent': undefined } },

if (sourceId) {
const { cookies = '' } = getSourceStorage(sourceId);

if (cookies) {
headers.append('cookie', cookies);
}
}

return fetch(url, { ...init, headers });
};

Expand Down

0 comments on commit 02b058d

Please sign in to comment.