Skip to content

Commit 75f66ec

Browse files
authored
Merge pull request #17 from ins0mn1a93/main
代码合并
2 parents 07d7aa3 + dcf3dfb commit 75f66ec

File tree

5 files changed

+140
-54
lines changed

5 files changed

+140
-54
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ web = '127.0.0.1:8080'
2424
username = 'username'
2525
password = 'password'
2626

27+
# Option to capture traffic, default is false
28+
capture = true
29+
30+
# Number of data items displayed on the front-end page
31+
# Default is 200
32+
page_size = 200.0
33+
2734
[[tic]]
2835
type = 'local'
2936

@@ -138,3 +145,8 @@ type Threat struct {
138145
- `cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH%`
139146

140147
Install `gcc` to fix it
148+
149+
- `# github.com/google/gopacket/pcap`
150+
`../go/pkg/mod/github.com/google/gopacket@v1.1.19/pcap/pcap_unix.go:34:10: fatal error: pcap.h: No such file or directory`
151+
152+
Run `apt-get install libpcap-dev` to solve it.

frontend/src/pages/config.vue

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,63 @@
11
<script setup lang="ts">
2-
import { ref, onMounted } from 'vue'
3-
import axios from 'axios'
4-
import { ElForm, ElFormItem, ElInput, ElButton, ElMessage, ElInputNumber, ElSwitch, ElSkeleton, ElSpace, ElMessageBox } from 'element-plus'
5-
import { user } from '../utils'
6-
import { useRouter } from 'vue-router';
2+
import { ref, onMounted } from 'vue'
3+
import axios from 'axios'
4+
import { ElForm, ElFormItem, ElInput, ElButton, ElMessage, ElInputNumber, ElSwitch, ElSkeleton, ElSpace, ElMessageBox } from 'element-plus'
5+
import { user } from '../utils'
6+
import { useRouter } from 'vue-router';
77
88
9-
const router = useRouter()
10-
const config = ref()
9+
const router = useRouter()
10+
const config = ref()
1111
12-
onMounted(() => {
13-
if (user.value) {
14-
axios.get('/api/config', {
15-
headers: {
16-
Authorization: `Bearer ${user.value.token}`
17-
}
18-
}).then(res => {
19-
config.value = res.data
20-
}).catch(e => {
21-
if (e.response.status === 401) {
22-
router.push('/login')
23-
} else {
24-
ElMessage.error(e.response.data.error ?? e.message)
25-
}
26-
})
27-
}
28-
})
12+
onMounted(() => {
13+
if (user.value) {
14+
axios.get('/api/config', {
15+
headers: {
16+
Authorization: `Bearer ${user.value.token}`
17+
}
18+
}).then(res => {
19+
config.value = res.data
20+
}).catch(e => {
21+
if (e.response.status === 401) {
22+
router.push('/login')
23+
} else {
24+
ElMessage.error(e.response.data.error ?? e.message)
25+
}
26+
})
27+
}
28+
})
2929
30-
const update = () => {
31-
ElMessageBox.confirm('确定要更新配置文件吗?').then(() => {
32-
axios.post('/api/config', config.value, {
33-
headers: {
34-
Authorization: `Bearer ${user.value?.token}`
35-
}
36-
}).then(() => {
37-
ElMessage.success('更新成功')
38-
}).catch(e => {
39-
if (e.response.status === 401) {
40-
router.push('/login')
41-
} else {
42-
ElMessage.error(e.response.data.error ?? e.message)
43-
}
30+
const update = () => {
31+
const checkPeriod = `${config.value.check_period}s`;
32+
const updatedConfig = { ...config.value, check_period: checkPeriod };
33+
ElMessageBox.confirm('确定要更新配置文件吗?').then(() => {
34+
axios.post('/api/config', updatedConfig, {
35+
headers: {
36+
Authorization: `Bearer ${user.value?.token}`
37+
}
38+
}).then(() => {
39+
ElMessage.success('更新成功,相关配置将会在重新登录后生效')
40+
}).catch(e => {
41+
if (e.response.status === 401) {
42+
router.push('/login')
43+
} else {
44+
ElMessage.error(e.response.data.error ?? e.message)
45+
}
46+
})
4447
})
45-
})
46-
}
48+
}
4749
</script>
4850

4951
<template>
5052
<ElForm label-position="left" label-width="auto">
5153
<div v-if="config">
52-
<ElFormItem label="抓包间隔">
53-
<ElInput v-model="config.capture_interval" />
54-
</ElFormItem>
5554
<ElFormItem label="检测间隔">
56-
<ElInput v-model="config.check_interval" />
55+
<ElInputNumber v-model="config.check_period" />
56+
<span>秒</span>
57+
</ElFormItem>
58+
<ElFormItem label="记录数据数">
59+
<ElInputNumber v-model="config.page_size" />
60+
<span>条</span>
5761
</ElFormItem>
5862
<ElFormItem label="管理地址">
5963
<ElInput v-model="config.web" />
@@ -85,7 +89,7 @@ const update = () => {
8589
</template>
8690

8791
<style scoped>
88-
.tics {
89-
flex-grow: 1;
90-
}
92+
.tics {
93+
flex-grow: 1;
94+
}
9195
</style>

frontend/src/pages/index.vue

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,66 @@ const mainOption = computed(() => {
3939
}
4040
})
4141
42-
const pieViewOption = computed(() => {
42+
const pieViewOption1 = computed(() => {
43+
return {
44+
dataset: {
45+
source: [
46+
['威胁等级', '数量'],
47+
['安全', stats.value[8][1]], // 近七日安全的数量
48+
['正常', stats.value[8][2]], // 近七日正常的数量
49+
['可疑', stats.value[8][3]], // 近七日可疑的数量
50+
['恶意', stats.value[8][4]], // 近七日恶意的数量
51+
['未知', stats.value[8][5]], // 近七日未知的数量
52+
],
53+
},
54+
color: ['#5470c6', '#91cc75', '#73c0de', '#fac858', '#ee6666'],
55+
tooltip: {
56+
trigger: 'item',
57+
formatter: '{a} <br/>{b}: {c} ({d}%)',
58+
},
59+
series: [
60+
{
61+
type: 'pie',
62+
radius: '55%',
63+
center: ['50%', '50%'],
64+
data: [
65+
{ value: stats.value[8][1], name: '安全' },
66+
{ value: stats.value[8][2], name: '正常' },
67+
{ value: stats.value[8][3], name: '可疑' },
68+
{ value: stats.value[8][4], name: '恶意' },
69+
{ value: stats.value[8][5], name: '未知' },
70+
],
71+
emphasis: {
72+
itemStyle: {
73+
shadowBlur: 10,
74+
shadowOffsetX: 0,
75+
shadowOffsetY: 0,
76+
shadowColor: 'rgba(0, 0, 0, 0.5)',
77+
},
78+
},
79+
},
80+
],
81+
}
82+
})
83+
84+
const pieViewOption2 = computed(() => {
85+
return {
86+
dataset: {
87+
source: stats.value,
88+
}, backgroundColor: '',
89+
color: ['#5470c6', '#91cc75', '#73c0de', '#fac858', '#ee6666'],
90+
tooltip: {},
91+
series: [
92+
{
93+
type: 'pie',
94+
seriesLayoutBy: 'row',
95+
encode: { itemName: 0, value: 8 },
96+
},
97+
],
98+
}
99+
})
100+
101+
const pieViewOption3 = computed(() => {
43102
return {
44103
dataset: {
45104
source: stats.value,
@@ -125,7 +184,7 @@ const lineTrendOption = computed(() => {
125184
<div class="subviews">
126185
<div class="subview-chart">
127186
<h3>近七日威胁度</h3>
128-
<VChart :option="pieViewOption" theme="dark" autoresize />
187+
<VChart :option="pieViewOption1" theme="dark" autoresize />
129188
</div>
130189
<div class="subview-chart">
131190
<h3>可疑及以上威胁度的频率</h3>
@@ -141,11 +200,11 @@ const lineTrendOption = computed(() => {
141200
</div>
142201
<div class="subview-chart">
143202
<h3>情报来源占比</h3>
144-
<VChart :option="pieViewOption" theme="dark" autoresize />
203+
<VChart :option="pieViewOption2" theme="dark" autoresize />
145204
</div>
146205
<div class="subview-chart">
147206
<h3>To be continue</h3>
148-
<VChart :option="pieViewOption" theme="dark" autoresize />
207+
<VChart :option="pieViewOption3" theme="dark" autoresize />
149208
</div>
150209
</div>
151210
</div>

util/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
)
88

99
func init() {
10-
viper.SetDefault("capture_interval", "10s")
1110
viper.SetDefault("check_period", "60s")
12-
viper.SetDefault("buffer_size", 2000)
1311
viper.SetDefault("page_size", 200)
1412
viper.SetConfigFile("config.toml")
1513
err := viper.ReadInConfig()

util/web.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,20 @@ func threatsHandler(c *gin.Context) {
8686
c.JSON(200, records)
8787
}
8888
func readConfigHandler(c *gin.Context) {
89-
c.JSON(200, viper.AllSettings())
89+
settings := viper.AllSettings()
90+
91+
if checkPeriod, ok := settings["check_period"].(string); ok {
92+
if strings.HasSuffix(checkPeriod, "s") {
93+
checkPeriodWithoutUnit := strings.TrimSuffix(checkPeriod, "s")
94+
if value, err := strconv.Atoi(checkPeriodWithoutUnit); err == nil {
95+
settings["check_period"] = value
96+
} else {
97+
fmt.Println("无法转换 check_period 为整数", err)
98+
}
99+
}
100+
}
101+
102+
c.JSON(200, settings)
90103
}
91104

92105
func threatsOperationHandler(c *gin.Context) {

0 commit comments

Comments
 (0)