Skip to content

Commit

Permalink
fix get public ip (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode authored Nov 2, 2023
1 parent 7d0d5da commit 653a7a5
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ func GetExternalIp() (string, error) {

ctx1, cancel1 := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel1()
output, err1 := exec.CommandContext(ctx1, "sh", "-c", "curl ifconfig.co").Output()
if err1 != nil {
errstr += fmt.Sprintf("[%v]", err1.Error())
}
if err1 == nil {
output, err := exec.CommandContext(ctx1, "sh", "-c", "curl ifconfig.co").Output()
if err != nil {
errstr += fmt.Sprintf("[%v]", err.Error())
} else {
externalIp = strings.ReplaceAll(string(output), "\n", "")
externalIp = strings.ReplaceAll(externalIp, " ", "")
if IsIPv4(externalIp) {
Expand All @@ -121,13 +120,10 @@ func GetExternalIp() (string, error) {

ctx2, cancel2 := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel2()
output, err2 := exec.CommandContext(ctx2, "sh", "-c", "curl cip.cc | grep IP | awk '{print $3;}'").Output()
if err2 != nil {
if err1.Error() != err2.Error() {
errstr += fmt.Sprintf("[%v]", err2.Error())
}
}
if err2 == nil {
output, err = exec.CommandContext(ctx2, "sh", "-c", "curl cip.cc | grep IP | awk '{print $3;}'").Output()
if err != nil {
errstr += fmt.Sprintf("[%v]", err.Error())
} else {
externalIp = strings.ReplaceAll(string(output), "\n", "")
externalIp = strings.ReplaceAll(externalIp, " ", "")
if IsIPv4(externalIp) {
Expand All @@ -137,13 +133,10 @@ func GetExternalIp() (string, error) {

ctx3, cancel3 := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel3()
output, err3 := exec.CommandContext(ctx3, "sh", "-c", `curl ipinfo.io | grep \"ip\" | awk '{print $2;}'`).Output()
if err3 != nil {
if err1.Error() != err3.Error() && err2.Error() != err3.Error() {
errstr += fmt.Sprintf("[%v]", err3.Error())
}
}
if err3 == nil {
output, err = exec.CommandContext(ctx3, "sh", "-c", `curl ipinfo.io | grep \"ip\" | awk '{print $2;}'`).Output()
if err != nil {
errstr += fmt.Sprintf("[%v]", err.Error())
} else {
externalIp = strings.ReplaceAll(string(output), "\"", "")
externalIp = strings.ReplaceAll(externalIp, ",", "")
externalIp = strings.ReplaceAll(externalIp, "\n", "")
Expand All @@ -154,13 +147,10 @@ func GetExternalIp() (string, error) {

ctx4, cancel4 := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel4()
output, err4 := exec.CommandContext(ctx4, "sh", "-c", `curl ifcfg.me`).Output()
if err4 != nil {
if err1.Error() != err4.Error() && err2.Error() != err4.Error() && err3.Error() != err4.Error() {
errstr += fmt.Sprintf("[%v]", err4.Error())
}
}
if err4 == nil {
output, err = exec.CommandContext(ctx4, "sh", "-c", `curl ifcfg.me`).Output()
if err != nil {
errstr += fmt.Sprintf("[%v]", err.Error())
} else {
externalIp = strings.ReplaceAll(string(output), "\"", "")
externalIp = strings.ReplaceAll(externalIp, ",", "")
externalIp = strings.ReplaceAll(externalIp, "\n", "")
Expand Down

0 comments on commit 653a7a5

Please sign in to comment.