Skip to content

Commit

Permalink
add basename
Browse files Browse the repository at this point in the history
  • Loading branch information
easychen committed Jan 19, 2023
1 parent 137ac5f commit 5978191
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 21 deletions.
Empty file added Docker-compose.yml
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ function cookie_decrypt( uuid, encrypted, password )
}
```

extension/function.js 查看更多
extension/function.js 查看更多
23 changes: 20 additions & 3 deletions RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@
*/
class RoboFile extends \Robo\Tasks
{
// define public methods as commands
public function buildDev()
public function apiDev()
{
$this->_exec("cd extension && npx browserslist@latest --update-db && yarn build");
$this->_exec("cd api && node_modules/nodemon/bin/nodemon.js app.js");
}

public function imagePub($uniqid = null)
{
if ($uniqid == null) {
$uniqid = date("Y.m.d.H.i");
;
}

$this->_exec("docker buildx create --use --name build-node-example --driver docker-container");
$this->_exec("docker buildx build -t easychen/cookiecloud:latest -t easychen/cookiecloud:$uniqid --platform=linux/amd64,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x --push ./docker");
$this->_exec("docker buildx rm build-node-example");

}

public function extBuild()
{
$this->_exec("cd extension && pnpm build && pnpm package");
}
}
9 changes: 4 additions & 5 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const data_dir = path.join(__dirname, 'data');
if (!fs.existsSync(data_dir)) fs.mkdirSync(data_dir);

var multer = require('multer');
var forms = multer({limits: { fieldSize: 100 * 1024 * 1024 }});
var forms = multer({limits: { fieldSize: 100*1024*1024 }});
const bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(forms.array());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({limit : 100*1024*1024 }));

app.all(`/`, (req, res) => {
res.send('Hello World!');
Expand All @@ -31,7 +32,7 @@ app.post(`/update`, (req, res) => {
}

// save encrypted to uuid file
const file_path = path.join(data_dir, uuid+'.json');
const file_path = path.join(data_dir, path.basename(uuid)+'.json');
const content = JSON.stringify({"encrypted":encrypted});
fs.writeFileSync(file_path, content);
if( fs.readFileSync(file_path) == content )
Expand All @@ -48,7 +49,7 @@ app.all(`/get/:uuid`, (req, res) => {
return;
}
// get encrypted from uuid file
const file_path = path.join(data_dir, uuid+'.json');
const file_path = path.join(data_dir, path.basename(uuid)+'.json');
if (!fs.existsSync(file_path)) {
res.status(404).send('Not Found');
return;
Expand All @@ -70,8 +71,6 @@ app.all(`/get/:uuid`, (req, res) => {
{
res.json(data);
}


}
});

Expand Down
Binary file modified design/logo.xd
Binary file not shown.
7 changes: 7 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM --platform=$TARGETPLATFORM node:16-alpine3.15
EXPOSE 8088
RUN mkdir -p /data/api
COPY app.js /data/api/app.js
COPY package.json /data/api/package.json
RUN npm install --prefix /data/api
CMD ["node", "/data/api/app.js"]
97 changes: 97 additions & 0 deletions docker/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();

const cors = require('cors');
app.use(cors());

const data_dir = path.join(__dirname, 'data');
// make dir if not exist
if (!fs.existsSync(data_dir)) fs.mkdirSync(data_dir);

var multer = require('multer');
var forms = multer({limits: { fieldSize: 100*1024*1024 }});
const bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(forms.array());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({limit : 100*1024*1024 }));

app.all(`/`, (req, res) => {
res.send('Hello World!');
});

app.post(`/update`, (req, res) => {
// {"encrypted":"123","uuid":String(short_uid.generate())}
const { encrypted, uuid } = req.body;
// none of the fields can be empty
if (!encrypted || !uuid) {
res.status(400).send('Bad Request');
return;
}

// save encrypted to uuid file
const file_path = path.join(data_dir, path.basename(uuid)+'.json');
const content = JSON.stringify({"encrypted":encrypted});
fs.writeFileSync(file_path, content);
if( fs.readFileSync(file_path) == content )
res.json({"action":"done"});
else
res.json({"action":"error"});
});

app.all(`/get/:uuid`, (req, res) => {
const { uuid } = req.params;
// none of the fields can be empty
if (!uuid) {
res.status(400).send('Bad Request');
return;
}
// get encrypted from uuid file
const file_path = path.join(data_dir, path.basename(uuid)+'.json');
if (!fs.existsSync(file_path)) {
res.status(404).send('Not Found');
return;
}
const data = JSON.parse(fs.readFileSync(file_path));
if( !data )
{
res.status(500).send('Internal Serverless Error');
return;
}
else
{
// 如果传递了password,则返回解密后的数据
if( req.body.password )
{
const parsed = cookie_decrypt( uuid, data.encrypted, req.body.password );
res.json(parsed);
}else
{
res.json(data);
}
}
});


app.use(function (err, req, res, next) {
console.error(err);
res.status(500).send('Internal Serverless Error');
});


const port = 8088;
app.listen(port, () => {
console.log(`Server start on http://localhost:${port}`);
});

