-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of routing feature based on a geolocation database
- Loading branch information
Showing
9 changed files
with
228 additions
and
184 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
module github.com/nodauf/Go-RouterSocks | ||
|
||
go 1.15 | ||
go 1.22 | ||
|
||
require ( | ||
github.com/c-bata/go-prompt v0.2.5 | ||
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5 | ||
github.com/mitchellh/go-homedir v1.1.0 | ||
github.com/oschwald/geoip2-golang v1.9.0 | ||
github.com/oschwald/maxminddb-golang v1.11.0 | ||
github.com/spf13/cobra v1.2.1 | ||
github.com/spf13/viper v1.8.1 | ||
golang.org/x/net v0.0.0-20210902165921-8d991716f632 | ||
) | ||
|
||
require ( | ||
github.com/inconshreveable/mousetrap v1.0.0 // indirect | ||
github.com/mattn/go-colorable v0.1.7 // indirect | ||
github.com/mattn/go-isatty v0.0.12 // indirect | ||
github.com/mattn/go-runewidth v0.0.9 // indirect | ||
github.com/mattn/go-tty v0.0.3 // indirect | ||
github.com/pkg/term v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/sys v0.9.0 // indirect | ||
) |
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
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,68 @@ | ||
package router | ||
|
||
import ( | ||
"fmt" | ||
"github.com/oschwald/geoip2-golang" | ||
"github.com/oschwald/maxminddb-golang" | ||
"log" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
var geoIPDB *geoip2.Reader | ||
var geoIPDB_low *maxminddb.Reader | ||
var geoipEnable bool | ||
var isoCodes []string | ||
var isoWithCountries = make(map[string]string) | ||
|
||
//var isoWithNetworks = make(map[string][]string) | ||
|
||
func LoadGeoIPDatabase(geoipDB string) error { | ||
var err error | ||
geoIPDB, err = geoip2.Open(geoipDB) | ||
if err != nil { | ||
return err | ||
} | ||
geoIPDB_low, err = maxminddb.Open(geoipDB) | ||
geoipEnable = true | ||
// Load the countries and the iso code | ||
var record struct { | ||
Country struct { | ||
ISOCode string `maxminddb:"iso_code"` | ||
CountryNames map[string]string `maxminddb:"names"` | ||
} `maxminddb:"country"` | ||
} // Or any appropriate struct | ||
networks := geoIPDB_low.Networks(maxminddb.SkipAliasedNetworks) | ||
for networks.Next() { | ||
_, err := networks.Network(&record) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ISOCode := strings.ToLower(record.Country.ISOCode) | ||
if !slices.Contains(isoCodes, ISOCode) { | ||
isoCodes = append(isoCodes, ISOCode) | ||
} | ||
isoWithCountries[ISOCode] = record.Country.CountryNames["en"] | ||
//isoWithNetworks[ISOCode] = append(isoWithNetworks[ISOCode], subnet.String()) | ||
} | ||
if networks.Err() != nil { | ||
log.Panic(networks.Err()) | ||
} | ||
slices.Sort(isoCodes) | ||
return err | ||
} | ||
|
||
func PrintCountry() { | ||
if !geoipEnable { | ||
fmt.Println("GeoIP database was not loaded") | ||
return | ||
} | ||
|
||
for _, isoCode := range isoCodes { | ||
fmt.Printf("%s => %s\n", isoCode, isoWithCountries[isoCode]) | ||
} | ||
} | ||
|
||
func IsValidIsoCode(ISOCode string) bool { | ||
return slices.Contains(isoCodes, strings.ToLower(ISOCode)) | ||
} |
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
Oops, something went wrong.