transfer-encryption

This commit is contained in:
King Wang 2021-06-29 22:18:47 +08:00
parent 8f86b7da96
commit 36a702ae9e
25 changed files with 750 additions and 0 deletions

View File

@ -0,0 +1,3 @@
node_modules
dist
.DS_STORE

View File

@ -0,0 +1,30 @@
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "mocha current file",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"${file}"
],
"internalConsoleOptions": "openOnSessionStart",
"cwd": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "ts-node current file",
"protocol": "inspector",
"args": [
"${relativeFile}"
],
"cwd": "${workspaceRoot}",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"internalConsoleOptions": "openOnSessionStart"
}
]
}

View File

@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
}

View File

@ -0,0 +1,35 @@
# TSRPC Server
## Run
### Local Dev Server
```
npm run dev
```
### Build
```
npm run build
```
---
## Files
### Generate ServiceProto
```
npm run proto
```
### Generate API templates
```
npm run api
```
### Sync shared code to client
```
npm run sync
```
> If you chose symlink when using `create-tsrpc-app`, it would re-create the symlink instead of copy files.

View File

@ -0,0 +1,23 @@
{
"name": "transfer-encryption-backend",
"version": "0.1.0",
"main": "index.js",
"private": true,
"scripts": {
"proto": "tsrpc proto -i src/shared/protocols -o src/shared/protocols/serviceProto.ts",
"sync": "tsrpc sync --from src/shared --to ../frontend/src/shared",
"api": "tsrpc api -i src/shared/protocols/serviceProto.ts -o src/api",
"dev": "onchange \"src/**/*.ts\" -i -k -- ts-node \"src/index.ts\"",
"build": "tsrpc build"
},
"devDependencies": {
"@types/node": "^15.12.5",
"onchange": "^7.1.0",
"ts-node": "^10.0.0",
"tsrpc-cli": "^2.0.3",
"typescript": "^4.3.4"
},
"dependencies": {
"tsrpc": "^3.0.4"
}
}

View File

@ -0,0 +1,26 @@
import { ApiCall } from "tsrpc";
import { ReqAddData, ResAddData } from "../shared/protocols/PtlAddData";
import { AllData } from "./ApiGetData";
// This is a demo code file
// Feel free to delete it
export async function ApiAddData(call: ApiCall<ReqAddData, ResAddData>) {
// Error
if (call.req.content === '') {
call.error('Content is empty');
return;
}
let time = new Date();
AllData.unshift({
content: call.req.content,
time: time
})
console.log('AllData', AllData)
// Success
call.succ({
time: time
});
}

View File

@ -0,0 +1,13 @@
import { ApiCall } from "tsrpc";
import { ReqGetData, ResGetData } from "../shared/protocols/PtlGetData";
// This is a demo code file
// Feel free to delete it
export async function ApiGetData(call: ApiCall<ReqGetData, ResGetData>) {
call.succ({
data: AllData
})
}
export const AllData: { content: string, time: Date }[] = [];

View File

@ -0,0 +1,39 @@
import * as path from "path";
import { HttpServer } from "tsrpc";
import { EncryptUtil } from "./shared/models/EncryptUtil";
import { serviceProto } from "./shared/protocols/serviceProto";
// Create the Server
const server = new HttpServer(serviceProto, {
port: 3000,
cors: '*',
debugBuf: true
});
// Encrypt
server.flows.preSendBufferFlow.push(v => {
v.buf = EncryptUtil.encrypt(v.buf);
return v;
});
// Decrypt
server.flows.preRecvBufferFlow.push(v => {
v.buf = EncryptUtil.decrypt(v.buf);
return v;
})
// Entry function
async function main() {
// Auto implement APIs
await server.autoImplementApi(path.resolve(__dirname, 'api'));
// TODO
// Prepare something... (e.g. connect the db)
await server.start();
};
main().catch(e => {
// Exit if any error during the startup
server.logger.error(e);
process.exit(-1);
});

View File

@ -0,0 +1,17 @@
export class EncryptUtil {
static encrypt(buf: Uint8Array): Uint8Array {
for (let i = 0; i < buf.length; ++i) {
buf[i] -= 1;
}
return buf;
}
static decrypt(buf: Uint8Array): Uint8Array {
for (let i = 0; i < buf.length; ++i) {
buf[i] += 1;
}
return buf;
}
}

