Skip to content

Commit 3293e7a

Browse files
committed
completing client_common library
1 parent 2da1db5 commit 3293e7a

File tree

2 files changed

+72
-56
lines changed

2 files changed

+72
-56
lines changed

client_common/src/appguard.ts

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ import {AppGuardResponse__Output} from './proto/appguard/AppGuardResponse'
77
import {AppGuardTcpConnection} from './proto/appguard/AppGuardTcpConnection'
88
import {AppGuardHttpResponse} from './proto/appguard/AppGuardHttpResponse'
99
import {AppGuardTcpResponse__Output} from "./proto/appguard/AppGuardTcpResponse";
10-
import {TOKEN_FILE} from "./auth";
11-
import {AppGuardFirewall, AppGuardFirewall__Output} from "./proto/appguard/AppGuardFirewall";
12-
import {FirewallPolicy} from "./proto/appguard/FirewallPolicy";
10+
import {APP_ID_FILE, APP_SECRET_FILE, FIREWALL_DEFAULTS_FILE, TOKEN_FILE} from "./auth";
1311
import {AuthorizationRequest} from "./proto/appguard_commands/AuthorizationRequest";
1412
import {ClientMessage} from "./proto/appguard_commands/ClientMessage";
1513
import {ServerMessage__Output} from "./proto/appguard_commands/ServerMessage";
14+
import {FirewallDefaults} from "./proto/appguard_commands/FirewallDefaults";
1615

