Skip to content

Commit

Permalink
[Feat]<Provider>
Browse files Browse the repository at this point in the history
  • Loading branch information
FMotalleb committed Oct 7, 2023
1 parent 9044b16 commit c9ceeec
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 42 deletions.
2 changes: 1 addition & 1 deletion config.new.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ default_providers: # (def(null))
- opendns

providers:
cf: # naming providers is mandatory
- name: cf # naming providers is mandatory
# DNS_SERVER_DEFINITION
type: raw # (tls || https || lua) (if lua addresses should point to lua file)
addresses:
Expand Down
24 changes: 19 additions & 5 deletions cord_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,34 @@ var (
)
var data = `
entry_points:
- name:
port: 53 # (def(53))
type: raw # (def(raw) || tls || https)
- name:
port: # (def(53))
type: raw # (def(raw) || tls || https)
providers:
- name: # 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
`

func main() {
testConfig := newconfig.Config{}
yaml.Unmarshal([]byte(data), &testConfig)
err := yaml.Unmarshal([]byte(data), &testConfig)
if err != nil {
println(err.Error())
}
confs := make([]string, 0)
for _, item := range testConfig.EntryPoints {
for _, item := range testConfig.Providers {
println(item.Validate().Error())
confs = append(confs, item.String())
}

log.Info().Msg(strings.Join(confs, ","))
return

log.Info().Msg("Starting DNS Server")
address := DNSConfig.Global.Address
udpServer := &dns.Server{Addr: address, Net: "udp"}
Expand Down Expand Up @@ -95,6 +108,7 @@ func init() {
}
}

return
configPath, hasConfigFile := os.LookupEnv("CONFIG_FILE")
if !hasConfigFile {
log.Warn().Msg("`CONFIG_FILE` was missing from environment table default value is `./config.yaml`")
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
)

require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/sourcegraph/conc v0.3.0
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/sagikazarmark/crypt v0.10.0 h1:96E1qrToLBU6fGzo+PRRz7KGOc9FkYFiPnR3/zf8Smg=
github.com/sagikazarmark/crypt v0.10.0/go.mod h1:gwTNHQVoOS3xp9Xvz5LLR+1AauC5M6880z5NWzdhOyQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
Expand All @@ -267,6 +269,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -303,6 +306,8 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
28 changes: 28 additions & 0 deletions lib/internal_dns/internal_messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package data

import (
"github.com/FMotalleb/cord-locator/lib/utils/iterable"
"github.com/miekg/dns"
)

type Question struct {
Type string
Class string
Name string
}

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,
}
}
return iterable.Map[dns.Question, Question](msg.Question, mapper)
}
46 changes: 23 additions & 23 deletions lib/new_config/entry_point/entry_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,49 @@ import (
"github.com/FMotalleb/cord-locator/lib/validator"
)

