Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Servemux #10

Merged
merged 7 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
xoracle
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
![cd-status](https://img.shields.io/github/actions/workflow/status/nronzel/xoracle/cd.yml?label=cd)
![top-lang](https://img.shields.io/github/languages/top/nronzel/xoracle?logo=go)
![docker-pulls](https://img.shields.io/docker/pulls/sutats/xoracle?logo=docker)
![licent](https://img.shields.io/github/license/nronzel/xoracle)
![license](https://img.shields.io/github/license/nronzel/xoracle)
![tag](https://img.shields.io/github/v/tag/nronzel/xoracle)

# XORacle

Expand All @@ -20,7 +21,8 @@ This project is dockerized and gets deployed to Google Cloud Run.
- [x] Rate limiting
- [ ] Better HTMX errors
- [ ] More tests
- [ ] Semver versioning
- [x] Use the new ServeMux in Go 1.22 to replace Chi
- [x] Semver versioning

## Features

Expand Down Expand Up @@ -104,7 +106,6 @@ cd xoracle

3. Install dependencies:

- Chi v5
- golang.org/x/time

Install dependencies with the command:
Expand Down
11 changes: 4 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ import (

"github.com/nronzel/xoracle/pkg/handlers"
limiter "github.com/nronzel/xoracle/pkg/rate_limiter"

"github.com/go-chi/chi/v5"
)

func main() {
r := chi.NewRouter()

r.Get("/", handlers.HandlerRoot)
mux := http.NewServeMux()

r.Post("/decrypt", handlers.HandlerDecrypt)
mux.HandleFunc("GET /", handlers.HandlerRoot)
mux.HandleFunc("POST /decrypt", handlers.HandlerDecrypt)

rl := limiter.NewRateLimiter(1, 3)

server := &http.Server{
Addr: ":8080",
Handler: rl.Limit(r),
Handler: rl.Limit(mux),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
Expand Down
2 changes: 1 addition & 1 deletion pkg/decryption/decryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type DecryptionResult struct {
DecryptedData string
}

// processKeySizes attempts to decrypt the provided byte slice (data) for each
// ProcessKeySizes attempts to decrypt the provided byte slice (data) for each
// of the top key sizes found. It attempts to break a repeating-key XOR cipher
// without directly knowing the key.
func ProcessKeySizes(topKeySizes []int, data []byte) []DecryptionResult {
Expand Down
1 change: 1 addition & 0 deletions pkg/rate_limiter/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (rl *RateLimiter) Limit(next http.Handler) http.Handler {
// Call getVisitor func to retrive the rate limiter for the current user
limiter := rl.getVisitor(ip)
if !limiter.Allow() {
log.Printf("%s has been rate limited", ip)
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
return
}
Expand Down