Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
vernemq
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
stack
vernemq
Commits
daabfd9d
Commit
daabfd9d
authored
Nov 19, 2018
by
zCaesar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add usage
parent
1b439aca
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
92 additions
and
42 deletions
+92
-42
authhook/auth_on_publish.js
+48
-42
authhook/redis/node.txt
+8
-0
authhook/redis/on_message.js
+35
-0
authhook/redis/on_register.js
+1
-0
authhook/redis/tracker.js
+0
-0
No files found.
authhook/auth_on_publish.js
View file @
daabfd9d
...
...
@@ -15,57 +15,63 @@ module.exports = function (options = {}) {
console
.
log
(
req
.
body
);
}
var
authstatus
;
var
topic
=
req
.
body
.
topic
;
// save redis
require
(
'./redis/on_message'
).
on_message_redis
(
req
.
body
.
client_id
,
req
.
body
.
payload
).
then
(
status
=>
{
if
(
status
)
{
var
authstatus
;
var
topic
=
req
.
body
.
topic
;
var
cachekey
=
'pub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
if
(
typeof
(
authstatus
)
===
'undefined'
)
{
cache
.
set
(
cachekey
,
true
);
// cache missed
}
var
response
var
decoded
=
require
(
'jwt-verify'
).
verify
(
req
.
body
.
username
).
res
if
(
decoded
)
{
if
(
decoded
.
role
===
'realtimedb'
)
response
=
{
'result'
:
'ok'
}
else
response
=
{
'result'
:
'no'
}
res
.
send
(
response
);
next
();
}
else
{
var
GGID
=
require
(
'./utils/getGroupID'
);
var
cachekey
=
'pub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
if
(
typeof
(
authstatus
)
===
'undefined'
)
{
cache
.
set
(
cachekey
,
true
);
// cache missed
}
var
output
=
{};
GGID
.
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
function
(
group
)
{
if
(
group
)
{
var
_ftopic
=
require
(
'./utils/router'
).
rewriteTopic
(
topic
,
'pub'
,
group
,
req
.
body
.
client_id
,
output
);
// get topic where concat with groupID
response
=
{
'result'
:
'ok'
,
'modifiers'
:
{
'topic'
:
_ftopic
,
'qos'
:
0
,
'retain'
:
false
}
}
var
response
if
(
options
.
debug
)
{
console
.
log
(
response
);
}
var
decoded
=
require
(
'jwt-verify'
).
verify
(
req
.
body
.
username
).
res
if
(
output
.
verb
==
'get'
||
output
.
verb
==
'read'
)
{
response
.
modifiers
.
payload
=
Buffer
.
from
(
req
.
body
.
client_id
).
toString
(
'base64'
);
}
if
(
decoded
)
{
if
(
decoded
.
role
===
'realtimedb'
)
response
=
{
'result'
:
'ok'
}
else
response
=
{
'result'
:
'no'
}
res
.
send
(
response
);
next
();
}
else
{
res
.
send
({
"result"
:
"no"
});
next
();
var
GGID
=
require
(
'./utils/getGroupID'
);
var
output
=
{};
GGID
.
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
function
(
group
)
{
if
(
group
)
{
var
_ftopic
=
require
(
'./utils/router'
).
rewriteTopic
(
topic
,
'pub'
,
group
,
req
.
body
.
client_id
,
output
);
// get topic where concat with groupID
response
=
{
'result'
:
'ok'
,
'modifiers'
:
{
'topic'
:
_ftopic
,
'qos'
:
0
,
'retain'
:
false
}
}
if
(
options
.
debug
)
{
console
.
log
(
response
);
}
if
(
output
.
verb
==
'get'
||
output
.
verb
==
'read'
)
{
response
.
modifiers
.
payload
=
Buffer
.
from
(
req
.
body
.
client_id
).
toString
(
'base64'
);
}
res
.
send
(
response
);
next
();
}
else
{
res
.
send
({
"result"
:
"no"
});
next
();
}
});
}
});
}
}
})
}
else
{
res
.
send
({
"result"
:
"no"
});
...
...
authhook/redis/node.txt
0 → 100644
View file @
daabfd9d
> 5k : 2msg
online_duration
last_checked
msg_count_real
msg_count_per_bit
msg_size
last_publish
\ No newline at end of file
authhook/redis/on_message.js
0 → 100644
View file @
daabfd9d
var
Redis
=
require
(
'ioredis'
)
var
redis
=
new
Redis
()
function
on_message_redis
(
deviceid
,
payload
)
{
// first time to access on authhook auth_on_register
return
new
Promise
((
resolve
,
reject
)
=>
{
var
information
=
setValue
(
deviceid
,
payload
)
redis
.
hincrby
(
information
.
keys
,
'actual_message_count'
,
1
)
redis
.
hincrby
(
information
.
keys
,
'charged_message_count'
,
information
.
charged_message_count
)
redis
.
hincrby
(
information
.
keys
,
'total_message_size'
,
information
.
total_message_size
)
redis
.
hset
(
information
.
keys
,
'last_publish'
,
information
.
last_publish
,
function
(
err
,
res
)
{
resolve
(
!
res
)
// 0 is ok, 1 is no => if ok is 0 then not 0 = 1
})
})
}
module
.
exports
.
on_message_redis
=
on_message_redis
function
setValue
(
deviceid
,
payload
)
{
var
dateNow
=
Math
.
floor
(
Date
.
now
()
/
1000
)
var
keys
=
'deviceid:'
+
deviceid
var
last_publish
=
dateNow
var
msg_count_size
=
payload
.
length
*
3
/
4
var
charged_message_count
=
1
while
(
msg_count_size
>
5000
)
{
charged_message_count
+=
1
msg_count_size
=
(
payload
.
length
*
3
/
4
)
-
5000
}
var
total_message_size
=
payload
.
length
*
3
/
4
return
{
keys
:
keys
,
last_publish
:
last_publish
,
charged_message_count
:
charged_message_count
,
total_message_size
:
total_message_size
}
}
\ No newline at end of file
authhook/redis/on_register.js
View file @
daabfd9d
...
...
@@ -4,6 +4,7 @@ var redis = new Redis()
function
on_register_redis
(
deviceid
)
{
// first time to access on authhook auth_on_register
return
new
Promise
((
resolve
,
reject
)
=>
{
var
information
=
setValue
(
deviceid
)
redis
.
hdel
(
information
.
keys
,
'offline_on'
)
redis
.
hset
(
information
.
keys
,
'status'
,
information
.
status
,
'register_on'
,
information
.
register_on
,
'last_check'
,
information
.
last_check
,
function
(
err
,
res
)
{
resolve
(
!
res
)
// 0 is ok, 1 is no => if ok is 0 then not 0 = 1
})
...
...
authhook/redis/tracker.js
0 → 100644
View file @
daabfd9d
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment