-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaf3236
commit f84bb1d
Showing
2 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,32 @@ | ||
package envporter | ||
|
||
import ( | ||
"github.com/darkjinnee/envporter/pkg/porter" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
var usedPorts []int | ||
|
||
func NumberRange(min, max int) int { | ||
rand.Seed(time.Now().UTC().UnixNano()) | ||
return min + rand.Intn(max-min) | ||
} | ||
|
||
func FreePort(min, max int) int { | ||
var port int | ||
for i := 1; i < 10; i++ { | ||
port = NumberRange(min, max) | ||
for _, value := range usedPorts { | ||
if port == value || !porter.CheckIpv4Tcp(port) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
usedPorts = append( | ||
usedPorts, | ||
port, | ||
) | ||
return port | ||
} |