This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/views/entityExplorer/EntityGraph.vue

509 lines
15 KiB
Vue
Raw Normal View History

2023-06-16 17:18:58 +08:00
<template>
<div class="entity-graph">
2023-06-29 14:40:50 +08:00
<div class="entity-graph__chart" id="entityGraph">
<relation-graph ref="relationGraph" :options="chartOption">
<template #node="{ node }">
<!-- root节点 -->
<template v-if="node.data.level === '1'">
<i :class="node.data.iconClass"></i>
<div class="graph-node__text">{{node.text}}</div>
</template>
<!-- primary节点 -->
<template v-else-if="node.data.level && node.data.level.length === 1">
<i :class="node.data.iconClass"></i>
2023-06-30 18:43:02 +08:00
<div class="graph-node__text">{{node.text}}({{node.data.count}})</div>
2023-06-29 14:40:50 +08:00
</template>
<!-- entity节点 -->
2023-06-30 18:43:02 +08:00
<template v-else-if="node.data.level && node.data.level.length === 3">
<i :class="node.data.iconClass"></i>
</template>
2023-06-29 14:40:50 +08:00
</template>
</relation-graph>
</div>
<div class="entity-graph__detail">
<ip-list
v-if="mode === 'ipList'"
:entity="entity"
@closeBlock="onCloseBlock"
@mouseenter="onMouseenter"
@expandDetail="onExpandDetail">
</ip-list>
<app-or-domain-list
v-if="mode === 'appList' || mode === 'domainList'"
:entity="entity"
@expandDetail="onExpandDetail"
@mouseenter="onMouseenter"
@closeBlock="onCloseBlock">
</app-or-domain-list>
<graph-detail
v-if="mode === 'appDetail' || mode === 'ipDetail' || mode === 'domainDetail'"
:entity="entity"
@expand="onExpand"
@closeBlock="onCloseBlock">
</graph-detail>
2023-06-16 17:18:58 +08:00
</div>
</div>
</template>
<script>
import IpList from '@/views/entityExplorer/entityGraphDetail/IpList'
import GraphDetail from '@/views/entityExplorer/entityGraphDetail/GraphDetail'
import AppOrDomainList from '@/views/entityExplorer/entityGraphDetail/AppOrDomainList'
import { useRoute } from 'vue-router'
2023-06-29 14:40:50 +08:00
import { ref, shallowRef } from 'vue'
import _ from 'lodash'
import { api } from '@/utils/api'
import axios from 'axios'
import RelationGraph from 'relation-graph/vue3'
2023-06-16 17:18:58 +08:00
export default {
name: 'EntityRelationship',
components: {
IpList,
GraphDetail,
2023-06-29 14:40:50 +08:00
AppOrDomainList,
RelationGraph
2023-06-16 17:18:58 +08:00
},
data () {
return {
2023-06-30 18:43:02 +08:00
chartOption: {
debug: false
}
}
},
methods: {
2023-06-29 14:40:50 +08:00
async init () {
/*
* 1.类型根节点 root状态ipappdomain;
* 2.类型普通节点 primary状态normalactive;
* 3.类型实体 entity状态1ipappdomain状态2normalactive.
2023-06-30 18:43:02 +08:00
*
2023-06-29 14:40:50 +08:00
* ip基色#7E9F54domain基色#38ACD2app基色E5A219
2023-06-30 18:43:02 +08:00
*
* 为方便在逻辑上区分实体列表圆圈节点和实体纯图标节点将层级分为整层和半层两种实体列表为整层实体为半层
2023-06-29 14:40:50 +08:00
* */
this.graphData.rootId = this.entity.entityName
2023-06-30 18:43:02 +08:00
// 初始化时加载到2层半
2023-06-29 14:40:50 +08:00
const rootNode = this.generateRootNode()
2023-06-30 18:43:02 +08:00
const secondLevelNodes = await this.generateSecondLevelNodes(rootNode)
const secondHalfLevelNodes = await this.generateHalfLevelNodes(secondLevelNodes)
this.graphData.nodes = [rootNode, ...secondLevelNodes, ...secondHalfLevelNodes]
this.graphData.lines = this.generateLines(rootNode)
this.graphData.lines.push(...this.generateLines(secondLevelNodes))
console.info(JSON.stringify(this.graphData))
2023-06-29 14:40:50 +08:00
this.$refs.relationGraph.setJsonData(this.graphData)
},
generateRootNode () {
2023-06-30 18:43:02 +08:00
let iconClass = ''
2023-06-29 14:40:50 +08:00
switch (this.entity.entityType) {
case 'ip': {
2023-06-30 18:43:02 +08:00
iconClass = 'cn-icon cn-icon-resolve-ip'
2023-06-29 14:40:50 +08:00
break
}
case 'domain': {
2023-06-30 18:43:02 +08:00
iconClass = 'cn-icon cn-icon-domain1'
2023-06-29 14:40:50 +08:00
break
}
case 'app': {
2023-06-30 18:43:02 +08:00
iconClass = 'cn-icon cn-icon-app-name'
2023-06-29 14:40:50 +08:00
break
}
}
return {
id: this.entity.entityName,
text: this.entity.entityName,
nodeShape: 0,
styleClass: `graph-node graph-node--root graph-node--${this.entity.entityType}`,
2023-06-30 18:43:02 +08:00
data: { level: '1', iconClass }
2023-06-29 14:40:50 +08:00
}
},
generatePrimaryNode (props) {
const node = {
2023-06-30 18:43:02 +08:00
styleClass: 'graph-node graph-node--primary'
2023-06-29 14:40:50 +08:00
}
return {
...node,
...props
}
},
2023-06-30 18:43:02 +08:00
generateEntityNode (level, relatedType, data) {
let iconClass = ''
switch (relatedType) {
case 'ip': {
iconClass = 'cn-icon cn-icon-resolve-ip'
break
}
case 'domain': {
iconClass = 'cn-icon cn-icon-subdomain'
break
}
case 'app': {
iconClass = 'cn-icon cn-icon-app-name'
break
}
}
return {
id: data.vertex,
text: data.vertex,
styleClass: `graph-node graph-node--entity graph-node--${relatedType}`,
data: {
level,
iconClass,
entityName: data.vertex,
entityType: relatedType
}
}
},
async generateSecondLevelNodes (rootNode) {
2023-06-29 14:40:50 +08:00
/*
2023-06-30 18:43:02 +08:00
* 2级及以上的整数层节点先查数量大于0的才展示
2023-06-29 14:40:50 +08:00
* */
const relatedEntityCount = await this.queryRelatedEntityCount(this.entity.entityType, this.entity.entityName)
const nodes = []
2023-06-30 18:43:02 +08:00
if (relatedEntityCount) {
const ipNode = this.generatePrimaryNode({
id: 'ip-1',
text: this.$t('entities.graph.resolveIp'),
data: {
level: '2',
iconClass: 'cn-icon cn-icon-resolve-ip',
entityName: this.entity.entityName,
entityType: this.entity.entityType,
relatedType: 'ip'
2023-06-29 14:40:50 +08:00
}
2023-06-30 18:43:02 +08:00
})
const domainNode = this.generatePrimaryNode({
id: 'domain-1',
text: this.$t('entity.graph.resolveDomain'),
data: {
level: '2',
iconClass: 'cn-icon cn-icon-subdomain',
entityName: this.entity.entityName,
entityType: this.entity.entityType,
relatedType: 'domain'
2023-06-29 14:40:50 +08:00
}
2023-06-30 18:43:02 +08:00
})
const subdomainNode = this.generatePrimaryNode({
id: 'domain-1',
text: this.$t('entities.subdomain'),
data: {
level: '2',
iconClass: 'cn-icon cn-icon-subdomain',
entityName: this.entity.entityName,
entityType: this.entity.entityType,
relatedType: 'domain'
2023-06-29 14:40:50 +08:00
}
2023-06-30 18:43:02 +08:00
})
const appNode = this.generatePrimaryNode({
id: 'app-1',
text: this.$t('entities.tab.relatedApp'),
data: {
level: '2',
iconClass: 'cn-icon cn-icon-app-name',
entityName: this.entity.entityName,
entityType: this.entity.entityType,
relatedType: 'app'
2023-06-29 14:40:50 +08:00
}
2023-06-30 18:43:02 +08:00
})
switch (this.entity.entityType) {
case 'ip': {
if (relatedEntityCount.domainCount) {
domainNode.data.count = relatedEntityCount.domainCount
nodes.push(domainNode)
}
if (relatedEntityCount.appCount) {
domainNode.data.count = relatedEntityCount.appCount
nodes.push(appNode)
}
break
2023-06-29 14:40:50 +08:00
}
2023-06-30 18:43:02 +08:00
case 'domain': {
if (relatedEntityCount.ipCount) {
ipNode.data.count = relatedEntityCount.ipCount
nodes.push(ipNode)
}
if (relatedEntityCount.subDomainCount) {
subdomainNode.data.count = relatedEntityCount.subDomainCount
nodes.push(subdomainNode)
}
if (relatedEntityCount.appCount) {
appNode.data.count = relatedEntityCount.appCount
nodes.push(appNode)
}
break
2023-06-29 14:40:50 +08:00
}
2023-06-30 18:43:02 +08:00
case 'app': {
if (relatedEntityCount.ipCount) {
ipNode.data.count = relatedEntityCount.ipCount
nodes.push(ipNode)
}
if (relatedEntityCount.domainCount) {
domainNode.data.count = relatedEntityCount.domainCount
nodes.push(domainNode)
}
break
2023-06-29 14:40:50 +08:00
}
}
2023-06-30 18:43:02 +08:00
rootNode.data.childNodes = nodes
2023-06-29 14:40:50 +08:00
}
return nodes
},
2023-06-30 18:43:02 +08:00
async generateHalfLevelNodes (nodes) {
const newNodes = []
for (const node of nodes) {
const newNodes2 = []
const data = await this.queryRelatedEntity(node.data.entityType, node.data.entityName, node.data.relatedType)
if (data) {
// 生成节点
data.list.forEach(d => {
newNodes2.push(this.generateEntityNode(String(parseInt(node.data.level) + 0.5), node.data.relatedType, d))
})
// 更新源节点的已拓展数和列表
this.handleLoaded(node, data, newNodes2)
newNodes.push(...newNodes2)
}
}
return newNodes
},
2023-06-29 14:40:50 +08:00
async queryRelatedEntityCount (entityType, entityName) {
const response = await axios.get(`${api.entity.entityGraph.relatedEntityCount}/${entityType}?resource=${entityName}`).catch(e => {
console.error(e)
this.showError = true
this.errorMsg = this.errorMsgHandler(e)
})
if (response.data && response.data.code === 200) {
return response.data.data
} else {
console.error(response)
this.showError = true
this.errorMsg = this.errorMsgHandler(response)
return null
}
},
2023-06-30 18:43:02 +08:00
async queryRelatedEntity (entityType, entityName, relatedType) {
if (entityType === relatedType) {
// 若源type和关联type都是domain说明关联type是subdomain
relatedType = 'subdomain'
}
const response = await axios.get(`${api.entity.entityGraph[`${entityType}Related${_.upperFirst(relatedType)}`]}?resource=${entityName}`).catch(e => {
console.error(e)
this.showError = true
this.errorMsg = this.errorMsgHandler(e)
})
if (response.data && response.data.code === 200) {
return response.data.data
} else {
console.error(response)
this.showError = true
this.errorMsg = this.errorMsgHandler(response)
return null
}
},
/*
* 若只有一个参数则取参数的childNodes集合作为line终点
* 若有两个参数则以第一个参数作为line起点第二个参数作为终点
* */
generateLines (source, target) {
2023-06-29 14:40:50 +08:00
const lines = []
2023-06-30 18:43:02 +08:00
if (!target) {
const sourceArr = []
if (!source.length) {
sourceArr.push(source)
} else {
sourceArr.push(...source)
}
sourceArr.forEach(s => {
if (s.data && s.data.childNodes) {
const lines2 = []
s.data.childNodes.forEach(c => {
lines2.push({
from: s.id,
to: c.id,
color: '#BEBEBE'
})
})
s.lines = lines2
lines.push(...lines2)
}
2023-06-29 14:40:50 +08:00
})
2023-06-30 18:43:02 +08:00
}
2023-06-29 14:40:50 +08:00
return lines
},
2023-06-30 18:43:02 +08:00
handleLoaded (node, data, childNodes) {
if (!node.data.loaded) {
node.data.loaded = []
}
node.data.loaded.push(...data.list)
if (!node.data.childNodes) {
node.data.childNodes = []
}
node.data.childNodes.push(...childNodes)
},
onCloseBlock () {
// todo 关闭右侧graph面板
this.mode = ''
},
onExpandDetail (mode) {
this.mode = mode
},
onExpand (val) {
// todo 调用接口,拓展关系
},
onMouseenter (val) {
// todo 鼠标移动过graph列表名称时graph图的分支图形会变大一点
if (!val.isTrusted) {
// 鼠标移动反馈
}
2023-06-16 17:18:58 +08:00
}
2023-06-29 14:40:50 +08:00
},
async mounted () {
if (this.entity.entityType && this.entity.entityName) {
await this.init()
}
},
setup () {
const route = useRoute()
const { entityType, entityName } = route.query
const entity = {
entityType,
entityName
}
const mode = ref('ipList') // ipList, ipDetail, domainList, domainDetail, appList, appDetail
// echarts关系图的节点和连线
const graphData = ref({})
// 从url获取实体类型、名称若为空直接报错
const myChart = shallowRef(null)
return {
graphData,
entity,
myChart,
mode
}
2023-06-16 17:18:58 +08:00
}
}
</script>
<style lang="scss">
.entity-graph {
display: flex;
2023-06-30 18:43:02 +08:00
.rel-node-peel {
padding: 4px;
}
2023-06-16 17:18:58 +08:00
.entity-graph__chart {
width: calc(100% - 360px);
2023-06-29 14:40:50 +08:00
.graph-node {
display: flex;
flex-direction: column;
align-items: center;
background-color: transparent !important;
2023-06-30 18:43:02 +08:00
transition: linear all .2s;
2023-06-29 14:40:50 +08:00
.graph-node__text {
width: 120px;
font-size: 12px;
color: #353636;
}
&.graph-node--root {
padding-top: 18px;
width: 82px;
height: 82px;
border: 5px solid !important;
// 覆盖自带的node点击效果
&.rel-node-checked {
box-shadow: none;
animation: none;
}
&.graph-node--ip {
border-color: #CBD9BB !important;
box-shadow: 0 0 0 8px #EDF1E7;
i {
color: #7E9F54;
}
}
&.graph-node--domain {
border-color: #AFDEED !important;
2023-06-30 18:43:02 +08:00
box-shadow: 0 0 0 8px rgba(56,172,210,0.14);
2023-06-29 14:40:50 +08:00
i {
color: #38ACD2;
}
}
&.graph-node--app {
border-color: #F5DAA3 !important;
box-shadow: 0 0 0 8px #FBF2DF;
i {
color: #E5A219;
}
}
i {
font-size: 36px;
}
.graph-node__text {
padding-top: 30px;
}
}
&.graph-node--primary {
padding-top: 20px;
width: 66px;
height: 66px;
border: 1px solid #A7B0B9 !important;
box-shadow: none;
// 覆盖自带的node点击效果
&.rel-node-checked {
2023-06-30 18:43:02 +08:00
box-shadow: 0 0 0 5px rgba(151,151,151,0.21);
2023-06-29 14:40:50 +08:00
animation: none;
border-color: #778391 !important;
}
i {
font-size: 24px;
color: #778391;
}
.graph-node__text {
padding-top: 24px;
}
}
2023-06-30 18:43:02 +08:00
&.graph-node--entity {
width: 21px;
height: 21px;
justify-content: center;
border: none !important;
&.graph-node--ip {
i {
color: #7E9F54;
}
}
&.graph-node--domain {
i {
color: #38ACD2;
}
}
&.graph-node--app {
i {
color: #E5A219;
}
}
i {
font-size: 21px;
}
}
2023-06-29 14:40:50 +08:00
}
2023-06-16 17:18:58 +08:00
}
.entity-graph__detail {
width: 360px;
border-left: 1px solid #E2E5EC;
overflow: auto;
padding: 20px;
2023-06-16 17:18:58 +08:00
}
}
</style>