function cookie_decrypt( uuid, encrypted, password )
{
const CryptoJS = require('crypto-js');
const the_key = CryptoJS.MD5(uuid+'-'+password).toString().substring(0,16);
const decrypted = CryptoJS.AES.decrypt(encrypted, the_key).toString(CryptoJS.enc.Utf8);
const parsed = JSON.parse(decrypted);
return parsed;
}

14 changes: 14 additions & 0 deletions docker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"dependencies": {
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"crypto-js": "^4.1.1",
"express": "^4.18.2",
"fs": "^0.0.1-security",
"multer": "^1.4.5-lts.1",
"path": "^0.12.7"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}
2 changes: 1 addition & 1 deletion extension/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ chrome.alarms.onAlarm.addListener( async a =>


}
});
});
6 changes: 4 additions & 2 deletions extension/background/messages/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export type RequestBody = {
}

export type ResponseBody = {
message: string
message: string,
note: string|null,
}

export const handler: PlasmoMessaging.MessageHandler<RequestBody,
Expand All @@ -16,7 +17,8 @@ ResponseBody> = async (req, res) => {
const payload = req.body.payload;
const result = (payload['type'] && payload['type'] == 'down') ? await download_cookie(payload) : await upload_cookie(payload);
res.send({
message: result['action']
message: result['action'],
note: result['note'],
})
}

8 changes: 4 additions & 4 deletions extension/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export async function upload_cookie( payload )
const endpoint = payload['endpoint'].trim().replace(/\/+$/, '')+'/update';

// get sha256 of the encrypted data
const sha256 = CryptoJS.SHA256(uuid+"-"+endpoint+"-"+JSON.stringify(cookies)).toString();
const sha256 = CryptoJS.SHA256(uuid+"-"+password+"-"+endpoint+"-"+JSON.stringify(cookies)).toString();
console.log( "sha256", sha256 );
const last_uploaded_info = await load_data( 'LAST_UPLOADED_COOKIE' );
// 如果24小时内已经上传过同样内容的数据,则不再上传
if( last_uploaded_info && last_uploaded_info.sha256 === sha256 && new Date().getTime() - last_uploaded_info.timestamp < 1000*60*60*24 )
{
console.log("same data in 24 hours, skip");
return {action:'done'};
console.log("same data in 24 hours, skip1");
return {action:'done',note:'本地Cookie数据无变动,不再上传'};
}

const payload2 = {
Expand Down Expand Up @@ -205,4 +205,4 @@ function buildUrl(secure, domain, path)
domain = domain.substr(1);
}
return `http${secure ? 's' : ''}://${domain}${path}`;
}
}
2 changes: 1 addition & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cookie-cloud",
"displayName": "CookieCloud",
"description": "__MSG_appDesc__",
"version": "0.1.2",
"version": "0.1.3",
"default_locale": "zh_CN",
"author": "easychen@gmail.com",
"license": "GPLv3",
Expand Down
8 changes: 8 additions & 0 deletions extension/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions extension/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { RadioChangeEvent } from 'antd';
import { Radio } from 'antd';

function IndexPopup() {
let init: Object={"endpoint":"http://127.0.0.1:8088","password":"123","interval":10,"domains":".jd.com","uuid":String(short_uid.generate()),"type":"up"};
let init: Object={"endpoint":"http://127.0.0.1:8088","password":"123","interval":10,"domains":"","uuid":String(short_uid.generate()),"type":"up"};
const [data, setData] = useState(init);

async function test()
Expand All @@ -20,7 +20,10 @@ function IndexPopup() {
console.log("ret888...",ret);
if( ret && ret['message'] == 'done' )
{
alert('测试成功');
if( ret['note'] )
alert(ret['note']);
else
alert('测试成功');
}else
{
alert('测试失败,请检查填写的信息是否正确');
Expand Down Expand Up @@ -90,8 +93,9 @@ function IndexPopup() {
<input type="text" className="border-1 my-2 p-2 rounded w-full" placeholder="丢失后数据失效,请妥善保管" value={data['password']} onChange={e=>onChange('password',e)}/>
<div className="">同步时间间隔·分钟</div>
<input type="number" className="border-1 my-2 p-2 rounded w-full" placeholder="最少10分钟" value={data['interval']} onChange={e=>onChange('interval',e)} />
<div className="">同步域名</div>
<textarea className="border-1 my-2 p-2 rounded w-full" style={{"height":"120px"}} placeholder="一行一个,支持.domain子域名匹配,留空默认同步全部" onChange={e=>onChange('domains',e)} value={data['domains']}/>
{data['type'] && data['type'] == 'up' && <><div className="">同步域名关键词</div>
<textarea className="border-1 my-2 p-2 rounded w-full" style={{"height":"120px"}} placeholder="一行一个,同步包含关键词的全部域名,如qq.com,jd.com会包含全部子域名,留空默认同步全部" onChange={e=>onChange('domains',e)} value={data['domains']}/>
</>}
<div className="flex flex-row justify-between mt-2">
<div className="left text-gray-400">
<Button className="hover:bg-blue-100" onClick={()=>test()}>测试</Button>
Expand Down

0 comments on commit 5978191

Please sign in to comment.