Skip to content

Commit 480aed9

Browse files
committed
wip
1 parent f9e3113 commit 480aed9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1280
-614
lines changed

README.md

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,40 @@
22
A simple DNS server that extracts IP address (or cname) from the requested domain name and sends it back in the response.
33

44
# Encoding rules
5-
DNS server parses requested name to extract the requested mode, IP or CNAME by the following rules:
5+
Since RIP extracts the response from the request, it's important to understand the encoding rules.
6+
RIP has three kinds of entities:
7+
- rr - something that generate response (e.g. IP, CNAME and so on):
68
```
7-
<optional-prefix>.<IPv4>.4.<zone> -> returns A record with <IPv4> address
8-
<optional-prefix>.<IPv6>.6.<zone> -> returns AAAA record with <IPv6> address
9-
<proxy-name>.p.<zone> -> resolve proxy name and returns it
10-
<ip1>.<ip2>.r.<zone> -> pick random <ip1> or <ip2>
11-
<ip1>.<ip2>.l.<zone> -> loop over <ip1> and <ip2>
12-
<ip1>.<ip2>.s.<zone> -> "sticky" - <ip1> for first request, then <ip2> in sticky TTL (30 sec by default)
13-
<cname>.c.<zone> -> return CNAME record with <cname>
14-
<any-name>.<zone> -> returns default address
15-
[(<IPv4>.4|<IPv6>.6)...(<IPv4>.4|<IPv6>.6)].m.<zone> -> returns multiple address according to order and type
9+
<IP> - returns IP address (guesses IPv4/IPv6)
10+
<IPv4>.[4|v4] - strictly returns IPv4 address only
11+
<IPv6>.[6|v6] - strictly returns IPv6 address only
12+
<cname>.[c|cname] - return CNAME record with <cname>
13+
<target>.[p|proxy] - resolve <target> name and returns it
14+
```
15+
- container - something that holds rr's (or another container), picked one on each request and response with it:
16+
```
17+
<rr>.<container>.[r|random] - pick random rr/container
18+
<rr>.<container>.[l|loop] - iterate over rr/container
19+
<rr1>.<rr0>.[s|sticky] - alias for loop container: <rr1-ttl-30>.<rr0-cnt-1>.l
20+
```
21+
- limit modifier - something that limit this kind of responses:
22+
```
23+
cnt-<num> - use rr <num> requests. e.g.:
24+
* 1-1-1-1.v4-cnt-10 - returns 1.1.1.1 10 times
25+
ttl-<duration> - use rr <duration> duration:
26+
* 2-2-2-2.v4-ttl-20s - returns 2.2.2.2 20 seconds from first v4-rr response
1627
```
1728

29+
Also, RIP allowing to use any prefixes (see examples below).
30+
1831
# IP address format
1932
IP address can be presented in two variants - dash-delimited and base16-form. For example, ips `0a000001` and `10-0-0-1` are equal and points to `10.0.0.1`
2033
You can also use the built-in converter to encode IP address:
2134
```
22-
$ rip ip2hex fe80::fa94:c2ff:fee5:3cf6 127.0.0.1
35+
$ rip encode fe80::fa94:c2ff:fee5:3cf6 127.0.0.1
2336
fe80000000000000fa94c2fffee53cf6 7f000001
2437
```
2538