View File

@ -0,0 +1,10 @@
// This is a demo code file
// Feel free to delete it
export interface ReqAddData {
content: string;
}
export interface ResAddData {
time: Date
}

View File

@ -0,0 +1,13 @@
// This is a demo code file
// Feel free to delete it
export interface ReqGetData {
}
export interface ResGetData {
data: {
content: string,
time: Date
}[]
}

View File

@ -0,0 +1,98 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqAddData, ResAddData } from './PtlAddData';
import { ReqGetData, ResGetData } from './PtlGetData';
// This is a demo service proto file (auto generated)
// Feel free to delete it
export interface ServiceType {
api: {
"AddData": {
req: ReqAddData,
res: ResAddData
},
"GetData": {
req: ReqGetData,
res: ResGetData
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 1,
"services": [
{
"id": 0,
"name": "AddData",
"type": "api"
},
{
"id": 1,
"name": "GetData",
"type": "api"
}
],
"types": {
"PtlAddData/ReqAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
}
]
},
"PtlAddData/ResAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"PtlGetData/ReqGetData": {
"type": "Interface"
},
"PtlGetData/ResGetData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "data",
"type": {
"type": "Array",
"elementType": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
}
}
]
}
}
};

View File

@ -0,0 +1,18 @@
{
"compilerOptions": {
"lib": [
"es2018"
],
"module": "commonjs",
"target": "es2018",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node"
},
"include": [
"src"
]
}

View File

@ -0,0 +1,3 @@
.DS_STORE
node_modules
dist

View File

@ -0,0 +1,24 @@
{
"name": "transfer-encryption-frontend",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "webpack serve --mode=development --open",
"build": "webpack --mode=production"
},
"devDependencies": {
"copy-webpack-plugin": "^9.0.1",
"html-webpack-plugin": "^5.3.2",
"ts-loader": "^9.2.3",
"typescript": "^4.3.4",
"webpack": "^5.41.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"tsrpc-browser": "^3.0.4"
},
"browserslist": [
"defaults"
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,83 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
text-align: center;
}
body>* {
margin: 20px auto;
}
.send,
.list {
width: 370px;
}
.send>* {
display: block;
width: 100%;
margin: 10px auto;
padding: 10px;
border-radius: 5px;
font-size: 16px;
border: none;
outline: none;
}
.send>textarea {
height: 80px;
background: #f7f7f7;
border: #eeeeee 1px solid;
}
.send>textarea:focus {
background: #fff;
border-color: #ccc;
}
.send>button {
background: #215fa4;
color: white;
cursor: pointer;
}
.send>button:hover {
background: #4b80bb;
}
.list {
list-style: none;
border-radius: 5px;
padding: 10px;
background: #f2f2f2;
}
.list>li {
margin-bottom: 10px;
padding: 10px;
background: #fff;
line-height: 1.5em;
border-radius: 5px;
}
.list>li>.content {
font-size: 14px;
text-align: left;
white-space: pre-wrap;
word-wrap: break-word;
}
.list>li>.time {
font-size: 12px;
color: #4b80bb;
text-align: right;
}
.list>li:last-child {
border-bottom: none;
margin-bottom: 0;
}

View File

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TSRPC Browser</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<h1>TSRPC Guestbook</h1>
<div class="send">
<textarea placeholder="Say something..."></textarea>
<button>Send</button>
</div>
<ul class="list">
<!-- <li>
<div class="content">Hello, World</div>
<div class="time">11:23:23</div>
</li> -->
</ul>
</body>
</html>

View File

