Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
chain33-pai
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
szh
chain33-pai
Commits
d7b8d132
Commit
d7b8d132
authored
Jun 28, 2020
by
szh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加根据name 获取地址ip
parent
3a5f9e75
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
2 deletions
+32
-2
main.go
main.go
+3
-2
util.go
pkg/util/util.go
+29
-0
No files found.
main.go
View file @
d7b8d132
...
@@ -95,7 +95,7 @@ func broadcast() {
...
@@ -95,7 +95,7 @@ func broadcast() {
}
}
//ip 会变化
//ip 会变化
ip
,
err
:=
util
.
GetLocalI
P
(
)
ip
,
err
:=
util
.
GetLocalI
pByName
(
"wlan0"
)
if
err
!=
nil
{
if
err
!=
nil
{
tlog
.
Error
(
"broadcast"
,
err
)
tlog
.
Error
(
"broadcast"
,
err
)
}
}
...
@@ -121,7 +121,7 @@ func broadcast() {
...
@@ -121,7 +121,7 @@ func broadcast() {
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
panic
(
err
)
}
}
tlog
.
Info
(
"broadcast"
,
"udp"
,
send
)
tlog
.
Info
(
"
ip:"
,
ip
.
IP
.
String
(),
"
broadcast"
,
"udp"
,
send
)
time
.
Sleep
(
time
.
Second
*
1
)
time
.
Sleep
(
time
.
Second
*
1
)
}
}
}
}
\ No newline at end of file
pkg/util/util.go
View file @
d7b8d132
...
@@ -44,6 +44,35 @@ func GetLocalIP() (ipv4 *net.IPNet, err error) {
...
@@ -44,6 +44,35 @@ func GetLocalIP() (ipv4 *net.IPNet, err error) {
return
return
}
}
func
GetLocalIpByName
(
name
string
)
(
ipv4
*
net
.
IPNet
,
err
error
)
{
if
name
==
""
{
return
nil
,
errors
.
New
(
"name not empty"
)
}
inter
,
err
:=
net
.
InterfaceByName
(
name
)
if
err
!=
nil
{
return
nil
,
err
}
var
(
addrs
[]
net
.
Addr
ipNet
*
net
.
IPNet
// IP地址
isIpNet
bool
)
if
addrs
,
err
=
inter
.
Addrs
();
err
!=
nil
{
return
nil
,
err
}
for
_
,
addr
:=
range
addrs
{
if
ipNet
,
isIpNet
=
addr
.
(
*
net
.
IPNet
);
isIpNet
&&
!
ipNet
.
IP
.
IsLoopback
(){
// 跳过IPV6
if
ipNet
.
IP
.
To4
()
!=
nil
{
ipv4
=
ipNet
// 192.168.1.1
return
}
}
}
return
nil
,
nil
}
func
GetExternal
()
(
string
,
error
)
{
func
GetExternal
()
(
string
,
error
)
{
resp
,
err
:=
http
.
Get
(
"http://myexternalip.com/raw"
)
resp
,
err
:=
http
.
Get
(
"http://myexternalip.com/raw"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment