Skip to content

Commit 708f323

Browse files
author
Giuliano Bellini
committed
Merge branch 'dev' into 'main'
Datastore authentication See merge request nullnet/appguard-express!1
2 parents 61329ca + 9779a4a commit 708f323

28 files changed

+322
-33
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
node_modules
44
dist
55
.idea
6+
token.txt
7+
.env

.gitlab-ci.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ workflow:
33
on_new_commit: conservative
44
on_job_failure: all
55

6-
variables:
7-
GIT_SUBMODULE_STRATEGY: recursive
8-
GIT_SUBMODULE_FORCE_HTTPS: "true"
9-
106
default:
117
tags:
128
- docker-52-1-21

.gitmodules

Lines changed: 0 additions & 4 deletions
This file was deleted.

appguard-protobuf

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

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

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"files": [
77
"dist",
88
"src",
9-
"appguard-protobuf"
9+
"proto"
1010
],
1111
"engines": {
1212
"node": ">=9"
@@ -24,6 +24,7 @@
2424
"@grpc/grpc-js": "^1.3.2",
2525
"@grpc/proto-loader": "^0.6.2",
2626
"body-parser": "^1.20.2",
27+
"dotenv": "^16.5.0",
2728
"express": "^4.19.2",
2829
"nodemon": "^3.1.2"
2930
},
@@ -63,4 +64,4 @@
6364
"publishConfig": {
6465
"registry": "https://npm.nullnet.ai"
6566
}
66-
}
67+
}

proto-gen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22
rm -rf src/proto
3-
./node_modules/.bin/proto-loader-gen-types --grpcLib=@grpc/grpc-js --outDir=src/proto/ ./appguard-protobuf/appguard.proto
3+
./node_modules/.bin/proto-loader-gen-types --grpcLib=@grpc/grpc-js --outDir=src/proto/ ./proto/appguard.proto

proto/appguard.proto

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
syntax = "proto3";
2+
3+
package appguard;
4+
5+
service AppGuard {
6+
// Authentication
7+
rpc Heartbeat (HeartbeatRequest) returns (stream HeartbeatResponse);
8+
// TCP
9+
rpc HandleTcpConnection (AppGuardTcpConnection) returns (AppGuardTcpResponse);
10+
// HTTP
11+
rpc HandleHttpRequest (AppGuardHttpRequest) returns (AppGuardResponse);
12+
rpc HandleHttpResponse (AppGuardHttpResponse) returns (AppGuardResponse);
13+
// SMTP
14+
rpc HandleSmtpRequest (AppGuardSmtpRequest) returns (AppGuardResponse);
15+
rpc HandleSmtpResponse (AppGuardSmtpResponse) returns (AppGuardResponse);
16+
}
17+
18+
// Authentication ------------------------------------------------------------------------------------------------------
19+
20+
message HeartbeatRequest {
21+
string app_id = 1;
22+
string app_secret = 2;
23+
string device_version = 3;
24+
string device_uuid = 4;
25+
}
26+
27+
enum DeviceStatus {
28+
DS_DRAFT = 0;
29+
DS_ACTIVE = 1;
30+
DS_ARCHIVED = 2;
31+
DS_DELETED = 3;
32+
DS_UNKNOWN = 4;
33+
}
34+
35+
message HeartbeatResponse {
36+
string token = 1;
37+
DeviceStatus status = 2;
38+
bool remote_shell_enabled = 3;
39+
bool remote_ui_enabled = 4;
40+
bool is_monitoring_enabled = 5;
41+
}
42+
43+
// TCP -----------------------------------------------------------------------------------------------------------------
44+
45+
message AppGuardTcpConnection {
46+
string token = 1;
47+
optional string source_ip = 2;
48+
optional uint32 source_port = 3;
49+
optional string destination_ip = 4;
50+
optional uint32 destination_port = 5;
51+
string protocol = 6;
52+
}
53+
54+
message AppGuardIpInfo {
55+
string ip = 1;
56+
optional string country = 2;
57+
optional string asn = 3;
58+
optional string org = 4;
59+
optional string continent_code = 5;
60+
optional string city = 6;
61+
optional string region = 7;
62+
optional string postal = 8;
63+
optional string timezone = 9;
64+
bool blacklist = 100;
65+
}
66+
67+
message AppGuardTcpInfo {
68+
AppGuardTcpConnection connection = 1;
69+
AppGuardIpInfo ip_info = 2;
70+
uint64 tcp_id = 3;
71+
}
72+
73+
// HTTP ----------------------------------------------------------------------------------------------------------------
74+
75+
message AppGuardHttpRequest {
76+
string token = 1;
77+
string original_url = 2;
78+
map<string, string> headers = 3;
79+
string method = 4;
80+
optional string body = 5;
81+
map<string, string> query = 6;
82+
AppGuardTcpInfo tcp_info = 100;
83+
}
84+
85+
message AppGuardHttpResponse {
86+
string token = 1;
87+
uint32 code = 2;
88+
map<string, string> headers = 3;
89+
AppGuardTcpInfo tcp_info = 100;
90+
}
91+
92+
// SMTP ----------------------------------------------------------------------------------------------------------------
93+
94+
message AppGuardSmtpRequest {
95+
string token = 1;
96+
map<string, string> headers = 2;
97+
optional string body = 3;
98+
AppGuardTcpInfo tcp_info = 100;
99+
}
100+
101+
message AppGuardSmtpResponse {
102+
string token = 1;
103+
optional uint32 code = 2;
104+
AppGuardTcpInfo tcp_info = 100;
105+
}
106+
107+
// Response ------------------------------------------------------------------------------------------------------------
108+
109+
message AppGuardResponse {
110+
FirewallPolicy policy = 2;
111+
}
112+
113+
message AppGuardTcpResponse {
114+
AppGuardTcpInfo tcp_info = 1;
115+
}
116+
117+
enum FirewallPolicy {
118+
UNKNOWN = 0;
119+
ALLOW = 1;
120+
DENY = 2;
121+
}

