Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed ports as per issue #3 #4

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Tests

on: [push]
on:
- push
- pull_request

env:
IMAGE_TAGGED: coap:${{ github.sha }}
Expand Down Expand Up @@ -36,7 +38,7 @@ jobs:
STORAGE_CONNECTION_STRING: ${{ secrets.STORAGE_CONNECTION_STRING }}
STORAGE_CONTAINER_NAME: ${{ vars.STORAGE_CONTAINER_NAME }}
run: |
docker run --cidfile container.cid --network=host -p 5688:5688/udp -p 5689:5689/udp -e STORAGE_CONNECTION_STRING -e STORAGE_CONTAINER_NAME -d ${{ env.IMAGE_TAGGED }}
docker run --cidfile container.cid --network=host -p 5683:5683/udp -p 5684:5684/udp -p 5688:5688/udp -p 5689:5689/udp -e STORAGE_CONNECTION_STRING -e STORAGE_CONTAINER_NAME -d ${{ env.IMAGE_TAGGED }}
CID=`cat container.cid`
echo "CID=${CID}" >> $GITHUB_ENV

Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ WORKDIR /home/coap
EXPOSE 5688/udp
EXPOSE 5689/udp

EXPOSE 5683/udp
EXPOSE 5684/udp

CMD [ "/home/coap/start.sh" ]
43 changes: 31 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"flag"
"fmt"
"github.com/google/uuid"
piondtls "github.com/pion/dtls/v2"
"github.com/plgd-dev/go-coap/v3/dtls"
"log"
"net"
"time"
Expand All @@ -12,11 +15,6 @@ import (
"github.com/plgd-dev/go-coap/v3/udp"
"github.com/plgd-dev/go-coap/v3/udp/client"

piondtls "github.com/pion/dtls/v2"
"github.com/plgd-dev/go-coap/v3/dtls"

"github.com/google/uuid"

"coap-client/internal"
)

Expand All @@ -26,6 +24,12 @@ func check(e error) {
}
}

type flags struct {
address string
password string
udp6 bool
}

func main() {
address := flag.String("address", "localhost",
"The UDP Server listen address, e.g. `localhost`.")
Expand All @@ -34,25 +38,40 @@ func main() {
udp6 := flag.Bool("udp6", false, "Whether to use IPv6")
flag.Parse()

var flagValues = flags{
address: *address,
password: *password,
udp6: *udp6,
}

// Testing with Old Config
testPorts(flagValues, 5688, 5689)

// Testing with New Config
testPorts(flagValues, 5683, 5684)

}

func testPorts(flagValues flags, udpPort int, dTLSPort int) {

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
defer cancel()

udpPort := 5688
dTLSPort := 5689
var udpAddr string
var dtlsAddr string
_ = dtlsAddr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it crept in while testing locally i guess. Removed it. :-)

config := client.Config{}
var opt udp.Option
if *udp6 {
ip6, err := net.DefaultResolver.LookupIP(context.Background(), "ip6", *address)
if flagValues.udp6 {
ip6, err := net.DefaultResolver.LookupIP(context.Background(), "ip6", flagValues.address)
if err != nil {
log.Fatal("Failed to resolve IPv6 address: ", err)
}
udpAddr = fmt.Sprintf("[%s]:%d", ip6[0].String(), udpPort)
dtlsAddr = fmt.Sprintf("[%s]:%d", ip6[0].String(), dTLSPort)
opt = options.WithNetwork("udp6")
} else {
ip4, err := net.DefaultResolver.LookupIP(context.Background(), "ip4", *address)
ip4, err := net.DefaultResolver.LookupIP(context.Background(), "ip4", flagValues.address)
if err != nil {
log.Fatal("Failed to resolve IPv4 address: ", err)
}
Expand All @@ -78,11 +97,11 @@ func main() {
}

log.Printf("dTLS Server listening on: %s\n", dtlsAddr)
log.Printf("dTLS PSK: %s\n", *password)
log.Printf("dTLS PSK: %s\n", flagValues.password)
codTLS, err := dtls.Dial(dtlsAddr, &piondtls.Config{
PSK: func(hint []byte) ([]byte, error) {
log.Printf("Server's hint: %s \n", hint)
return []byte(*password), nil
return []byte(flagValues.password), nil
},
PSKIdentityHint: []byte("Pion DTLS Client"),
CipherSuites: []piondtls.CipherSuiteID{piondtls.TLS_PSK_WITH_AES_128_CCM_8},
Expand Down
2 changes: 1 addition & 1 deletion client/internal/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"io"
"log"

"github.com/plgd-dev/go-coap/v3/udp/client"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/udp/client"
)

// Test Hello resource
Expand Down
41 changes: 37 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ func check(e error) {
}
}

type serverConfig struct {
oldUdpPort int
coderbyheart marked this conversation as resolved.
Show resolved Hide resolved
newUdpPort int
oldDtlsPort int
newDtlsPort int
}

var config = serverConfig{
oldUdpPort: 5688,
newUdpPort: 5683,
oldDtlsPort: 5689,
newDtlsPort: 5684,
}

func main() {
address := flag.String("address", "localhost",
"The UDP Server listen address, e.g. `localhost` or `0.0.0.0`.")
Expand All @@ -46,10 +60,29 @@ func main() {

r := internal.NewServer(storageClient, containerName)

udpPort := 5688
dTLSPort := 5689
udpAddr := fmt.Sprintf("%s:%d", *address, udpPort)
dtlsAddr := fmt.Sprintf("%s:%d", *address, dTLSPort)
go func() {
coderbyheart marked this conversation as resolved.
Show resolved Hide resolved
udpAddr := fmt.Sprintf("%s:%d", *address, config.oldUdpPort)
dtlsAddr := fmt.Sprintf("%s:%d", *address, config.oldDtlsPort)

if *dtls {
log.Printf("dTLS %s Server listening on: %s\n", *network, dtlsAddr)
log.Printf("dTLS PSK: %s\n", *password)
log.Fatal(coap.ListenAndServeDTLS(*network, dtlsAddr, &piondtls.Config{
PSK: func(hint []byte) ([]byte, error) {
log.Printf("Client's hint: %s \n", hint)
return []byte(*password), nil
},
PSKIdentityHint: []byte("Pion DTLS Client"),
CipherSuites: []piondtls.CipherSuiteID{piondtls.TLS_PSK_WITH_AES_128_CCM_8},
}, r))
} else {
log.Printf("%s Old Server listening on: %s\n", *network, udpAddr)
log.Fatal(coap.ListenAndServe(*network, udpAddr, r))
}
}()

udpAddr := fmt.Sprintf("%s:%d", *address, config.newUdpPort)
dtlsAddr := fmt.Sprintf("%s:%d", *address, config.newDtlsPort)

if *dtls {
log.Printf("dTLS %s Server listening on: %s\n", *network, dtlsAddr)
Expand Down
Loading