Skip to content

Commit

Permalink
Detect tor users
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Jan 18, 2020
1 parent 01cc882 commit 03191bb
Show file tree
Hide file tree
Showing 11 changed files with 310 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- NaN

### Added
- NaN

## [1.0.4] - 2020-01-18
- Population density omitted if empty or null
- Detect tor users

## [1.0.3] - 2020-01-18
- ASN information added
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ GoGeoIP - a lightweight geoip api written in GO. [Live Demo](https://www.gogeoip
* Serve the default [GeoLite2 City](https://dev.maxmind.com/geoip/geoip2/geolite2/) free database that is downloaded and updated automatically in background on a configurable schedule, or
* Serve the commercial [GeoIP2 City](https://www.maxmind.com/en/geoip2-city) database from MaxMind, either as a local file that you provide and update periodically (so the server can reload it), or configured to be downloaded periodically using your API key
* Multiple languages are supported (en, ru, es, jp, fr, de)
* Detect tor and bot users
* Supports Linux, OS X, FreeBSD, and Windows

### Requirements
Expand Down Expand Up @@ -270,7 +271,8 @@ curl :8080/json/?user
"mobile": false,
"tablet": false,
"desktop": true,
"bot": false
"bot": false,
"tor": true
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (s *Server) openDB() error {
if _, err := s.Api.asnDB.OpenURL(); err != nil {
return err
}
if _, err := s.Api.torDB.OpenURL(); err != nil {
return err
}

return nil
}
Expand All @@ -50,6 +53,7 @@ func (s *Server) IpLookUp(writer writerFunc) http.HandlerFunc {
w.Header().Set("X-Database-Date", s.Api.db.Date().Format(http.TimeFormat))
lang := getRequestParam(r, "lang")

q.IsTorUser = s.Api.torDB.Lookup(ip)
resp := q.Record(ip, lang, r)
writer(w, r, resp)
}
Expand Down Expand Up @@ -136,6 +140,7 @@ func (q *GeoIpQuery) Record(ip net.IP, lang string, request *http.Request) *resp
Tablet: userAgent.Tablet,
Desktop: userAgent.Desktop,
Bot: userAgent.Bot,
Tor: q.IsTorUser,
},
}
}
Expand All @@ -157,6 +162,7 @@ func (rr *responseRecord) String() string {
var SystemTablet int;if rr.User.System.Tablet {SystemTablet = 1}
var SystemDesktop int;if rr.User.System.Desktop {SystemDesktop = 1}
var SystemBot int;if rr.User.System.Bot {SystemBot = 1}
var SystemTor int;if rr.User.System.Tor {SystemTor = 1}

err = w.Write([]string{
rr.IP,
Expand Down Expand Up @@ -188,6 +194,7 @@ func (rr *responseRecord) String() string {
strconv.Itoa(SystemTablet),
strconv.Itoa(SystemDesktop),
strconv.Itoa(SystemBot),
strconv.Itoa(SystemTor),
})
}else{
err = w.Write([]string{
Expand Down
5 changes: 5 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"../utils/config"
"../utils/db"
"../utils/tor"
"encoding/xml"
"github.com/rs/cors"
"golang.org/x/time/rate"
Expand Down Expand Up @@ -33,6 +34,7 @@ type Server struct {
type GeoIpQuery struct {
db.DefaultQuery
db.ASNDefaultQuery
db.TorDefaultQuery
}

type responseRecord struct {
Expand Down Expand Up @@ -82,11 +84,13 @@ type SystemRecord struct {
Tablet bool `json:"tablet"`
Desktop bool `json:"desktop"`
Bot bool `json:"bot"`
Tor bool `json:"tor"`
}

type ApiHandler struct {
db *db.DB
asnDB *db.DB
torDB *tor.Config
cors *cors.Cors
}

Expand All @@ -111,6 +115,7 @@ func NewServerConfig(c *config.Config) *Server {
Api: &ApiHandler{
db: db.NewDefaultConfig(c, c.ProductID),
asnDB: db.NewDefaultConfig(c, c.ASNProductID),
torDB: tor.NewDefaultConfig(c),
},
}

Expand Down
2 changes: 2 additions & 0 deletions utils/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func DefaultConfig() *Config {
ReadTimeout: 30 * time.Second,
WriteTimeout: 15 * time.Second,
UpdateInterval: 24 * time.Hour,
TorUpdateInterval: 30 * time.Minute,
RetryInterval: 2 * time.Hour,
RateLimitLimit: 1,
RateLimitBurst: 3,
Expand All @@ -50,6 +51,7 @@ func DefaultConfig() *Config {
RateLimitBackend: "redis",
RateLimitInterval: time.Hour,
UpdatesHost: "download.maxmind.com",
TorExitCheck: "8.8.8.8",

// https://www.maxmind.com/en/accounts/{UserID}/geoip/downloads?direct=1
ProductID: "GeoLite2-City",
Expand Down
2 changes: 2 additions & 0 deletions utils/config/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
ReadTimeout time.Duration `json:"READ_TIMEOUT"`
WriteTimeout time.Duration `json:"WRITE_TIMEOUT"`
UpdateInterval time.Duration `json:"UPDATE_INTERVAL"`
TorUpdateInterval time.Duration `json:"TOR_UPDATE_INTERVAL"`
RetryInterval time.Duration `json:"RETRY_INTERVAL"`
UseXForwardedFor bool `json:"USE_X_FORWARDED_FOR"`
Silent bool `json:"SILENT"`
Expand All @@ -48,6 +49,7 @@ type Config struct {
ProductID string `json:"PRODUCT_ID"`
ASNProductID string `json:"ASN_PRODUCT_ID"`
GuiDir string `json:"GUI"`
TorExitCheck string `json:"TOR_EXIT"`

File string `json:"-"`
RootDir string `json:"-"`
Expand Down
6 changes: 3 additions & 3 deletions utils/db/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
)

func (d *DB) makeDir(filename string) (dbdir string, err error) {
func MakeDir(filename string) (dbdir string, err error) {
dbdir = filepath.Dir(filename)
_, err = os.Stat(dbdir)
if err != nil {
Expand All @@ -22,9 +22,9 @@ func (d *DB) makeDir(filename string) (dbdir string, err error) {
return dbdir, nil
}

func (d *DB) renameFile(fromName string, toName string) error {
func RenameFile(fromName string, toName string) error {
if err := os.Rename(toName, toName+".bak"); err != nil {}
if _, err := d.makeDir(toName); err != nil {
if _, err := MakeDir(toName); err != nil {
return err
}
return os.Rename(fromName, toName)
Expand Down
2 changes: 1 addition & 1 deletion utils/db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (d *DB) watchFile() error {
if err != nil {
return err
}
dbdir, err := d.makeDir(d.dbArchive)
dbdir, err := MakeDir(d.dbArchive)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions utils/db/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ type ASNDefaultQuery struct {
AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
}

type TorDefaultQuery struct {
IsTorUser bool
}
2 changes: 1 addition & 1 deletion utils/db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (d *DB) runUpdate(url string) error {
if err != nil {
return err
}
err = d.renameFile(tmpFile, d.dbArchive)
err = RenameFile(tmpFile, d.dbArchive)
if err != nil {
// Cleanup the temp file if renaming failed.
if err := os.RemoveAll(tmpFile); err != nil {}
Expand Down
Loading

0 comments on commit 03191bb

Please sign in to comment.