Private GIT
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wg-gen-web
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vlbox
wg-gen-web
Commits
6cafb97e
Commit
6cafb97e
authored
Feb 17, 2020
by
vx3r
Browse files
Options
Downloads
Patches
Plain Diff
issue #8, find available IP on the fly, not from complete list
parent
8d6a05e2
Branches
5980-traps-regexp
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ui/src/App.vue
+1
-1
1 addition, 1 deletion
ui/src/App.vue
util/util.go
+25
-20
25 additions, 20 deletions
util/util.go
with
26 additions
and
21 deletions
ui/src/App.vue
+
1
−
1
View file @
6cafb97e
...
...
@@ -2,7 +2,7 @@
<v-app
id=
"inspire"
>
<v-app-bar
app
>
<img
class=
"mr-3"
:src=
"require('./assets/logo.png')"
height=
"50"
/>
<img
class=
"mr-3"
:src=
"require('./assets/logo.png')"
height=
"50"
alt=
"Wg Gen Web"
/>
<v-toolbar-title>
Wg Gen Web
</v-toolbar-title>
</v-app-bar>
...
...
This diff is collapsed.
Click to expand it.
util/util.go
+
25
−
20
View file @
6cafb97e
...
...
@@ -52,42 +52,32 @@ func DirectoryExists(name string) bool {
// GetAvailableIp search for an available in cidr against a list of reserved ips
func
GetAvailableIp
(
cidr
string
,
reserved
[]
string
)
(
string
,
error
)
{
addresses
,
err
:=
G
et
AllAddressesFromCidr
(
cidr
)
ip
,
ipnet
,
err
:=
n
et
.
ParseCIDR
(
cidr
)
if
err
!=
nil
{
return
""
,
err
}
for
_
,
addresse
:=
range
addresses
{
// this two addresses are not usable
broadcastAddr
:=
BroadcastAddr
(
ipnet
)
.
String
()
networkAddr
:=
ipnet
.
IP
.
String
()
for
ip
:=
ip
.
Mask
(
ipnet
.
Mask
);
ipnet
.
Contains
(
ip
);
inc
(
ip
)
{
ok
:=
true
address
:=
ip
.
String
()
for
_
,
r
:=
range
reserved
{
if
address
e
==
r
{
if
address
==
r
{
ok
=
false
break
}
}
if
ok
{
return
address
e
,
nil
if
ok
&&
address
!=
networkAddr
&&
address
!=
broadcastAddr
{
return
address
,
nil
}
}
return
""
,
errors
.
New
(
"no more available address from cidr"
)
}
// GetAllAddressesFromCidr get all ip addresses from cidr
func
GetAllAddressesFromCidr
(
cidr
string
)
([]
string
,
error
)
{
ip
,
ipnet
,
err
:=
net
.
ParseCIDR
(
cidr
)
if
err
!=
nil
{
return
nil
,
err
}
var
ips
[]
string
for
ip
:=
ip
.
Mask
(
ipnet
.
Mask
);
ipnet
.
Contains
(
ip
);
inc
(
ip
)
{
ips
=
append
(
ips
,
ip
.
String
())
}
// remove network address and broadcast address (and server ip .1)
return
ips
[
2
:
len
(
ips
)
-
1
],
nil
}
// IsIPv6 check if given ip is IPv6
func
IsIPv6
(
address
string
)
bool
{
ip
:=
net
.
ParseIP
(
address
)
...
...
@@ -126,3 +116,18 @@ func inc(ip net.IP) {
}
}
}
// BroadcastAddr returns the last address in the given network, or the broadcast address.
func
BroadcastAddr
(
n
*
net
.
IPNet
)
net
.
IP
{
// The golang net package doesn't make it easy to calculate the broadcast address. :(
var
broadcast
net
.
IP
if
len
(
n
.
IP
)
==
4
{
broadcast
=
net
.
ParseIP
(
"0.0.0.0"
)
.
To4
()
}
else
{
broadcast
=
net
.
ParseIP
(
"::"
)
}
for
i
:=
0
;
i
<
len
(
n
.
IP
);
i
++
{
broadcast
[
i
]
=
n
.
IP
[
i
]
|
^
n
.
Mask
[
i
]
}
return
broadcast
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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