-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetData.js
More file actions
128 lines (104 loc) · 3.58 KB
/
getData.js
File metadata and controls
128 lines (104 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var container = require('rhea');
const https = require('https'); // or https for secure requests
let SERVICE_ACCOUNT_ID = "CHANGE TO YOUR SERVICE ACCOUNT ID";
let CLIENT_SECRET = "CHANGE TO YOU CLIENT SECRET";
let TENANT_ID = "CHANGE TO YOUR TENANT ID";
let AMQP_URL = "amqp://CHANGE/TO/YOUR/AMQP/URL";
let token = '';
const body = 'grant_type=client_credentials&client_id='+SERVICE_ACCOUNT_ID+'&client_secret='+CLIENT_SECRET+'&scope=https://servicebus.azure.net/.default';
const options = {
hostname: 'login.microsoftonline.com',
port: 443, // or 443 for https
path: '/'+TENANT_ID+'/oauth2/v2.0/token',
method: 'POST'
};
function renew_token() {
get_token();
}
function get_token() {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
//console.log(data);
token = JSON.parse(data).access_token;
//console.log(token);
send_token();
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.write(body);
req.end();
}
//// AMQP ////
var host = "sb-duvproddistribution.servicebus.windows.net"
var port = 5671
var sender;
var connection;
var hasAuth = false
container.sasl_server_mechanisms.enable_anonymous();
container.reconnect = false;
function amqpConnect() {
connection = container.connect({ port: port, host: host, hostname: host, username: SERVICE_ACCOUNT_ID, transport:'tls', reconnect:false });
sender = connection.open_sender('$cbs');
connection.open_receiver('$cbs');
}
function send_token() {
sender.send({
reply_to:'http://localhost',
application_properties:{
operation:'put-token',
type:'jwt',
name:'amqps://sb-duvproddistribution.servicebus.windows.net/'+AMQP_URL.split('/')[3]
},
message_id:1,
body:token
});
console.log('token sent');
}
container.on('connection_open', function (context) {
console.log('We are Connected!');
});
container.on('receiver_open', function (context) {
console.log( "receiver_open");
});
container.on('sender_open', function (context) {
console.log('sender_open');
if( !hasAuth) {
renew_token();
}
});
container.on('message', function (context) {
//console.log('got message '+JSON.stringify(context.message));
if(context.message.application_properties.hasOwnProperty('status-code')) {
//CBS-message
let code = context.message.application_properties['status-code'];
if(code >= 200 && code < 300) {
console.log('signin success');
if( !hasAuth) {
startListening();
setInterval(renew_token, 900000);
hasAuth = true;
}
}
else
console.log('signin failed!');
}
else {
console.log('\nGot message:\nApplicationProps:\n'+JSON.stringify(context.message.application_properties));
console.log('Body:\n'+JSON.stringify(context.message.body.content.toString('HEX')));
}
});
container.on('sendable', function (context) {
console.log('sendable');
});
function startListening() {
let topic = AMQP_URL.split('/')[3]+'/subscriptions/'+AMQP_URL.split('/')[4];
console.log('subscribing to: '+topic);
connection.open_receiver(topic);
}
amqpConnect();