This repository was archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathrtrdump.go
227 lines (198 loc) · 5.68 KB
/
rtrdump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
"fmt"
rtr "github.com/cloudflare/gortr/lib"
"github.com/cloudflare/gortr/prefixfile"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"net"
"os"
"runtime"
"time"
)
const (
AppVersion = "RTRdump 0.11.0"
ENV_SSH_PASSWORD = "RTR_SSH_PASSWORD"
ENV_SSH_KEY = "RTR_SSH_KEY"
METHOD_NONE = iota
METHOD_PASSWORD
METHOD_KEY
)
var (
Connect = flag.String("connect", "127.0.0.1:8282", "Connection address")
OutFile = flag.String("file", "output.json", "Output file")
InitSerial = flag.Bool("serial", false, "Send serial query instead of reset")
Serial = flag.Int("serial.value", 0, "Serial number")
Session = flag.Int("session.id", 0, "Session ID")
ConnType = flag.String("type", "plain", "Type of connection: plain, tls or ssh")
ValidateCert = flag.Bool("tls.validate", true, "Validate TLS")
ValidateSSH = flag.Bool("ssh.validate", false, "Validate SSH key")
SSHServerKey = flag.String("ssh.validate.key", "", "SSH server key SHA256 to validate")
SSHAuth = flag.String("ssh.method", "none", "Select SSH method (none, password or key)")
SSHAuthUser = flag.String("ssh.auth.user", "rpki", "SSH user")
SSHAuthPassword = flag.String("ssh.auth.password", "", fmt.Sprintf("SSH password (if blank, will use envvar %v)", ENV_SSH_PASSWORD))
SSHAuthKey = flag.String("ssh.auth.key", "id_rsa", fmt.Sprintf("SSH key file (if blank, will use envvar %v)", ENV_SSH_KEY))
RefreshInterval = flag.Int("refresh", 600, "Refresh interval in seconds")
LogLevel = flag.String("loglevel", "info", "Log level")
LogDataPDU = flag.Bool("datapdu", false, "Log data PDU")
Version = flag.Bool("version", false, "Print version")
typeToId = map[string]int{
"plain": rtr.TYPE_PLAIN,
"tls": rtr.TYPE_TLS,
"ssh": rtr.TYPE_SSH,
}
authToId = map[string]int{
"none": METHOD_NONE,
"password": METHOD_PASSWORD,
"key": METHOD_KEY,
}
)
type Client struct {
Data prefixfile.ROAList
InitSerial bool
Serial uint32
SessionID uint16
}
func (c *Client) HandlePDU(cs *rtr.ClientSession, pdu rtr.PDU) {
switch pdu := pdu.(type) {
case *rtr.PDUIPv4Prefix:
rj := prefixfile.ROAJson{
Prefix: pdu.Prefix.String(),
ASN: fmt.Sprintf("AS%v", pdu.ASN),
Length: pdu.MaxLen,
}
c.Data.Data = append(c.Data.Data, rj)
c.Data.Metadata.Counts++
if *LogDataPDU {
log.Debugf("Received: %v", pdu)
}
case *rtr.PDUIPv6Prefix:
rj := prefixfile.ROAJson{
Prefix: pdu.Prefix.String(),
ASN: fmt.Sprintf("AS%v", pdu.ASN),
Length: pdu.MaxLen,
}
c.Data.Data = append(c.Data.Data, rj)
c.Data.Metadata.Counts++
if *LogDataPDU {
log.Debugf("Received: %v", pdu)
}
case *rtr.PDUEndOfData:
t := time.Now().UTC().UnixNano() / 1000000000
c.Data.Metadata.Generated = int(t)
c.Data.Metadata.Valid = int(t) + int(pdu.RefreshInterval)
c.Data.Metadata.Serial = int(pdu.SerialNumber)
cs.Disconnect()
log.Debugf("Received: %v", pdu)
case *rtr.PDUCacheResponse:
log.Debugf("Received: %v", pdu)
default:
log.Debugf("Received: %v", pdu)
cs.Disconnect()
}
}
func (c *Client) ClientConnected(cs *rtr.ClientSession) {
if c.InitSerial {
cs.SendSerialQuery(c.SessionID, c.Serial)
} else {
cs.SendResetQuery()
}
}
func (c *Client) ClientDisconnected(cs *rtr.ClientSession) {
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
if *Version {
fmt.Println(AppVersion)
os.Exit(0)
}
lvl, _ := log.ParseLevel(*LogLevel)
log.SetLevel(lvl)
cc := rtr.ClientConfiguration{
ProtocolVersion: rtr.PROTOCOL_VERSION_1,
Log: log.StandardLogger(),
}
client := &Client{
Data: prefixfile.ROAList{
Metadata: prefixfile.MetaData{},
Data: make([]prefixfile.ROAJson, 0),
},
InitSerial: *InitSerial,
Serial: uint32(*Serial),
SessionID: uint16(*Session),
}
clientSession := rtr.NewClientSession(cc, client)
configTLS := &tls.Config{
InsecureSkipVerify: !*ValidateCert,
}
configSSH := &ssh.ClientConfig{
Auth: make([]ssh.AuthMethod, 0),
User: *SSHAuthUser,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
serverKeyHash := ssh.FingerprintSHA256(key)
if *ValidateSSH {
if serverKeyHash != fmt.Sprintf("SHA256:%v", *SSHServerKey) {
return errors.New(fmt.Sprintf("Server key hash %v is different than expected key hash SHA256:%v", serverKeyHash, *SSHServerKey))
}
}
log.Infof("Connected to server %v via ssh. Fingerprint: %v", remote.String(), serverKeyHash)
return nil
},
}
if authType, ok := authToId[*SSHAuth]; ok {
if authType == METHOD_PASSWORD {
password := *SSHAuthPassword
if password == "" {
password = os.Getenv(ENV_SSH_PASSWORD)
}
configSSH.Auth = append(configSSH.Auth, ssh.Password(password))
} else if authType == METHOD_KEY {
var keyBytes []byte
var err error
if *SSHAuthKey == "" {
keyBytesStr := os.Getenv(ENV_SSH_KEY)
keyBytes = []byte(keyBytesStr)
} else {
keyBytes, err = ioutil.ReadFile(*SSHAuthKey)
if err != nil {
log.Fatal(err)
}
}
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
log.Fatal(err)
}
configSSH.Auth = append(configSSH.Auth, ssh.PublicKeys(signer))
}
} else {
log.Fatalf("Auth type %v unknown", *SSHAuth)
}
log.Infof("Connecting with %v to %v", *ConnType, *Connect)
err := clientSession.Start(*Connect, typeToId[*ConnType], configTLS, configSSH)
if err != nil {
log.Fatal(err)
}
var f io.Writer
if *OutFile != "" {
ff, err := os.Create(*OutFile)
defer ff.Close()
if err != nil {
log.Fatal(err)
}
f = ff
} else {
f = os.Stdout
}
enc := json.NewEncoder(f)
err = enc.Encode(client.Data)
if err != nil {
log.Fatal(err)
}
}