Skip to content

Commit 2da1db5

Browse files
committed
set up skeleton for JS clients
1 parent 15a0e07 commit 2da1db5

21 files changed

+252
-141
lines changed

client_common/proto/appguard.proto

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ syntax = "proto3";
22

33
package appguard;
44

5+
import "google/protobuf/empty.proto";
6+
import "commands.proto";
7+
58
service AppGuard {
6-
// Authentication
7-
rpc Heartbeat (HeartbeatRequest) returns (stream HeartbeatResponse);
8-
// Firewall
9-
rpc UpdateFirewall (AppGuardFirewall) returns (Empty);
9+
// Control channel
10+
rpc ControlChannel(stream appguard_commands.ClientMessage)
11+
returns (stream appguard_commands.ServerMessage);
1012
// Logs
11-
rpc HandleLogs (Logs) returns (Empty);
13+
rpc HandleLogs (Logs) returns (google.protobuf.Empty);
1214
// TCP
1315
rpc HandleTcpConnection (AppGuardTcpConnection) returns (AppGuardTcpResponse);
1416
// HTTP
@@ -17,33 +19,8 @@ service AppGuard {
1719
// SMTP
1820
rpc HandleSmtpRequest (AppGuardSmtpRequest) returns (AppGuardResponse);
1921
rpc HandleSmtpResponse (AppGuardSmtpResponse) returns (AppGuardResponse);
20-
}
21-
22-
// Authentication ------------------------------------------------------------------------------------------------------
23-
24-
message HeartbeatRequest {
25-
string app_id = 1;
26-
string app_secret = 2;
27-
}
28-
29-
enum DeviceStatus {
30-
DRAFT = 0;
31-
ACTIVE = 1;
32-
ARCHIVED = 2;
33-
DELETED = 3;
34-
DS_UNKNOWN = 4;
35-
}
36-
37-
message HeartbeatResponse {
38-
string token = 1;
39-
DeviceStatus status = 2;
40-
}
41-
42-
// Firewall ------------------------------------------------------------------------------------------------------------
43-
44-
message AppGuardFirewall {
45-
string token = 1;
46-
string firewall = 2;
22+
// Other
23+
rpc FirewallDefaultsRequest (Token) returns (appguard_commands.FirewallDefaults);
4724
}
4825

4926
// Logs ----------------------------------------------------------------------------------------------------------------
@@ -124,18 +101,16 @@ message AppGuardSmtpResponse {
124101

125102
// Response ------------------------------------------------------------------------------------------------------------
126103

127-
message Empty {}
128-
129104
message AppGuardResponse {
130-
FirewallPolicy policy = 2;
105+
appguard_commands.FirewallPolicy policy = 2;
131106
}
132107

133108
message AppGuardTcpResponse {
134109
AppGuardTcpInfo tcp_info = 1;
135110
}
136111

137-
enum FirewallPolicy {
138-
UNKNOWN = 0;
139-
ALLOW = 1;
140-
DENY = 2;
141-
}
112+
// Other --------------------------------------------------------------------------------------
113+
114+
message Token {
115+
string token = 1;
116+
}

client_common/proto/commands.proto

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
syntax = "proto3";
2+
3+
package appguard_commands;
4+
5+
import "google/protobuf/empty.proto";
6+
7+
message AuthorizationRequest {
8+
string uuid = 1;
9+
string code = 2;
10+
string category = 3;
11+
string type = 4;
12+
string target_os = 5;
13+
}
14+
15+
message Authentication {
16+
string app_id = 1;
17+
string app_secret = 2;
18+
}
19+
20+
message ClientMessage {
21+
oneof message {
22+
AuthorizationRequest authorization_request = 1;
23+
Authentication authentication = 2;
24+
}
25+
}
26+
27+
message AuthenticationData {
28+
optional string app_id = 1;
29+
optional string app_secret = 2;
30+
}
31+
32+
message ServerMessage {
33+
oneof message {
34+
string update_token_command = 1;
35+
36+
FirewallDefaults set_firewall_defaults = 2;
37+
38+
google.protobuf.Empty heartbeat = 3;
39+
40+
AuthenticationData device_authorized = 4;
41+
google.protobuf.Empty device_deauthorized = 5;
42+
google.protobuf.Empty authorization_rejected = 6;
43+
}
44+
}
45+
46+
message FirewallDefaults {
47+
uint32 timeout = 1;
48+
FirewallPolicy policy = 2;
49+
}
50+
51+
enum FirewallPolicy {
52+
UNKNOWN = 0;
53+
ALLOW = 1;
54+
DENY = 2;
55+
}

client_common/src/appguard.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ 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 {HeartbeatRequest} from "./proto/appguard/HeartbeatRequest";
11-
import {HeartbeatResponse__Output} from "./proto/appguard/HeartbeatResponse";
12-
import {DeviceStatus} from "./proto/appguard/DeviceStatus";
1310
import {TOKEN_FILE} from "./auth";
1411
import {AppGuardFirewall, AppGuardFirewall__Output} from "./proto/appguard/AppGuardFirewall";
1512
import {FirewallPolicy} from "./proto/appguard/FirewallPolicy";
13+
import {AuthorizationRequest} from "./proto/appguard_commands/AuthorizationRequest";
14+
import {ClientMessage} from "./proto/appguard_commands/ClientMessage";
15+
import {ServerMessage__Output} from "./proto/appguard_commands/ServerMessage";
1616

1717
const opts = {includeDirs: [
1818
'node_modules/@nullnet/appguard-express/node_modules/appguard-client-common/proto/',
@@ -131,9 +131,20 @@ export class AppGuardService {
131131
}
132132
}
133133

134-
heartbeat(req: HeartbeatRequest) {
135-
let call = this.client.heartbeat(req);
136-
call.on('data', function(heartbeat: HeartbeatResponse__Output) {
134+
control_stream(req: AuthorizationRequest) {
135+
let call = this.client.controlChannel();
136+
137+
let authz_req: ClientMessage = {authorizationRequest: req};
138+
call.write(authz_req);
139+
140+
call.on('data', function(server_msg: ServerMessage__Output) {
141+
if (server_msg.deviceAuthorized) {
142+
143+
} else if (server_msg.updateTokenCommand) {
144+
145+
} else if (server_msg.setFirewallDefaults) {
146+
147+
}
137148
// handle the heartbeat response
138149
console.log("Received heartbeat from server");
139150
// write token to file
@@ -149,9 +160,9 @@ export class AppGuardService {
149160
call.on('error', (_e) => {
150161
// An error has occurred and the stream has been closed.
151162
// sleep for 10 seconds and try again
152-
console.log("Error in heartbeat, retrying in 10 seconds");
163+
console.log("Error in control stream");
153164
setTimeout(() => {
154-
this.heartbeat(req);
165+
this.control_stream(req);
155166
}, 10000);
156167
});
157168
}

client_common/src/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class AuthHandler {
3535
appSecret: this.app_secret,
3636
};
3737

38-
this.client.heartbeat(hb_req);
38+
this.client.control_stream(hb_req);
3939

4040
console.log("Waiting for the first server heartbeat...");
4141
while (this.token() === '') {

client_common/src/index.ts

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

client_common/src/proto/appguard.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ type SubtypeConstructor<Constructor extends new (...args: any) => any, Subtype>
1010
export interface ProtoGrpcType {
1111
appguard: {
1212
AppGuard: SubtypeConstructor<typeof grpc.Client, _appguard_AppGuardClient> & { service: _appguard_AppGuardDefinition }
13-
AppGuardFirewall: MessageTypeDefinition
1413
AppGuardHttpRequest: MessageTypeDefinition
1514
AppGuardHttpResponse: MessageTypeDefinition
1615
AppGuardIpInfo: MessageTypeDefinition
@@ -20,13 +19,23 @@ export interface ProtoGrpcType {
2019
AppGuardTcpConnection: MessageTypeDefinition
2120
AppGuardTcpInfo: MessageTypeDefinition
2221
AppGuardTcpResponse: MessageTypeDefinition
23-
DeviceStatus: EnumTypeDefinition
24-
Empty: MessageTypeDefinition
25-
FirewallPolicy: EnumTypeDefinition
26-
HeartbeatRequest: MessageTypeDefinition
27-
HeartbeatResponse: MessageTypeDefinition
2822
Log: MessageTypeDefinition
2923
Logs: MessageTypeDefinition
24+
Token: MessageTypeDefinition
25+
}
26+
appguard_commands: {
27+
Authentication: MessageTypeDefinition
28+
AuthenticationData: MessageTypeDefinition
29+
AuthorizationRequest: MessageTypeDefinition
30+
ClientMessage: MessageTypeDefinition
31+
FirewallDefaults: MessageTypeDefinition
32+
FirewallPolicy: EnumTypeDefinition
33+
ServerMessage: MessageTypeDefinition
34+
}
35+
google: {
36+
protobuf: {
37+
Empty: MessageTypeDefinition
38+
}
3039
}
3140
}
3241

0 commit comments

Comments
 (0)