-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from BOOMfinity/slave
rewritten to client
- Loading branch information
Showing
12 changed files
with
273 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,5 @@ | ||
module github.com/BOOMfinity/GrOxyP | ||
|
||
go 1.18 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/segmentio/encoding v0.3.6 | ||
github.com/yl2chen/cidranger v1.0.2 | ||
) | ||
|
||
require ( | ||
github.com/segmentio/asm v1.2.0 // indirect | ||
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect | ||
) | ||
require github.com/yl2chen/cidranger v1.0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yl2chen/cidranger" | ||
"net" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
// Config is a structure of environmental variables | ||
type Config struct { | ||
DatabaseDownloadURL string | ||
DatabaseUpdateInterval string | ||
WebserverPort string | ||
WebserverToken string | ||
Debug bool | ||
} | ||
|
||
type Client struct { | ||
Conf Config | ||
Database cidranger.Ranger | ||
HTTPClient *http.Client | ||
} | ||
|
||
// NewClient creates new client with the given config and immediately updates its database. It will also run automatic updates, if interval is specified. | ||
func NewClient(conf Config) (client *Client, err error) { | ||
client = &Client{ | ||
Conf: conf, | ||
HTTPClient: &http.Client{}, | ||
Database: cidranger.NewPCTrieRanger(), | ||
} | ||
err = client.Update() | ||
if err != nil { | ||
return &Client{}, err | ||
} | ||
if client.Conf.DatabaseUpdateInterval != "" { | ||
go func() { | ||
err = client.runAutoUpdates() | ||
if err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, "GrOxyP auto-update error:", err) | ||
} | ||
}() | ||
} | ||
return client, nil | ||
} | ||
|
||
// FindIP checks if a given IP is on the list. If so, returns `true` and the reason (IP block). | ||
func (c *Client) FindIP(query net.IP) (bool, string) { | ||
if containingNetworks, err := c.Database.ContainingNetworks(query); len(containingNetworks) > 0 && err == nil { | ||
network := containingNetworks[0].Network() | ||
return true, network.String() | ||
} | ||
return false, "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package client | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"github.com/yl2chen/cidranger" | ||
"log" | ||
"net" | ||
"time" | ||
) | ||
|
||
// Update will flush and repopulate database with freshly downloaded list. | ||
func (c *Client) Update() (err error) { | ||
if c.Conf.Debug { | ||
fmt.Println("INFO: Downloading database...") | ||
} | ||
response, err := c.HTTPClient.Get(c.Conf.DatabaseDownloadURL) | ||
if err != nil { | ||
return | ||
} | ||
defer response.Body.Close() | ||
if response.StatusCode != 200 { | ||
return errors.New(fmt.Sprintf("received code %v while downloading database", response.StatusCode)) | ||
} | ||
if c.Conf.Debug { | ||
fmt.Println("INFO: Database downloaded. Parsing...") | ||
} | ||
// Flushing entries | ||
c.Database = cidranger.NewPCTrieRanger() | ||
|
||
// Importing | ||
scanner := bufio.NewScanner(response.Body) | ||
for scanner.Scan() { | ||
if _, currNet, err := net.ParseCIDR(scanner.Text()); err == nil { | ||
err := c.Database.Insert(cidranger.NewBasicRangerEntry(*currNet)) | ||
if err != nil { | ||
fmt.Printf("Error while inserting CIDR to database: %v\n", err.Error()) | ||
} | ||
} | ||
} | ||
if c.Conf.Debug { | ||
fmt.Printf("INFO: Database parsed. %v entries.\n", c.Database.Len()) | ||
} | ||
return scanner.Err() | ||
} | ||
|
||
// runAutoUpdates is simple function to run Update at set interval | ||
func (c *Client) runAutoUpdates() error { | ||
interval, err := time.ParseDuration(c.Conf.DatabaseUpdateInterval) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for range time.Tick(interval) { | ||
if c.Conf.Debug { | ||
fmt.Println("INFO: Database update started...") | ||
} | ||
err := c.Update() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.