@samuraitruong/php-cookie-challenge
Version:
Axios wrapper with automatic cookie challenge detection and processing
150 lines (107 loc) โข 4.98 kB
Markdown
An axios wrapper library that automatically detects and processes cookie challenges from PHP free hosting providers, allowing seamless API access without manual cookie handling.
Many free PHP hosting providers (like free.nf, InfinityFree, etc.) implement cookie-based challenge systems to protect their servers from abuse. When you make an API request to these hosts:
1. **First Request**: The server returns an HTML page with a JavaScript challenge instead of your API response
2. **The Challenge**: Contains encrypted values that need to be decrypted using a server-provided AES library
3. **Cookie Requirement**: The decrypted value must be sent as a cookie (`__test`) in subsequent requests
4. **Manual Process**: Without automation, you'd need to:
- Parse the HTML response
- Extract encrypted values
- Load and execute the AES decryption library
- Generate the cookie
- Retry the request with the cookie
This process is tedious and error-prone when building API clients.
## The Solution
This library automatically handles all of the above steps transparently. Your API client code remains clean and simple - just make requests as you normally would, and the library handles the cookie challenge in the background.
## Features
- ๐ Automatic cookie challenge detection via response interceptors
- ๐ช Silent cookie processing to bypass server challenges
- ๐ฏ Easy-to-use axios client wrapper
- ๐งช Comprehensive unit tests
## Installation
```bash
npm install @samuraitruong/php-cookie-challenge
```
**TypeScript Support**: This package includes TypeScript type definitions. No additional `@types` package needed.
## Usage
### Simple Usage - Auto-configured Client
```javascript
import { createAxiosClient } from '@samuraitruong/php-cookie-challenge';
// Create a client with automatic cookie challenge handling
const client = createAxiosClient({
baseURL: 'https://example.com',
timeout: 5000,
});
// Use it like a regular axios client
// Cookie challenges are automatically detected and processed
const response = await client.get('/api/data');
const postResponse = await client.post('/api/submit', { data: 'value' });
```
```javascript
import axios from 'axios';
import { createCookieChallengeInterceptor } from '@samuraitruong/php-cookie-challenge';
// Create your axios client
const client = axios.create({
baseURL: 'https://example.com',
});
// Create and use the cookie challenge interceptor
const challengeCookie = createCookieChallengeInterceptor(client);
client.interceptors.response.use(challengeCookie);
// Now all requests will automatically handle cookie challenges
// The interceptor will make sequential API calls to get cookies
// and retry the original request with the new cookies
const response = await client.get('/api/data');
```
The cookie challenge detection and processing happens automatically and silently in the background. When a challenge is detected, the interceptor will:
1. Make sequential API calls to get the required cookies
2. Automatically retry the original request with the new cookies
3. Return the final response as if the challenge never happened
The library includes full TypeScript support:
```typescript
import { createAxiosClient } from '@samuraitruong/php-cookie-challenge';
import type { AxiosInstance, AxiosRequestConfig } from 'axios';
// Create a client with automatic cookie challenge handling
const client: AxiosInstance = createAxiosClient({
baseURL: 'https://example.com',
timeout: 5000,
} as AxiosRequestConfig);
// TypeScript will provide full type checking and autocomplete
const response = await client.get('/api/data');
```
```bash
npm test
npm run test:watch
npm run test:coverage
```
```
.
โโโ src/
โ โโโ index.js
โ โโโ client.js
โ โโโ cookieChallenge.js
โโโ tests/
โ โโโ client.test.js
โ โโโ integration.test.js
โโโ package.json
โโโ README.md
```
The library automatically handles cookie challenges by:
1. **Detection**: Detects when a server response contains a JavaScript-based cookie challenge (typically using slowAES encryption)
2. **Processing**:
- Extracts encrypted values from the challenge HTML
- Loads the slowAES library from the server
- Decrypts the challenge to generate the required cookie
3. **Retry**: Automatically retries the original request with the decrypted cookie and required parameters
All of this happens silently in the background - your code doesn't need to handle any of these details.
## License
ISC