Skip to content

Commit

Permalink
More stuff for banning
Browse files Browse the repository at this point in the history
  • Loading branch information
espebra committed Apr 20, 2024
1 parent 6da5295 commit 50ec1b6
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 222 deletions.
10 changes: 5 additions & 5 deletions dbl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,26 @@ func (c *ClientDao) Update(client *ds.Client) (err error) {
}

func (c *ClientDao) GetAll() (clients []ds.Client, err error) {
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client ORDER BY last_active_at ASC"
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client ORDER BY last_active_at DESC"
clients, err = c.clientQuery(sqlStatement)
return clients, err
}

func (c *ClientDao) GetByLastActiveAt(limit int) (clients []ds.Client, err error) {
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client ORDER BY last_active_at ASC LIMIT $1"
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client ORDER BY last_active_at DESC LIMIT $1"
clients, err = c.clientQuery(sqlStatement, limit)
return clients, err
}

func (c *ClientDao) GetByRequests(limit int) (clients []ds.Client, err error) {
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client ORDER BY requests ASC LIMIT $1"
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client ORDER BY requests DESC LIMIT $1"
clients, err = c.clientQuery(sqlStatement, limit)
return clients, err
}

func (c *ClientDao) GetByBannedAt(limit int) (clients []ds.Client, err error) {
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client ORDER BY banned_at ASC LIMIT $1"
clients, err = c.clientQuery(sqlStatement, limit)
sqlStatement := "SELECT ip, asn, network, city, country, continent, proxy, requests, first_active_at, last_active_at, banned_at, banned_by FROM client WHERE banned_at > $1 ORDER BY banned_at DESC LIMIT $2"
clients, err = c.clientQuery(sqlStatement, time.Unix(0, 0), limit)
return clients, err
}

Expand Down
9 changes: 7 additions & 2 deletions geoip/mmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ func (dao DAO) LookupCity(ip net.IP, client *ds.Client) error {

func (dao DAO) Lookup(remoteAddr string, client *ds.Client) (err error) {
// Parse the client IP address
host, _, _ := net.SplitHostPort(remoteAddr)
ip := net.ParseIP(host)
host, _, err := net.SplitHostPort(remoteAddr)
var ip net.IP
if err == nil {
ip = net.ParseIP(host)
} else {
ip = net.ParseIP(remoteAddr)
}
if ip == nil {
return errors.New(fmt.Sprintf("Unable to parse remote addr %s", remoteAddr))
}
Expand Down
21 changes: 13 additions & 8 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"embed"
"errors"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -321,14 +322,18 @@ func (h *HTTP) ParseTemplates() *template.Template {
return templ
}

func extractIP(addr string) (ip string, err error) {
host, _, _ := net.SplitHostPort(addr)
//if err != nil {
// fmt.Printf("Error 1: %s\n", err.Error())
// return ip, err
//}
ipRaw := net.ParseIP(host)
return ipRaw.String(), nil
func extractIP(addr string) (string, error) {
host, _, err := net.SplitHostPort(addr)
var ip net.IP
if err == nil {
ip = net.ParseIP(host)
} else {
ip = net.ParseIP(addr)
}
if ip == nil {
return ip.String(), errors.New(fmt.Sprintf("Unable to parse remote addr %s", addr))
}
return ip.String(), nil
}

func inStringSlice(needle string, haystack []string) bool {
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

var (
// Various
expirationFlag = flag.Int("expiration", 604800, "Bin expiration time in seconds since the last bin update")
tmpdirFlag = flag.String("tmpdir", os.TempDir(), "Directory for temporary files for upload and download")
baseURLFlag = flag.String("baseurl", "https://filebin.net", "The base URL to use. Required for self-hosted instances.")
expirationFlag = flag.Int("expiration", 604800, "Bin expiration time in seconds since the last bin update")
tmpdirFlag = flag.String("tmpdir", os.TempDir(), "Directory for temporary files for upload and download")
baseURLFlag = flag.String("baseurl", "https://filebin.net", "The base URL to use. Required for self-hosted instances.")
requireApprovalFlag = flag.Bool("manual-approval", false, "Require manual admin approval of new bins before files can be downloaded.")
//enableBanningFlag = flag.Bool("enable-banning", false, "Enable banning. This will allow anyone to ban client IP addresses that upload files to filebin.")
mmdbCityPathFlag = flag.String("mmdb-city", "", "The path to an mmdb formatted geoip database like GeoLite2-City.mmdb.")
Expand Down
8 changes: 4 additions & 4 deletions templates/admin_bar.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
<a class="nav-link" href="/admin">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/admin/clients">Top clients</a>
<a class="nav-link" href="/admin/bins">Top bins</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/admin/clients/all">All clients</a>
<a class="nav-link" href="/admin/clients">Top clients</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/admin/bins">Top bins</a>
<a class="nav-link" href="/admin/files">Top files</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/admin/bins/all">All bins</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/admin/files">Top files</a>
<a class="nav-link" href="/admin/clients/all">All clients</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/debug/pprof/">Profiling</a>
Expand Down
Loading

0 comments on commit 50ec1b6

Please sign in to comment.