Skip to content

Commit

Permalink
fix(ipv6): format addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 3, 2024
1 parent c3ac844 commit 54d92ac
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"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 @@ -26,6 +26,7 @@ func main() {
password := flag.String("password", "connect:anything",
"The password to use for the PSK in dTLS.")
dtls := flag.Bool("dTLS", false, "Start a dTLS server")
udp6 := flag.Bool("udp6", false, "Whether to use IPv6")
flag.Parse()

storageConnectionString, ok := os.LookupEnv("STORAGE_CONNECTION_STRING")
Expand All @@ -44,11 +45,25 @@ func main() {

r := internal.NewServer(storageClient, containerName)

udpPort := 5688
dTLSPort := 5689
var udpAddr string
var dtlsAddr string
var network string
if *udp6 {
udpAddr = fmt.Sprintf("[%s]:%d", *address, udpPort)
dtlsAddr = fmt.Sprintf("[%s]:%d", *address, dTLSPort)
network = "udp6"
} else {
udpAddr = fmt.Sprintf("%s:%d", *address, udpPort)
dtlsAddr = fmt.Sprintf("%s:%d", *address, dTLSPort)
network = "udp"
}

if *dtls {
dtlsAddr := fmt.Sprintf("%s:%d", *address, 5689)
log.Printf("dTLS UDP Server listening on: %s\n", dtlsAddr)
log.Printf("dTLS PSK: %s\n", *password)
log.Fatal(coap.ListenAndServeDTLS("udp", dtlsAddr, &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
Expand All @@ -57,8 +72,7 @@ func main() {
CipherSuites: []piondtls.CipherSuiteID{piondtls.TLS_PSK_WITH_AES_128_CCM_8},
}, r))
} else {
udpAddr := fmt.Sprintf("%s:%d", *address, 5688)
log.Printf("UDP Server listening on: %s\n", udpAddr)
log.Fatal(coap.ListenAndServe("udp", udpAddr, r))
log.Fatal(coap.ListenAndServe(network, udpAddr, r))
}
}

0 comments on commit 54d92ac

Please sign in to comment.