main.js:
var express = require('express')
var app = express()
...
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
...
var api_settings = require('./api/settings');
...
var session = require('express-session');
var partials = require('express-partials')
...
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(api_settings);
app.use(partials());
...
db.js:
/**
* Created by admin on 17/3/1.
*/
let path = require('path');
let log4js = require('log4js')
log4js.configure(path.join(__dirname,'./log4js.json'))
var logMongo=log4js.getLogger('mongo.js')
const API='/api';
const mongoose = require('mongoose');
// mongo0505 127.0.0.1:27017/eniw
// mongo test 127.0.0.1:27017/eniw_test
// 若不存在test数据库会自动生成
mongoose.connect('mongodb://127.0.0.1:27017/XXXX', { useMongoClient: true });
mongoose.Promise = global.Promise;
//为这次连接绑定事件
const db = mongoose.connection;
db.once('error', () => {
logMongo.error('Mongo connection error')
});
db.once('open', () => {
logMongo.info('Mongo connection successed')
});
//定义模式accountSchema
const accountSchema = mongoose.Schema({
username: String,
password: String,
authority: String
});
//定义模式serverSettings
const serverSettings = mongoose.Schema({
ip: String,
username: String,
password: String,
servername: String
});
//定义模式nodeGroups
const nodeGroups = mongoose.Schema({
nodegroupname: String
});
//定义模式nodeTypesAndreleases (Mixed混合型)
const nodeTypesAndreleases = mongoose.Schema({
nodetypesandreleases: mongoose.Schema.Types.Mixed
});
//定义模式interface
const interface=mongoose.Schema({
name:String
})
//定义模式wiresharkFilter
const wiresharkFilter = mongoose.Schema({
imsi:String,
protocol: String,
message: String,
ie: String,
expectedvalue: String
});
// 定义模式wiresharkFiltersUser
const wiresharkFiltersUser=mongoose.Schema({
filters:[wiresharkFilter]
});
//定义模式node
const extra = mongoose.Schema({
key: String,
value: String
});
const node = mongoose.Schema({
name: String,
ip: String,
type: String,
release: String,
username: String,
password: String,
extramd: [extra],
nodegroupnames: Array,
status: String
});
//定义模式mobile
const wifissid = mongoose.Schema({
ssid: String
});
const mobile = mongoose.Schema({
id: String,
ip: String,
imsi: String,
isdn: String,
mobiletype: String,
operationalstatus: String,
status: String,
wifilist: [wifissid],
ueIp: String,
ueUsername: String,
username:String
});
//定义模式phoneNumberCRSP
const phoneNumberCRSP = mongoose.Schema({
imsi:String,
phonenumber:String
})
//定义模式testSuite
const testcases = mongoose.Schema({
testcase_id: String,
testcasename:String
});
const testSuite = mongoose.Schema({
testsuitename: String,
testcases: [testcases]
});
//定义模式testCase
const testCase = mongoose.Schema({
testsuite_id: String,
testcasename: String,
workflow: Array,
nodes: Array,
dialine_imsi: String,
dialinessid: String,
dialedline_imsi: String,
dialedlinessid: String,
duration: String,
repetitionaltimes: String,
wiresharkfiltersuser_id: String,
interfaces: Array,
wifilatency: String
});
//定义模式report
const report = mongoose.Schema({
datetime: Date,
username: String,
detail: mongoose.Schema.Types.Mixed
})
const wireshark_ue_separator=mongoose.Schema({
protocol:mongoose.Schema.Types.Mixed,
message:mongoose.Schema.Types.Mixed,
filtername:mongoose.Schema.Types.Mixed,
filtervalue:mongoose.Schema.Types.Mixed,
ie:mongoose.Schema.Types.Mixed,
filter_name:mongoose.Schema.Types.Mixed,
expectedvalue:mongoose.Schema.Types.Mixed,
filter_value:mongoose.Schema.Types.Mixed
})
//定义模型Model
const Models = {
Account: mongoose.model('accounts', accountSchema),
ServerSettings: mongoose.model('serverSettings', serverSettings),
NodeGroups: mongoose.model('nodeGroups', nodeGroups),
// NodeTypes: mongoose.model('nodeTypes', nodeTypes),
// NodeReleases: mongoose.model('nodeReleases', nodeReleases),
NodeTypesAndreleases: mongoose.model('nodeTypesAndreleases', nodeTypesAndreleases),
Interfaces: mongoose.model('interface', interface),
WiresharkFilters: mongoose.model('wiresharkFilter', wiresharkFilter),
Nodes: mongoose.model('node', node),
PhoneNumberCRSPs: mongoose.model('phoneNumberCRSP', phoneNumberCRSP),
Mobiles: mongoose.model('mobile', mobile),
WiresharkFiltersUsers: mongoose.model('wiresharkFiltersUser', wiresharkFiltersUser),
TestSuites: mongoose.model('testSuite', testSuite),
TestCases: mongoose.model('testCase', testCase),
Reports: mongoose.model('report', report),
Wireshark_ue_separator:mongoose.model('wireshark_ue_separator',wireshark_ue_separator)
};
// 建表
// Models.Wireshark_ue_separator.create({
// protocol:"GTPv2",
// message:"Create Session Request",
// filtername:"gtpv2.message_type ",
// filtervalue:"32",
// ie:"RAT Type",
// filter_name:"gtpv2.rat_type",
// expectedvalue:"LTE",
// filter_value:""
// });
module.exports = {Models, API};
setting.js:
/**
* Created by hjw on 17/3/7.
*/
"use strict"
const modelsObject = require('../db.js');
const models=modelsObject.Models;
const API=modelsObject.API;
const express = require('express');
const router = express.Router();
var path = require('path');
let log4js = require('log4js')
log4js.configure(path.join(__dirname,'../log4js.json'))
let logSetting=log4js.getLogger('setting.js')
// node-group name已存储接口
router.get(API+'/nodegroupname', (req, res) => {
models.NodeGroups.find({}, (err, data) => {
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((element) => {
element._doc.dialogFormVisible = false; //该属性用于控制vue弹出框的弹出与否
});
logSetting.info('userInfo:', req.headers.userinfo+' get node-group info')
res.send(data);
}
})
});
// node-group name存储接口
router.post(API+'/nodegroupname', (req, res) => {
models.NodeGroups.find({nodegroupname: req.body.nodegroupname}, (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该组名
res.send('This node group name already exists.');
} else {
//不存在则添加
models.NodeGroups.create({
nodegroupname: req.body.nodegroupname
}, (err, data) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' create node-group')
res.send('OK');
}
})
}
});
});
// node-group name删除接口
router.delete(API + '/nodegroupname', (req, res) => {
models.NodeGroups.remove({ nodegroupname: req.query.nodegroupname }, (err) => {
if (!err) {
models.Nodes.find({ nodegroupnames: req.query.nodegroupname }).lean().exec((err, data) => {
if (err) {
res.sendStatus(500)
} else if (data.length === 0) {
res.send('OK')
} else if (data.length > 0) {
let flag = 0
let flagLength = data.length
data.forEach((node) => {
models.Nodes.update({ _id: node._id }, { $pull: { nodegroupnames: req.query.nodegroupname } }, (err, data) => {
if (err) {
logSetting.error('userInfo:', req.headers.userinfo + ' delete node-group ' + err)
res.send('false');
} else {
flag++
if (flag === flagLength) {
logSetting.info('userInfo:', req.headers.userinfo + ' delete node-group')
res.send('OK');
}
}
})
})
}
})
} else {
res.send('false')
}
})
});
// node-group name修改接口
router.put(API+'/nodegroupname', (req, res) => {
models.NodeGroups.find({nodegroupname: req.body.nodegroupname}, (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该组名
res.send('This node group name already exists.');
} else {
//不存在则添加
models.NodeGroups.update({_id: req.body._id},
{
$set: {nodegroupname: req.body.nodegroupname}
}, function () {
logSetting.info('userInfo:', req.headers.userinfo+' update node-group')
res.send('OK');
})
}
});
});
// 提供node搜索接口
router.get(API+'/node/search',(req,res)=>{
if(req.query[0]&&req.query[1]) {
let query={}
query['nodegroupnames']=new RegExp(req.query[0])
query['type']=new RegExp(req.query[1])
models.Nodes.find(query).lean().exec((err, data) => {
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((element) => {
//该属性用于控制vue弹出框的弹出与否
element.dialogFormVisible = false
//该属性用于可能出现的对node信息进行编辑时前端js变量中用来保存新增的nodegroupname值
element.nodegroupname=''
//该属性用于控制setting页面node group & node tab 编辑node功能时password输入框是否隐藏密码,默认隐藏
element.PWStyleNode=true
});
logSetting.info('userInfo:', req.headers.userinfo+' search nodes info')
res.send(data);
}
})
}
if(req.query[0]&&!req.query[1]){
let query={}
query['nodegroupnames']=new RegExp(req.query[0])
models.Nodes.find(query).lean().exec((err, data) => {
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((element) => {
//该属性用于控制vue弹出框的弹出与否
element.dialogFormVisible = false
//该属性用于可能出现的对node信息进行编辑时前端js变量中用来保存新增的nodegroupname值
element.nodegroupname=''
//该属性用于控制setting页面node group & node tab 编辑node功能时password输入框是否隐藏密码,默认隐藏
element.PWStyleNode=true
});
logSetting.info('userInfo:', req.headers.userinfo+' search nodes info')
res.send(data);
}
})
}
if(!req.query[0]&&req.query[1]){
let query={}
query['type']=new RegExp(req.query[1])
models.Nodes.find(query).lean().exec((err, data) => {
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((element) => {
//该属性用于控制vue弹出框的弹出与否
element.dialogFormVisible = false
//该属性用于可能出现的对node信息进行编辑时前端js变量中用来保存新增的nodegroupname值
element.nodegroupname=''
//该属性用于控制setting页面node group & node tab 编辑node功能时password输入框是否隐藏密码,默认隐藏
element.PWStyleNode=true
});
logSetting.info('userInfo:', req.headers.userinfo+' search nodes info')
res.send(data);
}
})
}
})
// 提供已储存的node接口
router.get(API+'/node', (req, res) => {
models.Nodes.find({}).sort('nodegroupname').sort('name').lean().exec((err, data) => {
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((element) => {
//该属性用于控制vue弹出框的弹出与否
element.dialogFormVisible = false
//该属性用于可能出现的对node信息进行编辑时前端js变量中用来保存新增的nodegroupname值
element.nodegroupname=''
//该属性用于控制setting页面node group & node tab 编辑node功能时password输入框是否隐藏密码,默认隐藏
element.PWStyleNode=true
});
logSetting.info('userInfo:', req.headers.userinfo+' get nodes info')
res.send(data);
}
})
});
// node存储接口
router.post(API+'/node', (req, res) => {
delete req.body.dialogFormVisible;
//经过前段检验,能传到后台的数据不存在name重复的问题
models.Nodes.create(req.body, (err, data) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' create node')
res.send(data._doc);
}
})
});
// node更新接口
router.put(API+'/node', (req, res) => {
models.Nodes.update({_id: req.body._id},
{
$set: {
name: req.body.name,
ip: req.body.ip,
type: req.body.type,
release: req.body.release,
username: req.body.username,
password: req.body.password,
extramd: req.body.extramd,
nodegroupnames: req.body.nodegroupnames,
status:req.body.status
}
}, (err,data)=>{
if(err){
res.send('false');
}else{
logSetting.info('userInfo:', req.headers.userinfo+' update node')
res.send('OK');
}
})
});
// node删除接口
router.delete(API+'/node',(req,res)=>{
models.Nodes.remove({_id: req.query._id}, (err) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' delete node')
res.send('OK');
}
})
});
// 提供test suite已储存接口
router.get(API+'/testsuite',(req,res)=>{
models.TestSuites.find({}, (err, data) => {
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((element) => {
element._doc.dialogFormVisible = false; //该属性用于控制vue弹出框的弹出与否
});
logSetting.info('userInfo:', req.headers.userinfo+' get test-suites info')
res.send(data);
}
})
});
// test suite存储接口
router.post(API+'/testsuite',(req,res)=>{
models.TestSuites.find({testsuitename:req.body.testsuitename} , (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该node
res.send('This test suite name already exists.');
} else {
//不存在则添加
models.TestSuites.create(req.body, (err, data) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' create test-suite')
res.send(data);
}
})
}
});
});
// test suite更新接口
router.put(API+'/testsuite',(req,res)=>{
models.TestSuites.find({testsuitename: req.body.testsuitename}, (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该组名
res.send('This test suite name already exists.');
} else {
//不存在则更新
models.TestSuites.update({_id: req.body._id},
{
$set: {
testsuitename: req.body.testsuitename,
testcases: req.body.testcases
}
}, function () {
logSetting.info('userInfo:', req.headers.userinfo+' update test-suite')
res.send('OK');
})
}
});
});
// test suite删除接口
router.delete(API + '/testsuite', (req, res) => {
models.TestSuites.remove({ _id: req.query._id }, (err) => {
if (!err) {
models.TestCases.remove({ testsuite_id: req.query._id }, (err) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' delete test-suite')
res.send('OK');
} else {
res.send('false');
}
})
} else {
res.send('false');
}
})
});
// 从数据库中获取node type和release的值
router.get(API + '/nodetypesAndreleases', (req, res) => {
models.NodeTypesAndreleases.find({}).lean().exec((err, data) => {
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
logSetting.info('userInfo:', req.headers.userinfo+' get nodetypesAndreleases')
res.send(data);
}
})
});
// 获取interface已存储信息接口
router.get(API+'/interface',(req,res)=>{
models.Interfaces.find({}).sort('name').lean().exec((err,data)=>{
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((intfc) => {
intfc.dialogFormVisible = false; //该属性用于控制vue弹出框的弹出与否
});
logSetting.info('userInfo:', req.headers.userinfo+' get interfaces info')
res.send(data);
}
})
})
// interface 存储接口
router.post(API+'/interface',(req,res)=>{
models.Interfaces.find(req.body, (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该组名
res.send('This interface already exists.');
} else {
//不存在则添加
models.Interfaces.create({
name:req.body.name
}, (err, data) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' create interface')
res.send('OK');
}
})
}
});
})
// interface 删除接口
router.delete(API+'/interface',(req,res)=>{
models.Interfaces.remove({_id: req.query._id}, (err) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' delete interface')
res.send('OK');
}
})
});
// interface 修改接口
router.put(API+'/interface',(req,res)=>{
models.Interfaces.find({name: req.body.name}, (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该组名
res.send('This interface already exists.');
} else {
//不存在则更新
models.Interfaces.update({_id: req.body._id},
{
$set: {
name: req.body.name
}
}, function () {
logSetting.info('userInfo:', req.headers.userinfo+' update interface')
res.send('OK');
})
}
});
})
// wireshark filter已储存信息接口
router.get(API+'/filter',(req,res)=>{
models.WiresharkFilters.find({}).sort('protocol').sort('message').sort('ie').exec((err,data)=>{
if (err) {
res.send(500)
} else if (!data) {
res.send(404)
} else {
data.forEach((element) => {
element._doc.dialogFormVisible = false; //该属性用于控制vue弹出框的弹出与否
});
logSetting.info('userInfo:', req.headers.userinfo+' get wireshark filter')
res.send(data);
}
})
});
// wireshark filter存储接口
router.post(API+'/filter', (req, res) => {
models.WiresharkFilters.find(req.body, (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该组名
res.send('This filter already exists.');
} else {
//不存在则添加
models.WiresharkFilters.create({
protocol:req.body.protocol,
message:req.body.message,
ie:req.body.ie,
expectedvalue:req.body.expectedvalue
}, (err, data) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' create wireshark filter')
res.send('OK');
}
})
}
});
});
// wireshark filter修改接口
router.put(API+'/filter', (req, res) => {
//进行重复查询时不带入_id查询
let checkIfRepeat={
protocol:req.body.protocol,
message:req.body.message,
ie:req.body.ie,
expectedvalue:req.body.expectedvalue
};
models.WiresharkFilters.find(checkIfRepeat, (err, data) => {
if (err) {
res.send('false')
} else if (data.length > 0) {
//数据库中已存在该组名
res.send('This filter already exists.');
} else {
//若数据库中不重复则更新
models.WiresharkFilters.update({_id: req.body._id},
{
$set: {
protocol: req.body.protocol,
message: req.body.message,
ie: req.body.ie,
expectedvalue: req.body.expectedvalue
}
}, (err,data)=>{
if(err){
res.send('false');
}else{
logSetting.info('userInfo:', req.headers.userinfo+' update wireshark filter')
res.send('OK');
}
})
}
});
});
// wireshark filter删除接口
router.delete(API+'/filter',(req,res)=>{
models.WiresharkFilters.remove({_id: req.query._id}, (err) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' delete wireshark filter')
res.send('OK');
}
})
});
// 提供已有的服务器设置
router.get(API+'/serversettings', (req, res) => {
models.ServerSettings.find({servername: req.query.servername}, (err, data) => {
if (err) {
res.send(false)
} else if (data.length === 0) {
res.send(false)
} else {
logSetting.info('userInfo:', req.headers.userinfo+' get serversettings info')
res.send(data);
}
})
});
// 保存&更新服务器设置信息接口
router.post(API+'/serversettings', (req, res) => {
if(req.body.servername==='UE Control'){
if(req.body._id){
//若已存在该_id的UEcontrol信息则修改
models.ServerSettings.update({_id: req.body._id}, {
$set: {
ip: req.body.ip,
username: req.body.username,
password: req.body.password,
servername: req.body.servername
}
}, function () {
//修改成功返回'OK'
logSetting.info('userInfo:', req.headers.userinfo+' update ue-control setting')
res.send('OK');
})
}else {
models.ServerSettings.create({
ip: req.body.ip,
username: req.body.username,
password: req.body.password,
servername: req.body.servername
}, (err, data) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' create ue-control setting')
res.send('OK');
}
})
}
}else {
models.ServerSettings.find({servername: req.body.servername}, (err, data) => {
if (err) {
res.send(false);
}
if (data.length === 0) {
//如果不存在相关server的数据则新增
models.ServerSettings.create({
ip: req.body.ip,
username: req.body.username,
password: req.body.password,
servername: req.body.servername
}, (err, data) => {
if (!err) {
//保存成功返回'OK'
logSetting.info('userInfo:', req.headers.userinfo+' create '+req.body.servername+' setting')
res.send('OK');
}
});
} else if (data.length > 0) {
//若已存在相关server的数据则修改
models.ServerSettings.update({servername: req.body.servername}, {
$set: {
ip: req.body.ip,
username: req.body.username,
password: req.body.password,
servername: req.body.servername
}
}, function () {
//修改成功返回'OK'
logSetting.info('userInfo:', req.headers.userinfo+' update'+req.body.servername+' setting')
res.send('OK');
})
}
});
}
});
// 删除服务器设置
router.delete(API+'/serversettings',(req,res)=>{
models.ServerSettings.remove({_id: req.query._id}, (err) => {
if (!err) {
logSetting.info('userInfo:', req.headers.userinfo+' delete server setting')
res.send('OK');
}
})
});
module.exports = router;