@@ -7,12 +7,11 @@ import {AppGuardResponse__Output} from './proto/appguard/AppGuardResponse'
77import { AppGuardTcpConnection } from './proto/appguard/AppGuardTcpConnection'
88import { AppGuardHttpResponse } from './proto/appguard/AppGuardHttpResponse'
99import { 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" ;
1311import { AuthorizationRequest } from "./proto/appguard_commands/AuthorizationRequest" ;
1412import { ClientMessage } from "./proto/appguard_commands/ClientMessage" ;
1513import { ServerMessage__Output } from "./proto/appguard_commands/ServerMessage" ;
14+ import { FirewallDefaults } from "./proto/appguard_commands/FirewallDefaults" ;
1615
1716const opts = { includeDirs : [
1817 'node_modules/@nullnet/appguard-express/node_modules/appguard-client-common/proto/' ,
@@ -21,6 +20,8 @@ const opts = {includeDirs: [
2120const packageDef = protoLoader . loadSync ( 'appguard.proto' , opts ) ;
2221const 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
4341export 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}
0 commit comments