Skip to content

Commit 0fa435b

Browse files
committed
split Express and Next.js clients
1 parent 735d62f commit 0fa435b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+14812
-22922
lines changed

client_common/package-lock.json

Lines changed: 13321 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client_common/package.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"version": "0.1.0",
3+
"license": "AGPL-3.0-only",
4+
"main": "dist/index.js",
5+
"typings": "dist/index.d.ts",
6+
"files": [
7+
"dist",
8+
"src",
9+
"proto"
10+
],
11+
"engines": {
12+
"node": ">=9"
13+
},
14+
"scripts": {
15+
"start": "tsdx watch",
16+
"build": "tsdx build --format esm",
17+
"test": "tsdx test",
18+
"lint": "tsdx lint",
19+
"prepare": "./proto-gen.sh & tsdx build",
20+
"size": "size-limit",
21+
"analyze": "size-limit --why"
22+
},
23+
"dependencies": {
24+
"@grpc/grpc-js": "^1.3.2",
25+
"@grpc/proto-loader": "^0.6.2"
26+
},
27+
"husky": {
28+
"hooks": {
29+
"pre-commit": "tsdx lint"
30+
}
31+
},
32+
"prettier": {
33+
"printWidth": 80,
34+
"semi": true,
35+
"singleQuote": true,
36+
"trailingComma": "es5"
37+
},
38+
"name": "@nullnet/appguard-client-common",
39+
"author": "Giuliano Bellini s294739",
40+
"module": "dist/appguard-client-common.esm.js",
41+
"size-limit": [
42+
{
43+
"path": "dist/appguard-client-common.cjs.production.min.js",
44+
"limit": "10 KB"
45+
},
46+
{
47+
"path": "dist/appguard-client-common.esm.js",
48+
"limit": "10 KB"
49+
}
50+
],
51+
"devDependencies": {
52+
"@size-limit/preset-small-lib": "^11.1.4",
53+
"husky": "^9.1.5",
54+
"size-limit": "^11.1.4",
55+
"tsdx": "^0.14.1",
56+
"tslib": "^2.7.0",
57+
"typescript": "^5.5.4"
58+
},
59+
"publishConfig": {
60+
"registry": "https://npm.nullnet.ai"
61+
}
62+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22
rm -rf src/proto
3-
./node_modules/.bin/proto-loader-gen-types --grpcLib=@grpc/grpc-js --outDir=src/proto/ ./proto/appguard.proto
3+
./node_modules/.bin/proto-loader-gen-types --grpcLib=@grpc/grpc-js --outDir=./src/proto/ ./proto/appguard.proto
File renamed without changes.

clients/nextjs/src/app-guard-nextjs.ts renamed to client_common/src/appguard.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import path from 'path'
21
import * as grpc from '@grpc/grpc-js'
32
import * as protoLoader from '@grpc/proto-loader'
43
import type {ProtoGrpcType} from './proto/appguard'
@@ -13,6 +12,7 @@ import {HeartbeatResponse__Output} from "./proto/appguard/HeartbeatResponse";
1312
import {DeviceStatus} from "./proto/appguard/DeviceStatus";
1413
import {TOKEN_FILE} from "./auth";
1514
import {AppGuardFirewall, AppGuardFirewall__Output} from "./proto/appguard/AppGuardFirewall";
15+
import {FirewallPolicy} from "./proto/appguard/FirewallPolicy";
1616

1717
const PROTO_FILE = process.cwd() + '/../proto/appguard.proto'
1818
const packageDef = protoLoader.loadSync(PROTO_FILE)
@@ -28,13 +28,25 @@ const grpcObj = (grpc.loadPackageDefinition(packageDef) as unknown) as ProtoGrpc
2828

2929
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
3030