sample/package-lock.json

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

src/app-guard-express.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import path from 'path'
22
import * as grpc from '@grpc/grpc-js'
33
import * as protoLoader from '@grpc/proto-loader'
4-
import type {ProtoGrpcType} from './proto/appguard'
5-
import { AppGuardClient } from './proto/appguard/AppGuard'
6-
import { AppGuardHttpRequest } from './proto/appguard/AppGuardHttpRequest'
7-
import { AppGuardResponse__Output } from './proto/appguard/AppGuardResponse'
8-
import { AppGuardTcpConnection } from './proto/appguard/AppGuardTcpConnection'
9-
import { AppGuardHttpResponse } from './proto/appguard/AppGuardHttpResponse'
4+
import type {ProtoGrpcType} from './proto/appguard'
5+
import {AppGuardClient} from './proto/appguard/AppGuard'
6+
import {AppGuardHttpRequest} from './proto/appguard/AppGuardHttpRequest'
7+
import {AppGuardResponse__Output} from './proto/appguard/AppGuardResponse'
8+
import {AppGuardTcpConnection} from './proto/appguard/AppGuardTcpConnection'
9+
import {AppGuardHttpResponse} from './proto/appguard/AppGuardHttpResponse'
1010
import {AppGuardTcpResponse__Output} from "./proto/appguard/AppGuardTcpResponse";
11+
import {HeartbeatRequest} from "./proto/appguard/HeartbeatRequest";
12+
import {HeartbeatResponse__Output} from "./proto/appguard/HeartbeatResponse";
13+
import {DeviceStatus} from "./proto/appguard/DeviceStatus";
14+
import {TOKEN_FILE} from "./auth";
1115

12-
const PROTO_FILE = __dirname + '/../appguard-protobuf/appguard.proto'
16+
const PROTO_FILE = __dirname + '/../proto/appguard.proto'
1317
const packageDef = protoLoader.loadSync(path.resolve(__dirname, PROTO_FILE))
1418
const grpcObj = (grpc.loadPackageDefinition(packageDef) as unknown) as ProtoGrpcType
1519

@@ -79,4 +83,28 @@ export class AppGuardService {
7983
})
8084
})
8185
}
86+
heartbeat(req: HeartbeatRequest) {
87+
let call = this.client.heartbeat(req);
88+
call.on('data', function(heartbeat: HeartbeatResponse__Output) {
89+
// handle the heartbeat response
90+
console.log("Received heartbeat from server");
91+
// write token to file
92+
const fs = require('fs');
93+
fs.writeFileSync(TOKEN_FILE, heartbeat.token, {flag: 'w'});
94+
let status = heartbeat.status;
95+
if (status == DeviceStatus.DS_ARCHIVED || status == DeviceStatus.DS_DELETED) {
96+
// terminate current process
97+
console.log("Device is archived or deleted, terminating process");
98+
process.exit(0);
99+
}
100+
});
101+
call.on('error', (_e) => {
102+
// An error has occurred and the stream has been closed.
103+
// sleep for 10 seconds and try again
104+
console.log("Error in heartbeat, retrying in 10 seconds");
105+
setTimeout(() => {
106+
this.heartbeat(req);
107+
}, 10000);
108+
});
109+
}
82110
}

0 commit comments

Comments
 (0)