Skip to content

Commit

Permalink
添加多浏览器Cookie同步
Browse files Browse the repository at this point in the history
  • Loading branch information
easychen committed Aug 17, 2022
1 parent 533795e commit 6c366c5
Show file tree
Hide file tree
Showing 18 changed files with 831 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

## 最新版本

- 插件·Chrome/Edge:2022.08.17.13.51 [下载](ckc.zip) ([Github下载地址,无需注册](https://github.com/easychen/checkchan-dist/raw/master/ckc.zip))
- Docker镜像(云端+远程桌面二合一):2022.08.17.12.05 [Docker Hub](https://hub.docker.com/repository/docker/easychen/checkchan)
- 文档:2022.08.17.13.51
- 插件·Chrome/Edge:2022.08.17.20.32 [下载](ckc.zip) ([Github下载地址,无需注册](https://github.com/easychen/checkchan-dist/raw/master/ckc.zip))
- Docker镜像(云端+远程桌面二合一):2022.08.17.19.39 [Docker Hub](https://hub.docker.com/repository/docker/easychen/checkchan)
- 文档:2022.08.17.20.32
- 更新日志:[GitHub](https://github.com/easychen/checkchan-dist/commits/main)
- 生态:RSSHub浏览器插件整合版([源码](https://github.com/easychen/RSSHub-Radar-with-checkchan) | [下载](https://github.com/easychen/RSSHub-Radar-with-checkchan/releases/download/main/1.7.0.with-checkchan.zip)

Expand Down
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"2022.08.17.13.51"
"2022.08.17.20.32"
Binary file modified ckc.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion docker.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"2022.08.17.12.05"
"2022.08.17.19.39"
30 changes: 30 additions & 0 deletions docker/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ app.post( `/rss/upload`, checkApiKey, (req, res) => {
}
});

app.post( `/cookie/sync`, checkApiKey, (req, res) => {
if( req.body.direction && req.body.direction == 'down' ) {
// 下行cookie
const user_name = req.body.password || "own";
let cookies_file = `${image_dir}/cookies.${user_name}.base64.txt`;
const cookies_64 = fs.readFileSync(cookies_file, 'utf8');
res.json({"code":0,"data":cookies_64});
}else
{
// 上行cookie
if( req.body.cookies_base64 )
{
let cookies_base64 = req.body.cookies_base64;
const user_name = req.body.password || "own";
let cookies_file = `${image_dir}/cookies.${user_name}.base64.txt`;
fs.writeFileSync(cookies_file,cookies_base64);
res.json({"code":0,"message":"保存成功"});
}else
{
res.json({"code":-1,"message":"cookie参数不能为空"});
}


// let cookies = Buffer.from(req.body.cookies_base64,'base64').toString();

}

// res.json(req.body);
});

app.post(`/checks/upload`, checkApiKey , (req, res) => {
const data = { checks: JSON.parse(req.body.checks)||[], cookies: JSON.parse(req.body.cookies) ||{} };
const data_file = get_data_dir()+'/data.json';
Expand Down
2 changes: 1 addition & 1 deletion docker/src/chrome_extension/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"message": "Check酱"
},
"appDesc": {
"message": "监控页面变动,并发送异动到微信。 Build.2022.08.17.13.51",
"message": "监控页面变动,并发送异动到微信。 Build.2022.08.17.20.32",
"description":"亦支持http status、json和rss监测。配合自架云端,关电脑后也能运行。"
}
}
10 changes: 5 additions & 5 deletions docker/src/chrome_extension/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"files": {
"main.css": "/static/css/main.f2766b2a.chunk.css",
"main.js": "/static/js/main.2411968d.chunk.js",
"main.js": "/static/js/main.e0ceed25.chunk.js",
"runtime-main.js": "/static/js/runtime-main.17191aad.js",
"static/css/2.65280f5c.chunk.css": "/static/css/2.65280f5c.chunk.css",
"static/js/2.f4e71d54.chunk.js": "/static/js/2.f4e71d54.chunk.js",
"static/js/2.fb5c1c43.chunk.js": "/static/js/2.fb5c1c43.chunk.js",
"index.html": "/index.html",
"precache-manifest.ef4e4009a9308c36ee12ad207daef55e.js": "/precache-manifest.ef4e4009a9308c36ee12ad207daef55e.js",
"precache-manifest.d081260a9ad335232527f33321ad446e.js": "/precache-manifest.d081260a9ad335232527f33321ad446e.js",
"service-worker.js": "/service-worker.js",
"static/media/index.scss": "/static/media/icons-20.cef8cdbb.woff"
},
"entrypoints": [
"static/js/runtime-main.17191aad.js",
"static/css/2.65280f5c.chunk.css",
"static/js/2.f4e71d54.chunk.js",
"static/js/2.fb5c1c43.chunk.js",
"static/css/main.f2766b2a.chunk.css",
"static/js/main.2411968d.chunk.js"
"static/js/main.e0ceed25.chunk.js"
]
}
120 changes: 114 additions & 6 deletions docker/src/chrome_extension/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import html2canvas from './html2canvas.esm.js';

import { Base64 } from './base64.mjs';

let inspector = {};
console.log( "load bg.js" );
Expand Down Expand Up @@ -283,6 +283,12 @@ chrome.runtime.onInstalled.addListener(function (details)
periodInMinutes: 1
});

chrome.alarms.create('cookie_sync',
{
when: Date.now(),
periodInMinutes: 1
});

// chrome.alarms.create('auto_sync',
// {
// when: Date.now(),
Expand All @@ -292,7 +298,7 @@ chrome.runtime.onInstalled.addListener(function (details)
chrome.alarms.create('bg_cookie_sync',
{
when: Date.now(),
periodInMinutes: 61
periodInMinutes: 11
});

console.log("create alarms");
Expand Down Expand Up @@ -405,6 +411,95 @@ function sleep(ms) {

chrome.alarms.onAlarm.addListener( async a =>
{
if( a.name == 'cookie_sync' )
{
const settings = await kv_load('settings');

// 如果没有配置上行URL,则不同步cookie
if( !settings._settings_cookie_sync_url ) return false;

// 每分钟检查,如果没有到设置的时间,则不同步cookie
const interval = settings._settings_cookie_sync_interval || 5;
const minute = new Date().getMinutes();
if( minute % interval != 0 ) return false;

if( settings._settings_cookie_sync_direction == 'up' )
{
// 获得cookie
const checks = await load_data('checks');
const cookies = await get_cookie_by_checks( checks ) || [];

// 引入base64 和 aes 库
let cookie_string = Base64.encode(JSON.stringify(cookies));

console.log("cookie_string", cookie_string);
const form = new FormData();
form.append( 'cookies_base64',cookie_string );
form.append( 'direction', settings._settings_cookie_sync_direction );
form.append( 'password', settings._settings_cookie_sync_password );
try {
const response = await fetch( settings._settings_cookie_sync_url, {
method: 'POST',
body: form
} );

const ret = await response.json();
console.log( "ret", ret );
return ret;

} catch (error) {
console.log("请求服务器失败。"+error);
return false;
}
}else
{
// Cookie 下行
const form = new FormData();
form.append( 'direction', settings._settings_cookie_sync_direction );
form.append( 'password', settings._settings_cookie_sync_password );
try {
const response = await fetch( settings._settings_cookie_sync_url, {
method: 'POST',
body: form
} );

const ret = await response.json();
if( ret.code == 0 && ret.data )
{
const cookies = JSON.parse(Base64.decode(ret.data));
// set cookies to browser
for( let domain in cookies )
{
// console.log( "domain" , cookies[domain] );
if( Array.isArray(cookies[domain]) )
{
for( let cookie of cookies[domain] )
{
let newcookie = {};
['name','value','domain','path','secure','httpOnly','sameSite'].forEach( key => {
newcookie[key] = cookie[key];
} );
newcookie.url = buildUrl(cookie.secure, cookie.domain, cookie.path);
// console.log(newcookie);
chrome.cookies.set(newcookie, (e)=>{console.log(e)});
}
}
}
}
// console.log( "ret", ret );
return ret;



} catch (error) {
console.log("请求服务器失败。"+error);
return false;
}
}


}

if( a.name == 'bg_cookie_sync' )
{
console.log( 'bg_cookie_sync' );
Expand Down Expand Up @@ -481,9 +576,15 @@ async function get_cookie_by_checks( checks )
for( const item of checks )
{
// console.log( "item", item );
const domain = new URL( item.url ).host;
if( !domains.includes( domain ) )
domains.push( domain );
if( !item.url ) continue;
try {
const domain = new URL( item.url ).host;
if( !domains.includes( domain ) )
domains.push( domain );
} catch (error) {

}

}
// console.log( domains );
for( const domain of domains )
Expand Down Expand Up @@ -544,4 +645,11 @@ async function save_data( data, key = 'checks')
// chrome.storage.local.set({key:JSON.stringify(data)});
const ret = chrome?.storage ? await storage_set( key, JSON.stringify(data) ) : window.localStorage.setItem( key, JSON.stringify(data) );
return ret;
}
}

function buildUrl(secure, domain, path) {
if (domain.startsWith('.')) {
domain = domain.substr(1);
}
return `http${secure ? 's' : ''}://${domain}${path}`;
}
Loading

0 comments on commit 6c366c5

Please sign in to comment.