Add rate limit
This commit is contained in:
parent
545ebe8321
commit
de4113154d
13
main.go
13
main.go
|
@ -17,7 +17,10 @@
|
||||||
*/
|
*/
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "log"
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Blog struct {
|
type Blog struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
|
@ -58,8 +61,16 @@ type Tag struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Rate struct {
|
||||||
|
Date time.Time
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
|
var rateMap map[string]Rate
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := getConfig()
|
config := getConfig()
|
||||||
|
rateMap = make(map[string]Rate)
|
||||||
|
|
||||||
listener := listen(config.Listen, config.CertPath, config.KeyPath)
|
listener := listen(config.Listen, config.CertPath, config.KeyPath)
|
||||||
log.Println("Server successfully started")
|
log.Println("Server successfully started")
|
||||||
|
|
10
server.go
10
server.go
|
@ -90,6 +90,16 @@ func getPath(conn *tls.Conn) (string, string, error) {
|
||||||
func handleConn(conn *tls.Conn, baseURL, title, home_message string) {
|
func handleConn(conn *tls.Conn, baseURL, title, home_message string) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
if !rateIsOk(rateMap, strings.Split(conn.RemoteAddr().String(), ":")[0], 60) {
|
||||||
|
log.Printf("Too many requests for %s\n", conn.RemoteAddr().String())
|
||||||
|
_, err := fmt.Fprintf(conn, "44 60\r\n")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("send error: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
path, query, err := getPath(conn)
|
path, query, err := getPath(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("get url: %s", err)
|
log.Println("get url: %s", err)
|
||||||
|
|
21
util.go
21
util.go
|
@ -25,6 +25,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getConfig() Config {
|
func getConfig() Config {
|
||||||
|
@ -104,3 +105,23 @@ func formatBlog(toot Blog) string {
|
||||||
|
|
||||||
return "### Written by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16] + "\n" + content + "\n=> /toot/" + toot.Id + " More informations about this toot"
|
return "### Written by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16] + "\n" + content + "\n=> /toot/" + toot.Id + " More informations about this toot"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rateIsOk(tab map[string]Rate, remoteIP string, limit int) bool {
|
||||||
|
elmt, ok := tab[remoteIP]
|
||||||
|
if ok == false {
|
||||||
|
tab[remoteIP] = Rate{time.Now(), 0}
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
if time.Since(elmt.Date).Minutes() >= 1 {
|
||||||
|
tab[remoteIP] = Rate{time.Now(), 0}
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
if elmt.Count < limit {
|
||||||
|
tab[remoteIP] = Rate{elmt.Date, elmt.Count + 1}
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue