-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolver): add resolvers for IPv4
- Loading branch information
Damien Abos
committed
Jan 7, 2023
1 parent
ae67e0f
commit 1331916
Showing
14 changed files
with
553 additions
and
69 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
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,69 @@ | ||
package backend | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const IPV4B32_BUILTIN_RESOLVER_NAME = "builtin:ipv4b32" | ||
|
||
type IPv4B32Resolver struct { | ||
settings map[string]interface{} | ||
regexp *regexp.Regexp | ||
ttl uint32 | ||
} | ||
|
||
func NewIPv4B32Resolver(settings map[string]interface{}) *IPv4B32Resolver { | ||
var domain, pattern string | ||
var ttl uint32 | ||
domain, _ = settings["domain"].(string) | ||
ttl, _ = settings["ttl"].(uint32) | ||
pattern = fmt.Sprintf("([0-9a-wA-W]{5,7})\\.%s$", strings.ReplaceAll(domain, ".", "\\.")) | ||
re, _ := regexp.Compile(pattern) | ||
return &IPv4B32Resolver{ | ||
settings: settings, | ||
regexp: re, | ||
ttl: ttl, | ||
} | ||
} | ||
|
||
func (resolver *IPv4B32Resolver) Lookup(qtype string, qname string, _ string) (lookupResultArray []LookupResult, err error) { | ||
lookupResultArray = []LookupResult{} | ||
if qtype == A || qtype == AAAA || qtype == ANY { | ||
a := resolver.regexp.FindStringSubmatch(qname) | ||
if len(a) > 1 { | ||
ipValue, convErr := strconv.ParseInt(a[1], 32, 33) | ||
if convErr == nil { | ||
var a, b, c, d byte | ||
a = byte(ipValue & 0xFF000000 >> 24) | ||
b = byte(ipValue & 0x00FF0000 >> 16) | ||
c = byte(ipValue & 0x0000FF00 >> 8) | ||
d = byte(ipValue & 0x000000FF) | ||
ip := net.IPv4(a, b, c, d) | ||
if ip != nil { | ||
var lookupResult LookupResult | ||
if qtype == A || qtype == ANY { | ||
lookupResult = LookupResult{ | ||
QType: A, | ||
QName: qname, | ||
Content: ip.String(), | ||
TTL: uint32(60), | ||
} | ||
} else { | ||
lookupResult = LookupResult{ | ||
QType: AAAA, | ||
QName: qname, | ||
Content: fmt.Sprintf("::ffff:%s", ip.String()), | ||
TTL: uint32(60), | ||
} | ||
} | ||
lookupResultArray = append(lookupResultArray, lookupResult) | ||
} | ||
} | ||
} | ||
} | ||
return | ||
} |
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,83 @@ | ||
package backend | ||
|
||
import ( | ||
"encoding/base32" | ||
"fmt" | ||
"net" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Base32 alphabets | ||
const ( | ||
IPV4B32CROCKFORD_BUILTIN_RESOLVER_NAME string = "builtin:ipv4b32crockford" | ||
lowercaseAlphabet string = "0123456789abcdefghjkmnpqrstvwxyz" | ||
) | ||
|
||
// Base32 encodings | ||
var ( | ||
lowerEncoding = base32.NewEncoding(lowercaseAlphabet).WithPadding(base32.NoPadding) | ||
) | ||
|
||
type IPv4B32CrockfordResolver struct { | ||
settings map[string]interface{} | ||
regexp *regexp.Regexp | ||
ttl uint32 | ||
} | ||
|
||
func NewIPv4B32CrockfordResolver(settings map[string]interface{}) *IPv4B32CrockfordResolver { | ||
var domain, pattern string | ||
var ttl uint32 | ||
domain, _ = settings["domain"].(string) | ||
ttl, _ = settings["ttl"].(uint32) | ||
pattern = fmt.Sprintf("([0123456789abcdefghjkmnpqrstvwxyzABCDEFGHJKMNPQRSTVWXYZ]{5,7})\\.%s$", strings.ReplaceAll(domain, ".", "\\.")) | ||
re, _ := regexp.Compile(pattern) | ||
return &IPv4B32CrockfordResolver{ | ||
settings: settings, | ||
regexp: re, | ||
ttl: ttl, | ||
} | ||
} | ||
|
||
func (resolver *IPv4B32CrockfordResolver) Lookup(qtype string, qname string, _ string) (lookupResultArray []LookupResult, err error) { | ||
lookupResultArray = []LookupResult{} | ||
if qtype == A || qtype == AAAA || qtype == ANY { | ||
a := resolver.regexp.FindStringSubmatch(qname) | ||
if len(a) > 1 { | ||
lower := strings.ToLower(a[1]) | ||
if len(lower) == 6 { | ||
// add padding | ||
lower = fmt.Sprintf("0%s", lower) | ||
} | ||
ipValue, convErr := lowerEncoding.DecodeString(lower) | ||
if convErr == nil && len(ipValue) == 4 { | ||
var a, b, c, d byte | ||
a = ipValue[0] | ||
b = ipValue[1] | ||
c = ipValue[2] | ||
d = ipValue[3] | ||
ip := net.IPv4(a, b, c, d) | ||
if ip != nil { | ||
var lookupResult LookupResult | ||
if qtype == A || qtype == ANY { | ||
lookupResult = LookupResult{ | ||
QType: A, | ||
QName: qname, | ||
Content: ip.String(), | ||
TTL: uint32(60), | ||
} | ||
} else { | ||
lookupResult = LookupResult{ | ||
QType: AAAA, | ||
QName: qname, | ||
Content: fmt.Sprintf("::ffff:%s", ip.String()), | ||
TTL: uint32(60), | ||
} | ||
} | ||
lookupResultArray = append(lookupResultArray, lookupResult) | ||
} | ||
} | ||
} | ||
} | ||
return | ||
} |
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,60 @@ | ||
package backend | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const IPV4DASHED_BUILTIN_RESOLVER_NAME = "builtin:ipv4dashed" | ||
|
||
type IPv4DashedResolver struct { | ||
settings map[string]interface{} | ||
regexp *regexp.Regexp | ||
ttl uint32 | ||
} | ||
|
||
func NewIPv4DashedResolver(settings map[string]interface{}) *IPv4DashedResolver { | ||
var domain, pattern string | ||
var ttl uint32 | ||
domain, _ = settings["domain"].(string) | ||
ttl, _ = settings["ttl"].(uint32) | ||
pattern = fmt.Sprintf("(([0-9]{1,3}-){3}([0-9]{1,3}))\\.%s$", strings.ReplaceAll(domain, ".", "\\.")) | ||
re, _ := regexp.Compile(pattern) | ||
return &IPv4DashedResolver{ | ||
settings: settings, | ||
regexp: re, | ||
ttl: ttl, | ||
} | ||
} | ||
|
||
func (resolver *IPv4DashedResolver) Lookup(qtype string, qname string, _ string) (lookupResultArray []LookupResult, err error) { | ||
lookupResultArray = []LookupResult{} | ||
if qtype == A || qtype == AAAA || qtype == ANY { | ||
a := resolver.regexp.FindStringSubmatch(qname) | ||
if len(a) > 1 { | ||
ip := net.ParseIP(strings.ReplaceAll(a[1], "-", ".")) | ||
if ip != nil { | ||
var lookupResult LookupResult | ||
if qtype == A || qtype == ANY { | ||
lookupResult = LookupResult{ | ||
QType: A, | ||
QName: qname, | ||
Content: ip.String(), | ||
TTL: uint32(60), | ||
} | ||
} else { | ||
lookupResult = LookupResult{ | ||
QType: AAAA, | ||
QName: qname, | ||
Content: fmt.Sprintf("::ffff:%s", ip.String()), | ||
TTL: uint32(60), | ||
} | ||
} | ||
lookupResultArray = append(lookupResultArray, lookupResult) | ||
} | ||
} | ||
} | ||
return | ||
} |
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,60 @@ | ||
package backend | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const IPV4DOTTED_BUILTIN_RESOLVER_NAME = "builtin:ipv4dotted" | ||
|
||
type IPv4DottedResolver struct { | ||
settings map[string]interface{} | ||
regexp *regexp.Regexp | ||
ttl uint32 | ||
} | ||
|
||
func NewIPv4DottedResolver(settings map[string]interface{}) *IPv4DottedResolver { | ||
var domain, pattern string | ||
var ttl uint32 | ||
domain, _ = settings["domain"].(string) | ||
ttl, _ = settings["ttl"].(uint32) | ||
pattern = fmt.Sprintf("(([0-9]{1,3}\\.){3}([0-9]{1,3}))\\.%s$", strings.ReplaceAll(domain, ".", "\\.")) | ||
re, _ := regexp.Compile(pattern) | ||
return &IPv4DottedResolver{ | ||
settings: settings, | ||
regexp: re, | ||
ttl: ttl, | ||
} | ||
} | ||
|
||
func (resolver *IPv4DottedResolver) Lookup(qtype string, qname string, _ string) (lookupResultArray []LookupResult, err error) { | ||
lookupResultArray = []LookupResult{} | ||
if qtype == A || qtype == AAAA || qtype == ANY { | ||
a := resolver.regexp.FindStringSubmatch(qname) | ||
if len(a) > 1 { | ||
ip := net.ParseIP(a[1]) | ||
if ip != nil { | ||
var lookupResult LookupResult | ||
if qtype == A || qtype == ANY { | ||
lookupResult = LookupResult{ | ||
QType: A, | ||
QName: qname, | ||
Content: ip.String(), | ||
TTL: uint32(60), | ||
} | ||
} else { | ||
lookupResult = LookupResult{ | ||
QType: AAAA, | ||
QName: qname, | ||
Content: fmt.Sprintf("::ffff:%s", ip.String()), | ||
TTL: uint32(60), | ||
} | ||
} | ||
lookupResultArray = append(lookupResultArray, lookupResult) | ||
} | ||
} | ||
} | ||
return | ||
} |
Oops, something went wrong.