1716
const opts = {includeDirs: [
1817
'node_modules/@nullnet/appguard-express/node_modules/appguard-client-common/proto/',
@@ -21,6 +20,8 @@ const opts = {includeDirs: [
2120
const packageDef = protoLoader.loadSync('appguard.proto', opts);
2221
const grpcObj = (grpc.loadPackageDefinition(packageDef) as unknown) as ProtoGrpcType
2322

23+
const fs = require('fs');
24+
2425
// it doesn't work with .cer files, convert them to .pem with the following command:
2526
// openssl x509 -inform der -in ca.cer -out ca.pem
2627

@@ -35,9 +36,6 @@ export type AppGuardConfig = {
3536
host: string;
3637
port: number;
3738
tls: boolean;
38-
timeout?: number;
39-
defaultPolicy: FirewallPolicy;
40-
firewall: string;
4139
};
4240

4341
export class AppGuardService {
@@ -103,10 +101,13 @@ export class AppGuardService {
103101
}
104102

105103
firewallPromise = (promise: Promise<AppGuardResponse__Output>): Promise<AppGuardResponse__Output> => {
106-
if (this.config.timeout !== undefined) {
104+
let firewallDefaults: FirewallDefaults = getFirewallDefaults();
105+
let timeout = firewallDefaults.timeout;
106+
let defaultPolicy = firewallDefaults.policy;
107+
if (timeout !== undefined) {
107108
let timeoutPromise: Promise<AppGuardResponse__Output> = new Promise((resolve, _reject) => {
108-
setTimeout(resolve, this.config.timeout, {
109-
policy: this.config.defaultPolicy
109+
setTimeout(resolve, timeout, {
110+
policy: defaultPolicy
110111
})
111112
});
112113
return Promise.race([promise, timeoutPromise])
@@ -116,10 +117,12 @@ export class AppGuardService {
116117
}
117118

118119
connectionPromise = (connection: AppGuardTcpConnection): Promise<AppGuardTcpResponse__Output> => {
120+
let firewallDefaults: FirewallDefaults = getFirewallDefaults();
121+
let timeout = firewallDefaults.timeout;
119122
let promise = this.handleTcpConnection(connection);
120-
if (this.config.timeout !== undefined) {
123+
if (timeout !== undefined) {
121124
let timeoutPromise: Promise<AppGuardTcpResponse__Output> = new Promise((resolve, _reject) => {
122-
setTimeout(resolve, this.config.timeout, {
125+
setTimeout(resolve, timeout, {
123126
tcpInfo: {
124127
connection: connection,
125128
}
@@ -139,24 +142,47 @@ export class AppGuardService {
139142

140143
call.on('data', function(server_msg: ServerMessage__Output) {
141144
if (server_msg.deviceAuthorized) {
145+
// save app secret and app id (if defined)
146+
let auth_data = server_msg.deviceAuthorized;
147+
if (auth_data.appId) {
148+
fs.writeFileSync(APP_ID_FILE, auth_data.appId, {flag: 'w'});
149+
}
150+
if (auth_data.appSecret) {
151+
fs.writeFileSync(APP_SECRET_FILE, auth_data.appSecret, {flag: 'w'});
152+
}
142153

143-
} else if (server_msg.updateTokenCommand) {
144-
145-
} else if (server_msg.setFirewallDefaults) {
154+
// read app id and app secret from files
155+
let appId = fs.readFileSync(APP_ID_FILE, 'utf8').trim();
156+
let appSecret = fs.readFileSync(APP_SECRET_FILE, 'utf8').trim();
146157

158+
// send authenticate
159+
let auth: ClientMessage = {authentication: {
160+
appId: appId,
161+
appSecret: appSecret,
162+
}};
163+
call.write(auth);
164+
}
165+
if (server_msg.updateTokenCommand) {
166+
// save token
167+
let token = server_msg.updateTokenCommand;
168+
fs.writeFileSync(TOKEN_FILE, token, {flag: 'w'});
147169
}
148-
// handle the heartbeat response
149-
console.log("Received heartbeat from server");
150-
// write token to file
151-
const fs = require('fs');
152-
fs.writeFileSync(TOKEN_FILE, heartbeat.token, {flag: 'w'});
153-
let status = heartbeat.status;
154-
if (status == DeviceStatus.ARCHIVED || status == DeviceStatus.DELETED) {
155-
// terminate current process
156-
console.log("Device is archived or deleted, terminating process");
157-
process.exit(0);
170+
if (server_msg.setFirewallDefaults) {
171+
// save firewall defaults
172+
let firewallDefaults: FirewallDefaults = server_msg.setFirewallDefaults;
173+
console.log("Received firewall defaults from server:", firewallDefaults);
174+
fs.writeFileSync(FIREWALL_DEFAULTS_FILE, JSON.stringify(firewallDefaults), {flag: 'w'});
175+
}
176+
if (server_msg.deviceDeauthorized) {
177+
// delete saved app secret and app id
178+
fs.writeFileSync(APP_ID_FILE, '', {flag: 'w'});
179+
fs.writeFileSync(APP_SECRET_FILE, '', {flag: 'w'});
180+
}
181+
if (server_msg.heartbeat) {
182+
console.log("Received heartbeat from server");
158183
}
159184
});
185+
160186
call.on('error', (_e) => {
161187
// An error has occurred and the stream has been closed.
162188
// sleep for 10 seconds and try again
@@ -166,16 +192,9 @@ export class AppGuardService {
166192
}, 10000);
167193
});
168194
}
195+
}
169196

170-
async updateFirewall(req: AppGuardFirewall): Promise<AppGuardFirewall__Output>{
171-
return new Promise((resolve, reject) => {
172-
this.client.updateFirewall(req, (err, res) => {
173-
if(err){
174-
reject(err)
175-
} else {
176-
resolve(res as AppGuardFirewall__Output)
177-
}
178-
})
179-
})
180-
}
197+
function getFirewallDefaults(): FirewallDefaults {
198+
let text = fs.readFileSync(FIREWALL_DEFAULTS_FILE, 'utf8');
199+
return JSON.parse(text);
181200
}

client_common/src/auth.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
import {AppGuardService} from "./appguard";
2-
import {HeartbeatRequest} from "./proto/appguard/HeartbeatRequest";
2+
import {AuthorizationRequest} from "./proto/appguard_commands/AuthorizationRequest";
3+
import {FirewallDefaults} from "./proto/appguard_commands/FirewallDefaults";
34

45
export const TOKEN_FILE = process.cwd() + '/../token.txt'
6+
export const APP_ID_FILE = process.cwd() + '/../app_id.txt'
7+
export const APP_SECRET_FILE = process.cwd() + '/../app_secret.txt'
8+
export const FIREWALL_DEFAULTS_FILE = process.cwd() + '/../firewall_defaults.json'
9+
10+
const fs = require('fs');
511

612
export class AuthHandler {
7-
private app_id: string
8-
private app_secret: string
13+
private installation_code: string
914
private client: AppGuardService
1015

1116
constructor(client: AppGuardService) {
1217
require('dotenv').config()
1318

14-
this.app_id = process.env.APP_ID || ''
15-
this.app_secret = process.env.APP_SECRET || ''
19+
this.installation_code = process.env.INSTALLATION_CODE || ''
1620
this.client = client
1721

18-
if (this.app_id === '') {
19-
console.log('APP_ID environment variable is not set')
20-
process.exit(1)
21-
}
22-
if (this.app_secret === '') {
23-
console.log('APP_SECRET environment variable is not set')
24-
process.exit(1)
25-
}
26-
2722
// empty token file content
28-
const fs = require('fs');
2923
fs.writeFileSync(TOKEN_FILE, '', {flag: 'w'});
3024
}
3125

32-
async init(){
33-
let hb_req: HeartbeatRequest = {
34-
appId: this.app_id,
35-
appSecret: this.app_secret,
26+
async init(type: string){
27+
28+
let req: AuthorizationRequest = {
29+
uuid: "",
30+
code: this.installation_code,
31+
category: "AppGuard Client",
32+
targetOs: undefined,
33+
type: type,
3634
};
3735

38-
this.client.control_stream(hb_req);
36+
this.client.control_stream(req);
3937

4038
console.log("Waiting for the first server heartbeat...");
4139
while (this.token() === '') {
@@ -46,7 +44,6 @@ export class AuthHandler {
4644
}
4745

4846
token(): string {
49-
const fs = require('fs');
5047
return fs.readFileSync(TOKEN_FILE, 'utf8');
5148
}
5249
}

0 commit comments

Comments
 (0)