-
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
a5fcee6
commit 2f1a8e1
Showing
4 changed files
with
106 additions
and
92 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,13 @@ | ||
package src | ||
|
||
import "fmt" | ||
|
||
// Generate a DNS record ready to be paste on a Bind compatible zone | ||
func GenerateDNSRecord(ip string, ttl int, hostname string) string { | ||
return fmt.Sprintf("%s\t%d\tIN\tAAAA\t%s", hostname, ttl, ip) | ||
} | ||
|
||
// Generate a ARPA record ready to be paste on a Bind compatible zone | ||
func GenerateReverseDNSRecord(ip string, ttl int, hostname string) string { | ||
return fmt.Sprintf("%s.\t%d\tIN\tPTR\t%s", ip, ttl, hostname) | ||
} |
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,79 @@ | ||
package src | ||
|
||
import ( | ||
"net" | ||
"strings" | ||
|
||
"github.com/c-robinson/iplib" | ||
) | ||
|
||
// Function stollen from c-robinson/iplib, method non-exposed by the lib ¯\_(ツ)_/¯ | ||
func Net6wildcard(n iplib.Net6) net.IPMask { | ||
wc := make([]byte, len(n.Mask())) | ||
for i, b := range n.Mask() { | ||
wc[i] = 0xff - b | ||
} | ||
return wc | ||
} | ||
|
||
// Generate a random IP address based on the given IP subnet. | ||
func GenerateRandomIP(net iplib.Net6) net.IP { | ||
finalIP := net.FirstAddress() | ||
|
||
for idx, b := range net.Mask() { | ||
if b == 0xff { | ||
continue | ||
} else if b != 0x0 { | ||
randByte := byte(RandomNumber(0, 255)) | ||
wildCardByte := Net6wildcard(net)[idx] | ||
|
||
finalIP[idx] = net.IP()[idx] + wildCardByte&randByte | ||
} else { | ||
finalIP[idx] = byte(RandomNumber(0, 255)) | ||
} | ||
} | ||
|
||
return finalIP | ||
} | ||
|
||
// Generate an IP address based on the hostname. | ||
// The algorithm first tries with the complete hostname, if it does not work, | ||
// it tries by removing vowels and iterate by removing character by character. | ||
func GenerateIPFromHostname(net iplib.Net6, hostname string) net.IP { | ||
finalIP := []byte(net.FirstAddress()) | ||
cidr, _ := net.Mask().Size() | ||
availableBits := 128 - cidr | ||
hostname = strings.Split(hostname, ".")[0] | ||
|
||
if len(hostname)*8 > availableBits { | ||
// Remove vowels | ||
shrinkedHostname := []byte{} | ||
for _, c := range hostname { | ||
asVowel := false | ||
|
||
for _, v := range "aiueo" { | ||
if c == v { | ||
asVowel = true | ||
} | ||
} | ||
|
||
if !asVowel { | ||
shrinkedHostname = append(shrinkedHostname, byte(c)) | ||
} | ||
} | ||
|
||
// In case it's not enough, shrink last character until it fits | ||
for len(shrinkedHostname)*8 > availableBits { | ||
shrinkedHostname = shrinkedHostname[0 : len(shrinkedHostname)-1] | ||
} | ||
|
||
hostname = string(shrinkedHostname) | ||
} | ||
|
||
for i, c := range hostname { | ||
pos := len(finalIP) - len(hostname) + i | ||
finalIP[pos] = byte(c) | ||
} | ||
|
||
return finalIP | ||
} |
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,11 @@ | ||
package src | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func RandomNumber(min int, max int) int { | ||
rand.Seed(time.Now().UnixNano()) | ||
return rand.Intn(max-min) + min | ||
} |