31+
export type AppGuardConfig = {
32+
host: string;
33+
port: number;
34+
tls: boolean;
35+
timeout?: number;
36+
defaultPolicy: FirewallPolicy;
37+
firewall: string;
38+
};
39+
3140
export class AppGuardService {
3241
private client: AppGuardClient
33-
constructor(host: string, port: number, tls: boolean){
42+
private config: AppGuardConfig
43+
44+
constructor(config: AppGuardConfig){
3445
this.client = new grpcObj.appguard.AppGuard(
35-
`${host}:${port}`,
36-
tls ? grpc.credentials.createSsl() : grpc.credentials.createInsecure()
37-
)
46+
`${config.host}:${config.port}`,
47+
config.tls ? grpc.credentials.createSsl() : grpc.credentials.createInsecure()
48+
);
49+
this.config = config;
3850
}
3951
async onModuleInit(){
4052
return new Promise((resolve, reject) => {
@@ -62,6 +74,7 @@ export class AppGuardService {
6274
})
6375
})
6476
}
77+
6578
async handleHttpResponse(req: AppGuardHttpResponse): Promise<AppGuardResponse__Output>{
6679
return new Promise((resolve, reject) => {
6780
this.client.handleHttpResponse(req, (err, res) => {
@@ -73,6 +86,7 @@ export class AppGuardService {
7386
})
7487
})
7588
}
89+
7690
async handleTcpConnection(req: AppGuardTcpConnection): Promise<AppGuardTcpResponse__Output>{
7791
return new Promise((resolve, reject) => {
7892
this.client.handleTcpConnection(req, (err, res) => {
@@ -84,6 +98,36 @@ export class AppGuardService {
8498
})
8599
})
86100
}
101+
102+
firewallPromise = (promise: Promise<AppGuardResponse__Output>): Promise<AppGuardResponse__Output> => {
103+
if (this.config.timeout !== undefined) {
104+
let timeoutPromise: Promise<AppGuardResponse__Output> = new Promise((resolve, _reject) => {
105+
setTimeout(resolve, this.config.timeout, {
106+
policy: this.config.defaultPolicy
107+
})
108+
});
109+
return Promise.race([promise, timeoutPromise])
110+
} else {
111+
return promise
112+
}
113+
}
114+
115+
connectionPromise = (connection: AppGuardTcpConnection): Promise<AppGuardTcpResponse__Output> => {
116+
let promise = this.handleTcpConnection(connection);
117+
if (this.config.timeout !== undefined) {
118+
let timeoutPromise: Promise<AppGuardTcpResponse__Output> = new Promise((resolve, _reject) => {
119+
setTimeout(resolve, this.config.timeout, {
120+
tcpInfo: {
121+
connection: connection,
122+
}
123+
})
124+
});
125+
return Promise.race([promise, timeoutPromise])
126+
} else {
127+
return promise
128+
}
129+
}
130+
87131
heartbeat(req: HeartbeatRequest) {
88132
let call = this.client.heartbeat(req);
89133
call.on('data', function(heartbeat: HeartbeatResponse__Output) {
@@ -108,6 +152,7 @@ export class AppGuardService {
108152
}, 10000);
109153
});
110154
}
155+
111156
async updateFirewall(req: AppGuardFirewall): Promise<AppGuardFirewall__Output>{
112157
return new Promise((resolve, reject) => {
113158
this.client.updateFirewall(req, (err, res) => {

clients/nextjs/src/auth.ts renamed to client_common/src/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AppGuardService} from "./app-guard-nextjs";
1+
import {AppGuardService} from "./appguard";
22
import {HeartbeatRequest} from "./proto/appguard/HeartbeatRequest";
33

44
export const TOKEN_FILE = process.cwd() + '/../token.txt'

client_common/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export {FirewallPolicy} from "./proto/appguard/FirewallPolicy";
2+
export {AppGuardService, AppGuardConfig} from './appguard';
3+
export {AuthHandler} from './auth';
4+
export {AppGuardTcpInfo} from './proto/appguard/AppGuardTcpInfo';
File renamed without changes.

0 commit comments

Comments
 (0)