Skip to content

Commit e0f7b32

Browse files
NOISSUE - Add DTLS config to coap-cli (#11)
* feat: Migrate gocoap from v2 to v3.3 Signed-off-by: 1998-felix <felix.gateru@gmail.com> * refactor: Remove .env file Signed-off-by: 1998-felix <felix.gateru@gmail.com> * feat: add options command, example with coap.me Signed-off-by: 1998-felix <felix.gateru@gmail.com> * feat: add dtls config Signed-off-by: 1998-felix <felix.gateru@gmail.com> * fix(ci): change base branch to master * chore: update golangci-linter * chore(dependabot): group gh and go dependencies * chore: update CI and go version to 1.25 * chore: update dependencies * refactor: remove examples * refactor: cleanup dependencies * fix(Makefile): change command used to build cli --------- Signed-off-by: 1998-felix <felix.gateru@gmail.com> Co-authored-by: Rodney Osodo <socials@rodneyosodo.com>
1 parent 404de36 commit e0f7b32

File tree

9 files changed

+257
-134
lines changed

9 files changed

+257
-134
lines changed

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,20 @@ updates:
77
directory: ".github/workflows"
88
schedule:
99
interval: "monthly"
10+
day: "monday"
11+
timezone: "Europe/Paris"
12+
groups:
13+
gh-dependency:
14+
patterns:
15+
- "*"
1016

1117
- package-ecosystem: "gomod"
1218
directory: "/"
1319
schedule:
1420
interval: "monthly"
21+
day: "monday"
22+
timezone: "Europe/Paris"
23+
groups:
24+
go-dependency:
25+
patterns:
26+
- "*"

.github/workflows/ci.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ name: Continuous Integration
66
on:
77
pull_request:
88
branches:
9-
- master
9+
- main
1010
push:
1111
branches:
12-
- master
12+
- main
1313

1414
jobs:
1515
lint-and-build:
@@ -18,18 +18,19 @@ jobs:
1818

1919
steps:
2020
- name: Checkout Code
21-
uses: actions/checkout@v4
21+
uses: actions/checkout@v5
2222

2323
- name: Setup Go
2424
uses: actions/setup-go@v5
2525
with:
26-
go-version: 1.22.x
26+
go-version: 1.25.x
2727
cache-dependency-path: "go.sum"
2828

2929
- name: Check linting
30-
uses: golangci/golangci-lint-action@v6
30+
uses: golangci/golangci-lint-action@v8
3131
with:
32-
version: v1.56.2
32+
version: v2.4.0
33+
args: --config ./.golangci.yml
3334

3435
- name: Build Binaries
3536
run: |

.golangci.yml

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,94 @@
11
# Copyright (c) Abstract Machines
22
# SPDX-License-Identifier: Apache-2.0
33

4+
version: "2"
45
run:
56
concurrency: 4
6-
timeout: 1m
7-
8-
linters-settings:
9-
importas:
10-
no-unaliased: true
11-
no-extra-aliases: false
12-
gocritic:
13-
enabled-checks:
14-
- importShadow
15-
- httpNoBody
16-
- paramTypeCombine
17-
- emptyStringTest
18-
- builtinShadow
19-
- exposedSyncMutex
20-
disabled-checks:
21-
- appendAssign
22-
enabled-tags:
23-
- diagnostic
24-
disabled-tags:
25-
- performance
26-
- style
27-
- experimental
28-
- opinionated
29-
stylecheck:
30-
checks: ["-ST1000", "-ST1003", "-ST1020", "-ST1021", "-ST1022"]
31-
goheader:
32-
template: |-
33-
Copyright (c) Abstract Machines
34-
SPDX-License-Identifier: Apache-2.0
35-
367
linters:
37-
disable-all: true
38-
fast: true
8+
default: none
399
enable:
40-
- gocritic
41-
- gosimple
42-
- errcheck
43-
- govet
44-
- unused
45-
- goconst
46-
- godot
47-
- godox
48-
- ineffassign
49-
- misspell
50-
- stylecheck
51-
- whitespace
52-
- gci
53-
- gofmt
54-
- goimports
55-
- loggercheck
56-
- goheader
5710
- asasalint
5811
- asciicheck
5912
- bidichk
6013
- contextcheck
14+
- copyloopvar
6115
- decorder
6216
- dogsled
17+
- dupword
18+
- errcheck
6319
- errchkjson
6420
- errname
65-
- execinquery
66-
- exportloopref
6721
- ginkgolinter
6822
- gocheckcompilerdirectives
69-
- gofumpt
23+
- goconst
24+
- gocritic
25+
- godot
26+
- godox
27+
- goheader
7028
- goprintffuncname
29+
- govet
7130
- importas
31+
- ineffassign
32+
- loggercheck
7233
- makezero
7334
- mirror
35+
- misspell
7436
- nakedret
75-
- dupword
37+
- staticcheck
38+
- unused
39+
- whitespace
40+
settings:
41+
gocritic:
42+
enabled-checks:
43+
- importShadow
44+
- httpNoBody
45+
- paramTypeCombine
46+
- emptyStringTest
47+
- builtinShadow
48+
- exposedSyncMutex
49+
disabled-checks:
50+
- appendAssign
51+
enabled-tags:
52+
- diagnostic
53+
disabled-tags:
54+
- performance
55+
- style
56+
- experimental
57+
- opinionated
58+
goheader:
59+
template: |-
60+
Copyright (c) Abstract Machines
61+
SPDX-License-Identifier: Apache-2.0
62+
importas:
63+
no-unaliased: true
64+
no-extra-aliases: false
65+
staticcheck:
66+
checks:
67+
- -ST1000
68+
- -ST1003
69+
- -ST1020
70+
- -ST1021
71+
- -ST1022
72+
exclusions:
73+
generated: lax
74+
presets:
75+
- comments
76+
- common-false-positives
77+
- legacy
78+
- std-error-handling
79+
paths:
80+
- third_party$
81+
- builtin$
82+
- examples$
83+
formatters:
84+
enable:
85+
- gci
86+
- gofmt
87+
- gofumpt
88+
- goimports
89+
exclusions:
90+
generated: lax
91+
paths:
92+
- third_party$
93+
- builtin$
94+
- examples$

Makefile

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
# Copyright (c) Abstract Machines
22
# SPDX-License-Identifier: Apache-2.0
33

4-
all:
5-
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o build/coap-cli-linux main.go
6-
CGO_ENABLED=0 GOOS=darwin go build -ldflags "-s -w" -o build/coap-cli-darwin main.go
7-
CGO_ENABLED=0 GOOS=windows go build -ldflags "-s -w" -o build/coap-cli-windows main.go
4+
INSTALL_DIR=/usr/local/bin
5+
BUILD_DIR=build
6+
BUILD_FLAGS=-ldflags "-s -w"
7+
8+
.PHONY: all linux darwin windows install install-linux
9+
10+
all: linux darwin windows
11+
12+
linux:
13+
CGO_ENABLED=0 GOOS=linux go build $(BUILD_FLAGS) -o $(BUILD_DIR)/coap-cli-linux main.go
14+
15+
darwin:
16+
CGO_ENABLED=0 GOOS=darwin go build $(BUILD_FLAGS) -o $(BUILD_DIR)/coap-cli-darwin main.go
17+
18+
windows:
19+
CGO_ENABLED=0 GOOS=windows go build $(BUILD_FLAGS) -o $(BUILD_DIR)/coap-cli-windows main.go
20+
21+
install: install-linux
22+
23+
install-linux:
24+
@cp $(BUILD_DIR)/coap-cli-linux $(INSTALL_DIR)/coap-cli || { echo "Installation failed"; exit 1; }
25+
26+
clean:
27+
rm -rf $(BUILD_DIR)/*

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ Available Commands:
1818

1919
Flags:
2020
-a, --auth string Auth
21+
-A, --ca-file string Client CA file
22+
-C, --cert-file string Client certificate file
2123
-c, --content-format int Content format (default 50)
2224
-h, --help help for coap-cli
2325
-H, --host string Host (default "localhost")
2426
-k, --keep-alive uint Send a ping after interval seconds of inactivity. If not specified (or 0), keep-alive is disabled (default).
27+
-K, --key-file string Client key file
2528
-m, --max-retries uint32 Max retries for keep alive (default 10)
2629
-O, --options num,text 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.
2730
-p, --port string Port (default "5683")
@@ -44,14 +47,23 @@ coap-cli get m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26
4447
coap-cli get m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic --options 6,0x00 --options 15,auth=1e1017e6-dee7-45b4-8a13-00e6afeb66eb
4548
```
4649

50+
```bash
51+
coap-cli get m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic --options 6,0x00 --options 15,auth=1e1017e6-dee7-45b4-8a13-00e6afeb66eb --ca-file ssl/certs/ca.crt --cert-file ssl/certs/client.crt --key-file ssl/certs/client.key
52+
```
53+
4754
```bash
4855
coap-cli post m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic --auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world"
4956
```
5057

58+
5159
```bash
5260
coap-cli post m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic --auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world" -H 0.0.0.0 -p 1234
5361
```
5462

5563
```bash
5664
coap-cli post m/aa844fac-2f74-4ec3-8318-849b95d03bcc/c/0bb5ba61-a66e-4972-bab6-26f19962678f/subtopic --options 15,auth=1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world" -H 0.0.0.0 -p 5683
5765
```
66+
67+
```bash
68+
coap-cli post channels/0bb5ba61-a66e-4972-bab6-26f19962678f/messages/subtopic --auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world" --ca-file ssl/certs/ca.crt --cert-file ssl/certs/client.crt --key-file ssl/certs/client.key
69+
```

coap/client.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"log"
1212
"time"
1313

14+
piondtls "github.com/pion/dtls/v3"
15+
"github.com/plgd-dev/go-coap/v3/dtls"
1416
"github.com/plgd-dev/go-coap/v3/message"
1517
"github.com/plgd-dev/go-coap/v3/message/codes"
1618
"github.com/plgd-dev/go-coap/v3/message/pool"
@@ -39,12 +41,18 @@ type Client struct {
3941
}
4042

4143
// NewClient returns new CoAP client connecting it to the server.
42-
func NewClient(addr string, keepAlive uint64, maxRetries uint32) (Client, error) {
44+
func NewClient(addr string, keepAlive uint64, maxRetries uint32, dtlsConfig *piondtls.Config) (Client, error) {
4345
var dialOptions []udp.Option
4446
if keepAlive > 0 {
4547
dialOptions = append(dialOptions, options.WithKeepAlive(maxRetries, time.Duration(keepAlive)*time.Second, onInactive))
4648
}
47-
c, err := udp.Dial(addr, dialOptions...)
49+
var c *client.Conn
50+
var err error
51+
if dtlsConfig != nil {
52+
c, err = dtls.Dial(addr, dtlsConfig, dialOptions...)
53+
} else {
54+
c, err = udp.Dial(addr, dialOptions...)
55+
}
4856
if err != nil {
4957
return Client{}, errors.Join(errDialFailed, err)
5058
}

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
module github.com/absmach/coap-cli
22

3-
go 1.24
3+
go 1.25.0
44

55
require (
66
github.com/fatih/color v1.18.0
7+
github.com/pion/dtls/v3 v3.0.7
78
github.com/plgd-dev/go-coap/v3 v3.4.0
8-
github.com/spf13/cobra v1.9.1
9+
github.com/spf13/cobra v1.10.1
910
)
1011

1112
require (
1213
github.com/dsnet/golib/memfile v1.0.0 // indirect
1314
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1415
github.com/mattn/go-colorable v0.1.14 // indirect
1516
github.com/mattn/go-isatty v0.0.20 // indirect
16-
github.com/pion/dtls/v3 v3.0.6 // indirect
1717
github.com/pion/logging v0.2.4 // indirect
1818
github.com/pion/transport/v3 v3.0.7 // indirect
19-
github.com/spf13/pflag v1.0.7 // indirect
19+
github.com/spf13/pflag v1.0.10 // indirect
2020
go.uber.org/atomic v1.11.0 // indirect
21-
golang.org/x/crypto v0.40.0 // indirect
22-
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792 // indirect
23-
golang.org/x/net v0.42.0 // indirect
21+
golang.org/x/crypto v0.41.0 // indirect
22+
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b // indirect
23+
golang.org/x/net v0.43.0 // indirect
2424
golang.org/x/sync v0.16.0 // indirect
25-
golang.org/x/sys v0.34.0 // indirect
25+
golang.org/x/sys v0.35.0 // indirect
2626
)

go.sum

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
1111
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
1212
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
1313
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
14-
github.com/pion/dtls/v3 v3.0.6 h1:7Hkd8WhAJNbRgq9RgdNh1aaWlZlGpYTzdqjy9x9sK2E=
15-
github.com/pion/dtls/v3 v3.0.6/go.mod h1:iJxNQ3Uhn1NZWOMWlLxEEHAN5yX7GyPvvKw04v9bzYU=
14+
github.com/pion/dtls/v3 v3.0.7 h1:bItXtTYYhZwkPFk4t1n3Kkf5TDrfj6+4wG+CZR8uI9Q=
15+
github.com/pion/dtls/v3 v3.0.7/go.mod h1:uDlH5VPrgOQIw59irKYkMudSFprY9IEFCqz/eTz16f8=
1616
github.com/pion/logging v0.2.4 h1:tTew+7cmQ+Mc1pTBLKH2puKsOvhm32dROumOZ655zB8=
1717
github.com/pion/logging v0.2.4/go.mod h1:DffhXTKYdNZU+KtJ5pyQDjvOAh/GsNSyv1lbkFbe3so=
1818
github.com/pion/transport/v3 v3.0.7 h1:iRbMH05BzSNwhILHoBoAPxoB9xQgOaJk+591KC9P1o0=
@@ -22,26 +22,26 @@ github.com/plgd-dev/go-coap/v3 v3.4.0/go.mod h1:azpceqoHFeGzzNVm3RX4ox6xKHLOJ+pD
2222
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2323
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2424
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
25-
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
26-
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
27-
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
28-
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
29-
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
25+
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
26+
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
27+
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
28+
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
29+
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
3030
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
3131
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3232
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
3333
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
34-
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
35-
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
36-
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792 h1:R9PFI6EUdfVKgwKjZef7QIwGcBKu86OEFpJ9nUEP2l4=
37-
golang.org/x/exp v0.0.0-20250718183923-645b1fa84792/go.mod h1:A+z0yzpGtvnG90cToK5n2tu8UJVP2XUATh+r+sfOOOc=
38-
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
39-
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
34+
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
35+
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
36+
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b h1:DXr+pvt3nC887026GRP39Ej11UATqWDmWuS99x26cD0=
37+
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b/go.mod h1:4QTo5u+SEIbbKW1RacMZq1YEfOBqeXa19JeshGi+zc4=
38+
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
39+
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
4040
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
4141
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
4242
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
43-
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
44-
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
43+
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
44+
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
4545
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4646
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
4747
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)