type EntryPoint struct {
type EntryPoint interface {
GetName() string
GetPort() int
GetType() EntryType
String() string
validator.Validatable
}
type EntryPointData struct {
Name *string `yaml:"name,omitempty"`
Port *int `yaml:"port,omitempty"`
Type *string `yaml:"type,omitempty"`
EntryPoint
}

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

func (receiver EntryPoint) getTypeRaw() string {
func (receiver EntryPointData) getTypeRaw() string {
if receiver.Type == nil {
return "<unset>"
return "Raw"
}
return *receiver.Type
}

func (receiver EntryPoint) GetType() EntryType {
actual := parseType(receiver.Type)
switch actual {
case Undefined:
func (receiver EntryPointData) GetType() EntryType {
if receiver.Type == nil {
return Raw
default:
return actual
}
current := parseType(receiver.Type)
return current
}

func (receiver EntryPoint) String() string {
func (receiver EntryPointData) String() string {
buffer := strings.Builder{}
buffer.WriteString("EntryPoint(")
if len(receiver.GetName()) > 0 {
Expand All @@ -52,29 +58,23 @@ func (receiver EntryPoint) String() string {
buffer.WriteString("Name<Empty>: '', ")
}
buffer.WriteString(fmt.Sprintf("Port: %d, ", receiver.GetPort()))
buffer.WriteString(fmt.Sprintf("Type: %v", parseType(receiver.Type).String()))
buffer.WriteString(fmt.Sprintf("Type: %v", receiver.GetType().String()))
buffer.WriteString(")")
return buffer.String()
}
func (receiver EntryPoint) Validate() error {
// if len(receiver.GetName()) == 0 {
// return validator.NewValidationError(
// "to receive entry_points.name",
// "no name or empty name received",
// "missing name for an entry point in config file")
// }
func (receiver EntryPointData) Validate() error {
if receiver.GetPort() < 1 || receiver.GetPort() > 65535 {
return validator.NewValidationError(
"to receive a valid entry_points.port value (1-65535)",
"a valid entry_points.*.port value (1-65535)",
fmt.Sprintf("%d", receiver.GetPort()),
fmt.Sprintf("port value in entrypoint: %s", receiver.GetName()))
}

switch parseType(receiver.Type) {
case Undefined:
return validator.NewValidationError(
"to receive one of (raw | tls | https) in entry_points.type",
fmt.Sprintf("given type does not match expectee %s", receiver.getTypeRaw()),
"one of (raw | 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:
break
Expand Down
20 changes: 10 additions & 10 deletions lib/new_config/entry_point/entry_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ type EntryType int

const (
Raw EntryType = iota
TLS
HTTPS
// TLS
// HTTPS
Undefined = -1
)

Expand All @@ -16,10 +16,10 @@ func parseType(typeName *string) EntryType {
switch *typeName {
case "raw":
return Raw
case "tls":
return TLS
case "https":
return HTTPS
// case "tls":
// return TLS
// case "https":
// return HTTPS
default:
return Undefined
}
Expand All @@ -28,10 +28,10 @@ func (receiver EntryType) String() string {
switch receiver {
case 0:
return "Raw"
case 1:
return "TLS"
case 2:
return "HTTPS"
// case 1:
// return "TLS"
// case 2:
// return "HTTPS"
default:
return "Undefined"
}
Expand Down
17 changes: 15 additions & 2 deletions lib/new_config/new_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package newconfig

import entrypoint "github.com/FMotalleb/cord-locator/lib/new_config/entry_point"
import (
entrypoint "github.com/FMotalleb/cord-locator/lib/new_config/entry_point"
"github.com/FMotalleb/cord-locator/lib/new_config/provider"
"github.com/FMotalleb/cord-locator/lib/utils/iterable"
)

type Config struct {
EntryPoints []entrypoint.EntryPoint `yaml:"entry_points,flow"`
EntryPoints []entrypoint.EntryPointData `yaml:"entry_points,flow"`
Providers []provider.ProviderData `yaml:"providers,flow"`
}

func (conf Config) GetEntryPoints() []entrypoint.EntryPoint {
mapper := func(entry entrypoint.EntryPointData) entrypoint.EntryPoint {
return entrypoint.EntryPoint(entry)
}
return iterable.Map(conf.EntryPoints, mapper)

}
76 changes: 76 additions & 0 deletions lib/new_config/provider/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package provider

import (
"fmt"
"strings"

"github.com/FMotalleb/cord-locator/lib/validator"
"github.com/miekg/dns"
)

type Provider interface {
GetName() string
GetType() ProviderType
Resolve([]dns.Question) (answer []dns.RR, err error)
String() string
validator.Validatable
}

type ProviderData struct {
Name *string `yaml:"name,omitempty"`
Type *string `yaml:"type,omitempty"`
Addresses *[]string `yaml:"addresses,omitempty"`
Provider
}

func (receiver ProviderData) GetName() string {
if receiver.Name == nil {
panic("provider name is missing")
}
return *receiver.Name
}

func (receiver ProviderData) GetType() ProviderType {
if receiver.Type == nil {
return Raw
}
current := parseType(receiver.Type)
return current
}

func (receiver ProviderData) Resolve([]dns.Question) (answer []dns.RR, err error) {
// TODO: Implement Resolve method
panic("UnImplemented")
}

func (receiver ProviderData) String() string {
buffer := strings.Builder{}
buffer.WriteString("Provider(")
buffer.WriteString(fmt.Sprintf("Name: %s, ", receiver.GetName()))
if receiver.Addresses != nil && len(*receiver.Addresses) > 0 {
addr := *receiver.Addresses
buffer.WriteString(fmt.Sprintf("Addresses: %s, ", strings.Join(addr, ", ")))
}

buffer.WriteString(fmt.Sprintf("Type: %v", receiver.GetType().String()))
buffer.WriteString(")")
return buffer.String()
}
func (receiver ProviderData) Validate() error {
if receiver.Name == nil {
return validator.NewValidationError(
"a name for every provider -> providers.*.name",
"no name",
"missing name on a provider in yaml config")
}
switch receiver.GetType() {
case Undefined:
return validator.NewValidationError(
"one of (raw,lua,https,tls...) in providers.*.type",
fmt.Sprintf("`%s`", *receiver.Type),
fmt.Sprintf("the type in provider: `%s`", receiver.GetName()))
default:
break
}
return nil
}
Loading

0 comments on commit c9ceeec

Please sign in to comment.