Skip to content

Commit f8c7407

Browse files
committed
fix cache serialization and deserialization
1 parent 2f433bd commit f8c7407

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ app_id.txt
88
app_secret.txt
99
firewall_defaults.json
1010
uuid.txt
11+
cache.txt
1112
.env
1213
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
1314

client_common/src/appguard.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {AuthorizationRequest} from "./proto/appguard_commands/AuthorizationReque
1212
import {ClientMessage} from "./proto/appguard_commands/ClientMessage";
1313
import {ServerMessage__Output} from "./proto/appguard_commands/ServerMessage";
1414
import {FirewallDefaults, FirewallDefaults__Output} from "./proto/appguard_commands/FirewallDefaults";
15-
import {Cache, CacheKey} from "./cache";
15+
import {CacheKey} from "./cache";
1616
import {FirewallPolicy} from "./proto/appguard_commands/FirewallPolicy";
1717

1818
const opts = {includeDirs: [
@@ -189,8 +189,8 @@ export class AppGuardService {
189189
console.log("Received firewall defaults from server:", firewallDefaults);
190190
fs.writeFileSync(FIREWALL_DEFAULTS_FILE, JSON.stringify(firewallDefaults), {flag: 'w'});
191191
// empty and update cache
192-
let cache = new Cache(firewallDefaults.cache);
193-
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache), {flag: 'w'});
192+
let cache: Map<string, FirewallPolicy> = new Map([]);
193+
writeCache(cache);
194194
}
195195
if (server_msg.deviceDeauthorized) {
196196
// delete saved app secret and app id
@@ -213,28 +213,47 @@ export class AppGuardService {
213213
}
214214

215215
getFromCache(key: CacheKey) : FirewallPolicy | undefined {
216-
let cache = readCache();
217-
return cache.get(key);
216+
let defaults = readFirewallDefaults();
217+
if (defaults.cache) {
218+
let cache = readCache();
219+
return cache.get(JSON.stringify(key));
220+
}
218221
}
219222

220223
insertToCache(key: CacheKey, policy: FirewallPolicy) {
221-
let cache = readCache();
222-
cache.insert(key, policy);
223-
fs.writeFileSync(CACHE_FILE, JSON.stringify(cache), {flag: 'w'});
224+
let defaults = readFirewallDefaults();
225+
if (defaults.cache) {
226+
let cache = readCache();
227+
cache.set(JSON.stringify(key), policy);
228+
writeCache(cache);
229+
}
224230
}
225231
}
226232

227233
function readFirewallDefaults(): FirewallDefaults {
228234
let text = fs.readFileSync(FIREWALL_DEFAULTS_FILE, 'utf8');
229-
return JSON.parse(text);
235+
if (text.trim() === '') {
236+
return {
237+
policy: FirewallPolicy.ALLOW,
238+
timeout: 1000,
239+
cache: true,
240+
};
241+
} else {
242+
return JSON.parse(text);
243+
}
230244
}
231245

232-
function readCache(): Cache {
246+
function readCache(): Map<string, FirewallPolicy> {
233247
let text = fs.readFileSync(CACHE_FILE, 'utf8');
234248
if (text.trim() === '') {
235-
let firewallDefaults = readFirewallDefaults();
236-
return new Cache(firewallDefaults.cache);
249+
return new Map([]);
237250
} else {
238-
return JSON.parse(text);
251+
let map: Map<string, FirewallPolicy> = new Map(JSON.parse(text))
252+
return map;
239253
}
240254
}
255+
256+
function writeCache(cache: Map<string, FirewallPolicy>) {
257+
let mySerialMap = JSON.stringify(Array.from(cache.entries()));
258+
fs.writeFileSync(CACHE_FILE, mySerialMap, {flag: 'w'});
259+
}

client_common/src/auth.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import {AuthorizationRequest} from "./proto/appguard_commands/AuthorizationReque
33
import {FirewallDefaults} from "./proto/appguard_commands/FirewallDefaults";
44
import { v4 as uuidv4 } from 'uuid';
55

6-
export const TOKEN_FILE = process.cwd() + '/../token.txt'
7-
export const APP_ID_FILE = process.cwd() + '/../app_id.txt'
8-
export const APP_SECRET_FILE = process.cwd() + '/../app_secret.txt'
9-
export const FIREWALL_DEFAULTS_FILE = process.cwd() + '/../firewall_defaults.json'
10-
export const UUID_FILE = process.cwd() + '/../uuid.txt'
11-
export const CACHE_FILE = process.cwd() + '/../cache.txt'
6+
export const TOKEN_FILE = process.cwd() + '/token.txt'
7+
export const APP_ID_FILE = process.cwd() + '/app_id.txt'
8+
export const APP_SECRET_FILE = process.cwd() + '/app_secret.txt'
9+
export const FIREWALL_DEFAULTS_FILE = process.cwd() + '/firewall_defaults.json'
10+
export const UUID_FILE = process.cwd() + '/uuid.txt'
11+
export const CACHE_FILE = process.cwd() + '/cache.txt'
1212

1313
const fs = require('fs');
1414

@@ -33,6 +33,10 @@ export class AuthHandler {
3333

3434
// empty token file content
3535
fs.writeFileSync(TOKEN_FILE, '', {flag: 'w'});
36+
// empty cache file content
37+
fs.writeFileSync(CACHE_FILE, '', {flag: 'w'});
38+
// empty firewall defaults file content
39+
fs.writeFileSync(FIREWALL_DEFAULTS_FILE, '', {flag: 'w'});
3640
}
3741

3842
async init(type: string){

client_common/src/cache.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,4 @@ export class CacheKey {
77
sourceIp: string;
88
userAgent: string;
99
query: Record<string, string>;
10-
}
11-
12-
export class Cache {
13-
private active: boolean;
14-
private cache: Map<CacheKey, FirewallPolicy>;
15-
16-
constructor(active: boolean) {
17-
this.active = active;
18-
this.cache = new Map<CacheKey, FirewallPolicy>();
19-
}
20-
21-
get(key: CacheKey): FirewallPolicy | undefined {
22-
if (this.active) {
23-
return this.cache.get(key);
24-
} else {
25-
return undefined;
26-
}
27-
}
28-
29-
insert(key: CacheKey, policy: FirewallPolicy) {
30-
if (this.active) {
31-
this.cache.set(key, policy);
32-
}
33-
}
3410
}

0 commit comments

Comments
 (0)