8000 Added feature for listing holidays for the running month (statewise for India) by siddhant-nair · Pull Request #86 · knadh/dns.toys · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added feature for listing holidays for the running month (statewise for India) #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
43762d0
init
siddhant-nair Jan 5, 2025
fc8b460
shallow clone problem fixed
siddhant-nair Jan 5, 2025
7a7116e
output query done fetching remains
siddhant-nair Jan 5, 2025
3a7156b
holidays added till may my claude tokens are over for the day lmao
siddhant-nair Jan 6, 2025
68782ae
reading the json succesful now have to parse it and cache
siddhant-nair Jan 6, 2025
f78eb54
parsing done
siddhant-nair Jan 6, 2025
bd6d0a7
excuse file taken back to normal
siddhant-nair Jan 6, 2025
f7f052e
holiday functionality added, json for june - dec remains
siddhant-nair Jan 6, 2025
98aa25f
holiday.json complete
siddhant-nair Jan 6, 2025
acb9e3d
holiday dns.toys complete
siddhant-nair Jan 6, 2025
bbb5c94
month name to lower case
siddhant-nair Jan 6, 2025
ebf76a6
daman-diu code added
siddhant-nair Jan 7, 2025
67a4e80
holiday json for 2025 added and holiday service done
siddhant-nair Jan 7, 2025
79ecf0f
updated readme and index.html
siddhant-nair Jan 7, 2025
8b40a62
coutry support added - to be improved
siddhant-nair Jan 16, 2025
6bd3777
added json for 7 countries will add a few more
siddhant-nair Jan 17, 2025
c77fb49
added functioning of handling any tld instead of just countries
siddhant-nair Jan 18, 2025
d2735b3
removed debug prints
siddhant-nair Jan 18, 2025
f9b52b9
TLD support done and internal map has been restructured
siddhant-nair Jan 18, 2025
9ef4ad6
updated readme, help and index.html
siddhant-nair Jan 18, 2025
6a83482
toml updated
siddhant-nair Jan 18, 2025
5174f76
added ttl of 24 hours
siddhant-nair Jan 20, 2025
c4c1277
solving merge conflicts
siddhant-nair Jan 20, 2025
cd1f053
Merge branch 'master' into refined-branch
siddhant-nair Jan 20, 2025
ae19fcb
Merge branch 'master' into refined-branch
siddhant-nair Feb 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_COMMIT_DATE := $(shell git show -s --format=%ci ${LAST_COMMIT})
VERSION := $(shell git describe --abbrev=1)
VERSION := $(shell git describe --always --abbrev=1)
BUILDSTR := ${VERSION} (build "\\\#"${LAST_COMMIT} $(shell date '+%Y-%m-%d %H:%M:%S'))

GOPATH ?= $(HOME)/go
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ dig fun.dict @dns.toys

dig excuse @dns.toys

dig kerala.holidays.in or holidays.us @dns.toys

dig 5.16.nanoid @dns.toys

dig A12.9352,77.6245/12.9698,77.7500.aerial @dns.toys

