Skip to content

Commit

Permalink
add: backup geoip resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dickymuliafiqri committed Jan 8, 2025
1 parent 497c76c commit 166c6a9
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions sandbox/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
)

var orgPattern = regexp.MustCompile(`(\w*)`)

const connectivityTest = "https://myip.shylook.workers.dev"
var connectivityTestList = []string{
"https://myip.shylook.workers.dev",
"https://cloudflare-ip.html.zone/geo",
}

func testSingConfigWithContext(singConfig option.Options, ctx context.Context) (configGeoipStruct, error) {
// Re-allocate free port
Expand Down Expand Up @@ -43,22 +45,28 @@ func testSingConfigWithContext(singConfig option.Options, ctx context.Context) (

session.SetProxy(fmt.Sprintf("socks5://0.0.0.0:%v", freePort))

if err := session.Connect(connectivityTest); err != nil {
return configGeoip, err
}
for _, connectivityTest := range connectivityTestList {
if err := session.Connect(connectivityTest); err != nil {
return configGeoip, err
}

resp, err := session.Get(connectivityTest)
if err != nil {
return configGeoip, err
} else {
if resp.StatusCode == 200 {
json.Unmarshal(resp.Body, &configGeoip)
resp, err := session.Get(connectivityTest)
if err != nil {
return configGeoip, err
} else {
if resp.StatusCode == 200 {
json.Unmarshal(resp.Body, &configGeoip)
}
}
}

// Post-processing geoip
filteredAsOrganization := orgPattern.FindAllString(configGeoip.AsOrganization, -1)
configGeoip.AsOrganization = strings.Join(filteredAsOrganization, " ")
// Post-processing geoip
filteredAsOrganization := orgPattern.FindAllString(configGeoip.AsOrganization, -1)
configGeoip.AsOrganization = strings.Join(filteredAsOrganization, " ")

if configGeoip.AsOrganization != "" && configGeoip.Country != "" {
break
}
}

return configGeoip, nil
}

0 comments on commit 166c6a9

Please sign in to comment.