Skip to content

Commit

Permalink
Add CookieReader.getCookieFirefoxLinux
Browse files Browse the repository at this point in the history
  • Loading branch information
TaYaKi71751 committed Dec 20, 2023
1 parent 6488126 commit 5eb68fa
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 2 deletions.
92 changes: 92 additions & 0 deletions lib/database/user/cookie.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This source code is a part of Project Violet.
// Copyright (C) 2020-2023. violet-team. Licensed under the Apache-2.0 License.

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:sqflite/sqflite.dart';
import 'package:flutter/services.dart';

class CookiesManager {
String? dbPath;
Database? db;
static CookiesManager? _instance;

CookiesManager({this.dbPath});

static CookiesManager create(String dbPath) {
return CookiesManager(dbPath: dbPath);
}

@protected
@mustCallSuper
void dispose() async {
print('close: ${dbPath!}');
if (db != null) db!.close();
}

static Future<CookiesManager> getInstance(String dbPath) async {
if (_instance == null) {
_instance = create(dbPath);
await _instance!.open();
}
return _instance!;
}

static Future<void> reloadInstance(String dbPath) async {
_instance = create(dbPath);
}

Future open() async {
db ??= await openDatabase(dbPath!);
}

Future checkOpen() async {
if(db != null){
if (!db!.isOpen) db = await openDatabase(dbPath!);
} else if(db == null){
db = await openDatabase(dbPath!);
}
}

Future<List<Map<String, dynamic>>> query(String str) async {
List<Map<String, dynamic>> result = [];
await checkOpen();
result = await db!.rawQuery(str);
return result;
}

Future<void> execute(String str) async {
await checkOpen();
await db!.execute(str);
}

Future<int> insert(String name, Map<String, dynamic> wh) async {
int result = -1;
await checkOpen();
result = await db!.insert(name, wh);
return result;
}

Future<void> update(String name, Map<String, dynamic> wh, String where,
List<dynamic> args) async {
await checkOpen();
await db!.update(name, wh, where: where, whereArgs: args);
}

Future<void> swap(String name, String key, String what, int key1, int key2,
int s1, int s2) async {
await checkOpen();
await db!.transaction((txn) async {
await txn.rawUpdate('UPDATE $name SET $what=? WHERE $key=?', [s2, key1]);
await txn.rawUpdate('UPDATE $name SET $what=? WHERE $key=?', [s1, key2]);
});
}

Future<void> delete(String name, String where, List<dynamic> args) async {
await checkOpen();
await db!.delete(name, where: where, whereArgs: args);
}
}
13 changes: 11 additions & 2 deletions lib/pages/settings/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import 'package:violet/pages/settings/version_page.dart';
import 'package:violet/pages/splash/splash_page.dart';
import 'package:violet/platform/misc.dart';
import 'package:violet/server/violet.dart';
import 'package:violet/settings/cookie.dart';
import 'package:violet/settings/path.dart';
import 'package:violet/settings/settings.dart';
import 'package:violet/style/palette.dart';
Expand Down Expand Up @@ -2230,8 +2231,16 @@ class _SettingsPageState extends State<SettingsPage>
style: ElevatedButton.styleFrom(
backgroundColor: Settings.majorColor,
),
child: const Text('Login From WebPage'),
onPressed: () => Navigator.pop(context, 1),
child: ((){
if(Platform.isAndroid || Platform.isIOS) return const Text('Login From WebPage');
if(Platform.isLinux) return const Text('Read cookies from firefox');
})(),
onPressed: (() {
if(Platform.isAndroid || Platform.isIOS) Navigator.pop(context, 1);
if(Platform.isLinux) {
CookieReader.getCookieFirefoxLinux();
}
}),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
Expand Down
67 changes: 67 additions & 0 deletions lib/settings/cookie.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:convert';
import 'dart:io';

import 'package:violet/component/eh/eh_headers.dart';
import 'package:violet/database/user/cookie.dart';
import 'package:violet/log/log.dart';
import 'package:violet/settings/path.dart';
import 'package:violet/settings/settings.dart';

class CookieReader {
static Future<void> getCookieFirefoxLinux() async {
final find_path = '${(await DefaultPathProvider.getHomeDirectory())}';
var db_path_list = [];
try{
var process = await Process.start("find", [
'${find_path}','-name',"cookies.sqlite",'-type','f'
]);

await process.stdout
.transform(utf8.decoder)
.forEach((db_path){
Logger.info(db_path);
if(db_path.isNotEmpty) db_path_list.add(db_path);
});
} catch(e,st){
Logger.error("$e\n"
"$st");
}
db_path_list = (db_path_list.join('\n').split('\n')).where((String element) => (element.isNotEmpty)).toList();

for(final db_path in db_path_list){
final tmp_db_path = '${(await DefaultPathProvider.getBaseDirectory())}/cookies.db.tmp';
Logger.info(db_path);
if((await File('${tmp_db_path}').exists())){
await File('${tmp_db_path}').delete();
}
final db_path_find_result = await Process.run("cp", [
'${db_path}','${tmp_db_path}'
]);
final result = await (await CookiesManager.getInstance(tmp_db_path))
.query("SELECT * FROM moz_cookies WHERE host LIKE '%e-hentai.org'");
var cookie_str = '';
result.forEach((row) {
var name,value;
row.entries.forEach((col) {
switch(col.key){
case 'name': name = col.value; break;
case 'value': value = col.value; break;
}
});
cookie_str += '${name}=${value}; ';
});
await (await MultiPreferences.getInstance())
.setString('eh_cookies', cookie_str);
var login_result_html = await EHSession.requestRedirect('https://e-hentai.org/favorites.php');
if(login_result_html != null){
await (await MultiPreferences.getInstance())
.setString('eh_cookies', '');
continue;
}
if((await File('${tmp_db_path}').exists())){
await File('${tmp_db_path}').delete();
}
break;
}
}
}

0 comments on commit 5eb68fa

Please sign in to comment.