@ -0,0 +1,67 @@
import { HttpClient } from 'tsrpc-browser';
import { EncryptUtil } from './shared/models/EncryptUtil';
import { serviceProto } from './shared/protocols/serviceProto';
// Create Client
let client = new HttpClient(serviceProto, {
server: 'http://127.0.0.1:3000',
logger: console,
debugBuf: true
});
// Encrypt
client.flows.preSendBufferFlow.push(v => {
v.buf = EncryptUtil.encrypt(v.buf);
return v;
});
// Decrypt
client.flows.preRecvBufferFlow.push(v => {
v.buf = EncryptUtil.decrypt(v.buf);
return v;
})
// Reload message list
async function loadList() {
let ret = await client.callApi('GetData', {});
// Error
if (!ret.isSucc) {
alert(ret.err.message);
return;
}
// Success
const list = document.querySelector('.list')!;
list.innerHTML = '';
ret.res.data.forEach(v => {
let li = document.createElement('li');
li.innerHTML = `<div class="content"></div><div class="time"></div>`;
(li.querySelector('.content') as HTMLDivElement).innerText = v.content;
(li.querySelector('.time') as HTMLDivElement).innerText = v.time.toLocaleTimeString();
list.appendChild(li);
})
}
// Send Message
async function send() {
const textarea = document.querySelector('.send>textarea') as HTMLTextAreaElement;
let ret = await client.callApi('AddData', {
content: textarea.value
});
// Error
if (!ret.isSucc) {
alert(ret.err.message);
return;
}
// Success
textarea.value = '';
loadList();
}
// Bind Events
(document.querySelector('.send>button') as HTMLButtonElement).onclick = send;
// Load list after page load
loadList();

View File

@ -0,0 +1,17 @@
export class EncryptUtil {
static encrypt(buf: Uint8Array): Uint8Array {
for (let i = 0; i < buf.length; ++i) {
buf[i] -= 1;
}
return buf;
}
static decrypt(buf: Uint8Array): Uint8Array {
for (let i = 0; i < buf.length; ++i) {
buf[i] += 1;
}
return buf;
}
}

View File

@ -0,0 +1,10 @@
// This is a demo code file
// Feel free to delete it
export interface ReqAddData {
content: string;
}
export interface ResAddData {
time: Date
}

View File

@ -0,0 +1,13 @@
// This is a demo code file
// Feel free to delete it
export interface ReqGetData {
}
export interface ResGetData {
data: {
content: string,
time: Date
}[]
}

View File

@ -0,0 +1,98 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqAddData, ResAddData } from './PtlAddData';
import { ReqGetData, ResGetData } from './PtlGetData';
// This is a demo service proto file (auto generated)
// Feel free to delete it
export interface ServiceType {
api: {
"AddData": {
req: ReqAddData,
res: ResAddData
},
"GetData": {
req: ReqGetData,
res: ResGetData
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 1,
"services": [
{
"id": 0,
"name": "AddData",
"type": "api"
},
{
"id": 1,
"name": "GetData",
"type": "api"
}
],
"types": {
"PtlAddData/ReqAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
}
]
},
"PtlAddData/ResAddData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"PtlGetData/ReqGetData": {
"type": "Interface"
},
"PtlGetData/ResGetData": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "data",
"type": {
"type": "Array",
"elementType": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
}
}
}
]
}
}
};

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"lib": [
"dom",
"es2015"
],
"module": "esnext",
"target": "es5",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"outDir": "dist",
"skipLibCheck": true,
"strict": true,
"jsx": "react-jsx",
"sourceMap": true,
"isolatedModules": true
},
"include": [
"src"
]
}

View File

@ -0,0 +1,56 @@
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const isProduction = process.argv.indexOf('--mode=production') > -1;
module.exports = {
entry: './src/index.ts',
output: {
filename: 'bundle.[contenthash].js',
path: path.resolve(__dirname, 'dist'),
clean: true
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.mjs', '.cjs']
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [{
loader: 'ts-loader',
options: {
// Compile to ES5 in production mode for better compatibility
// Compile to ES2018 in development for better debugging (like async/await)
compilerOptions: !isProduction ? {
"target": "es2018",
} : undefined
}
}],
exclude: /node_modules/
},
]
},
plugins: [
// Copy "public" to "dist"
new CopyWebpackPlugin({
patterns: [{
from: 'public',
to: '.',
toType: 'dir',
globOptions: {
gitignore: true,
ignore: [path.resolve(__dirname, 'public/index.html').replace(/\\/g, '/')]
},
noErrorOnMissing: true
}]
}),
// Auto add <script> to "index.html"
new HtmlWebpackPlugin({
template: 'public/index.html'
}),
],
devtool: isProduction ? false : 'inline-source-map'
}