26-
27-
# Cname/ProxyName format
28-
`cname` and `proxy` modes support two name resolution logic - prefixed and dash-delimited:
29-
Eg:
30-
```
31-
something.victim.com.c.evil.com -> CNAME to something.victim.com
32-
something.victim-com.c.evil.com -> CNAME to victim.com
33-
```
34-
3539
# Usage
3640
Run NS server for zone `example.com` with default IP `77.88.55.70` and `2a02:6b8: a:: a`:
3741
```
@@ -41,32 +45,38 @@ $ rip ns --zone=example.com --ipv4=77.88.55.70 --ipv6=2a02:6b8:a::a
4145
When requesting it, we should get the following responses:
4246
```
4347
# IPv4
44-
1-1-1-1.4.example.com -> 1.1.1.1
45-
foo.1-1-1-1.4.example.com -> 1.1.1.1
46-
bar.foo.1-1-1-1.4.example.com -> 1.1.1.1
48+
1-1-1-1.example.com -> 1.1.1.1
49+
1-1-1-1.v4.example.com -> 1.1.1.1
50+
foo.1-1-1-1.v4.example.com -> 1.1.1.1
51+
bar.foo.1-1-1-1.v4.example.com -> 1.1.1.1
52+
1010101.v4.example.com -> 1.1.1.1
4753
4854
# IPv6
49-
2a01-7e01--f03c-91ff-fe3b-c9ba.6.example.com -> 2a01:7e01::f03c:91ff:fe3b:c9ba
50-
foo.2a01-7e01--f03c-91ff-fe3b-c9ba.6.example.com -> 2a01:7e01::f03c:91ff:fe3b:c9ba
55+
2a01-7e01--f03c-91ff-fe3b-c9ba.example.com -> 2a01:7e01::f03c:91ff:fe3b:c9ba
56+
2a01-7e01--f03c-91ff-fe3b-c9ba.v6.example.com -> 2a01:7e01::f03c:91ff:fe3b:c9ba
57+
2a017e0100000000f03c91fffe3bc9ba.v6.example.com -> 2a017e0100000000f03c91fffe3bc9ba
58+
foo.2a01-7e01--f03c-91ff-fe3b-c9ba.v6.example.com -> 2a01:7e01::f03c:91ff:fe3b:c9ba
5159
foo.--1.6.example.com -> ::1
5260
5361
# Random
54-
0a000002.0a000001.r.example.com -> random between 10.0.0.1 and 10.0.0.2
62+
0a000002.0a000001.random.example.com -> random between 10.0.0.1 and 10.0.0.2
63+
0a000003.0a000002.0a000001.random.example.com -> random between 10.0.0.1 and 10.0.0.2
5564
5665
# Loop
57-
8ba299a7.8ba299a8.l.example.com -> loop over 139.162.153.167 and 139.162.153.168
66+
8ba299a7.8ba299a8.loop.example.com -> loop over 139.162.153.168 and 139.162.153.167
67+
8ba299a7.v4-ttl-5s.8ba299a8.v4-cnt-5.loop.example.com -> 139.162.153.168 (first 5 requests), then 139.162.153.167 (next 5s), then 139.162.153.168 (next 5 requests), and so on
68+
8ba299a7.v4-ttl-5s.b32-onxw2zlunbuw4zzomnxw63bnmnxs44tv.c-cnt-5.loop.example.com -> CNAME "something.cool.co.ru." (first 5 requests), then 139.162.153.167 (next 5s), CNAME "something.cool.co.ru." (first 5 requests), and so on
69+
8ba299a6.v4.8ba299a7.v4.loop-ttl-5s.8ba299a8.v4-cnt-5.loop.example.com -> 139.162.153.168 (first 5 requests), then 139.162.153.167/139.162.153.166 (next 5s), then 139.162.153.168 (next 5 requests) and so on
5870
5971
# Sticky
60-
8ba299a7.8ba299a8.s.example.com -> 139.162.153.167 then 139.162.153.168, then 139.162.153.168 and so on
72+
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
6173
6274
# Cname
63-
ya.ru.c.example.com -> canonical name ya.ru
64-
google.com.c.example.com -> canonical name google.com
75+
ya-ru.c.example.com -> canonical name ya.ru
76+
google-com.c.example.com -> canonical name google.com
77+
b32-onxw2zlunbuw4zzomnxw63bnmnxs44tv.c.example.com -> canonical name something.cool.co.ru
6578
6679
# Proxy
67-
ya.ru.p.example.com -> 87.250.250.242 and 2a02:6b8::2:242
68-
google.com.p.example.com -> 64.233.164.102 and 2a00:1450:4010:c07::64
69-
70-
# Multi
71-
1-1-1-1.4.8ba299a7.4.2a017e0100000000f03c91fffe3bc9ba.6.m.example.com -> 1.1.1.1, 139.162.153.167, 2a01:7e01::f03c:91ff:fe3b:c9ba
80+
ya-ru.p.example.com -> 87.250.250.242 and 2a02:6b8::2:242
81+
google-com.p.example.com -> 64.233.164.102 and 2a00:1450:4010:c07::64
7282
```

commands/encode.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package commands
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net"
7+
"strings"
8+
9+
"github.com/spf13/cobra"
10+
11+
"github.com/buglloc/rip/v2/pkg/iputil"
12+
)
13+
14+
var ip2Hex = &cobra.Command{
15+
Use: "encode [IP] IP",
16+
Short: "Encode IPs",
17+
RunE: func(_ *cobra.Command, args []string) error {
18+
if len(args) < 1 {
19+
return errors.New("please provide IP")
20+
}
21+
22+
results := make([]string, len(args))
23+
for i, ip := range args {
24+
if strings.Contains(ip, ":") {
25+
results[i] = iputil.EncodeIP6(net.ParseIP(ip))
26+
} else {
27+
results[i] = iputil.EncodeIP4(net.ParseIP(ip))
28+
}
29+
}
30+
31+
fmt.Println(strings.Join(results, "\t"))
32+
return nil
33+
},
34+
}
35+
36+
func init() {
37+
RootCmd.AddCommand(ip2Hex)
38+
}

commands/ip2hex.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

commands/ns.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"github.com/spf13/cobra"
1414
"github.com/spf13/viper"
1515

16-
"github.com/buglloc/rip/pkg/cfg"
17-
"github.com/buglloc/rip/pkg/cli"
18-
"github.com/buglloc/rip/pkg/nssrv"
16+
"github.com/buglloc/rip/v2/pkg/cfg"
17+
"github.com/buglloc/rip/v2/pkg/cli"
18+
"github.com/buglloc/rip/v2/pkg/nssrv"
1919
)
2020

2121
var nsServerCmd = &cobra.Command{
@@ -27,7 +27,7 @@ var nsServerCmd = &cobra.Command{
2727

2828
func init() {
2929
flags := nsServerCmd.PersistentFlags()
30-
flags.String("listen", ":53",
30+
flags.String("addr", ":53",
3131
"address to listen on")
3232
flags.StringSlice("zone", []string{"."},
3333
"your zone name (e.g. 'buglloc.com')")
@@ -51,7 +51,10 @@ func init() {
5151
}
5252

5353
func runServerCmd(_ *cobra.Command, _ []string) error {
54-
srv := nssrv.NewSrv()
54+
srv, err := nssrv.NewSrv()
55+
if err != nil {
56+
return err
57+
}
5558

5659
doneChan := make(chan error)
5760
go func() {
@@ -85,7 +88,7 @@ func parseServerConfig(_ *cobra.Command, _ []string) error {
8588
return errors.New("empty zone list, please provide at leas one")
8689
}
8790

88-
cfg.Addr = viper.GetString("Listen")
91+
cfg.Addr = viper.GetString("Addr")
8992
cfg.IPv4 = net.ParseIP(viper.GetString("Ipv4"))
9093
cfg.IPv6 = net.ParseIP(viper.GetString("Ipv6"))
9194
cfg.AllowProxy = !viper.GetBool("NoProxy")

commands/root.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ import (
99
"github.com/spf13/cobra"
1010
"github.com/spf13/viper"
1111

12-
"github.com/buglloc/rip/pkg/cli"
12+
"github.com/buglloc/rip/v2/pkg/cli"
1313
)
1414

15-
var (
16-
RootCmd = &cobra.Command{
17-
Use: "rip",
18-
Short: "Wildcard DNS",
19-
SilenceUsage: false,
20-
PreRunE: parseRootConfig,
21-
}
22-
)
15+
var RootCmd = &cobra.Command{
16+
Use: "rip",
17+
Short: "Wildcard DNS",
18+
SilenceUsage: true,
19+
PreRunE: parseRootConfig,
20+
}
2321

2422
func init() {
2523
cobra.OnInitialize(initConfig)

commands/obfuscator.go renamed to commands/uglify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66

77
"github.com/spf13/cobra"
88

9-
obfuscator "github.com/buglloc/rip/pkg/obfustacor"
9+
obfuscator "github.com/buglloc/rip/v2/pkg/obfustacor"
1010
)
1111

1212
var uglify = &cobra.Command{
1313
Use: "uglify IP",
14-
Short: "Obfuscate IP",
14+
Short: "Uglify (obfuscate) IP",
1515
RunE: runUglifyCmd,
1616
}
1717

commands/version.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ import (
55

66
"github.com/spf13/cobra"
77

8-
"github.com/buglloc/rip/pkg/cfg"
8+
"github.com/buglloc/rip/v2/pkg/cfg"
99
)
1010

1111
var version = &cobra.Command{
1212
Use: "version",
1313
Short: "Print rip version",
14-
RunE: versionCmd,
14+
RunE: func(_ *cobra.Command, _ []string) error {
15+
fmt.Printf("RIP v%s\n", cfg.Version)
16+
return nil
17+
},
1518
}
1619

1720
func init() {
1821
RootCmd.AddCommand(version)
1922
}
20-
21-
func versionCmd(cmd *cobra.Command, _ []string) error {
22-
fmt.Printf("RIP v%s\n", cfg.Version)
23-
return nil
24-
}

go.mod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
module github.com/buglloc/rip
1+
module github.com/buglloc/rip/v2
22

3-
go 1.15
3+
go 1.16
44

55
require (
66
github.com/buglloc/simplelog v0.0.0-20190311170333-2fbd6fd42b73
77
github.com/karlseguin/ccache v2.0.3+incompatible
88
github.com/karlseguin/expect v1.0.8 // indirect
9-
github.com/miekg/dns v1.1.41
9+
github.com/miekg/dns v1.1.42
1010
github.com/mitchellh/go-homedir v1.1.0
1111
github.com/spf13/cobra v1.1.3
1212
github.com/spf13/pflag v1.0.5
1313
github.com/spf13/viper v1.7.1
14+
github.com/stretchr/testify v1.7.0
1415
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
1516
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
17+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1618
)

go.sum

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
122122
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
123123
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
124124
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
125-
github.com/miekg/dns v1.1.41 h1:WMszZWJG0XmzbK9FEmzH2TVcqYzFesusSIB41b8KHxY=
126-
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
125+
github.com/miekg/dns v1.1.42 h1:gWGe42RGaIqXQZ+r3WUGEKBEtvPHY2SXo4dqixDNxuY=
126+
github.com/miekg/dns v1.1.42/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
127127
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
128128
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
129129
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
@@ -185,8 +185,9 @@ github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
185185
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
186186
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
187187
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
188-
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
189188
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
189+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
190+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
190191
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
191192
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
192193
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -327,6 +328,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
327328
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
328329
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
329330
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
331+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
332+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
333+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
330334
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
331335
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
332336
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package main
22

3-
import "github.com/buglloc/rip/commands"
3+
import (
4+
"math/rand"
5+
"time"
6+
7+
"github.com/buglloc/rip/v2/commands"
8+
)
49

510
func main() {
11+
rand.Seed(time.Now().Unix())
612
commands.Execute()
713
}

0 commit comments

Comments
 (0)