Make rate limite configurable, little entrypoint fix
This commit is contained in:
parent
de4113154d
commit
92ee4fe2bd
@ -4,5 +4,6 @@
|
||||
"key_path": "certs/key.rsa",
|
||||
"base_url": "https://mamot.fr",
|
||||
"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
|
||||
}
|
||||
|
3
main.go
3
main.go
@ -38,6 +38,7 @@ type Config struct {
|
||||
BaseURL string `json:"base_url"`
|
||||
Title string `json:"title"`
|
||||
HomeMessage string `json:"home_message"`
|
||||
RateLimit int `json:"rate_limit"`
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
@ -76,5 +77,5 @@ func main() {
|
||||
log.Println("Server successfully started")
|
||||
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)
|
||||
}
|
||||
|
@ -48,14 +48,14 @@ func listen(address, certFile, keyFile string) net.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 {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
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())
|
||||
_, err := fmt.Fprintf(conn, "44 60\r\n")
|
||||
if err != nil {
|
||||
|
12
start.sh
12
start.sh
@ -15,18 +15,27 @@ fi
|
||||
if [ ! -f /key.rsa ]
|
||||
then
|
||||
echo "You must bind a private key at /key.rsa"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$TITLE" ]
|
||||
then
|
||||
echo "Using default title"
|
||||
TITLE=MastoGem
|
||||
fi
|
||||
|
||||
if [ -z "$HOME_MESSAGE" ]
|
||||
then
|
||||
echo "Using default home message"
|
||||
HOME_MESSAGE="Welcome on MastoGem, a Mastodon proxy for Gemini !"
|
||||
fi
|
||||
|
||||
if [ -z "$RATE_LIMIT" ]
|
||||
then
|
||||
echo "Using default rate limit"
|
||||
RATE_LIMIT=45
|
||||
fi
|
||||
|
||||
cat << EOF > /config.json
|
||||
{
|
||||
"listen": "0.0.0.0:1965",
|
||||
@ -34,7 +43,8 @@ cat << EOF > /config.json
|
||||
"key_path": "/key.rsa",
|
||||
"base_url": "$MASTODON_BASE_URL",
|
||||
"title": "$TITLE",
|
||||
"home_message": "$HOME_MESSAGE"
|
||||
"home_message": "$HOME_MESSAGE",
|
||||
"rate_limit": $RATE_LIMIT
|
||||
}
|
||||
EOF
|
||||
|
||||
|
5
util.go
5
util.go
@ -40,6 +40,7 @@ func getConfig() Config {
|
||||
BaseURL: "https://mamot.fr",
|
||||
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.",
|
||||
RateLimit: 45,
|
||||
}
|
||||
|
||||
return config
|
||||
@ -109,11 +110,11 @@ func formatBlog(toot Blog) string {
|
||||
func rateIsOk(tab map[string]Rate, remoteIP string, limit int) bool {
|
||||
elmt, ok := tab[remoteIP]
|
||||
if ok == false {
|
||||
tab[remoteIP] = Rate{time.Now(), 0}
|
||||
tab[remoteIP] = Rate{time.Now(), 1}
|
||||
return true
|
||||
} else {
|
||||
if time.Since(elmt.Date).Minutes() >= 1 {
|
||||
tab[remoteIP] = Rate{time.Now(), 0}
|
||||
tab[remoteIP] = Rate{time.Now(), 1}
|
||||
return true
|
||||
} else {
|
||||
if elmt.Count < limit {
|
||||
|
Loading…
Reference in New Issue
Block a user