Make rate limite configurable, little entrypoint fix

This commit is contained in:
Romain de Laage 2021-04-18 10:43:12 +02:00
parent de4113154d
commit 92ee4fe2bd
Signed by: rdelaage
GPG Key ID: 534845FADDF0C329
5 changed files with 22 additions and 9 deletions

View File

@ -4,5 +4,6 @@
"key_path": "certs/key.rsa", "key_path": "certs/key.rsa",
"base_url": "https://mamot.fr", "base_url": "https://mamot.fr",
"title": "MastoGem", "title": "MastoGem",
"home_message": "Welcome on MastoGem, a Mastodon proxy for Gemini.\nYou can view the last 20 toots of a Mastodon account by providing its id, for example:\n=> gemini://localhost/310515 My Mastodon account" "home_message": "Welcome on MastoGem, a Mastodon proxy for Gemini.\nYou can view the last 20 toots of a Mastodon account by providing its id, for example:\n=> gemini://localhost/310515 My Mastodon account",
"rate_limit": 45
} }

View File

@ -38,6 +38,7 @@ type Config struct {
BaseURL string `json:"base_url"` BaseURL string `json:"base_url"`
Title string `json:"title"` Title string `json:"title"`
HomeMessage string `json:"home_message"` HomeMessage string `json:"home_message"`
RateLimit int `json:"rate_limit"`
} }
type Account struct { type Account struct {
@ -76,5 +77,5 @@ func main() {
log.Println("Server successfully started") log.Println("Server successfully started")
log.Println("Server is listening at " + config.Listen) log.Println("Server is listening at " + config.Listen)
serve(listener, config.BaseURL, config.Title, config.HomeMessage) serve(listener, config.BaseURL, config.Title, config.HomeMessage, config.RateLimit)
} }

View File

@ -48,14 +48,14 @@ func listen(address, certFile, keyFile string) net.Listener {
return listener return listener
} }
func serve(listener net.Listener, baseURL, title, home_message string) { func serve(listener net.Listener, baseURL, title, home_message string, rateLimit int) {
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
go handleConn(conn.(*tls.Conn), baseURL, title, home_message) go handleConn(conn.(*tls.Conn), baseURL, title, home_message, rateLimit)
} }
} }
@ -87,10 +87,10 @@ func getPath(conn *tls.Conn) (string, string, error) {
return parsedURL.Path, parsedURL.RawQuery, nil return parsedURL.Path, parsedURL.RawQuery, nil
} }
func handleConn(conn *tls.Conn, baseURL, title, home_message string) { func handleConn(conn *tls.Conn, baseURL, title, home_message string, rateLimit int) {
defer conn.Close() defer conn.Close()
if !rateIsOk(rateMap, strings.Split(conn.RemoteAddr().String(), ":")[0], 60) { if !rateIsOk(rateMap, strings.Split(conn.RemoteAddr().String(), ":")[0], rateLimit) {
log.Printf("Too many requests for %s\n", conn.RemoteAddr().String()) log.Printf("Too many requests for %s\n", conn.RemoteAddr().String())
_, err := fmt.Fprintf(conn, "44 60\r\n") _, err := fmt.Fprintf(conn, "44 60\r\n")
if err != nil { if err != nil {

View File

@ -15,18 +15,27 @@ fi
if [ ! -f /key.rsa ] if [ ! -f /key.rsa ]
then then
echo "You must bind a private key at /key.rsa" echo "You must bind a private key at /key.rsa"
exit 1
fi fi
if [ -z "$TITLE" ] if [ -z "$TITLE" ]
then then
echo "Using default title"
TITLE=MastoGem TITLE=MastoGem
fi fi
if [ -z "$HOME_MESSAGE" ] if [ -z "$HOME_MESSAGE" ]
then then
echo "Using default home message"
HOME_MESSAGE="Welcome on MastoGem, a Mastodon proxy for Gemini !" HOME_MESSAGE="Welcome on MastoGem, a Mastodon proxy for Gemini !"
fi fi
if [ -z "$RATE_LIMIT" ]
then
echo "Using default rate limit"
RATE_LIMIT=45
fi
cat << EOF > /config.json cat << EOF > /config.json
{ {
"listen": "0.0.0.0:1965", "listen": "0.0.0.0:1965",
@ -34,7 +43,8 @@ cat << EOF > /config.json
"key_path": "/key.rsa", "key_path": "/key.rsa",
"base_url": "$MASTODON_BASE_URL", "base_url": "$MASTODON_BASE_URL",
"title": "$TITLE", "title": "$TITLE",
"home_message": "$HOME_MESSAGE" "home_message": "$HOME_MESSAGE",
"rate_limit": $RATE_LIMIT
} }
EOF EOF

View File

@ -40,6 +40,7 @@ func getConfig() Config {
BaseURL: "https://mamot.fr", BaseURL: "https://mamot.fr",
Title: "MastoGem", Title: "MastoGem",
HomeMessage: "Welcome on MastoGem, this is a Mastodon proxy for Gemini. You can view the last 20 toots of a Mastodon account by providing its ID.", HomeMessage: "Welcome on MastoGem, this is a Mastodon proxy for Gemini. You can view the last 20 toots of a Mastodon account by providing its ID.",
RateLimit: 45,
} }
return config return config
@ -109,11 +110,11 @@ func formatBlog(toot Blog) string {
func rateIsOk(tab map[string]Rate, remoteIP string, limit int) bool { func rateIsOk(tab map[string]Rate, remoteIP string, limit int) bool {
elmt, ok := tab[remoteIP] elmt, ok := tab[remoteIP]
if ok == false { if ok == false {
tab[remoteIP] = Rate{time.Now(), 0} tab[remoteIP] = Rate{time.Now(), 1}
return true return true
} else { } else {
if time.Since(elmt.Date).Minutes() >= 1 { if time.Since(elmt.Date).Minutes() >= 1 {
tab[remoteIP] = Rate{time.Now(), 0} tab[remoteIP] = Rate{time.Now(), 1}
return true return true
} else { } else {
if elmt.Count < limit { if elmt.Count < limit {