Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
FMotalleb committed Aug 5, 2023
1 parent fa8e6e7 commit ef370cf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
18 changes: 11 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ WORKDIR /app
RUN go build -o dns .

FROM alpine:latest AS runtime
COPY --from=builder /app/dns /usr/bin/
RUN chmod +x /usr/bin/dns
RUN mkdir /app
WORKDIR /app
COPY --from=builder /app/dns /app/
RUN chmod +x /app/dns
COPY ./config.yaml /app/config.yaml
EXPOSE 53
EXPOSE 53/udp
RUN apk del apk-tools

# ENV LOG_LEVEL info
# ENV LOG_FILE /dns.log
# ENV CONFIG_FILE "config.yaml"
RUN apk del apk-tools
ENV PATH "/app:$PATH"
ENV LOG_LEVEL info
ENV LOG_FILE "/app/dns.log"
ENV CONFIG_FILE "/app/config.yaml"

# watching is not supported in container
# ENV WATCH_CONFIG_FILE "false"

ENTRYPOINT [ "dns" ]
ENTRYPOINT [ "/app/dns" ]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
> instead of handling request and writing response in provider
> just handle request in provider dus making it testable
* [X] dns provider ip fallback instead of random
* [ ] change provider params
* [ ] dns providers (for each rule)
* [ ] fill `README.md`
* [ ] rule grouping
Expand Down
22 changes: 15 additions & 7 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ func HandleRequest(c config.Config, w dns.ResponseWriter, req *dns.Msg) {
responseErrorToRequest(w, req)
return
}

requestHostname := req.Question[0].Name
log.Debug().Msgf("received request to find `%s`", requestHostname)
r := c.FindRuleFor(requestHostname)
dnsProvider := provider.Provider{}
if r == nil {
switch {
case r == nil:
dnsProvider = *c.GetDefaultProvider()

} else if r.Resolver != nil {
case r.Resolver != nil:
dnsProvider = *c.FindProvider(*r.Resolver)
} else if r.Raw != nil {
case r.Raw != nil:
mapper := make(map[string]string, 0)
mapper["address"] = requestHostname
raw := r.GetRaw(dns.TypeToString[req.Question[0].Qtype])
Expand Down Expand Up @@ -59,7 +58,7 @@ func HandleRequest(c config.Config, w dns.ResponseWriter, req *dns.Msg) {
transport = "tcp"
}
resp := dnsProvider.Handle(transport, req)
w.WriteMsg(resp)
_ = w.WriteMsg(resp)
}

func responseErrorToRequest(w dns.ResponseWriter, r *dns.Msg) {
Expand Down Expand Up @@ -97,7 +96,6 @@ func isTransfer(req *dns.Msg) bool {
return false
}
func formatString(text string, hashmap map[string]string) string {

tmpl, err := template.New("Mapper").Parse(text)
if err != nil {
panic(err)
Expand All @@ -109,3 +107,13 @@ func formatString(text string, hashmap map[string]string) string {
}
return writer.String()
}

// FindFirst non-null value in items
func FindFirst[T any](items ...*T) (t *T) {
for _, item := range items {
if item != nil {
return item
}
}
return nil
}

0 comments on commit ef370cf

Please sign in to comment.