-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.go
More file actions
264 lines (243 loc) · 8.42 KB
/
main.go
File metadata and controls
264 lines (243 loc) · 8.42 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
coap "github.com/absmach/coap-cli/coap"
"github.com/fatih/color"
piondtls "github.com/pion/dtls/v3"
coapmsg "github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/message/pool"
"github.com/spf13/cobra"
)
const verboseFmt = `Date: %s
Code: %s
Type: %s
Token: %s
Message-ID: %d
`
func main() {
req := &request{}
rootCmd := &cobra.Command{
Use: "coap-cli <method> <URL> [options]",
Short: "CLI for CoAP",
}
getCmd := &cobra.Command{
Use: "get <url>",
Short: "Perform a GET request on a COAP resource",
Example: "coap-cli get m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic -a 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -H localhost -p 5683 -O 17,50 -o \n" +
"coap-cli get m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic --auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb --host localhost --port 5683 --options 17,50 --observe",
Run: runCmd(req, codes.GET),
}
getCmd.Flags().BoolVarP(&req.observe, "observe", "o", false, "Observe resource")
putCmd := &cobra.Command{
Use: "put <url>",
Short: "Perform a PUT request on a COAP resource",
Example: "coap-cli put /test -H coap.me -p 5683 -c 50 -d 'hello, world'\n" +
"coap-cli put /test --host coap.me --port 5683 --content-format 50 --data 'hello, world'",
Run: runCmd(req, codes.PUT),
}
putCmd.Flags().StringVarP(&req.data, "data", "d", "", "Data")
postCmd := &cobra.Command{
Use: "post <url>",
Short: "Perform a POST request on a COAP resource",
Example: "coap-cli post m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic -a 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -H localhost -p 5683 -c 50 -d 'hello, world'\n" +
"coap-cli post m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic --auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb --host localhost --port 5683 --content-format 50 --data 'hello, world'",
Run: runCmd(req, codes.POST),
}
postCmd.Flags().StringVarP(&req.data, "data", "d", "", "Data")
deleteCmd := &cobra.Command{
Use: "delete <url>",
Short: "Perform a DELETE request on a COAP resource",
Example: "coap-cli delete /test -H coap.me -p 5683 -c 50 -d 'hello, world' -O 17,50\n" +
"coap-cli delete /test --host coap.me --port 5683 --content-format 50 --data 'hello, world' --options 17,50",
Run: runCmd(req, codes.DELETE),
}
deleteCmd.Flags().StringVarP(&req.data, "data", "d", "", "Data")
rootCmd.AddCommand(getCmd, putCmd, postCmd, deleteCmd)
rootCmd.PersistentFlags().StringVarP(&req.host, "host", "H", "localhost", "Host")
rootCmd.PersistentFlags().StringVarP(&req.port, "port", "p", "5683", "Port")
rootCmd.PersistentFlags().StringVarP(&req.auth, "auth", "a", "", "Auth")
rootCmd.PersistentFlags().IntVarP(&req.contentFormat, "content-format", "c", 50, "Content format")
rootCmd.PersistentFlags().StringArrayVarP(&req.options, "options", "O", []string{}, "Add option num with contents of text to the request. If the text begins with 0x, then the hex text (two [0-9a-f] per byte) is converted to binary data.")
rootCmd.PersistentFlags().Uint64VarP(&req.keepAlive, "keep-alive", "k", 0, "Send a ping after interval seconds of inactivity. If not specified (or 0), keep-alive is disabled (default).")
rootCmd.PersistentFlags().Uint32VarP(&req.maxRetries, "max-retries", "m", 10, "Max retries for keep alive")
rootCmd.PersistentFlags().BoolVarP(&req.verbose, "verbose", "v", false, "Verbose output")
rootCmd.PersistentFlags().StringVarP(&req.certFile, "cert-file", "C", "", "Client certificate file")
rootCmd.PersistentFlags().StringVarP(&req.keyFile, "key-file", "K", "", "Client key file")
rootCmd.PersistentFlags().StringVarP(&req.clientCAFile, "ca-file", "A", "", "Client CA file")
rootCmd.PersistentFlags().BoolVarP(&req.useDTLS, "use DTLS", "s", false, "Use DTLS")
if err := rootCmd.Execute(); err != nil {
log.Fatalf("Error executing command: %v", err)
}
}
func printMsg(m *pool.Message, verbose bool) {
if m != nil && verbose {
fmt.Printf(verboseFmt,
time.Now().Format(time.RFC1123),
m.Code(),
m.Type(),
m.Token(),
m.MessageID())
cf, err := m.ContentFormat()
if err == nil {
fmt.Printf("Content-Format: %s \n", cf.String())
}
bs, err := m.BodySize()
if err == nil {
fmt.Printf("Content-Length: %d\n", bs)
}
}
body, err := m.ReadBody()
if err != nil {
log.Fatalf("failed to read body %v", err)
}
if len(body) > 0 {
fmt.Printf("\n%s\n", string(body))
}
}
func makeRequest(req *request, args []string) {
dtlsConfig, err := req.createDTLSConfig()
if err != nil {
log.Fatalf("Error creating DTLS config: %v", err)
}
client, err := coap.NewClient(req.host+":"+req.port, req.keepAlive, req.maxRetries, dtlsConfig)
if err != nil {
log.Fatalf("Error coap creating client: %v", err)
}
var opts coapmsg.Options
for _, optString := range req.options {
opt := strings.Split(optString, ",")
if len(opt) < 2 {
log.Fatal("Invalid option format")
}
optId, err := strconv.ParseUint(opt[0], 10, 16)
if err != nil {
log.Fatal("Error parsing option id")
}
if strings.HasPrefix(opt[1], "0x") {
op := strings.TrimPrefix(opt[1], "0x")
optValue, err := hex.DecodeString(op)
if err != nil {
log.Fatal("Invalid option value ", err.Error())
}
opts = append(opts, coapmsg.Option{ID: coapmsg.OptionID(optId), Value: optValue})
} else {
opts = append(opts, coapmsg.Option{ID: coapmsg.OptionID(optId), Value: []byte(opt[1])})
}
}
if req.auth != "" {
opts = append(opts, coapmsg.Option{ID: coapmsg.URIQuery, Value: []byte("auth=" + req.auth)})
}
if opts.HasOption(coapmsg.Observe) {
if value, _ := opts.GetBytes(coapmsg.Observe); len(value) == 1 && value[0] == 0 && !req.observe {
req.observe = true
}
}
switch req.code {
case codes.GET:
switch {
case req.observe:
obs, err := client.Receive(args[0], req.verbose, opts...)
if err != nil {
log.Fatalf("Error observing resource: %v", err)
}
errs := make(chan error, 1)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()
err = <-errs
if err := obs.Cancel(context.Background(), opts...); err != nil {
log.Fatalf("Error cancelling observation: %v", err)
}
log.Fatalf("Observation terminated: %v", err)
default:
res, err := client.Send(args[0], req.code, coapmsg.MediaType(req.contentFormat), nil, opts...)
if err != nil {
log.Fatalf("Error sending message: %v", err)
}
printMsg(res, req.verbose)
}
default:
pld := strings.NewReader(req.data)
res, err := client.Send(args[0], req.code, coapmsg.MediaType(req.contentFormat), pld, opts...)
if err != nil {
log.Fatalf("Error sending message: %v", err)
}
printMsg(res, req.verbose)
}
}
func runCmd(req *request, code codes.Code) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Fprintf(os.Stdout, color.YellowString("\nusage: %s\n\n"), cmd.Use)
return
}
req.code = code
makeRequest(req, args)
}
}
type request struct {
code codes.Code
host string
port string
contentFormat int
auth string
observe bool
data string
options []string
keepAlive uint64
verbose bool
maxRetries uint32
certFile string
keyFile string
clientCAFile string
useDTLS bool
}
func (r *request) createDTLSConfig() (*piondtls.Config, error) {
if !r.useDTLS {
return nil, nil
}
dc := &piondtls.Config{}
if r.certFile != "" && r.keyFile != "" {
cert, err := tls.LoadX509KeyPair(r.certFile, r.keyFile)
if err != nil {
return nil, errors.Join(errors.New("failed to load certificates"), err)
}
dc.Certificates = []tls.Certificate{cert}
}
rootCA, err := loadCertFile(r.clientCAFile)
if err != nil {
return nil, errors.Join(errors.New("failed to load Client CA"), err)
}
if len(rootCA) > 0 {
if dc.RootCAs == nil {
dc.RootCAs = x509.NewCertPool()
}
if !dc.RootCAs.AppendCertsFromPEM(rootCA) {
return nil, errors.New("failed to append root ca tls.Config")
}
}
return dc, nil
}
func loadCertFile(certFile string) ([]byte, error) {
if certFile != "" {
return os.ReadFile(certFile)
}
return []byte{}, nil
}