Skip to content

Commit b6f032b

Browse files
authored
reworked cname/proxy values (#6)
1 parent 6ab66c1 commit b6f032b

File tree

10 files changed

+135
-58
lines changed

10 files changed

+135
-58
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ When requesting it, we should get the following responses:
7878
8ba299a7.8ba299a8.s.example.com -> 139.162.153.168 (first A request) then 139.162.153.167 (30s), then 139.162.153.168 (next A request) and so on
7979
8080
# Cname
81-
ya-ru.c.example.com -> canonical name ya.ru
82-
google-com.c.example.com -> canonical name google.com
81+
ya.ru.c.example.com -> canonical name ya.ru
82+
google.com.c.example.com -> canonical name google.com
8383
b32-onxw2zlunbuw4zzomnxw63bnmnxs44tv.c.example.com -> canonical name something.cool.co.ru
8484
8585
# Proxy
86-
ya-ru.p.example.com -> 87.250.250.242 and 2a02:6b8::2:242
87-
google-com.p.example.com -> 64.233.164.102 and 2a00:1450:4010:c07::64
86+
ya.ru.p.example.com -> 87.250.250.242 and 2a02:6b8::2:242
87+
google.com.p.example.com -> 64.233.164.102 and 2a00:1450:4010:c07::64
8888
```

pkg/cfg/cfg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"time"
66
)
77

8-
const Version = "2.0.0"
8+
const Version = "2.0.1"
99

1010
var (
1111
// Addr is address to listen on, ":dns" if empty.

pkg/handlers/cname/handler.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package cname
22

33
import (
4+
"strings"
5+
46
"github.com/miekg/dns"
57

68
"github.com/buglloc/rip/v2/pkg/cfg"
79
"github.com/buglloc/rip/v2/pkg/handlers"
810
"github.com/buglloc/rip/v2/pkg/handlers/limiter"
11+
"github.com/buglloc/rip/v2/pkg/handlers/slices"
912
)
1013

1114
const ShortName = "c"
@@ -31,12 +34,13 @@ func (h *Handler) Name() string {
3134
}
3235

3336
func (h *Handler) Init(p handlers.Parser) error {
34-
name, _ := p.NextRaw()
35-
if name == "" {
37+
parts, _ := p.RestValues()
38+
if len(parts) == 0 {
3639
return handlers.ErrUnexpectedEOF
3740
}
3841

39-
h.TargetFQDN = handlers.PartToFQDN(name)
42+
slices.StringsReverse(parts)
43+
h.TargetFQDN = dns.Fqdn(strings.Join(parts, "."))
4044
return nil
4145
}
4246

pkg/handlers/common.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@ import (
1010
"github.com/buglloc/rip/v2/pkg/iputil"
1111
)
1212

13-
func PartToFQDN(part string) string {
14-
if strings.Count(part, "-") > 0 {
15-
// We have request like something.example-com.c.example.com
16-
part = strings.Replace(part, "-", ".", -1)
17-
}
18-
19-
return dns.Fqdn(part)
20-
}
21-
2213
func PartToIP(part string) net.IP {
2314
dotCounts := strings.Count(part, "-")
2415

pkg/handlers/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package handlers
33
type Parser interface {
44
All() ([]Handler, error)
55
Next() (Handler, error)
6+
RestValues() ([]string, error)
67
NextRaw() (string, error)
78
}

pkg/handlers/parser/parser.go

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,35 @@ import (
1818
"github.com/buglloc/rip/v2/pkg/handlers/notify"
1919
"github.com/buglloc/rip/v2/pkg/handlers/proxy"
2020
"github.com/buglloc/rip/v2/pkg/handlers/random"
21+
"github.com/buglloc/rip/v2/pkg/handlers/slices"
2122
"github.com/buglloc/rip/v2/pkg/handlers/sticky"
2223
)
2324

2425
var _ handlers.Parser = (*Parser)(nil)
2526

2627
type Parser struct {
27-
cur int
28-
maxPart int
29-
parts []string
28+
cur int
29+
maxLabel int
30+
labels []string
3031
}
3132

3233
func NewParser(req string) *Parser {
33-
parts := strings.Split(req, ".")
34-
reverse(parts)
34+
labels := strings.Split(req, ".")
35+
slices.StringsReverse(labels)
3536
return &Parser{
36-
cur: 0,
37-
maxPart: len(parts),
38-
parts: parts,
37+
cur: 0,
38+
maxLabel: len(labels),
39+
labels: labels,
3940
}
4041
}
4142

4243
func (p *Parser) Next() (handlers.Handler, error) {
43-
if p.cur >= p.maxPart {
44+
if p.cur >= p.maxLabel {
4445
return nil, handlers.ErrEOF
4546
}
4647

47-
part := p.parts[p.cur]
48-
h := parsePart(part)
48+
part := p.labels[p.cur]
49+
h := parseHandler(part)
4950
if h == nil {
5051
return nil, handlers.ErrEOF
5152
}
@@ -60,11 +61,11 @@ func (p *Parser) Next() (handlers.Handler, error) {
6061
}
6162

6263
func (p *Parser) NextRaw() (string, error) {
63-
if p.cur >= p.maxPart {
64+
if p.cur >= p.maxLabel {
6465
return "", handlers.ErrEOF
6566
}
6667

67-
ret := p.parts[p.cur]
68+
ret := p.labels[p.cur]
6869
if strings.HasPrefix(ret, "b32-") {
6970
decoded, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(ret[4:]))
7071
if err == nil {
@@ -75,6 +76,22 @@ func (p *Parser) NextRaw() (string, error) {
7576
return ret, nil
7677
}
7778

79+
func (p *Parser) RestValues() ([]string, error) {
80+
var out []string
81+
for {
82+
v, err := p.NextValue()
83+
if v == "" {
84+
return out, nil
85+
}
86+
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
out = append(out, v)
92+
}
93+
}
94+
7895
func (p *Parser) All() ([]handlers.Handler, error) {
7996
var out []handlers.Handler
8097
for {
@@ -91,13 +108,55 @@ func (p *Parser) All() ([]handlers.Handler, error) {
91108
}
92109
}
93110

94-
func parsePart(part string) handlers.Handler {
95-
if len(part) == 0 {
111+
func (p *Parser) NextValue() (string, error) {
112+
if p.cur >= p.maxLabel {
113+
return "", handlers.ErrEOF
114+
}
115+
116+
label := p.labels[p.cur]
117+
handlerName := label
118+
if indx := strings.IndexByte(handlerName, '-'); indx > 0 {
119+
handlerName = handlerName[:indx]
120+
}
121+
122+
switch handlerName {
123+
case ipv4.ShortName, ipv4.Name:
124+
fallthrough
125+
case ipv6.ShortName, ipv6.Name:
126+
fallthrough
127+
case cname.ShortName, cname.Name:
128+
fallthrough
129+
case proxy.ShortName, proxy.Name:
130+
fallthrough
131+
case random.ShortName, random.Name:
132+
fallthrough
133+
case loop.ShortName, loop.Name:
134+
fallthrough
135+
case sticky.ShortName, sticky.Name:
136+
fallthrough
137+
case notify.ShortName, notify.Name:
138+
fallthrough
139+
case defaultip.ShortName, defaultip.Name:
140+
return "", handlers.ErrEOF
141+
}
142+
143+
if strings.HasPrefix(label, "b32-") {
144+
decoded, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(label[4:]))
145+
if err == nil {
146+
label = string(decoded)
147+
}
148+
}
149+
p.cur++
150+
return label, nil
151+
}
152+
153+
func parseHandler(label string) handlers.Handler {
154+
if len(label) == 0 {
96155
return nil
97156
}
98157

99-
parts := strings.Split(part, "-")
100-
parseModifiers := func() []limiter.Limiter {
158+
parts := strings.Split(label, "-")
159+
parseLimiters := func() []limiter.Limiter {
101160
if len(parts) <= 1 {
102161
return nil
103162
}
@@ -109,7 +168,7 @@ func parsePart(part string) handlers.Handler {
109168

110169
ret, err := limiter.ParseLimiters(opts)
111170
if err != nil {
112-
log.Error("can't parse modifiers", "part", part, "err", err)
171+
log.Error("can't parse limiter", "label", label, "err", err)
113172
return nil
114173
}
115174

@@ -118,25 +177,25 @@ func parsePart(part string) handlers.Handler {
118177

119178
switch parts[0] {
120179
case ipv4.ShortName, ipv4.Name:
121-
return ipv4.NewHandler(parseModifiers()...)
180+
return ipv4.NewHandler(parseLimiters()...)
122181
case ipv6.ShortName, ipv6.Name:
123-
return ipv6.NewHandler(parseModifiers()...)
182+
return ipv6.NewHandler(parseLimiters()...)
124183
case cname.ShortName, cname.Name:
125-
return cname.NewHandler(parseModifiers()...)
184+
return cname.NewHandler(parseLimiters()...)
126185
case proxy.ShortName, proxy.Name:
127-
return proxy.NewHandler(parseModifiers()...)
186+
return proxy.NewHandler(parseLimiters()...)
128187
case random.ShortName, random.Name:
129-
return random.NewHandler(parseModifiers()...)
188+
return random.NewHandler(parseLimiters()...)
130189
case loop.ShortName, loop.Name:
131-
return loop.NewHandler(parseModifiers()...)
190+
return loop.NewHandler(parseLimiters()...)
132191
case sticky.ShortName, sticky.Name:
133-
return sticky.NewHandler(parseModifiers()...)
192+
return sticky.NewHandler(parseLimiters()...)
134193
case notify.ShortName, notify.Name:
135194
return notify.NewHandler()
136195
case defaultip.ShortName, defaultip.Name:
137-
return defaultip.NewHandler(parseModifiers()...)
196+
return defaultip.NewHandler(parseLimiters()...)
138197
default:
139-
return parseIPHandler(part)
198+
return parseIPHandler(label)
140199
}
141200
}
142201

@@ -151,10 +210,3 @@ func parseIPHandler(part string) handlers.Handler {
151210
return nil
152211
}
153212
}
154-
155-
func reverse(ss []string) {
156-
last := len(ss) - 1
157-
for i := 0; i < len(ss)/2; i++ {
158-
ss[i], ss[last-i] = ss[last-i], ss[i]
159-
}
160-
}

pkg/handlers/parser/parser_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,26 @@ func TestParser(t *testing.T) {
9393
},
9494
},
9595
{
96-
in: "lalala.example-com.c",
96+
in: "lalala.example.com.c",
97+
handlers: []handlers.Handler{
98+
&cname.Handler{TargetFQDN: "lalala.example.com."},
99+
},
100+
},
101+
{
102+
in: "1-1-1-1.v4.example.com.c",
97103
handlers: []handlers.Handler{
98104
&cname.Handler{TargetFQDN: "example.com."},
105+
&ipv4.Handler{IP: net.ParseIP("1.1.1.1").To4()},
99106
},
100107
},
101108
{
102-
in: "lalala.example-com.p",
109+
in: "lalala.example.com.p",
103110
handlers: []handlers.Handler{
104-
&proxy.Handler{TargetFQDN: "example.com."},
111+
&proxy.Handler{TargetFQDN: "lalala.example.com."},
105112
},
106113
},
107114
{
108-
in: "lalala.d.lala-com.p.d.example-com.c.d",
115+
in: "lalala.d.lala.com.p.d.example.com.c.d",
109116
handlers: []handlers.Handler{
110117
&defaultip.Handler{},
111118
&cname.Handler{TargetFQDN: "example.com."},

pkg/handlers/proxy/handler.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package proxy
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/miekg/dns"
78

89
"github.com/buglloc/rip/v2/pkg/cfg"
910
"github.com/buglloc/rip/v2/pkg/handlers"
1011
"github.com/buglloc/rip/v2/pkg/handlers/limiter"
12+
"github.com/buglloc/rip/v2/pkg/handlers/slices"
1113
"github.com/buglloc/rip/v2/pkg/resolver"
1214
)
1315

@@ -38,12 +40,13 @@ func (h *Handler) Init(p handlers.Parser) error {
3840
return handlers.ErrNotAllowed
3941
}
4042

41-
name, _ := p.NextRaw()
42-
if name == "" {
43+
parts, _ := p.RestValues()
44+
if len(parts) == 0 {
4345
return handlers.ErrUnexpectedEOF
4446
}
4547

46-
h.TargetFQDN = handlers.PartToFQDN(name)
48+
slices.StringsReverse(parts)
49+
h.TargetFQDN = dns.Fqdn(strings.Join(parts, "."))
4750
return nil
4851
}
4952

pkg/handlers/slices/slices.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package slices
2+
3+
func StringsReverse(ss []string) {
4+
last := len(ss) - 1
5+
for i := 0; i < len(ss)/2; i++ {
6+
ss[i], ss[last-i] = ss[last-i], ss[i]
7+
}
8+
}

pkg/www/static/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ <h5>Loop</h5>
104104
</p>
105105
</div>
106106

107+
<div class="col">
108+
<h5>CName</h5>
109+
<p>
110+
<ul>
111+
<li><code>ya.ru.c.{{zone}}</code><div> => returns CNAME <code>ya.ru</code></div></li>
112+
<li><code>google.com.c.{{zone}}</code><div> => returns CNAME <code>google.com</code></div></li>
113+
<li><code>b32-onxw2zlunbuw4zzomnxw63bnmnxs44tv.c.{{zone}}</code><div> => returns CNAME <code>something.cool.co.ru</code></div></li>
114+
</ul>
115+
</p>
116+
</div>
117+
107118
</div>
108119
</div>
109120
</main>

0 commit comments

Comments
 (0)