Skip to content

Commit 79bf6fa

Browse files
committed
update client libraries and samples
1 parent 3293e7a commit 79bf6fa

File tree

10 files changed

+53
-70
lines changed

10 files changed

+53
-70
lines changed

client_common/src/appguard.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {APP_ID_FILE, APP_SECRET_FILE, FIREWALL_DEFAULTS_FILE, TOKEN_FILE} from "
1111
import {AuthorizationRequest} from "./proto/appguard_commands/AuthorizationRequest";
1212
import {ClientMessage} from "./proto/appguard_commands/ClientMessage";
1313
import {ServerMessage__Output} from "./proto/appguard_commands/ServerMessage";
14-
import {FirewallDefaults} from "./proto/appguard_commands/FirewallDefaults";
14+
import {FirewallDefaults, FirewallDefaults__Output} from "./proto/appguard_commands/FirewallDefaults";
1515

1616
const opts = {includeDirs: [
1717
'node_modules/@nullnet/appguard-express/node_modules/appguard-client-common/proto/',
@@ -32,22 +32,21 @@ const fs = require('fs');
3232

3333
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
3434

35-
export type AppGuardConfig = {
36-
host: string;
37-
port: number;
38-
tls: boolean;
39-
};
40-
4135
export class AppGuardService {
4236
private client: AppGuardClient
43-
private config: AppGuardConfig
4437

45-
constructor(config: AppGuardConfig){
38+
constructor(){
39+
require('dotenv').config()
40+
let host = process.env.CONTROL_SERVICE_ADDR || '0.0.0.0'
41+
let port = process.env.CONTROL_SERVICE_PORT || '50051'
42+
let tls = false;
43+
44+
console.log(`Connecting to ${host}:${port}`);
45+
4646
this.client = new grpcObj.appguard.AppGuard(
47-
`${config.host}:${config.port}`,
48-
config.tls ? grpc.credentials.createSsl() : grpc.credentials.createInsecure()
47+
`${host}:${port}`,
48+
tls ? grpc.credentials.createSsl() : grpc.credentials.createInsecure()
4949
);
50-
this.config = config;
5150
}
5251
async onModuleInit(){
5352
return new Promise((resolve, reject) => {
@@ -100,8 +99,23 @@ export class AppGuardService {
10099
})
101100
}
102101

102+
async firewallDefaultsRequest(token: string): Promise<FirewallDefaults__Output>{
103+
let req = {
104+
token: token,
105+
};
106+
return new Promise((resolve, reject) => {
107+
this.client.firewallDefaultsRequest(req, (err, res) => {
108+
if(err){
109+
reject(err)
110+
} else {
111+
resolve(res as FirewallDefaults__Output)
112+
}
113+
})
114+
})
115+
}
116+
103117
firewallPromise = (promise: Promise<AppGuardResponse__Output>): Promise<AppGuardResponse__Output> => {
104-
let firewallDefaults: FirewallDefaults = getFirewallDefaults();
118+
let firewallDefaults: FirewallDefaults = readFirewallDefaults();
105119
let timeout = firewallDefaults.timeout;
106120
let defaultPolicy = firewallDefaults.policy;
107121
if (timeout !== undefined) {
@@ -117,7 +131,7 @@ export class AppGuardService {
117131
}
118132

119133
connectionPromise = (connection: AppGuardTcpConnection): Promise<AppGuardTcpResponse__Output> => {
120-
let firewallDefaults: FirewallDefaults = getFirewallDefaults();
134+
let firewallDefaults: FirewallDefaults = readFirewallDefaults();
121135
let timeout = firewallDefaults.timeout;
122136
let promise = this.handleTcpConnection(connection);
123137
if (timeout !== undefined) {
@@ -194,7 +208,7 @@ export class AppGuardService {
194208
}
195209
}
196210

197-
function getFirewallDefaults(): FirewallDefaults {
211+
function readFirewallDefaults(): FirewallDefaults {
198212
let text = fs.readFileSync(FIREWALL_DEFAULTS_FILE, 'utf8');
199213
return JSON.parse(text);
200214
}

client_common/src/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ export class AuthHandler {
4646
token(): string {
4747
return fs.readFileSync(TOKEN_FILE, 'utf8');
4848
}
49+
50+
writeFirewallDefaults(firewallDefaults: FirewallDefaults) {
51+
fs.writeFileSync(FIREWALL_DEFAULTS_FILE, JSON.stringify(firewallDefaults), {flag: 'w'});
52+
}
4953
}

client_common/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export {FirewallPolicy} from "./proto/appguard_commands/FirewallPolicy";
2-
export {AppGuardService, AppGuardConfig} from './appguard';
2+
export {AppGuardService} from './appguard';
33
export {AuthHandler} from './auth';
44
export {AppGuardTcpInfo} from './proto/appguard/AppGuardTcpInfo';

clients/express/sample/src/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import express from 'express'
2-
import {AppGuardConfig, createAppGuardMiddleware, FirewallPolicy} from '@nullnet/appguard-express'
2+
import {createAppGuardMiddleware, FirewallPolicy} from '@nullnet/appguard-express'
33

44
const app = express()
55

6-
const appGuardConfig: AppGuardConfig = {
7-
host: 'localhost',
8-
port: 50051,
9-
tls: false,
10-
defaultPolicy: FirewallPolicy.ALLOW,
11-
timeout: 1_000,
12-
firewall: "[]",
13-
}
14-
15-
const appGuardMiddleware = createAppGuardMiddleware(appGuardConfig)
6+
const appGuardMiddleware = createAppGuardMiddleware()
167

178
// AC #1:
189
// Able to use as direct express module

clients/express/src/express-middleware.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import { NextFunction, Request, Response, Send } from 'express';
2-
import { FirewallPolicy, AppGuardTcpInfo, AppGuardService, AuthHandler, AppGuardConfig } from 'appguard-client-common';
2+
import { FirewallPolicy, AppGuardTcpInfo, AppGuardService, AuthHandler } from 'appguard-client-common';
33

44
type ExpressMiddleware = (
55
req: Request,
66
res: Response,
77
next: NextFunction
88
) => void | Promise<void>;
99

10-
export const createAppGuardMiddleware = (config: AppGuardConfig) => {
11-
const appGuardService = new AppGuardService(config);
10+
export const createAppGuardMiddleware = () => {
11+
const appGuardService = new AppGuardService();
1212
let authHandler = new AuthHandler(appGuardService);
1313

1414
async function initialize() {
1515
await appGuardService.onModuleInit();
16-
await authHandler.init();
17-
await appGuardService.updateFirewall({
18-
// @ts-ignore
19-
token: authHandler.token(),
20-
// @ts-ignore
21-
firewall: config.firewall
22-
})
16+
await authHandler.init("ExpressJS");
17+
let fw_defaults = await appGuardService.firewallDefaultsRequest(authHandler.token());
18+
authHandler.writeFirewallDefaults(fw_defaults);
2319
}
2420
initialize();
2521

clients/express/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export {createAppGuardMiddleware} from './express-middleware';
2-
export {FirewallPolicy, AppGuardConfig} from "appguard-client-common";
2+
export {FirewallPolicy} from "appguard-client-common";

clients/nextjs/package-lock.json

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

clients/nextjs/sample/src/middleware.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import type {NextRequest} from 'next/server'
2-
import {AppGuardConfig, createAppGuardMiddleware, FirewallPolicy} from "../../src";
2+
import {createAppGuardMiddleware} from "../../src";
33

4-
const appGuardConfig: AppGuardConfig = {
5-
host: 'localhost',
6-
port: 50051,
7-
tls: false,
8-
defaultPolicy: FirewallPolicy.ALLOW,
9-
timeout: 1_000,
10-
firewall: '[{"policy": "deny", "infix_tokens": [{"type": "predicate", "condition": "contains", "http_request_url": [".php", ".env"]}]}]',
11-
}
12-
13-
let appGuardMiddleware = await createAppGuardMiddleware(appGuardConfig);
4+
let appGuardMiddleware = await createAppGuardMiddleware();
145

156
export default async function middleware(request: NextRequest) {
167
return await appGuardMiddleware(request);

clients/nextjs/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export {createAppGuardMiddleware} from './nextjs-middleware';
22
export {FirewallPolicy} from "appguard-client-common";
3-
export type { AppGuardConfig } from "appguard-client-common";

clients/nextjs/src/nextjs-middleware.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import {NextRequest, NextResponse} from 'next/server';
2-
import { FirewallPolicy, AppGuardTcpInfo, AppGuardService, AuthHandler, AppGuardConfig } from 'appguard-client-common';
2+
import { FirewallPolicy, AppGuardTcpInfo, AppGuardService, AuthHandler } from 'appguard-client-common';
33

44
type NextjsMiddleware = (req: NextRequest) => Promise<NextResponse>;
55

6-
export const createAppGuardMiddleware = async (config: AppGuardConfig) => {
7-
const appGuardService = new AppGuardService(config);
6+
export const createAppGuardMiddleware = async () => {
7+
const appGuardService = new AppGuardService();
88
let authHandler = new AuthHandler(appGuardService);
99

1010
async function initialize() {
1111
await appGuardService.onModuleInit();
12-
await authHandler.init();
13-
await appGuardService.updateFirewall({
14-
token: authHandler.token(),
15-
firewall: config.firewall
16-
})
12+
await authHandler.init("NextJS");
13+
let fw_defaults = await appGuardService.firewallDefaultsRequest(authHandler.token());
14+
authHandler.writeFirewallDefaults(fw_defaults);
1715
}
1816
await initialize();
1917

0 commit comments

Comments
 (0)