Skip to content
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
22 changes: 21 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,24 @@ race:
lint:
golangci-lint run

.PHONY: build install clean test integrationtest bench race lint
DOCKER_COMPOSE := docker compose -f docker/compose.yml

docker-build:
$(DOCKER_COMPOSE) build

docker-up:
$(DOCKER_COMPOSE) up

docker-up-d:
$(DOCKER_COMPOSE) up -d

docker-stop:
$(DOCKER_COMPOSE) stop

docker-down:
$(DOCKER_COMPOSE) down

docker-logs:
$(DOCKER_COMPOSE) logs -f

.PHONY: build install clean test integrationtest bench race lint docker-build docker-up docker-up-d docker-stop docker-down docker-logs
18 changes: 16 additions & 2 deletions cmd/trecsd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"log/slog"
"net"
"os"
"os/signal"
"syscall"
Expand All @@ -28,10 +29,10 @@ var (
flagLogFormat string
flagNoPRACK bool
flagTrunks string
flagExternalIP string
flagNATAddress string
)

var serverIP = "127.0.0.1"

func init() {
flag.StringVar(&flagAddr, "addr", ":5060", "SIP listen address")
flag.IntVar(&flagRTPMin, "rtp-min", 0, "RTP port range start (0 = OS-assigned)")
Expand All @@ -43,6 +44,8 @@ func init() {
flag.StringVar(&flagLogFormat, "log-format", "text", "Log format (text, json, or compact)")
flag.BoolVar(&flagNoPRACK, "no-prack", false, "Disable PRACK (RFC 3262) support for reliable provisional responses")
flag.StringVar(&flagTrunks, "trunks", "", "Path to trunk configuration JSON file")
flag.StringVar(&flagExternalIP, "external-ip", "", "External IP for SDP c= lines and Contact headers")
flag.StringVar(&flagNATAddress, "nat-address", "", "NAT address to replace loopback in client SDP (e.g., host.docker.internal)")
flag.Parse()
}

Expand Down Expand Up @@ -78,6 +81,16 @@ func main() {
}
slog.SetDefault(slog.New(slogHandler))

serverIP := flagExternalIP
if serverIP == "" {
host, _, err := net.SplitHostPort(flagAddr)
if err != nil || host == "" || host == "0.0.0.0" || host == "::" {
serverIP = "127.0.0.1"
} else {
serverIP = host
}
}

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

Expand Down Expand Up @@ -147,6 +160,7 @@ func main() {
RTPPortMax: flagRTPMax,
PRACKEnabled: !flagNoPRACK,
TrunkMgr: trunkMgr,
NATAddress: flagNATAddress,
})

if flagAuthMaxFailed < 1 || flagAuthMaxFailed > 10 {
Expand Down
12 changes: 12 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.26-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /trecsd ./cmd/trecsd/

FROM alpine:3.20
RUN apk add --no-cache ca-certificates
COPY --from=builder /trecsd /usr/local/bin/trecsd

ENTRYPOINT ["trecsd"]
45 changes: 45 additions & 0 deletions docker/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
services:
front:
build:
context: ..
dockerfile: docker/Dockerfile
command:
- -addr=:5060
- -external-ip=127.0.0.1
- -nat-address=host.docker.internal
- -rtp-min=40000
- -rtp-max=40010
- -dialplan=/conf/dialplan.json
- -trunks=/conf/trunks.json
- -log-level=debug
ports:
- "5060:5060/udp"
- "5060:5060/tcp"
- "40000-40010:40000-40010/udp"
volumes:
- ./configs/front/dialplan.json:/conf/dialplan.json:ro
- ./configs/front/trunks.json:/conf/trunks.json:ro
- ../audio-files:/srv/audio:ro
depends_on:
- back
networks:
- trec-net

back:
build:
context: ..
dockerfile: docker/Dockerfile
command:
- -addr=:5060
- -external-ip=back
- -dialplan=/conf/dialplan.json
- -log-level=debug
volumes:
- ./configs/back/dialplan.json:/conf/dialplan.json:ro
- ../audio-files:/srv/audio:ro
networks:
- trec-net

networks:
trec-net:
driver: bridge
5 changes: 5 additions & 0 deletions docker/configs/back/dialplan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extensions": {
"*": { "action": "play", "file": "/srv/audio/the_mentor_8k_lpcm16.wav" }
}
}
6 changes: 6 additions & 0 deletions docker/configs/front/dialplan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extensions": {
"local": { "action": "play", "file": "/srv/audio/EDIS-SCD-02_8k_lpcm16.wav" },
"mentor": { "action": "play", "file": "/srv/audio/the_mentor_8k_lpcm16.wav" }
}
}
20 changes: 20 additions & 0 deletions docker/configs/front/trunks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"trunks": [
{
"name": "back",
"type": "static",
"host": "back",
"port": 5060,
"transport": "udp",
"local_ip": "front",
"max_channels": 50
}
],
"outbound_routes": [
{
"name": "catch-all",
"pattern": "^\\d+$",
"trunk": "back"
}
]
}
1 change: 1 addition & 0 deletions docs/examples/trunks.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"port": 5061,
"transport": "tcp",
"trusted_ips": ["10.0.1.0/24"],
"local_ip": "10.0.1.10",
"max_channels": 20,
"caller_id": "pbx-main",
"strip_headers": ["X-Extension"]
Expand Down
22 changes: 22 additions & 0 deletions integrationtest/dialplan/dialplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ func TestIntegration_DialplanEcho(t *testing.T) {

runEchoTest(t, ts, "tcp", false)
})

t.Run("UDP_EarlyOffer_NATAddress", func(t *testing.T) {
if _, err := net.LookupHost("host.docker.internal"); err != nil {
t.Skip("host.docker.internal not resolvable (not in Docker)")
}
ts := integrationtest.StartTestServerWithDialplan(t, "127.0.0.1", dp,
integrationtest.WithNATAddress("host.docker.internal"))
defer ts.Stop()

runEchoTest(t, ts, "udp", true)
})

t.Run("TCP_EarlyOffer_NATAddress", func(t *testing.T) {
if _, err := net.LookupHost("host.docker.internal"); err != nil {
t.Skip("host.docker.internal not resolvable (not in Docker)")
}
ts := integrationtest.StartTestServerWithDialplan(t, "127.0.0.1", dp,
integrationtest.WithNATAddress("host.docker.internal"))
defer ts.Stop()

runEchoTest(t, ts, "tcp", true)
})
}

func TestIntegration_DialplanPlayback(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions integrationtest/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func WithTrunkManager(tm *trunk.TrunkManager) ServerOption {
}
}

// WithNATAddress sets the NAT address for replacing loopback client SDP addresses.
func WithNATAddress(natAddr string) ServerOption {
return func(cfg *b2bua.Config) {
cfg.NATAddress = natAddr
}
}

// StartTestServer creates and starts a trecs server with a registrar, dialplan,
// and B2BUA handler for integration testing. Logging is routed to t.Log(). The server
// binds to host:0 (random OS-assigned port). The caller must call Stop() when done.
Expand Down
Loading
Loading