Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
f9169a7b
Commit
f9169a7b
authored
5 years ago
by
Ian Luo
Browse files
Options
Downloads
Patches
Plain Diff
fix issue #207: common/utils/net_test.go:25 cannot pass
parent
941517d0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/utils/net.go
+55
-17
55 additions, 17 deletions
common/utils/net.go
with
55 additions
and
17 deletions
common/utils/net.go
+
55
−
17
View file @
f9169a7b
...
...
@@ -19,6 +19,7 @@ package utils
import
(
"net"
"strings"
)
import
(
...
...
@@ -39,38 +40,49 @@ func init() {
// ref: https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
func
GetLocalIP
()
(
string
,
error
)
{
i
fs
,
err
:=
net
.
Interfaces
()
f
ace
s
,
err
:=
net
.
Interfaces
()
if
err
!=
nil
{
return
""
,
perrors
.
WithStack
(
err
)
}
var
ipAddr
[]
byte
for
_
,
i
:=
range
ifs
{
addrs
,
err
:=
i
.
Addrs
()
var
privateIpv4Addr
,
ipv4Addr
net
.
IP
for
_
,
face
:=
range
faces
{
if
face
.
Flags
&
net
.
FlagUp
==
0
{
// interface down
continue
}
if
face
.
Flags
&
net
.
FlagLoopback
!=
0
{
// loopback interface
continue
}
if
strings
.
Contains
(
strings
.
ToLower
(
face
.
Name
),
"docker"
)
{
continue
}
addrs
,
err
:=
face
.
Addrs
()
if
err
!=
nil
{
return
""
,
perrors
.
WithStack
(
err
)
}
var
ip
net
.
IP
for
_
,
addr
:=
range
addrs
{
switch
v
:=
addr
.
(
type
)
{
case
*
net
.
IPNet
:
ip
=
v
.
IP
case
*
net
.
IPAddr
:
ip
=
v
.
IP
}
if
!
ip
.
IsLoopback
()
&&
ip
.
To4
()
!=
nil
&&
isPrivateIP
(
ip
.
String
())
{
ipAddr
=
ip
break
if
ipv4
,
ok
:=
getValidIPv4
(
addrs
);
ok
{
ipv4Addr
=
ipv4
if
isPrivateIP
(
ipv4
.
String
())
{
privateIpv4Addr
=
ipv4
}
}
}
if
ipAddr
==
nil
{
if
ip
v4
Addr
==
nil
{
return
""
,
perrors
.
Errorf
(
"can not get local IP"
)
}
return
net
.
IP
(
ipAddr
)
.
String
(),
nil
if
privateIpv4Addr
==
nil
{
return
ipv4Addr
.
String
(),
nil
}
return
privateIpv4Addr
.
String
(),
nil
}
func
isPrivateIP
(
ipAddr
string
)
bool
{
...
...
@@ -82,3 +94,29 @@ func isPrivateIP(ipAddr string) bool {
}
return
false
}
func
getValidIPv4
(
addrs
[]
net
.
Addr
)
(
net
.
IP
,
bool
)
{
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
{
// not an valid ipv4 address
continue
}
return
ip
,
true
}
return
nil
,
false
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment