Skip to content

Commit

Permalink
[Minor]
Browse files Browse the repository at this point in the history
  • Loading branch information
FMotalleb committed Oct 29, 2023
1 parent 83120b0 commit ff41544
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 69 deletions.
4 changes: 2 additions & 2 deletions config.new.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
entry_points:
- name: normal # (Optional)
port: 53 # (def(53))
type: raw # (def(raw) | tls | https)
type: udp # (def(udp) | tcp | tls | https)
# reserved keys are (https|tls)_params respectively

# (Mandatory) uses fallback method to iterate over items
Expand All @@ -12,7 +12,7 @@ default_providers: # (def(null))
providers:
- name: cf # naming providers is mandatory
# DNS_SERVER_DEFINITION
type: raw # (tls || https || lua) (if lua addresses should point to lua file)
type: udp # (tls || https || lua) (if lua addresses should point to lua file)
addresses:
- 1.1.1.1:53
- 1.0.0.1:53
Expand Down
49 changes: 28 additions & 21 deletions cord_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
"syscall"

"github.com/FMotalleb/cord-locator/lib/data"
newconfig "github.com/FMotalleb/cord-locator/lib/new_config"
"gopkg.in/yaml.v3"

"github.com/FMotalleb/cord-locator/lib/config"
"github.com/FMotalleb/cord-locator/lib/utils"
Expand All @@ -26,22 +26,28 @@ var (
// DNSConfig is the configuration data of the instance
DNSConfig config.Config
)
var data = `
entry_points:
- name:
port: # (def(53))
type: raw # (def(raw) || tls || https)
providers:
- name: cf # naming providers is mandatory
# DNS_SERVER_DEFINITION
type: raw # (tls || https || lua) (if lua addresses should point to lua file)
addresses:
- 1.1.1.1:53
- 1.0.0.1:53
`

// var data = `
// entry_points:
// - name:
// port: # (def(53))
// type: udp

// providers:
// - name: cf # naming providers is mandatory
// # DNS_SERVER_DEFINITION
// type: udp
// addresses:
// - 1.1.1.1:53
// - 1.0.0.1:53
// `

func main() {
// dd := data.NewAnswer("google.com.", "A", "IN", "216.239.38.120", 80)
rr, _ := dns.NewRR("stackexchange.com. 1800 IN SOA damian.ns.cloudflare.com. dns.cloudflare.com. 2322332295 10000 2400 604800 1800")
// dns.
data.NewAnswerFromRR(rr)
return
ctx := context.Background()
go func() {
select {
Expand All @@ -56,13 +62,14 @@ func main() {

ctx, _ = context.WithCancelCause(ctx)
// cancel(errors.New("WTF"))
testConfig := newconfig.ConfigData{}
err := yaml.Unmarshal([]byte(data), &testConfig)
if err != nil {
println(err.Error())
}
testConfig := newconfig.Data{}
// err := yaml.Unmarshal([]byte(data), &testConfig)
// if err != nil {
// println(err.Error())
// }
confs := make([]string, 0)
for _, item := range testConfig.Providers {
config := testConfig.BuildConfig()
for _, item := range config.Providers {
// println(item.Validate().Error())
confs = append(confs, item.String())
}
Expand Down
80 changes: 80 additions & 0 deletions lib/data/answer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package data

import (
"fmt"
"strconv"
"strings"

"github.com/miekg/dns"
)

// Answer of a dns query
type Answer struct {
TTL int
Name string
Address string
Type string
Class string
}

// NewAnswer is generated using given parameters
func NewAnswer(aName string, aType string, aClass string, aAddress string, aTTL int) Answer {
return Answer{
TTL: aTTL,
Name: aName,
Address: aAddress,
Type: aType,
Class: aClass,
}
}

func (answer Answer) String() string {
return fmt.Sprintf("%s\t%d\t%s\t%s\t%s", answer.Name, answer.TTL, answer.Class, answer.Type, answer.Address)
}

func (answer Answer) ToRR() (result dns.RR, err error) {
return dns.NewRR(answer.String())
}

func NewAnswerFromString(str string) Answer {
var name string
var aType string
var class string
var address string
var ttl int
lastFilled := 0
for _, v := range strings.Split(str, "\t") {
if len(v) > 0 {
switch lastFilled {
case 0:
name = v
break
case 1:
ttl, _ = strconv.Atoi(v)
break
case 2:
class = v
break
case 3:
aType = v
break
case 5:
address = v
break
default:
address = address + "\t" + v

}
lastFilled++
}
}
if lastFilled != 5 {
panic("unexpected length")
}

ans := NewAnswer(name, aType, class, address, ttl)
return ans
}
func NewAnswerFromRR(r dns.RR) Answer {
return NewAnswerFromString(r.String())
}
23 changes: 16 additions & 7 deletions lib/internal_dns/internal_messages.go → lib/data/question.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ import (
"github.com/miekg/dns"
)

// Question of a dns query
type Question struct {
Type string
Class string
Name string
From *string
}

// NewQuestion built with given parameters
func NewQuestion(qType string, qClass string, qName string) Question {
return Question{
Type: qType,
Class: qClass,
Name: qName,
}
}

// GetQuestions from a dns message
func GetQuestions(msg *dns.Msg) []Question {

if msg == nil || len(msg.Question) == 0 {
return make([]Question, 0)
}
mapper := func(entry dns.Question) Question {
Qtype := dns.TypeToString[entry.Qtype]
Qclass := dns.ClassToString[entry.Qclass]
return Question{
Type: Qtype,
Class: Qclass,
Name: entry.Name,
}
qType := dns.TypeToString[entry.Qtype]
qClass := dns.ClassToString[entry.Qclass]
return NewQuestion(qType, qClass, entry.Name)
}
return iterable.Map[dns.Question, Question](msg.Question, mapper)
}
16 changes: 15 additions & 1 deletion lib/new_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ import (
"github.com/FMotalleb/cord-locator/lib/new_config/provider"
)

type configFace interface {
GetEntryPoints() []entrypoint.EntryPoint
GetProviders() []provider.Provider
}

// Config of the dns server
type Config struct {
EntryPoints []entrypoint.EntryPoint
entryPoints []entrypoint.EntryPoint
Providers []provider.Provider
configFace
}

func (conf Config) GetEntryPoints() []entrypoint.EntryPoint {
return conf.entryPoints
}

func (conf Config) GetProviders() []provider.Provider {
return conf.Providers
}
28 changes: 18 additions & 10 deletions lib/new_config/config_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,36 @@ import (
"github.com/FMotalleb/cord-locator/lib/utils/iterable"
)

// ConfigData representation of yaml config file
type ConfigData struct {
EntryPoints []entrypoint.EntryPointData `yaml:"entry_points,flow"`
Providers []provider.Data `yaml:"providers,flow"`
// Data representation of yaml config file
type Data struct {
EntryPoints []entrypoint.Data `yaml:"entry_points,flow"`
Providers []provider.Data `yaml:"providers,flow"`
}

// Finalize the config data and returns new config object with given configuration
func (conf ConfigData) Finalize() Config {
// BuildConfig the config data and returns new config object with given configuration
func (conf Data) BuildConfig() Config {
return Config{
EntryPoints: conf.getEntryPoints(),
entryPoints: conf.getEntryPoints(),
Providers: conf.getProviders(),
}
}

func (conf ConfigData) getEntryPoints() []entrypoint.EntryPoint {
mapper := func(entry entrypoint.EntryPointData) entrypoint.EntryPoint {
func (conf Data) getEntryPoints() []entrypoint.EntryPoint {
mapper := func(entry entrypoint.Data) entrypoint.EntryPoint {
err := entry.Validate()
if err != nil {
panic(err)
}
return entrypoint.EntryPoint(entry)
}
return iterable.Map(conf.EntryPoints, mapper)
}
func (conf ConfigData) getProviders() []provider.Provider {
func (conf Data) getProviders() []provider.Provider {
mapper := func(entry provider.Data) provider.Provider {
err := entry.Validate()
if err != nil {
panic(err)
}
return provider.Provider(entry)
}
return iterable.Map(conf.Providers, mapper)
Expand Down
28 changes: 17 additions & 11 deletions lib/new_config/entry_point/entry_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,55 @@ import (
"github.com/FMotalleb/cord-locator/lib/validator"
)

type DnsQueryCallback func()

// EntryPoint of dns server
// indicates where to listen for dns queries
type EntryPoint interface {
GetName() string
GetPort() int
GetType() EntryType
GetType() Type
String() string
validator.Validatable
}
type EntryPointData struct {

// Data of entry point contains its (optional) name, port and type
type Data struct {
Name *string `yaml:"name,omitempty"`
Port *int `yaml:"port,omitempty"`
Type *string `yaml:"type,omitempty"`
EntryPoint
}

func (receiver EntryPointData) GetName() string {
func (receiver Data) GetName() string {
if receiver.Name == nil {
return ""
}
return *receiver.Name
}
func (receiver EntryPointData) GetPort() int {
func (receiver Data) GetPort() int {
if receiver.Port == nil {
return 53
}
return *receiver.Port
}

func (receiver EntryPointData) getTypeRaw() string {
func (receiver Data) getTypeRaw() string {
if receiver.Type == nil {
return "Raw"
return "UDP"
}
return *receiver.Type
}

func (receiver EntryPointData) GetType() EntryType {
func (receiver Data) GetType() Type {
if receiver.Type == nil {
return Raw
return UDP
}
current := parseType(receiver.Type)
return current
}

func (receiver EntryPointData) String() string {
func (receiver Data) String() string {
buffer := strings.Builder{}
buffer.WriteString("EntryPoint(")
if len(receiver.GetName()) > 0 {
Expand All @@ -62,7 +68,7 @@ func (receiver EntryPointData) String() string {
buffer.WriteString(")")
return buffer.String()
}
func (receiver EntryPointData) Validate() error {
func (receiver Data) Validate() error {
if receiver.GetPort() < 1 || receiver.GetPort() > 65535 {
return validator.NewValidationError(
"a valid entry_points.*.port value (1-65535)",
Expand All @@ -73,7 +79,7 @@ func (receiver EntryPointData) Validate() error {
switch parseType(receiver.Type) {
case Undefined:
return validator.NewValidationError(
"one of (raw | tls | https) in entry_points.*.type",
"one of (udp | tcp | tls | https) in entry_points.*.type",
fmt.Sprintf("given type (%s) does not match expected values", receiver.getTypeRaw()),
fmt.Sprintf("type value in entrypoint: %s", receiver.GetName()))
default:
Expand Down
Loading

0 comments on commit ff41544

Please sign in to comment.