From 051d7195e342dd5622868bdcfc8fba0a0a19ceb3 Mon Sep 17 00:00:00 2001 From: HEMANT KUMAR Date: Thu, 16 May 2024 07:47:50 +0000 Subject: [PATCH 1/8] changed ports as per issue #3 --- .github/workflows/test.yaml | 2 +- client/client.go | 4 ++-- server/server.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index cef54fd..d98e131 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -36,7 +36,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 -e STORAGE_CONNECTION_STRING -e STORAGE_CONTAINER_NAME -d ${{ env.IMAGE_TAGGED }} CID=`cat container.cid` echo "CID=${CID}" >> $GITHUB_ENV diff --git a/client/client.go b/client/client.go index a645da9..6647dcc 100644 --- a/client/client.go +++ b/client/client.go @@ -37,8 +37,8 @@ func main() { ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10)*time.Second) defer cancel() - udpPort := 5688 - dTLSPort := 5689 + udpPort := 5683 + dTLSPort := 5684 var udpAddr string var dtlsAddr string config := client.Config{} diff --git a/server/server.go b/server/server.go index a67e7ce..73b69fa 100644 --- a/server/server.go +++ b/server/server.go @@ -46,8 +46,8 @@ func main() { r := internal.NewServer(storageClient, containerName) - udpPort := 5688 - dTLSPort := 5689 + udpPort := 5683 + dTLSPort := 5684 udpAddr := fmt.Sprintf("%s:%d", *address, udpPort) dtlsAddr := fmt.Sprintf("%s:%d", *address, dTLSPort) From 102830e775c5d43523ea35f36f7082c22f2e3baf Mon Sep 17 00:00:00 2001 From: Hemant Date: Thu, 16 May 2024 21:16:56 +0530 Subject: [PATCH 2/8] Enabled service on previous ports alongwith new . --- .github/workflows/test.yaml | 2 +- Dockerfile | 3 +++ client/internal/requests.go | 2 +- server/server.go | 41 +++++++++++++++++++++++++++++++++---- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index d98e131..576aadf 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -36,7 +36,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 5683:5683/udp -p 5684:5684/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 diff --git a/Dockerfile b/Dockerfile index feeffd3..06639ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,4 +22,7 @@ WORKDIR /home/coap EXPOSE 5688/udp EXPOSE 5689/udp +EXPOSE 5683/udp +EXPOSE 5684/udp + CMD [ "/home/coap/start.sh" ] diff --git a/client/internal/requests.go b/client/internal/requests.go index f0594a8..733a44e 100644 --- a/client/internal/requests.go +++ b/client/internal/requests.go @@ -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 diff --git a/server/server.go b/server/server.go index 73b69fa..d2009ec 100644 --- a/server/server.go +++ b/server/server.go @@ -20,6 +20,20 @@ func check(e error) { } } +type serverConfig struct { + oldUdpPort int + newUdpPort int + dtlsOldPort int + dtlsNewPort int +} + +var config = serverConfig{ + oldUdpPort: 5688, + newUdpPort: 5683, + dtlsOldPort: 5689, + dtlsNewPort: 5684, +} + func main() { address := flag.String("address", "localhost", "The UDP Server listen address, e.g. `localhost` or `0.0.0.0`.") @@ -46,10 +60,29 @@ func main() { r := internal.NewServer(storageClient, containerName) - udpPort := 5683 - dTLSPort := 5684 - udpAddr := fmt.Sprintf("%s:%d", *address, udpPort) - dtlsAddr := fmt.Sprintf("%s:%d", *address, dTLSPort) + go func() { + udpAddrOld := fmt.Sprintf("%s:%d", *address, config.oldUdpPort) + dtlsAddrOld := fmt.Sprintf("%s:%d", *address, config.dtlsOldPort) + + if *dtls { + log.Printf("dTLS %s Server listening on: %s\n", *network, dtlsAddrOld) + log.Printf("dTLS PSK: %s\n", *password) + log.Fatal(coap.ListenAndServeDTLS(*network, dtlsAddrOld, &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, udpAddrOld) + log.Fatal(coap.ListenAndServe(*network, udpAddrOld, r)) + } + }() + + udpAddr := fmt.Sprintf("%s:%d", *address, config.newUdpPort) + dtlsAddr := fmt.Sprintf("%s:%d", *address, config.dtlsNewPort) if *dtls { log.Printf("dTLS %s Server listening on: %s\n", *network, dtlsAddr) From 52d2ecf6c34070da6057514b0cafffe1a1f08544 Mon Sep 17 00:00:00 2001 From: Hemant Date: Thu, 16 May 2024 22:13:24 +0530 Subject: [PATCH 3/8] added tests in client for both old and new ports. --- client/client.go | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/client/client.go b/client/client.go index 6647dcc..24a9230 100644 --- a/client/client.go +++ b/client/client.go @@ -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" @@ -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" ) @@ -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`.") @@ -34,17 +38,32 @@ 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 := 5683 - dTLSPort := 5684 var udpAddr string var dtlsAddr string + _ = dtlsAddr 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) } @@ -52,7 +71,7 @@ func main() { 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) } @@ -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}, From e93f66fc550892e3f6e109ea6d5549634d25d983 Mon Sep 17 00:00:00 2001 From: HEMANT KUMAR Date: Fri, 17 May 2024 05:25:17 +0000 Subject: [PATCH 4/8] renamed config struct fields. --- server/server.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/server/server.go b/server/server.go index d2009ec..cab0e6f 100644 --- a/server/server.go +++ b/server/server.go @@ -23,15 +23,15 @@ func check(e error) { type serverConfig struct { oldUdpPort int newUdpPort int - dtlsOldPort int - dtlsNewPort int + oldDtlsPort int + newDtlsPort int } var config = serverConfig{ oldUdpPort: 5688, newUdpPort: 5683, - dtlsOldPort: 5689, - dtlsNewPort: 5684, + oldDtlsPort: 5689, + newDtlsPort: 5684, } func main() { @@ -61,13 +61,13 @@ func main() { r := internal.NewServer(storageClient, containerName) go func() { - udpAddrOld := fmt.Sprintf("%s:%d", *address, config.oldUdpPort) - dtlsAddrOld := fmt.Sprintf("%s:%d", *address, config.dtlsOldPort) + 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, dtlsAddrOld) + log.Printf("dTLS %s Server listening on: %s\n", *network, dtlsAddr) log.Printf("dTLS PSK: %s\n", *password) - log.Fatal(coap.ListenAndServeDTLS(*network, dtlsAddrOld, &piondtls.Config{ + 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 @@ -76,13 +76,13 @@ func main() { CipherSuites: []piondtls.CipherSuiteID{piondtls.TLS_PSK_WITH_AES_128_CCM_8}, }, r)) } else { - log.Printf("%s Old Server listening on: %s\n", *network, udpAddrOld) - log.Fatal(coap.ListenAndServe(*network, udpAddrOld, r)) + 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.dtlsNewPort) + dtlsAddr := fmt.Sprintf("%s:%d", *address, config.newDtlsPort) if *dtls { log.Printf("dTLS %s Server listening on: %s\n", *network, dtlsAddr) From f9db535919580e52c1119f68a99cf951a82bbd61 Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Fri, 17 May 2024 07:58:57 +0200 Subject: [PATCH 5/8] ci: run tests for PRs --- .github/workflows/test.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 576aadf..f6b7466 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,6 +1,8 @@ name: Tests -on: [push] +on: + - push + - pull_request env: IMAGE_TAGGED: coap:${{ github.sha }} From 1e05d2e8e159d3444c8443e9970bd41c926a594c Mon Sep 17 00:00:00 2001 From: HEMANT KUMAR Date: Fri, 17 May 2024 06:41:27 +0000 Subject: [PATCH 6/8] refactored server code to remove code duplicacy. --- server/server.go | 77 +++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/server/server.go b/server/server.go index cab0e6f..299a726 100644 --- a/server/server.go +++ b/server/server.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/plgd-dev/go-coap/v3/mux" "log" "os" @@ -20,18 +21,18 @@ func check(e error) { } } -type serverConfig struct { +type serverPorts struct { oldUdpPort int newUdpPort int oldDtlsPort int newDtlsPort int } -var config = serverConfig{ - oldUdpPort: 5688, - newUdpPort: 5683, - oldDtlsPort: 5689, - newDtlsPort: 5684, +type flags struct { + address string + password string + dtls bool + network string } func main() { @@ -44,6 +45,20 @@ func main() { "The network to use, `udp4` or `udp6`.") flag.Parse() + var flagValues = flags{ + address: *address, + password: *password, + dtls: *dtls, + network: *network, + } + + var ports = 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") @@ -60,43 +75,31 @@ func main() { r := internal.NewServer(storageClient, containerName) - go func() { - 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) - log.Printf("dTLS PSK: %s\n", *password) - log.Fatal(coap.ListenAndServeDTLS(*network, dtlsAddr, &piondtls.Config{ + // old ports + go launchServer(flagValues, ports.oldUdpPort, ports.oldDtlsPort, r) + + // new ports + launchServer(flagValues, ports.newUdpPort, ports.newDtlsPort, r) + +} + +func launchServer(flagValues flags, 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(*password), nil + 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", *network, udpAddr) - log.Fatal(coap.ListenAndServe(*network, udpAddr, r)) + log.Printf("%s Server listening on: %s\n", flagValues.network, udpAddr) + log.Fatal(coap.ListenAndServe(flagValues.network, udpAddr, r)) } } From 91ddaaeba2010b81baf0b810de09efeee2493c8b Mon Sep 17 00:00:00 2001 From: HEMANT KUMAR Date: Fri, 17 May 2024 09:05:57 +0000 Subject: [PATCH 7/8] removed erroneous unwanted dtlsAddr assignment --- client/client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client/client.go b/client/client.go index 24a9230..fb69c59 100644 --- a/client/client.go +++ b/client/client.go @@ -59,7 +59,6 @@ func testPorts(flagValues flags, udpPort int, dTLSPort int) { var udpAddr string var dtlsAddr string - _ = dtlsAddr config := client.Config{} var opt udp.Option if flagValues.udp6 { From 7938d5c15d0677b445c8282a8681c343a7505864 Mon Sep 17 00:00:00 2001 From: HEMANT KUMAR Date: Fri, 17 May 2024 11:06:02 +0000 Subject: [PATCH 8/8] refactored server code by moving away from main file --- server/launch/server.go | 44 ++++++++++++++++++++++++++++ server/server.go | 65 +++++++++-------------------------------- 2 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 server/launch/server.go diff --git a/server/launch/server.go b/server/launch/server.go new file mode 100644 index 0000000..848b35b --- /dev/null +++ b/server/launch/server.go @@ -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)) + } +} diff --git a/server/server.go b/server/server.go index 299a726..89b795e 100644 --- a/server/server.go +++ b/server/server.go @@ -1,17 +1,13 @@ package main import ( + "coap-server/launch" "flag" - "fmt" - "github.com/plgd-dev/go-coap/v3/mux" "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" ) @@ -21,20 +17,6 @@ func check(e error) { } } -type serverPorts struct { - oldUdpPort int - newUdpPort int - oldDtlsPort int - newDtlsPort int -} - -type flags struct { - address string - password string - dtls bool - network string -} - func main() { address := flag.String("address", "localhost", "The UDP Server listen address, e.g. `localhost` or `0.0.0.0`.") @@ -45,18 +27,18 @@ func main() { "The network to use, `udp4` or `udp6`.") flag.Parse() - var flagValues = flags{ - address: *address, - password: *password, - dtls: *dtls, - network: *network, + var flagValues = launch.ServerFlags{ + Address: *address, + Password: *password, + Dtls: *dtls, + Network: *network, } - var ports = serverPorts{ - oldUdpPort: 5688, - newUdpPort: 5683, - oldDtlsPort: 5689, - newDtlsPort: 5684, + var ports = launch.ServerPorts{ + OldUdpPort: 5688, + NewUdpPort: 5683, + OldDtlsPort: 5689, + NewDtlsPort: 5684, } storageConnectionString, ok := os.LookupEnv("STORAGE_CONNECTION_STRING") @@ -76,30 +58,9 @@ func main() { r := internal.NewServer(storageClient, containerName) // old ports - go launchServer(flagValues, ports.oldUdpPort, ports.oldDtlsPort, r) + go launch.Server(flagValues, ports.OldUdpPort, ports.OldDtlsPort, r) // new ports - launchServer(flagValues, ports.newUdpPort, ports.newDtlsPort, r) + launch.Server(flagValues, ports.NewUdpPort, ports.NewDtlsPort, r) } - -func launchServer(flagValues flags, 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)) - } -}