cloudflare_bypass 0.0.1
cloudflare_bypass: ^0.0.1 copied to clipboard
A Flutter package for bypassing CloudFlare protection using Dio HTTP client and WebView. Includes cookie management, interceptor chain, and automatic captcha resolution.
import 'package:cloudflare_bypass/cloudflare_bypass.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CloudFlare Bypass Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with BypassHttpClientMixin {
final _urlController = TextEditingController(
text: 'https://annas-archive.li/slow_download/9eab7566b2982b1d748cff45e50841c7/0/2',
);
String _status = 'Ready';
String _response = '';
String _cookies = '';
bool _isLoading = false;
BypassHttpClient? _client;
@override
void initState() {
super.initState();
_initClient();
}
Future<void> _initClient() async {
_client = await BypassHttpClient.create(
config: const ClientConfig(
enableLogging: true,
cloudFlareBypassTimeout: Duration(minutes: 2),
),
);
initHttpClient(_client!);
setState(() {
_status = 'Client initialized';
});
}
@override
void dispose() {
_urlController.dispose();
_client?.close();
super.dispose();
}
Future<void> _makeRequest() async {
if (_client == null) {
setState(() {
_status = 'Client not initialized';
});
return;
}
final url = _urlController.text.trim();
if (url.isEmpty) {
setState(() {
_status = 'Please enter a URL';
});
return;
}
setState(() {
_isLoading = true;
_status = 'Loading...';
_response = '';
_cookies = '';
});
try {
final response = await _client!.get(url);
setState(() {
_status = 'Success: ${response.statusCode}';
_response = _truncateResponse(response.data?.toString() ?? 'No data');
});
// Get cookies
await _loadCookies(Uri.parse(url));
} on CloudFlareDioError catch (e) {
setState(() {
_status = 'CloudFlare Error: ${e.cloudFlareException.message}';
_response = 'CloudFlare protection detected.\n\n'
'URL: ${e.cloudFlareException.url}\n'
'State: ${e.cloudFlareException.state}';
});
final result = await CloudFlareWebView.show(
context,
url: url,
cookieJar: _client!.cookieJar,
title: 'Solve Captcha',
);
if (result.success) {
_makeRequest();
return;
}
} on DioException catch (e) {
setState(() {
_status = 'Error: ${e.type}';
_response = e.message ?? 'Unknown error';
});
} catch (e) {
setState(() {
_status = 'Error';
_response = e.toString();
});
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _manualBypass() async {
if (_client == null) {
setState(() {
_status = 'Client not initialized';
});
return;
}
final url = _urlController.text.trim();
if (url.isEmpty) {
setState(() {
_status = 'Please enter a URL';
});
return;
}
setState(() {
_isLoading = true;
_status = 'Starting bypass...';
});
try {
final result = await CloudFlareWebView.show(
context,
url: url,
cookieJar: _client!.cookieJar,
title: 'Solve Captcha',
);
if (result.success) {
setState(() {
_status = 'Bypass successful!';
});
await _loadCookies(Uri.parse(url));
} else {
setState(() {
_status = 'Bypass failed: ${result.error}';
});
}
} catch (e) {
setState(() {
_status = 'Error: $e';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _loadCookies(Uri uri) async {
if (_client == null) return;
try {
final cookies = await _client!.getCookies(uri);
final cookieStrings = cookies.map((c) {
final cookie = c as dynamic;
return '${cookie.name}=${cookie.value}';
}).toList();
setState(() {
_cookies = cookieStrings.isEmpty
? 'No cookies'
: cookieStrings.join('\n');
});
} catch (e) {
setState(() {
_cookies = 'Error loading cookies: $e';
});
}
}
Future<void> _clearCookies() async {
if (_client == null) return;
await _client!.clearCookies();
setState(() {
_status = 'Cookies cleared';
_cookies = '';
});
}
String _truncateResponse(String response) {
const maxLength = 2000;
if (response.length <= maxLength) {
return response;
}
return '${response.substring(0, maxLength)}\n\n... (truncated)';
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('CloudFlare Bypass Demo'),
actions: [
IconButton(
icon: const Icon(Icons.delete),
onPressed: _clearCookies,
tooltip: 'Clear Cookies',
),
],
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// URL Input
TextField(
controller: _urlController,
decoration: const InputDecoration(
labelText: 'URL',
hintText: 'Enter URL to test',
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.link),
),
keyboardType: TextInputType.url,
),
const SizedBox(height: 16),
// Action Buttons
Row(
children: [
Expanded(
child: ElevatedButton.icon(
onPressed: _isLoading ? null : _makeRequest,
icon: _isLoading
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.send),
label: const Text('Make Request'),
),
),
const SizedBox(width: 8),
Expanded(
child: OutlinedButton.icon(
onPressed: _isLoading ? null : _manualBypass,
icon: const Icon(Icons.security),
label: const Text('Manual Bypass'),
),
),
],
),
const SizedBox(height: 16),
// Status
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
const Icon(Icons.info_outline),
const SizedBox(width: 8),
Expanded(
child: Text(
'Status: $_status',
style: Theme.of(context).textTheme.bodyMedium,
),
),
],
),
),
const SizedBox(height: 16),
// Tabs for Response and Cookies
Expanded(
child: DefaultTabController(
length: 2,
child: Column(
children: [
const TabBar(
tabs: [
Tab(text: 'Response'),
Tab(text: 'Cookies'),
],
),
Expanded(
child: TabBarView(
children: [
// Response Tab
Container(
margin: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade300),
borderRadius: BorderRadius.circular(8),
),
child: SingleChildScrollView(
child: SelectableText(
_response.isEmpty
? 'No response yet'
: _response,
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 12,
),
),
),
),
// Cookies Tab
Container(
margin: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade300),
borderRadius: BorderRadius.circular(8),
),
child: SingleChildScrollView(
child: SelectableText(
_cookies.isEmpty
? 'No cookies yet'
: _cookies,
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 12,
),
),
),
),
],
),
),
],
),
),
),
],
),
),
);
}
}