Skip to content

Commit

Permalink
#5 Feat: implement whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanBlacky committed Feb 1, 2019
1 parent 8e670fe commit 9d87cf2
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ GLS_CONNECTOR_HOST=
GLS_CONNECTOR_PORT=
GLS_WIF=
GLS_LOGIN=
GLS_MONGO_CONNECT=mongodb://bandwidth_mongo:27017/admin
14 changes: 14 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,19 @@ services:
env_file:
- .env

mongo:
container_name: bandwidth_mongo
image: mongo
restart: always
volumes:
- badwidth_mongodb_vol:/data/db
ports:
- 127.0.0.1:27017:27017
networks:
- services-tier

networks:
services-tier:

volumes:
badwidth_mongodb_vol:
1 change: 0 additions & 1 deletion src/Main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const core = require('gls-core-service');
const stats = core.utils.statsClient;
const InnerGate = core.services.Connector;
const BasicMain = core.services.BasicMain;
const env = require('./data/env');
const Connector = require('./services/Connector');
Expand Down
16 changes: 14 additions & 2 deletions src/controllers/BandwidthProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ const core = require('gls-core-service');
const Basic = core.controllers.Basic;
const env = require('../data/env');
const { GLS_WIF, GLS_LOGIN } = env;

class BandwidthProvider extends Basic {
constructor({ connector }) {
constructor({ connector, whitelist }) {
super({ connector });

this.whitelist = whitelist;

this._WIF = GLS_WIF;
this._login = GLS_LOGIN;

Expand Down Expand Up @@ -41,11 +44,20 @@ class BandwidthProvider extends Basic {
}
}

async provideBandwidth() {
async provideBandwidth({ username, channelId, transaction }) {
if (!this.serviceReady) {
await this.authorize();
}

const isAllowed = await this.whitelist.isAllowed({ channelId, username });

if (!isAllowed) {
throw {
code: 1103,
message: 'This user is not allowed to require bandwidth',
};
}

/*
Контракт: eosio
Действие: providebw
Expand Down
79 changes: 79 additions & 0 deletions src/controllers/Whitelist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const core = require('gls-core-service');
const BasicController = core.controllers.Basic;
const Whitelist = require('../model/Whitelist');

class WhitelistController extends BasicController {
constructor({ connector }) {
super({ connector });

this._whitelistMap = new Map(); // username -> set of cids
this._cidSet = new Set(); // set of cids
}

async _askRegService({ username }) {
// TODO: implement call to service
return true;
}

_addInMem({ username, channelId }) {
this._cidSet.add(channelId);

let userCids = this._whitelistMap.get(username);

if (userCids) {
userCids.add(channelId);
} else {
userCids = new Set(channelId);
}

this._whitelistMap.set(username, userCids);
}

_removeFromMem(username) {
const cids = this._whitelistMap.get(username);
if (cids) {
cids.forEach(cid => {
this._cidSet.delete(cid);
});
}
}

async isAllowed({ channelId, username }) {
// in memory -> allowed
if (this._cidSet.has(channelId)) return true;

const user = await Whitelist.findOne({ username });

// explicitly banned -> not allowed
if (user && user.banned) {
return false;
}

// in db -> allowed and should be stored in memory
if (user && !user.banned) {
this._addInMem({ username, channelId });

return true;
}

const inRegService = await this._askRegService({ username });

if (!inRegService) {
return false;
}

// in reg service -> add to mongo and to in-mem
await Whitelist.create({ username });
this._addInMem({ username, channelId });

return true;
}

async banUser(username) {
await Whitelist.findOneAndRemove({ username });

this._removeFromMem(username);
}
}

module.exports = WhitelistController;
14 changes: 14 additions & 0 deletions src/model/Whitelist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const core = require('gls-core-service');
const MongoDB = core.services.MongoDB;

module.exports = MongoDB.makeModel('Whitelist', {
username: {
type: String,
required: true,
},
banned: {
type: Boolean,
required: true,
default: false,
},
});
10 changes: 8 additions & 2 deletions src/services/Connector.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const core = require('gls-core-service');
const BasicConnector = core.services.Connector;
const BandwidthProvider = require('../controllers/BandwidthProvider');
const Whitelist = require('../controllers/Whitelist');

class Connector extends BasicConnector {
constructor() {
super();

this._bandwidthProvider = new BandwidthProvider({ connector: this });
this._whitelistController = new Whitelist({ connector: this });
this._bandwidthProvider = new BandwidthProvider({
connector: this,
whitelist: this._whitelistController,
});
}

async start() {
const provider = this._bandwidthProvider;
const whitelist = this._whitelistController;

await super.start({
serverRoutes: {
'bandwidth.Provide': provider.provideBandwidth.bind(provider),
'bandwidth.BanUser': whitelist.banUser.bind(provider),
},
});
}
Expand Down

0 comments on commit 9d87cf2

Please sign in to comment.