This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publicip.go
97 lines (80 loc) · 2.54 KB
/
publicip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// This file is part of the ego-cli distribution.
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/)
//
// ego-cli is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3.
//
// ego-cli is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/thatisuday/commando"
)
func publicip_detectIPv4(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
var ipV4OrErrorMsg = ""
respV4, errV4 := http.Get("https://api.ipify.org/")
if errV4 != nil {
ipV4OrErrorMsg = fmt.Sprintf("ERROR: %v", errV4)
} else {
defer respV4.Body.Close()
body, err := ioutil.ReadAll(respV4.Body)
if err != nil {
ipV4OrErrorMsg = fmt.Sprintf("ERROR: %v", err)
} else {
ipV4OrErrorMsg = string(body)
}
}
fmt.Printf("%v", ipV4OrErrorMsg)
fmt.Println()
}
func publicip_detectIPv6(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
var ipV6OrErrorMsg = ""
respV6, errV6 := http.Get("https://api6.ipify.org/")
if errV6 != nil {
ipV6OrErrorMsg = fmt.Sprintf("ERROR: %v", errV6)
} else {
defer respV6.Body.Close()
body, err := ioutil.ReadAll(respV6.Body)
if err != nil {
ipV6OrErrorMsg = fmt.Sprintf("ERROR: %v", err)
} else {
ipV6OrErrorMsg = string(body)
}
}
fmt.Printf("%v", ipV6OrErrorMsg)
fmt.Println()
}
func publicip_detectIP(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
ip4, err := flags["ip4"].GetBool()
detectV4 := err == nil && ip4
ip6, err := flags["ip6"].GetBool()
detectV6 := err == nil && ip6
if !detectV4 && !detectV6 {
detectV4 = true
detectV6 = true
}
if detectV4 {
publicip_detectIPv4(args, flags)
}
if detectV6 {
publicip_detectIPv6(args, flags)
}
}
func Setup_publicip_Command() {
commando.
Register("public-ip").
SetShortDescription("detect public IP address(es)").
SetDescription("Tries to detect public IPv4 and IPv6 address(es) by using ipify.org service").
AddFlag("ip4,4", "detect IPv4", commando.Bool, nil).
AddFlag("ip6,6", "detect IPv6", commando.Bool, nil).
SetAction(publicip_detectIP)
}