dig 5.16.nanoid @dns.toys
Expand Down
91 changes: 90 additions & 1 deletion cmd/dnstoys/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (h *handlers) handleEchoIP(w dns.ResponseWriter, r *dns.Msg) {
m.Answer = append(m.Answer, rr)
// Handle ipv6.
case ip.To16() != nil:
rr, err := dns.NewRR(fmt.Sprintf("ip. %d TXT \"%s\"",IP_TTL, ip.To16().String()))
rr, err := dns.NewRR(fmt.Sprintf("ip. %d TXT \"%s\"", IP_TTL, ip.To16().String()))
if err != nil {
lo.Printf("error preparing ip response: %v", err)
return
Expand All @@ -137,6 +137,74 @@ func (h *handlers) handleEchoIP(w dns.ResponseWriter, r *dns.Msg) {
w.WriteMsg(m)
}

func (h *handlers) registerWithTLDSupport(queryName string, s Service, customTLDs []string, mux *dns.ServeMux) func(w dns.ResponseWriter, r *dns.Msg) {
//Since the dns package deos an exact match of the suffix we will manually have to add the codes
country_codes := []string{".us", ".uk", ".ca", ".au", ".de", ".fr", ".in", ".jp", ".cn", ".br", ".ru", ".za", ".it", ".es", ".mx", ".kr", ".nl", ".se", ".ch", ".sg"}
var TLDs []string

if len(customTLDs) == 0 {
TLDs = country_codes
} else {
TLDs = customTLDs
}

f := func(w dns.ResponseWriter, r *dns.Msg) {
m := &dns.Msg{}
m.SetReply(r)
m.Compress = false

if r.Opcode != dns.OpcodeQuery {
w.WriteMsg(m)
return
}

if len(m.Question) > 5 {
respErr(errors.New("too many queries."), w, m)
return
}

// Execute the service on all the questions.
out := []dns.RR{}
for _, q := range m.Question {
if q.Qtype != dns.TypeTXT && q.Qtype != dns.TypeA {
continue
}

// Call the service with the incoming query.
// Strip the service from the query
//
// eg: kerala.holidays.in will return "kerala.in"
// or holidays.in. will return "in"
ans, err := s.Query(cleanQueryWithTLD(q.Name, queryName))
if err != nil {
respErr(err, w, m)
return
}

// Convert string responses to dns.RR{}.
o, err := makeResp(ans)
if err != nil {
log.Printf("error preparing response: %v", err)
respErr(errors.New("error preparing response."), w, m)
return
}

out = append(out, o...)
}

// Write the response.
m.Answer = out
w.WriteMsg(m)
}

h.services[queryName] = s

for _, v := range TLDs {
mux.HandleFunc(queryName+v+".", f)
}
return f
}

// handlePi returns values of pi relevant for the record type.
// TXT record: "3.141592653589793238462643383279502884197169"
// A record: 3.141.59.27
Expand Down Expand Up @@ -201,6 +269,27 @@ func cleanQuery(q, trimSuffix string) string {
return reClean.ReplaceAllString(strings.TrimSuffix(q, trimSuffix), "")
}

// Strips the query and returns the argument and code
func cleanQueryWithTLD(q, queryName string) string {
cleaned := reClean.ReplaceAllString(q, "")
splitQuery := strings.Split(cleaned, ".")

strippedQuery := ""

for _, val := range splitQuery[:len(splitQuery)-1] {
if val == queryName {
if strippedQuery != "" {
strippedQuery += "."
}
continue
}

strippedQuery += val
}

return strippedQuery
}

// makeResp converts a []string of DNS responses to []dns.RR.
func makeResp(ans []string) ([]dns.RR, error) {
out := make([]dns.RR, 0, len(ans))
Expand Down
17 changes: 15 additions & 2 deletions cmd/dnstoys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/knadh/dns.toys/internal/services/epoch"
"github.com/knadh/dns.toys/internal/services/excuse"
"github.com/knadh/dns.toys/internal/services/fx"
"github.com/knadh/dns.toys/internal/services/holiday"
"github.com/knadh/dns.toys/internal/services/nanoid"
"github.com/knadh/dns.toys/internal/services/num2words"
"github.com/knadh/dns.toys/internal/services/random"
Expand Down Expand Up @@ -57,6 +58,7 @@ func initConfig() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}

f.StringSlice("config", []string{"config.toml"}, "path to one or more TOML config files to load in order")
f.Bool("version", false, "show build version")
f.Parse(os.Args[1:])
Expand Down Expand Up @@ -348,11 +350,22 @@ func main() {
help = append(help, []string{"return a developer excuse", "dig excuse @%s"})
}

// NanoID Generator
// Public Holidays
if ko.Bool("holidays.enabled") {
hldy, err := holiday.New(ko.MustString("holidays.file"))
if err != nil {
lo.Fatalf("this is the error lmao: %v", err)
}

h.registerWithTLDSupport("holidays", hldy, nil, mux)
help = append(help, []string{"return public holidays of the current month", "dig goa.holidays.in or holidays.us @%s"})
}

//NanoID Generator
if ko.Bool("nanoid.enabled") {
n := nanoid.New(ko.MustInt("nanoid.max_results"), ko.MustInt("nanoid.max_length"))
h.register("nanoid", n, mux)

help = append(help, []string{"generate random NanoIDs", "dig 2.10.nanoid @%s"})
}

Expand Down
4 changes: 4 additions & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ enabled = true
enabled = true
file = "data/excuses.txt"

[holidays]
enabled = true
file = "data/holiday_data/holidays.%s.json"

[nanoid]
enabled = true
max_length = 64
Expand Down
Loading
0