-
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
Showing
12 changed files
with
239 additions
and
42 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
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,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) | ||
} |
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 |
---|---|---|
@@ -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) | ||
|
||
} |
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,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 | ||
} |
Oops, something went wrong.