-
Notifications
You must be signed in to change notification settings - Fork 3
Getting own public ip #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e938613
get own public ip
astaphobia 58baf38
Merge branch 'develop' of github.com:zoobc/zoobc-core into getting-ow…
astaphobia a26f7b7
allowing to get public ip via checkip.dyndns.org
astaphobia 2db181d
make sure the parsed ip is valid ip address
astaphobia 7259b44
IPUtil methods
astaphobia 6807f31
deal with interface, allowing to mock method
astaphobia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,115 @@ | ||
package util | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
type ( | ||
IPUtil struct { | ||
} | ||
IPUtilInterface interface { | ||
GetPublicIP() (ip net.IP, err error) | ||
GetPublicIPDYNDNS() (ip net.IP, err error) | ||
IsDomain(address string) bool | ||
IsPublicIP(ip net.IP) bool | ||
} | ||
) | ||
|
||
// GetPublicIP allowing to get own external/public ip, | ||
// Work perfectly on server not on local machine / laptop / PC | ||
// more accurate if getting from request header https://golangcode.com/get-the-request-ip-addr/ | ||
func (ipu *IPUtil) GetPublicIP() (net.IP, error) { | ||
faces, err := net.Interfaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, face := range faces { | ||
if face.Flags&net.FlagUp == 0 { | ||
continue // interface down | ||
} | ||
if face.Flags&net.FlagLoopback != 0 { | ||
continue // loopback interface | ||
} | ||
addrs, err := face.Addrs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, addr := range addrs { | ||
var ip net.IP | ||
switch v := addr.(type) { | ||
case *net.IPNet: | ||
ip = v.IP | ||
case *net.IPAddr: | ||
ip = v.IP | ||
} | ||
if ip == nil || ip.IsLoopback() { | ||
continue | ||
} | ||
ip = ip.To4() | ||
if ip == nil { | ||
continue // not an ipv4 address | ||
} | ||
return ip, nil | ||
} | ||
} | ||
return nil, errors.New("fail caused the internet connection") | ||
} | ||
|
||
// GetPublicIPDYNDNS allowing to get own public ip via http://checkip.dyndns.org | ||
func (ipu *IPUtil) GetPublicIPDYNDNS() (net.IP, error) { | ||
var ( | ||
err error | ||
bt []byte | ||
resp *http.Response | ||
rgx = regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`) | ||
) | ||
resp, err = http.Get("http://checkip.dyndns.org") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
bt, err = ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// unfortunately the response is <html> tag, need to get the ip only via regexp | ||
ipStr := rgx.FindAllString(string(bt), -1) | ||
ip := net.ParseIP(ipStr[0]) | ||
if ip != nil { | ||
return ip, nil | ||
} | ||
return nil, fmt.Errorf("invalid ip address") | ||
} | ||
|
||
// IsDomain willing to check what kinda address given | ||
func (ipu *IPUtil) IsDomain(address string) bool { | ||
addr := net.ParseIP(address) | ||
return addr == nil | ||
} | ||
|
||
// IsPublicIP make sure that ip is a public ip or not | ||
func (ipu *IPUtil) IsPublicIP(ip net.IP) bool { | ||
if ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() { | ||
return false | ||
} | ||
if ip4 := ip.To4(); ip4 != nil { | ||
switch { | ||
case ip4[0] == 10: | ||
return false | ||
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31: | ||
return false | ||
case ip4[0] == 192 && ip4[1] == 168: | ||
return false | ||
default: | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains hidden or 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,120 @@ | ||
package util | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestGetPublicIP(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "WantSuccess", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ipu := &IPUtil{} | ||
got, err := ipu.GetPublicIP() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetPublicIP() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if ipu.IsPublicIP(got) { | ||
t.Errorf("GetPublicIP() got = %v ", got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsPublicIP(t *testing.T) { | ||
type args struct { | ||
IP net.IP | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "WantPublicIP", | ||
args: args{ | ||
IP: net.ParseIP("172.104.34.10"), | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "WantPrivateIP", | ||
args: args{ | ||
IP: net.ParseIP("192.168.10.1"), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ipu := &IPUtil{} | ||
if got := ipu.IsPublicIP(tt.args.IP); got != tt.want { | ||
t.Errorf("IsPublicIP() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsDomain(t *testing.T) { | ||
type args struct { | ||
address string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "WantDomain", | ||
args: args{ | ||
address: "zoobc.com", | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "WantIP", | ||
args: args{ | ||
address: "172.104.34.10", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ipu := &IPUtil{} | ||
if got := ipu.IsDomain(tt.args.address); got != tt.want { | ||
t.Errorf("IsDomain() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetPublicIPDYNDNS(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "WantSuccess", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ipu := &IPUtil{} | ||
got, err := ipu.GetPublicIPDYNDNS() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetPublicIPDYNDNS() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !ipu.IsPublicIP(got) { // perhaps is public ip | ||
t.Errorf("GetPublicIPDYNDNS() got = %v", got) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome 💯