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

Whitelist #414

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"sync/atomic"
"time"

"fmt"
"regexp"

"github.com/gorilla/mux"

"github.com/sammy007/open-ethereum-pool/storage"
Expand Down Expand Up @@ -45,6 +48,10 @@ type Entry struct {
updatedAt int64
}

type WhiteList struct {
IPs []string `json:"ips"`
}

func NewApiServer(cfg *ApiConfig, backend *storage.RedisClient) *ApiServer {
hashrateWindow := util.MustParseDuration(cfg.HashrateWindow)
hashrateLargeWindow := util.MustParseDuration(cfg.HashrateLargeWindow)
Expand Down Expand Up @@ -108,13 +115,51 @@ func (s *ApiServer) listen() {
r.HandleFunc("/api/blocks", s.BlocksIndex)
r.HandleFunc("/api/payments", s.PaymentsIndex)
r.HandleFunc("/api/accounts/{login:0x[0-9a-fA-F]{40}}", s.AccountIndex)
r.HandleFunc("/api/whitelist", s.AddToWhiteListIndex)
r.NotFoundHandler = http.HandlerFunc(notFound)
err := http.ListenAndServe(s.config.Listen, r)
if err != nil {
log.Fatalf("Failed to start API: %v", err)
}
}

func (s *ApiServer) AddToWhiteListIndex(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Cache-Control", "no-cache")

reply := make(map[string]interface{})
decoder := json.NewDecoder(r.Body)
var whiteList WhiteList
err := decoder.Decode(&whiteList)

if err != nil {
reply["success"] = false
reply["message"] = fmt.Sprintf("Error occured: %v", err)
err := json.NewEncoder(w).Encode(reply)
if err != nil {
log.Println("Error serializing API response: ", err)
}
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
reg, _ := regexp.Compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")
for _, ip := range whiteList.IPs {
if reg.MatchString(ip){
err = s.backend.AddToWhiteList(ip)
if err != nil {
log.Println(err)
}
}
}
reply["success"] = true
reply["message"] = fmt.Sprintf("Add to IP white list successful.")
}else{
http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
}
}

func notFound(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
Expand Down
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"limit": 30,
"grace": "5m",
"limitJump": 10
}
},
"whiteListOnly": true
}
},

Expand Down
4 changes: 4 additions & 0 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
Limits Limits `json:"limits"`
ResetInterval string `json:"resetInterval"`
RefreshInterval string `json:"refreshInterval"`
WhiteListOnly bool `json:"whiteListOnly"`
}

type Limits struct {
Expand Down Expand Up @@ -186,6 +187,9 @@ func (s *PolicyServer) BanClient(ip string) {
}

func (s *PolicyServer) IsBanned(ip string) bool {
if(s.config.whiteListOnly){
return !s.InWhiteList(ip)
}
x := s.Get(ip)
return atomic.LoadInt32(&x.Banned) > 0
}
Expand Down
8 changes: 8 additions & 0 deletions storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,3 +957,11 @@ func convertPaymentsResults(raw *redis.ZSliceCmd) []map[string]interface{} {
}
return result
}

func (r *RedisClient) AddToWhiteList(ip string) error {
cmd := r.client.SAdd(r.formatKey("whitelist"), ip)
if cmd.Err() != nil {
return cmd.Err()
}
return nil
}