增加响应频率限制

This commit is contained in:
Srar 2019-08-14 12:51:24 +08:00
parent 6b441c520c
commit 51a37041f1
3 changed files with 25 additions and 7 deletions

View File

@ -53,4 +53,5 @@ addr = 0.0.0.0:21 # Ftp 服务端地址 注意端口
[memcache]
status = 0 # 是否启动 Memcache 1 启动 0 关闭
addr = 0.0.0.0:11211 # Memcache 服务端地址 注意端口冲突
addr = 0.0.0.0:11211 # Memcache 服务端地址 注意端口冲突
ratelimit = 4 # 每秒响应次数

View File

@ -396,7 +396,7 @@ var commands = map[string]func([]string) ([]byte, int){
}
// TCP服务端连接
func tcpServer(address string, exitChan chan int) {
func tcpServer(address string, rateLimitChan chan int, exitChan chan int) {
l, err := net.Listen("tcp", address)
if err != nil {
@ -422,6 +422,7 @@ func tcpServer(address string, exitChan chan int) {
reader := bufio.NewReader(conn)
log.Printf("[Memcache TCP %d] Accepted a client socket from %s\n", trackID, conn.RemoteAddr().String())
for {
<-rateLimitChan
str, err := reader.ReadString('\n')
if skip {
skip = false
@ -470,7 +471,7 @@ func tcpServer(address string, exitChan chan int) {
}
func udpServer(address string, exitChan chan int) {
func udpServer(address string, rateLimitChan chan int, exitChan chan int) {
udpAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
fmt.Println(err.Error())
@ -488,6 +489,7 @@ func udpServer(address string, exitChan chan int) {
go func() {
buf := make([]byte, 1500)
for {
<-rateLimitChan
plen, addr, _ := l.ReadFromUDP(buf)
/* UDP协议需要8个字节的头 */
if plen < 8 {
@ -522,13 +524,27 @@ func udpServer(address string, exitChan chan int) {
}()
}
func Start(addr string) {
func Start(addr string, rateLimitStr string) {
// 创建一个程序结束码的通道
exitChan := make(chan int)
// 响应间隔限制
rateLimitChan := make(chan int)
rateLimit, err := strconv.Atoi(rateLimitStr)
if err != nil {
panic(err)
}
go func() {
sleepTime := 1000 / rateLimit
for {
rateLimitChan <- 1
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
}
}()
// 将服务器并发运行
go tcpServer(addr, exitChan)
go udpServer(addr, exitChan)
go tcpServer(addr, rateLimitChan, exitChan)
go udpServer(addr, rateLimitChan, exitChan)
// 通道阻塞,等待接受返回值
code := <-exitChan

View File

@ -211,11 +211,12 @@ func Run() {
// 启动 Memcache 蜜罐
memcacheStatus := conf.Get("memcache", "status")
memcacheRateLimit := conf.Get("memcache", "ratelimit")
// 判断 暗网 Web 蜜罐 是否开启
if memcacheStatus == "1" {
memcacheAddr := conf.Get("memcache", "addr")
go memcache.Start(memcacheAddr)
go memcache.Start(memcacheAddr, memcacheRateLimit)
}
//=========================//