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 all 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" ]
42 changes: 30 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,39 @@ 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
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 +96,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
44 changes: 44 additions & 0 deletions server/launch/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package launch

import (
"fmt"
piondtls "github.com/pion/dtls/v2"
"github.com/plgd-dev/go-coap/v3"
"github.com/plgd-dev/go-coap/v3/mux"
"log"
)

type ServerPorts struct {
OldUdpPort int
NewUdpPort int
OldDtlsPort int
NewDtlsPort int
}

type ServerFlags struct {
Address string
Password string
Dtls bool
Network string
}

func Server(flagValues ServerFlags, udpPort int, dtlsPort int, r *mux.Router) {
udpAddr := fmt.Sprintf("%s:%d", flagValues.Address, udpPort)
dtlsAddr := fmt.Sprintf("%s:%d", flagValues.Address, dtlsPort)

if flagValues.Dtls {
log.Printf("dTLS %s Server listening on: %s\n", flagValues.Network, dtlsAddr)
log.Printf("dTLS PSK: %s\n", flagValues.Password)
log.Fatal(coap.ListenAndServeDTLS(flagValues.Network, dtlsAddr, &piondtls.Config{
PSK: func(hint []byte) ([]byte, error) {
log.Printf("Client's hint: %s \n", hint)
return []byte(flagValues.Password), nil
},
PSKIdentityHint: []byte("Pion DTLS Client"),
CipherSuites: []piondtls.CipherSuiteID{piondtls.TLS_PSK_WITH_AES_128_CCM_8},
}, r))
} else {
log.Printf("%s Server listening on: %s\n", flagValues.Network, udpAddr)
log.Fatal(coap.ListenAndServe(flagValues.Network, udpAddr, r))
}
}
43 changes: 20 additions & 23 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package main

import (
"coap-server/launch"
"flag"
"fmt"
"log"
"os"

"coap-server/internal"

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

"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)

Expand All @@ -30,6 +27,20 @@ func main() {
"The network to use, `udp4` or `udp6`.")
flag.Parse()

var flagValues = launch.ServerFlags{
Address: *address,
Password: *password,
Dtls: *dtls,
Network: *network,
}

var ports = launch.ServerPorts{
OldUdpPort: 5688,
NewUdpPort: 5683,
OldDtlsPort: 5689,
NewDtlsPort: 5684,
}

storageConnectionString, ok := os.LookupEnv("STORAGE_CONNECTION_STRING")
if !ok {
log.Fatal("the environment variable 'STORAGE_CONNECTION_STRING' could not be found")
Expand All @@ -46,24 +57,10 @@ 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)
// old ports
go launch.Server(flagValues, ports.OldUdpPort, ports.OldDtlsPort, r)

// new ports
launch.Server(flagValues, ports.NewUdpPort, ports.NewDtlsPort, r)

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 Server listening on: %s\n", *network, udpAddr)
log.Fatal(coap.ListenAndServe(*network, udpAddr, r))
}
}
Loading