-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from himanshu8443/feature
update v2.5.5
- Loading branch information
Showing
13 changed files
with
329 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,192 @@ | ||
import axios from 'axios'; | ||
import {getBaseUrl} from '../getBaseUrl'; | ||
import * as cheerio from 'cheerio'; | ||
import {MmmkvCache} from '../../Mmkv'; | ||
import {ToastAndroid} from 'react-native'; | ||
|
||
export async function nfGetCookie() { | ||
try { | ||
const baseUrl = await getBaseUrl('nfMirror'); | ||
const res = await axios.get(baseUrl + '/home', {withCredentials: false}); | ||
const $ = cheerio.load(res.data); | ||
// console.log('nf cookie html', res.data); | ||
const addhash = $('body').attr('data-addhash'); | ||
console.log('nf addhash', addhash); | ||
export class NetMirrorCookieFetcher { | ||
private static readonly COOKIE_CACHE_KEY = 'nfCookie'; | ||
private static readonly COOKIE_TIME_KEY = 'nfCookieTime'; | ||
private static readonly COOKIE_EXPIRY_MS = 79200000; // 22 hours | ||
private static readonly VERIFY_TIMEOUT_MS = 25000; | ||
private static readonly MAX_RETRY_ATTEMPTS = 50; | ||
|
||
/** | ||
* Fetches a cookie with intelligent caching and error handling | ||
* @returns Promise resolving to the cookie string | ||
*/ | ||
static async fetchCookie(): Promise<string> { | ||
try { | ||
const addRes = await fetch( | ||
baseUrl + '/v.php?hash=' + addhash + '&t=' + Math.random(), | ||
{ | ||
credentials: 'omit', | ||
}, | ||
// Check cached cookie first | ||
const cachedCookie = this.getCachedCookie(); | ||
if (cachedCookie) { | ||
return cachedCookie; | ||
} | ||
|
||
// Show waiting toast | ||
ToastAndroid.show( | ||
'Getting cookie, please wait 20 sec...', | ||
ToastAndroid.SHORT, | ||
); | ||
|
||
// Fetch new cookie with retry mechanism | ||
return await this.fetchFreshCookie(); | ||
} catch (err) { | ||
console.log('nf addhash error ', err); | ||
console.error('NetMirror cookie fetch failed:', err); | ||
return ''; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves cached cookie if still valid | ||
* @returns Cached cookie or null if expired | ||
*/ | ||
private static getCachedCookie(): string | null { | ||
const nfCookieTime = MmmkvCache.getString(this.COOKIE_TIME_KEY); | ||
if (nfCookieTime) { | ||
const timeDiff = new Date().getTime() - parseInt(nfCookieTime); | ||
if (timeDiff < this.COOKIE_EXPIRY_MS) { | ||
const nfCookie = MmmkvCache.getString(this.COOKIE_CACHE_KEY); | ||
console.log('NetMirror cookie retrieved from cache'); | ||
return nfCookie || null; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Fetches a fresh cookie with multiple verification steps | ||
* @returns Promise resolving to the new cookie | ||
*/ | ||
private static async fetchFreshCookie(): Promise<string> { | ||
const baseUrl = await getBaseUrl('nfMirror'); | ||
|
||
// Fetch initial page to get addhash | ||
const res = await axios.get(`${baseUrl}/home`, {withCredentials: false}); | ||
const $ = cheerio.load(res.data); | ||
const addhash = $('body').attr('data-addhash'); | ||
|
||
if (!addhash) { | ||
throw new Error('Unable to extract addhash'); | ||
} | ||
|
||
// Preliminary verification | ||
await this.performPreliminaryVerification(addhash); | ||
|
||
// Cookie verification with retry logic | ||
const cookie = await this.verifyCookie(baseUrl, addhash); | ||
|
||
// Cache the new cookie | ||
this.cacheCookie(cookie); | ||
ToastAndroid.show('Cookie saved for 24 hours', ToastAndroid.SHORT); | ||
|
||
return cookie; | ||
} | ||
|
||
/** | ||
* Performs preliminary verification request | ||
* @param addhash Verification hash | ||
*/ | ||
private static async performPreliminaryVerification( | ||
addhash: string, | ||
): Promise<void> { | ||
try { | ||
const addRes = await fetch( | ||
'https://userverify.netmirror.app/verify?vhf=' + | ||
addhash + | ||
'&t=' + | ||
Math.random(), | ||
{ | ||
credentials: 'omit', | ||
}, | ||
await fetch( | ||
`https://userverify.netmirror.app/verify?dp1=${addhash}&a=y&t=${Math.random()}`, | ||
{credentials: 'omit'}, | ||
); | ||
} catch (err) { | ||
console.log('nf addhash error ', err); | ||
console.warn('Preliminary verification failed:', err); | ||
} | ||
const form = new FormData(); | ||
form.append('verify', addhash); | ||
const res2 = await fetch(baseUrl + '/verify2.php', { | ||
} | ||
|
||
/** | ||
* Verifies and retrieves the cookie with retry mechanism | ||
* @param baseUrl Base URL for verification | ||
* @param addhash Verification hash | ||
* @returns Promise resolving to the cookie string | ||
*/ | ||
private static async verifyCookie( | ||
baseUrl: string, | ||
addhash: string, | ||
): Promise<string> { | ||
for (let attempt = 1; attempt <= this.MAX_RETRY_ATTEMPTS; attempt++) { | ||
const form = new FormData(); | ||
form.append('verify', addhash); | ||
|
||
const verificationPromise = new Promise<string>((resolve, reject) => { | ||
const timeoutId = setTimeout(() => { | ||
reject(new Error('Cookie verification timed out')); | ||
}, this.VERIFY_TIMEOUT_MS); | ||
|
||
this.performCookieVerification(baseUrl, form) | ||
.then(cookie => { | ||
clearTimeout(timeoutId); | ||
resolve(cookie); | ||
}) | ||
.catch(err => { | ||
clearTimeout(timeoutId); | ||
reject(err); | ||
}); | ||
}); | ||
|
||
try { | ||
return await verificationPromise; | ||
} catch (err) { | ||
console.warn(`Cookie verification attempt ${attempt} failed:`, err); | ||
if (attempt === this.MAX_RETRY_ATTEMPTS) { | ||
throw err; | ||
} | ||
// Optional: add a small delay between retries | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
} | ||
} | ||
|
||
throw new Error('Failed to retrieve cookie after multiple attempts'); | ||
} | ||
|
||
/** | ||
* Performs the actual cookie verification request | ||
* @param baseUrl Base URL for verification | ||
* @param form FormData for verification | ||
* @returns Promise resolving to the cookie string | ||
*/ | ||
private static async performCookieVerification( | ||
baseUrl: string, | ||
form: FormData, | ||
): Promise<string> { | ||
const res2 = await fetch(`${baseUrl}/verify2.php`, { | ||
method: 'POST', | ||
body: form, | ||
credentials: 'omit', | ||
}); | ||
|
||
const res2Json = await res2.json(); | ||
console.log('NetMirror verification response:', res2Json); | ||
|
||
if (res2Json.statusup !== 'All Done') { | ||
throw new Error('Verification not complete'); | ||
} | ||
|
||
const cookie2 = res2.headers.get('set-cookie'); | ||
console.log('nfCookie2', cookie2); | ||
return cookie2?.split(';')[0] + ';' || ''; | ||
} catch (err) { | ||
console.error('nf cookie error: ', err); | ||
return ''; | ||
if (!cookie2) { | ||
throw new Error('No cookie found in response'); | ||
} | ||
|
||
return `${cookie2.split(';')[0]};`; | ||
} | ||
|
||
/** | ||
* Caches the retrieved cookie | ||
* @param cookie Cookie string to cache | ||
*/ | ||
private static cacheCookie(cookie: string): void { | ||
MmmkvCache.setString(this.COOKIE_CACHE_KEY, cookie); | ||
MmmkvCache.setString(this.COOKIE_TIME_KEY, new Date().getTime().toString()); | ||
} | ||
} | ||
|
||
// Usage | ||
export async function nfGetCookie(): Promise<string> { | ||
return NetMirrorCookieFetcher.fetchCookie(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.