Compare commits
74 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
f097bb3241
|
|||
|
22fc5093db
|
|||
|
fc95e54b2e
|
|||
|
636d400f1e
|
|||
| fd5eb65bc0 | |||
| 4e13fe523b | |||
| dd2cc3e3d9 | |||
| 353e9c6d9b | |||
| 088ba3337d | |||
| 4cd2338095 | |||
| c6cc1d8ca8 | |||
| 0afb0e9ade | |||
| feeaa72102 | |||
| bb964445f3 | |||
| 5369037a74 | |||
| bb98248830 | |||
| 575657bbc5 | |||
| 8da4a759f5 | |||
| dff1489ee5 | |||
| 30c97bc9dd | |||
| 005e2566aa | |||
| cc10830df3 | |||
| b548e5711e | |||
| cc89bfef6e | |||
| 45a3ac1e87 | |||
| e39936ac30 | |||
| b601ae1c51 | |||
| 7d7a022736 | |||
| 0ac2a8d200 | |||
| f3808a73e1 | |||
| cb908fb143 | |||
| f53194be25 | |||
| ad732d1465 | |||
| b70a7d03af | |||
| 911fe3ea8e | |||
| b59bb349dc | |||
| 08cbacd69f | |||
| 9a70a92261 | |||
| be34168a1b | |||
| cebab6b2f3 | |||
| fdcb371582 | |||
| f01b1f8bac | |||
| a0eca36884 | |||
| 972d00df92 | |||
| 483b6e562b | |||
| cbb5ffa970 | |||
| b7cc0c39b4 | |||
| 982c173760 | |||
| 49ca73ab3a | |||
| 43b224b4d7 | |||
| 456a95d569 | |||
| 53b2d18a79 | |||
| 8d7f86e15a | |||
| 40213eeac9 | |||
| 5cb8b12a0f | |||
| 2f165186d1 | |||
| 6cd3b15d78 | |||
| 98c8d35f1e | |||
| 064b2b10b8 | |||
| a8d78d2784 | |||
| 5a0c70190f | |||
| d5bf7dc720 | |||
| 8b4bca7939 | |||
| c004ff1a97 | |||
| 38323da57d | |||
| 2ffd12b3e1 | |||
| 069d4163eb | |||
| 93e1317789 | |||
| 3b270e05c4 | |||
| a05818b3a7 | |||
| df2b0a0079 | |||
| c507e9125b | |||
| 767110f3d0 | |||
|
|
8e5f193caf |
17
.github/workflows/bearer.yml
vendored
17
.github/workflows/bearer.yml
vendored
@@ -1,17 +0,0 @@
|
||||
name: Bearer
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
rule_check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Bearer
|
||||
uses: bearer/bearer-action@v2
|
||||
43
.github/workflows/benchmark-gc.yml
vendored
Normal file
43
.github/workflows/benchmark-gc.yml
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
name: Benchmark GC Performance
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, master ]
|
||||
pull_request:
|
||||
branches: [ main, master ]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
actions: write
|
||||
|
||||
jobs:
|
||||
benchmark:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
|
||||
with:
|
||||
go-version: '1.25'
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-
|
||||
|
||||
- name: Install dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run benchmark (standard GC)
|
||||
run: make bench
|
||||
|
||||
- name: Run benchmark (experimental GC)
|
||||
run: make bench-experimental
|
||||
16
.github/workflows/build.yml
vendored
16
.github/workflows/build.yml
vendored
@@ -2,11 +2,11 @@ name: Go Build Multi-Platform
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
branches: [ "main", "master" ]
|
||||
tags:
|
||||
- 'v*'
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
branches: [ "main", "master" ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
@@ -27,12 +27,12 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
|
||||
with:
|
||||
go-version: '1.24'
|
||||
go-version: '1.25'
|
||||
|
||||
- name: Build
|
||||
id: build_step
|
||||
@@ -58,7 +58,7 @@ jobs:
|
||||
echo "Calculated SHA256 for ${output_name}"
|
||||
|
||||
- name: Upload Artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
|
||||
with:
|
||||
name: reticulum-go-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
path: reticulum-go-${{ matrix.goos }}-${{ matrix.goarch }}*
|
||||
@@ -74,7 +74,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Download All Build Artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
|
||||
with:
|
||||
path: ./release-assets
|
||||
|
||||
@@ -82,6 +82,6 @@ jobs:
|
||||
run: ls -R ./release-assets
|
||||
|
||||
- name: Create GitHub Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
uses: softprops/action-gh-release@6da8fa9354ddfdc4aeace5fc48d7f679b5214090 # v2.4.1
|
||||
with:
|
||||
files: ./release-assets/*/*
|
||||
|
||||
90
.github/workflows/go-test.yml
vendored
90
.github/workflows/go-test.yml
vendored
@@ -1,27 +1,103 @@
|
||||
name: Go Test
|
||||
name: Go Test Multi-Platform
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
name: Test (${{ matrix.os }}, ${{ matrix.goarch }})
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
# AMD64 testing across major platforms
|
||||
- os: ubuntu-latest
|
||||
goarch: amd64
|
||||
- os: windows-latest
|
||||
goarch: amd64
|
||||
- os: macos-latest
|
||||
goarch: amd64
|
||||
# ARM64 testing on supported platforms
|
||||
- os: ubuntu-latest
|
||||
goarch: arm64
|
||||
- os: macos-latest
|
||||
goarch: arm64
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- name: Checkout Source
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
|
||||
- name: Set up Go 1.24
|
||||
uses: actions/setup-go@v4
|
||||
- name: Set up Go 1.25
|
||||
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
|
||||
with:
|
||||
go-version: '1.24'
|
||||
go-version: '1.25'
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
|
||||
with:
|
||||
path: |
|
||||
~/go/pkg/mod
|
||||
~/.cache/go-build
|
||||
key: ${{ runner.os }}-go-${{ matrix.goarch }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-${{ matrix.goarch }}-
|
||||
|
||||
- name: Run Go tests
|
||||
run: go test ./...
|
||||
run: go test -v ./...
|
||||
|
||||
- name: Run Go tests with race detector (Linux AMD64 only)
|
||||
if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64'
|
||||
run: go test -race -v ./...
|
||||
|
||||
- name: Test build (ensure compilation works)
|
||||
run: |
|
||||
# Test that we can build for the current platform
|
||||
echo "Testing build for current platform (${{ matrix.os }}, ${{ matrix.goarch }})..."
|
||||
go build -v ./cmd/reticulum-go
|
||||
|
||||
- name: Test binary execution (Linux/macOS)
|
||||
if: matrix.os != 'windows-latest'
|
||||
run: |
|
||||
echo "Testing binary execution on (${{ matrix.os }}, ${{ matrix.goarch }})..."
|
||||
timeout 5s ./reticulum-go || echo "Binary started successfully (timeout expected)"
|
||||
|
||||
- name: Test binary execution (Windows)
|
||||
if: matrix.os == 'windows-latest'
|
||||
run: |
|
||||
echo "Testing binary execution on (${{ matrix.os }}, ${{ matrix.goarch }})..."
|
||||
# Start the binary and kill after 5 seconds to verify it can start
|
||||
Start-Process -FilePath ".\reticulum-go.exe" -NoNewWindow
|
||||
Start-Sleep -Seconds 5
|
||||
Stop-Process -Name "reticulum-go" -Force -ErrorAction SilentlyContinue
|
||||
echo "Binary started successfully"
|
||||
shell: pwsh
|
||||
|
||||
- name: Test cross-compilation (AMD64 runners only)
|
||||
if: matrix.goarch == 'amd64'
|
||||
run: |
|
||||
echo "Testing ARM64 cross-compilation from AMD64..."
|
||||
go build -v ./cmd/reticulum-go
|
||||
env:
|
||||
GOOS: linux
|
||||
GOARCH: arm64
|
||||
|
||||
- name: Test ARMv6 cross-compilation (AMD64 runners only)
|
||||
if: matrix.goarch == 'amd64'
|
||||
run: |
|
||||
echo "Testing ARMv6 cross-compilation from AMD64..."
|
||||
go build -v ./cmd/reticulum-go
|
||||
env:
|
||||
GOOS: linux
|
||||
GOARCH: arm
|
||||
GOARM: 6
|
||||
|
||||
7
.github/workflows/gosec.yml
vendored
7
.github/workflows/gosec.yml
vendored
@@ -1,13 +1,18 @@
|
||||
name: Run Gosec
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -15,7 +20,7 @@ jobs:
|
||||
GO111MODULE: on
|
||||
steps:
|
||||
- name: Checkout Source
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
- name: Run Gosec Security Scanner
|
||||
uses: securego/gosec@master
|
||||
with:
|
||||
|
||||
31
.github/workflows/performance-monitor.yml
vendored
Normal file
31
.github/workflows/performance-monitor.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
name: Performance Monitor
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, master ]
|
||||
pull_request:
|
||||
branches: [ main, master ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
performance-monitor:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
|
||||
with:
|
||||
go-version: '1.25'
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
go build -o bin/reticulum-go ./cmd/reticulum-go
|
||||
|
||||
- name: Run Performance Monitor
|
||||
id: monitor
|
||||
run: |
|
||||
cp tests/scripts/monitor_performance.sh .
|
||||
chmod +x monitor_performance.sh
|
||||
./monitor_performance.sh
|
||||
10
.github/workflows/revive.yml
vendored
10
.github/workflows/revive.yml
vendored
@@ -2,9 +2,9 @@ name: Go Revive Lint
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
branches: [ "main", "master" ]
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
branches: [ "main", "master" ]
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
@@ -14,12 +14,12 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
|
||||
with:
|
||||
go-version: '1.24'
|
||||
go-version: '1.25'
|
||||
|
||||
- name: Install revive
|
||||
run: go install github.com/mgechev/revive@latest
|
||||
|
||||
64
.github/workflows/tinygo.yml
vendored
Normal file
64
.github/workflows/tinygo.yml
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
name: TinyGo Build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "tinygo" ]
|
||||
pull_request:
|
||||
branches: [ "tinygo" ]
|
||||
|
||||
jobs:
|
||||
tinygo-build:
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- name: tinygo-default
|
||||
target: ""
|
||||
output: reticulum-go-tinygo
|
||||
make_target: tinygo-build
|
||||
- name: tinygo-wasm
|
||||
target: wasm
|
||||
output: reticulum-go.wasm
|
||||
make_target: tinygo-wasm
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
outputs:
|
||||
build_complete: ${{ steps.build_step.outcome == 'success' }}
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00
|
||||
with:
|
||||
go-version: '1.24'
|
||||
|
||||
- name: Install TinyGo
|
||||
run: |
|
||||
wget https://github.com/tinygo-org/tinygo/releases/download/v0.37.0/tinygo_0.37.0_amd64.deb
|
||||
sudo dpkg -i tinygo_0.37.0_amd64.deb
|
||||
|
||||
- name: Build with TinyGo
|
||||
id: build_step
|
||||
run: |
|
||||
make ${{ matrix.make_target }}
|
||||
output_name="${{ matrix.output }}"
|
||||
if [ -f "bin/${output_name}" ]; then
|
||||
sha256sum "bin/${output_name}" | cut -d' ' -f1 > "bin/${output_name}.sha256"
|
||||
echo "Built: ${output_name}"
|
||||
echo "Generated checksum: bin/${output_name}.sha256"
|
||||
else
|
||||
echo "Build output not found: bin/${output_name}"
|
||||
ls -la bin/
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Upload Artifact
|
||||
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
|
||||
with:
|
||||
name: ${{ matrix.name }}
|
||||
path: bin/${{ matrix.output }}*
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -5,3 +5,5 @@ logs/
|
||||
.json
|
||||
|
||||
bin/
|
||||
|
||||
examples/
|
||||
@@ -2,20 +2,34 @@
|
||||
|
||||
Be good to each other.
|
||||
|
||||
## Development
|
||||
|
||||
By contributing to this project you agree to the following:
|
||||
|
||||
- All code must be tested using `gosec`.
|
||||
- All code must be formatted with `gofmt`.
|
||||
- All code must be documented.
|
||||
|
||||
## Communication
|
||||
|
||||
Feel free to join our seperate matrix channel for this implementation.
|
||||
Feel free to join our telegram or matrix channels for this implementation.
|
||||
|
||||
- [Matrix](https://matrix.to/#/#reticulum-go-dev:matrix.org)
|
||||
- [Telegram](https://t.me/reticulum_go)
|
||||
|
||||
## Usage of LLMs and other Generative AI tools
|
||||
|
||||
We would prefer if you did not use LLMs and other generative AI tools to write critical parts of the code.
|
||||
You should not use LLMs and other generative AI tools to write critical parts of the code. They can produce lots of security issues and outdated code when used incorrectly. You are not required to report that you are using these tools.
|
||||
|
||||
## Static Analysis Tools
|
||||
|
||||
You are welcome to use the following tools, however there are actions in place to ensure the code is linted and checked with gosec.
|
||||
|
||||
### Linting (optional)
|
||||
|
||||
[Revive](https://github.com/mgechev/revive)
|
||||
|
||||
```bash
|
||||
revive -config revive.toml -formatter friendly ./pkg/* ./cmd/* ./internal/*
|
||||
```
|
||||
|
||||
### Security (optional)
|
||||
|
||||
[Gosec](https://github.com/securego/gosec)
|
||||
|
||||
```bash
|
||||
gosec ./...
|
||||
```
|
||||
|
||||
|
||||
72
Makefile
72
Makefile
@@ -1,5 +1,8 @@
|
||||
GOCMD=go
|
||||
GOBUILD=$(GOCMD) build
|
||||
GOBUILD_DEBUG=$(GOCMD) build
|
||||
GOBUILD_RELEASE=CGO_ENABLED=0 $(GOCMD) build -ldflags="-s -w"
|
||||
GOBUILD_EXPERIMENTAL=GOEXPERIMENT=greenteagc $(GOCMD) build
|
||||
GOCLEAN=$(GOCMD) clean
|
||||
GOTEST=$(GOCMD) test
|
||||
GOGET=$(GOCMD) get
|
||||
@@ -13,13 +16,38 @@ MAIN_PACKAGE=./cmd/reticulum-go
|
||||
|
||||
ALL_PACKAGES=$$(go list ./... | grep -v /vendor/)
|
||||
|
||||
.PHONY: all build clean test coverage deps help
|
||||
.PHONY: all build build-experimental experimental release debug lint bench bench-experimental bench-compare clean test coverage deps help tinygo-build tinygo-wasm run
|
||||
|
||||
all: clean deps build test
|
||||
|
||||
build:
|
||||
build:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PACKAGE)
|
||||
$(GOBUILD_RELEASE) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PACKAGE)
|
||||
|
||||
debug:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
$(GOBUILD_DEBUG) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PACKAGE)
|
||||
|
||||
build-experimental:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
$(GOBUILD_EXPERIMENTAL) -o $(BUILD_DIR)/$(BINARY_NAME)-experimental $(MAIN_PACKAGE)
|
||||
|
||||
experimental: build-experimental
|
||||
|
||||
release:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
$(GOBUILD_RELEASE) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PACKAGE)
|
||||
|
||||
lint:
|
||||
revive -config revive.toml -formatter friendly ./pkg/* ./cmd/* ./internal/*
|
||||
|
||||
bench:
|
||||
$(GOTEST) -bench=. -benchmem ./...
|
||||
|
||||
bench-experimental:
|
||||
GOEXPERIMENT=greenteagc $(GOTEST) -bench=. -benchmem ./...
|
||||
|
||||
bench-compare: bench bench-experimental
|
||||
|
||||
clean:
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@@ -33,7 +61,11 @@ coverage:
|
||||
$(GOCMD) tool cover -html=coverage.out
|
||||
|
||||
deps:
|
||||
@GOPROXY=$${GOPROXY:-https://proxy.golang.org,direct}; \
|
||||
export GOPROXY; \
|
||||
$(GOMOD) download
|
||||
@GOPROXY=$${GOPROXY:-https://proxy.golang.org,direct}; \
|
||||
export GOPROXY; \
|
||||
$(GOMOD) verify
|
||||
|
||||
build-linux:
|
||||
@@ -80,19 +112,35 @@ build-riscv:
|
||||
build-all: build-linux build-windows build-darwin build-freebsd build-openbsd build-netbsd build-arm build-riscv
|
||||
|
||||
run:
|
||||
@./$(BUILD_DIR)/$(BINARY_NAME)
|
||||
$(GOCMD) run $(MAIN_PACKAGE)
|
||||
|
||||
tinygo-build:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
tinygo build -o $(BUILD_DIR)/$(BINARY_NAME)-tinygo -size short $(MAIN_PACKAGE)
|
||||
|
||||
tinygo-wasm:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
tinygo build -target wasm -o $(BUILD_DIR)/$(BINARY_NAME).wasm $(MAIN_PACKAGE)
|
||||
|
||||
install:
|
||||
$(GOMOD) download
|
||||
|
||||
help:
|
||||
@echo "Available targets:"
|
||||
@echo " all - Clean, download dependencies, build and test"
|
||||
@echo " build - Build binary"
|
||||
@echo " clean - Remove build artifacts"
|
||||
@echo " test - Run tests"
|
||||
@echo " coverage - Generate test coverage report"
|
||||
@echo " deps - Download dependencies"
|
||||
@echo " all - Clean, download dependencies, build and test"
|
||||
@echo " build - Build release binary (no debug symbols, static)"
|
||||
@echo " debug - Build debug binary"
|
||||
@echo " build-experimental - Build binary with experimental features (GOEXPERIMENT=greenteagc)"
|
||||
@echo " experimental - Alias for build-experimental"
|
||||
@echo " release - Build stripped static binary for release (alias for build)"
|
||||
@echo " lint - Run revive linter"
|
||||
@echo " bench - Run benchmarks with standard GC"
|
||||
@echo " bench-experimental - Run benchmarks with experimental GC"
|
||||
@echo " bench-compare - Run benchmarks with both GC settings"
|
||||
@echo " clean - Remove build artifacts"
|
||||
@echo " test - Run tests"
|
||||
@echo " coverage - Generate test coverage report"
|
||||
@echo " deps - Download dependencies"
|
||||
@echo " build-linux - Build for Linux (amd64, arm64, arm)"
|
||||
@echo " build-windows- Build for Windows (amd64, arm64)"
|
||||
@echo " build-darwin - Build for MacOS (amd64, arm64)"
|
||||
@@ -102,5 +150,7 @@ help:
|
||||
@echo " build-arm - Build for ARM architectures (arm, arm64)"
|
||||
@echo " build-riscv - Build for RISC-V architecture (riscv64)"
|
||||
@echo " build-all - Build for all platforms and architectures"
|
||||
@echo " run - Run reticulum binary"
|
||||
@echo " run - Run with go run"
|
||||
@echo " tinygo-build - Build binary with TinyGo compiler"
|
||||
@echo " tinygo-wasm - Build WebAssembly binary with TinyGo compiler"
|
||||
@echo " install - Install dependencies"
|
||||
65
README.md
65
README.md
@@ -1,37 +1,64 @@
|
||||
[](https://socket.dev/go/package/github.com/sudo-ivan/reticulum-go)
|
||||

|
||||

|
||||
[](https://github.com/Sudo-Ivan/Reticulum-Go/actions/workflows/build.yml)
|
||||
[](https://github.com/Sudo-Ivan/Reticulum-Go/actions/workflows/revive.yml)
|
||||
|
||||
# Reticulum-Go
|
||||
|
||||
A Go implementation of the [Reticulum Network Protocol](https://github.com/markqvist/Reticulum).
|
||||
|
||||
> [!WARNING]
|
||||
> This project is still work in progress. Currently not compatible with the Python version.
|
||||
> This project is currently in development and is not yet compatible with the Python reference implementation.
|
||||
|
||||
[](https://socket.dev/go/package/github.com/sudo-ivan/reticulum-go)
|
||||

|
||||

|
||||
[](https://github.com/Sudo-Ivan/Reticulum-Go/actions/workflows/bearer.yml)
|
||||
[](https://github.com/Sudo-Ivan/Reticulum-Go/actions/workflows/build.yml)
|
||||
[](https://github.com/Sudo-Ivan/Reticulum-Go/actions/workflows/revive.yml)
|
||||
## Goals
|
||||
|
||||
[Reticulum Network](https://github.com/markqvist/Reticulum) implementation in Go `1.24+`.
|
||||
- To be fully compatible with the Python reference implementation.
|
||||
- Additional privacy and security features.
|
||||
- Support for a broader range of platforms and architectures old and new.
|
||||
|
||||
Aiming to be fully compatible with the Python version.
|
||||
## Quick Start
|
||||
|
||||
## Usage
|
||||
### Prerequisites
|
||||
|
||||
Requires Go 1.24+
|
||||
- Go 1.24 or later
|
||||
|
||||
```
|
||||
make install
|
||||
### Build
|
||||
|
||||
```bash
|
||||
make build
|
||||
```
|
||||
|
||||
### Run
|
||||
|
||||
```bash
|
||||
make run
|
||||
```
|
||||
|
||||
## Linter
|
||||
|
||||
[Revive](https://github.com/mgechev/revive)
|
||||
### Test
|
||||
|
||||
```bash
|
||||
revive -config revive.toml -formatter friendly ./pkg/* ./cmd/* ./internal/*
|
||||
make test
|
||||
```
|
||||
|
||||
## External Packages
|
||||
## Embedded systems and WebAssembly
|
||||
|
||||
- `golang.org/x/crypto` `v0.39.0` - Cryptographic primitives
|
||||
For building for WebAssembly and embedded systems, see the [tinygo branch](https://github.com/Sudo-Ivan/Reticulum-Go/tree/tinygo). Requires TinyGo 0.37.0+.
|
||||
|
||||
```bash
|
||||
make tinygo-build
|
||||
make tinygo-wasm
|
||||
```
|
||||
|
||||
### Experimental Features
|
||||
|
||||
Build with experimental Green Tea GC (Go 1.25+):
|
||||
|
||||
```bash
|
||||
make build-experimental
|
||||
```
|
||||
|
||||
## Official Channels
|
||||
|
||||
- [Telegram](https://t.me/reticulum_go)
|
||||
- [Matrix](https://matrix.to/#/#reticulum-go-dev:matrix.org)
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
We use [Socket](https://socket.dev/), [Deepsource](https://deepsource.com/) and [gosec](https://github.com/securego/gosec) for this project.
|
||||
|
||||
## Strict Verfication of Contributors and Code Quality
|
||||
## Supply Chain Security
|
||||
|
||||
We are strict about the quality of the code and the contributors. Please read the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.
|
||||
- All actions are pinned to a commit hash.
|
||||
|
||||
## Cryptography Dependencies
|
||||
|
||||
|
||||
17
TODO.md
17
TODO.md
@@ -1,6 +1,8 @@
|
||||
### Core Components (In Progress)
|
||||
|
||||
Last Updated: 2025-07-06
|
||||
*Needs verification with Reticulum 1.0.0.*
|
||||
|
||||
Last Updated: 2025-09-25
|
||||
|
||||
- [x] Basic Configuration System
|
||||
- [x] Basic config structure
|
||||
@@ -25,7 +27,7 @@ Last Updated: 2025-07-06
|
||||
- [x] Cryptographic Primitives (Testing required)
|
||||
- [x] Ed25519
|
||||
- [x] Curve25519
|
||||
- [x] AES-128-CBC
|
||||
- [x] ~~AES-128-CBC~~ (Deprecated)
|
||||
- [x] AES-256-CBC
|
||||
- [x] SHA-256
|
||||
- [x] HKDF
|
||||
@@ -131,7 +133,6 @@ Last Updated: 2025-07-06
|
||||
- [ ] RNS Utilities.
|
||||
- [ ] Reticulum config.
|
||||
|
||||
|
||||
### Testing & Validation (Priority)
|
||||
- [ ] Unit tests for all components
|
||||
- [ ] Identity tests
|
||||
@@ -152,8 +153,7 @@ Last Updated: 2025-07-06
|
||||
- [ ] Channel system end-to-end
|
||||
- [ ] Buffer system performance
|
||||
- [ ] Cross-client compatibility tests
|
||||
- [ ] Performance benchmarks
|
||||
- [ ] Security auditing (When Reticulum is 1.0 / stable)
|
||||
- [ ] Performance and memory benchmarks
|
||||
|
||||
### Documentation
|
||||
- [ ] API documentation
|
||||
@@ -164,4 +164,9 @@ Last Updated: 2025-07-06
|
||||
- [ ] Move constants to their own files
|
||||
- [ ] Remove default community interfaces in default config creation after testing
|
||||
- [ ] Optimize announce packet creation and caching
|
||||
- [ ] Improve debug logging system
|
||||
- [ ] Improve debug logging system
|
||||
|
||||
### Experimental Features
|
||||
- [x] Experimental Green Tea GC (build option) (Go 1.25+)
|
||||
- [ ] MicroVM (firecracker)
|
||||
- [ ] Kata Container Support
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/binary"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
@@ -16,6 +15,7 @@ import (
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/buffer"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/channel"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/debug"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/destination"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/identity"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/interfaces"
|
||||
@@ -24,29 +24,15 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
debugLevel = flag.Int("debug", 7, "Debug level (0-7)")
|
||||
interceptPackets = flag.Bool("intercept-packets", false, "Enable packet interception")
|
||||
interceptOutput = flag.String("intercept-output", "packets.log", "Output file for intercepted packets")
|
||||
)
|
||||
|
||||
func debugLog(level int, format string, v ...interface{}) {
|
||||
if *debugLevel >= level {
|
||||
log.Printf("[DEBUG-%d] %s", level, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
ANNOUNCE_RATE_TARGET = 3600 // Default target time between announces (1 hour)
|
||||
ANNOUNCE_RATE_GRACE = 3 // Number of grace announces before enforcing rate
|
||||
ANNOUNCE_RATE_PENALTY = 7200 // Additional penalty time for rate violations
|
||||
MAX_ANNOUNCE_HOPS = 128 // Maximum number of hops for announces
|
||||
DEBUG_CRITICAL = 1 // Critical errors
|
||||
DEBUG_ERROR = 2 // Non-critical errors
|
||||
DEBUG_INFO = 3 // Important information
|
||||
DEBUG_VERBOSE = 4 // Detailed information
|
||||
DEBUG_TRACE = 5 // Very detailed tracing
|
||||
DEBUG_PACKETS = 6 // Packet-level details
|
||||
DEBUG_ALL = 7 // Everything including identity operations
|
||||
APP_NAME = "Go-Client"
|
||||
APP_ASPECT = "node" // Always use "node" for node announces
|
||||
)
|
||||
@@ -90,31 +76,31 @@ func NewReticulum(cfg *common.ReticulumConfig) (*Reticulum, error) {
|
||||
if err := initializeDirectories(); err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize directories: %v", err)
|
||||
}
|
||||
debugLog(3, "Directories initialized")
|
||||
debug.Log(debug.DEBUG_INFO, "Directories initialized")
|
||||
|
||||
t := transport.NewTransport(cfg)
|
||||
debugLog(3, "Transport initialized")
|
||||
debug.Log(debug.DEBUG_INFO, "Transport initialized")
|
||||
|
||||
identity, err := identity.NewIdentity()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create identity: %v", err)
|
||||
}
|
||||
debugLog(2, "Created new identity: %x", identity.Hash())
|
||||
debug.Log(debug.DEBUG_ERROR, "Created new identity", "hash", fmt.Sprintf("%x", identity.Hash()))
|
||||
|
||||
// Create destination
|
||||
debugLog(DEBUG_INFO, "Creating destination...")
|
||||
debug.Log(debug.DEBUG_INFO, "Creating destination...")
|
||||
dest, err := destination.New(
|
||||
identity,
|
||||
destination.IN,
|
||||
destination.SINGLE,
|
||||
"reticulum",
|
||||
"nomadnetwork",
|
||||
t,
|
||||
"node",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create destination: %v", err)
|
||||
}
|
||||
debugLog(DEBUG_INFO, "Created destination with hash: %x", dest.GetHash())
|
||||
debug.Log(debug.DEBUG_INFO, "Created destination with hash", "hash", fmt.Sprintf("%x", dest.GetHash()))
|
||||
|
||||
// Set node metadata
|
||||
nodeTimestamp := time.Now().Unix()
|
||||
@@ -143,7 +129,7 @@ func NewReticulum(cfg *common.ReticulumConfig) (*Reticulum, error) {
|
||||
ratchetPath := ".reticulum-go/storage/ratchets/" + r.identity.GetHexHash()
|
||||
dest.EnableRatchets(ratchetPath)
|
||||
dest.SetProofStrategy(destination.PROVE_APP)
|
||||
debugLog(DEBUG_VERBOSE, "Configured destination features")
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Configured destination features")
|
||||
|
||||
// Initialize interfaces from config
|
||||
for name, ifaceConfig := range cfg.Interfaces {
|
||||
@@ -160,9 +146,9 @@ func NewReticulum(cfg *common.ReticulumConfig) (*Reticulum, error) {
|
||||
name,
|
||||
ifaceConfig.TargetHost,
|
||||
ifaceConfig.TargetPort,
|
||||
ifaceConfig.KISSFraming,
|
||||
ifaceConfig.I2PTunneled,
|
||||
ifaceConfig.Enabled,
|
||||
true, // IN
|
||||
true, // OUT
|
||||
)
|
||||
case "UDPInterface":
|
||||
iface, err = interfaces.NewUDPInterface(
|
||||
@@ -174,7 +160,7 @@ func NewReticulum(cfg *common.ReticulumConfig) (*Reticulum, error) {
|
||||
case "AutoInterface":
|
||||
iface, err = interfaces.NewAutoInterface(name, ifaceConfig)
|
||||
default:
|
||||
debugLog(1, "Unknown interface type: %s", ifaceConfig.Type)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Unknown interface type", "type", ifaceConfig.Type)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -182,27 +168,30 @@ func NewReticulum(cfg *common.ReticulumConfig) (*Reticulum, error) {
|
||||
if cfg.PanicOnInterfaceErr {
|
||||
return nil, fmt.Errorf("failed to create interface %s: %v", name, err)
|
||||
}
|
||||
debugLog(1, "Error creating interface %s: %v", name, err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Error creating interface", "name", name, "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Set packet callback
|
||||
iface.SetPacketCallback(func(data []byte, ni common.NetworkInterface) {
|
||||
debug.Log(debug.DEBUG_INFO, "Packet callback called for interface", "name", ni.GetName(), "data_len", len(data))
|
||||
if r.transport != nil {
|
||||
r.transport.HandlePacket(data, ni)
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Transport is nil in packet callback")
|
||||
}
|
||||
})
|
||||
|
||||
debugLog(2, "Configuring interface %s (type=%s)...", name, ifaceConfig.Type)
|
||||
debug.Log(debug.DEBUG_ERROR, "Configuring interface", "name", name, "type", ifaceConfig.Type)
|
||||
r.interfaces = append(r.interfaces, iface)
|
||||
debugLog(3, "Interface %s started successfully", name)
|
||||
debug.Log(debug.DEBUG_INFO, "Interface started successfully", "name", name)
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (r *Reticulum) handleInterface(iface common.NetworkInterface) {
|
||||
debugLog(DEBUG_INFO, "Setting up interface %s (type=%T)", iface.GetName(), iface)
|
||||
debug.Log(debug.DEBUG_INFO, "Setting up interface", "name", iface.GetName(), "type", fmt.Sprintf("%T", iface))
|
||||
|
||||
ch := channel.NewChannel(&transportWrapper{r.transport})
|
||||
r.channels[iface.GetName()] = ch
|
||||
@@ -213,11 +202,11 @@ func (r *Reticulum) handleInterface(iface common.NetworkInterface) {
|
||||
ch,
|
||||
func(size int) {
|
||||
data := make([]byte, size)
|
||||
debugLog(DEBUG_PACKETS, "Interface %s: Reading %d bytes from buffer", iface.GetName(), size)
|
||||
debug.Log(debug.DEBUG_PACKETS, "Interface reading bytes from buffer", "name", iface.GetName(), "size", size)
|
||||
iface.ProcessIncoming(data)
|
||||
|
||||
if len(data) > 0 {
|
||||
debugLog(DEBUG_TRACE, "Interface %s: Received packet type 0x%02x", iface.GetName(), data[0])
|
||||
debug.Log(debug.DEBUG_TRACE, "Interface received packet type", "name", iface.GetName(), "type", fmt.Sprintf("0x%02x", data[0]))
|
||||
r.transport.HandlePacket(data, iface)
|
||||
}
|
||||
},
|
||||
@@ -248,7 +237,7 @@ func (r *Reticulum) monitorInterfaces() {
|
||||
stats = fmt.Sprintf("%s, RTT: %v", stats, tcpClient.GetRTT())
|
||||
}
|
||||
|
||||
debugLog(DEBUG_VERBOSE, "%s", stats)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Interface status", "stats", stats)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,22 +245,30 @@ func (r *Reticulum) monitorInterfaces() {
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
debugLog(1, "Initializing Reticulum (Debug Level: %d)...", *debugLevel)
|
||||
debug.Init()
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Initializing Reticulum", "debug_level", debug.GetDebugLevel())
|
||||
|
||||
cfg, err := config.InitConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize config: %v", err)
|
||||
debug.GetLogger().Error("Failed to initialize config", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
debugLog(2, "Configuration loaded from: %s", cfg.ConfigPath)
|
||||
debug.Log(debug.DEBUG_ERROR, "Configuration loaded", "path", cfg.ConfigPath)
|
||||
|
||||
// Add default TCP interfaces if none configured
|
||||
if len(cfg.Interfaces) == 0 {
|
||||
debugLog(2, "No interfaces configured, adding default TCP interfaces")
|
||||
debug.Log(debug.DEBUG_ERROR, "No interfaces configured, adding default interfaces")
|
||||
cfg.Interfaces = make(map[string]*common.InterfaceConfig)
|
||||
|
||||
// Auto interface for local discovery
|
||||
cfg.Interfaces["Auto Discovery"] = &common.InterfaceConfig{
|
||||
Type: "AutoInterface",
|
||||
Enabled: true,
|
||||
Name: "Auto Discovery",
|
||||
}
|
||||
|
||||
cfg.Interfaces["Go-RNS-Testnet"] = &common.InterfaceConfig{
|
||||
Type: "TCPClientInterface",
|
||||
Enabled: true,
|
||||
Enabled: false,
|
||||
TargetHost: "127.0.0.1",
|
||||
TargetPort: 4242,
|
||||
Name: "Go-RNS-Testnet",
|
||||
@@ -288,7 +285,8 @@ func main() {
|
||||
|
||||
r, err := NewReticulum(cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create Reticulum instance: %v", err)
|
||||
debug.GetLogger().Error("Failed to create Reticulum instance", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Start monitoring interfaces
|
||||
@@ -300,18 +298,19 @@ func main() {
|
||||
|
||||
// Start Reticulum
|
||||
if err := r.Start(); err != nil {
|
||||
log.Fatalf("Failed to start Reticulum: %v", err)
|
||||
debug.GetLogger().Error("Failed to start Reticulum", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
|
||||
debugLog(1, "Shutting down...")
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Shutting down...")
|
||||
if err := r.Stop(); err != nil {
|
||||
debugLog(1, "Error during shutdown: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Error during shutdown", "error", err)
|
||||
}
|
||||
debugLog(1, "Goodbye!")
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Goodbye!")
|
||||
}
|
||||
|
||||
type transportWrapper struct {
|
||||
@@ -380,37 +379,44 @@ func initializeDirectories() error {
|
||||
}
|
||||
|
||||
func (r *Reticulum) Start() error {
|
||||
debugLog(2, "Starting Reticulum...")
|
||||
debug.Log(debug.DEBUG_ERROR, "Starting Reticulum...")
|
||||
|
||||
if err := r.transport.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start transport: %v", err)
|
||||
}
|
||||
debugLog(3, "Transport started successfully")
|
||||
debug.Log(debug.DEBUG_INFO, "Transport started successfully")
|
||||
|
||||
// Start interfaces
|
||||
for _, iface := range r.interfaces {
|
||||
debugLog(2, "Starting interface %s...", iface.GetName())
|
||||
debug.Log(debug.DEBUG_ERROR, "Starting interface", "name", iface.GetName())
|
||||
if err := iface.Start(); err != nil {
|
||||
if r.config.PanicOnInterfaceErr {
|
||||
return fmt.Errorf("failed to start interface %s: %v", iface.GetName(), err)
|
||||
}
|
||||
debugLog(1, "Error starting interface %s: %v", iface.GetName(), err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Error starting interface", "name", iface.GetName(), "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if netIface, ok := iface.(common.NetworkInterface); ok {
|
||||
// Register interface with transport
|
||||
if err := r.transport.RegisterInterface(iface.GetName(), netIface); err != nil {
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to register interface with transport", "name", iface.GetName(), "error", err)
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "Registered interface with transport", "name", iface.GetName())
|
||||
}
|
||||
r.handleInterface(netIface)
|
||||
}
|
||||
debugLog(3, "Interface %s started successfully", iface.GetName())
|
||||
debug.Log(debug.DEBUG_INFO, "Interface started successfully", "name", iface.GetName())
|
||||
}
|
||||
|
||||
// Wait for interfaces to initialize
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// Send initial announce
|
||||
debugLog(2, "Sending initial announce")
|
||||
if err := r.destination.Announce(r.createNodeAppData()); err != nil {
|
||||
debugLog(1, "Failed to send initial announce: %v", err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Sending initial announce")
|
||||
nodeName := "Go-Client"
|
||||
if err := r.destination.Announce([]byte(nodeName)); err != nil {
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to send initial announce", "error", err)
|
||||
}
|
||||
|
||||
// Start periodic announce goroutine
|
||||
@@ -419,41 +425,40 @@ func (r *Reticulum) Start() error {
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
for {
|
||||
debugLog(3, "Announcing destination...")
|
||||
err := r.destination.Announce(r.createNodeAppData())
|
||||
debug.Log(debug.DEBUG_INFO, "Announcing destination...")
|
||||
err := r.destination.Announce([]byte(nodeName))
|
||||
if err != nil {
|
||||
debugLog(1, "Could not send announce: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Could not send announce", "error", err)
|
||||
}
|
||||
|
||||
// Announce every 5 minutes
|
||||
time.Sleep(5 * time.Minute)
|
||||
time.Sleep(60 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
go r.monitorInterfaces()
|
||||
|
||||
debugLog(2, "Reticulum started successfully")
|
||||
debug.Log(debug.DEBUG_ERROR, "Reticulum started successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Reticulum) Stop() error {
|
||||
debugLog(2, "Stopping Reticulum...")
|
||||
debug.Log(debug.DEBUG_ERROR, "Stopping Reticulum...")
|
||||
|
||||
for _, buf := range r.buffers {
|
||||
if err := buf.Close(); err != nil {
|
||||
debugLog(1, "Error closing buffer: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Error closing buffer", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, ch := range r.channels {
|
||||
if err := ch.Close(); err != nil {
|
||||
debugLog(1, "Error closing channel: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Error closing channel", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, iface := range r.interfaces {
|
||||
if err := iface.Stop(); err != nil {
|
||||
debugLog(1, "Error stopping interface %s: %v", iface.GetName(), err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Error stopping interface", "name", iface.GetName(), "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -461,7 +466,7 @@ func (r *Reticulum) Stop() error {
|
||||
return fmt.Errorf("failed to close transport: %v", err)
|
||||
}
|
||||
|
||||
debugLog(2, "Reticulum stopped successfully")
|
||||
debug.Log(debug.DEBUG_ERROR, "Reticulum stopped successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -482,102 +487,58 @@ func (h *AnnounceHandler) AspectFilter() []string {
|
||||
}
|
||||
|
||||
func (h *AnnounceHandler) ReceivedAnnounce(destHash []byte, id interface{}, appData []byte) error {
|
||||
debugLog(DEBUG_INFO, "Received announce from %x", destHash)
|
||||
debugLog(DEBUG_PACKETS, "Raw announce data: %x", appData)
|
||||
debug.Log(debug.DEBUG_INFO, "Received announce", "hash", fmt.Sprintf("%x", destHash))
|
||||
debug.Log(debug.DEBUG_PACKETS, "Raw announce data", "data", fmt.Sprintf("%x", appData))
|
||||
debug.Log(debug.DEBUG_INFO, "MAIN HANDLER: Received announce", "hash", fmt.Sprintf("%x", destHash), "appData_len", len(appData))
|
||||
|
||||
var isNode bool
|
||||
var nodeEnabled bool
|
||||
var nodeTimestamp int64
|
||||
var nodeMaxSize int16
|
||||
|
||||
// Parse msgpack array
|
||||
// Parse msgpack appData from transport announce format
|
||||
if len(appData) > 0 {
|
||||
if appData[0] == 0x92 {
|
||||
// Format [name, ticket] for standard peers
|
||||
debugLog(DEBUG_VERBOSE, "Received standard peer announce")
|
||||
isNode = false
|
||||
var pos = 1
|
||||
|
||||
// Parse first element (NameBytes)
|
||||
if pos+1 < len(appData) && appData[pos] == 0xc4 {
|
||||
// appData is msgpack array [name, customData]
|
||||
if appData[0] == 0x92 { // array of 2 elements
|
||||
// Skip array header and first element (name)
|
||||
pos := 1
|
||||
if pos < len(appData) && appData[pos] == 0xc4 { // bin 8
|
||||
nameLen := int(appData[pos+1])
|
||||
if pos+2+nameLen <= len(appData) {
|
||||
nameBytes := appData[pos+2 : pos+2+nameLen]
|
||||
name := string(nameBytes)
|
||||
pos += 2 + nameLen
|
||||
debugLog(DEBUG_VERBOSE, "Peer name: %s (bytes: %x)", name, nameBytes)
|
||||
|
||||
// Parse second element (TicketValue)
|
||||
if pos < len(appData) {
|
||||
ticketValue := appData[pos] // Assuming fixint for now
|
||||
debugLog(DEBUG_VERBOSE, "Peer ticket value: %d", ticketValue)
|
||||
} else {
|
||||
debugLog(DEBUG_ERROR, "Could not parse ticket value from announce appData")
|
||||
pos += 2 + nameLen
|
||||
if pos < len(appData) && appData[pos] == 0xc4 { // bin 8
|
||||
dataLen := int(appData[pos+1])
|
||||
if pos+2+dataLen <= len(appData) {
|
||||
customData := appData[pos+2 : pos+2+dataLen]
|
||||
nodeName := string(customData)
|
||||
debug.Log(debug.DEBUG_INFO, "Parsed node name", "name", nodeName)
|
||||
debug.Log(debug.DEBUG_INFO, "Announced node", "name", nodeName)
|
||||
}
|
||||
} else {
|
||||
debugLog(DEBUG_ERROR, "Could not parse name bytes from announce appData")
|
||||
}
|
||||
} else {
|
||||
debugLog(DEBUG_ERROR, "Announce appData name is not in expected bin 8 format")
|
||||
}
|
||||
} else if appData[0] == 0x93 {
|
||||
// Format [enable, timestamp, maxsize] for nodes
|
||||
debugLog(DEBUG_VERBOSE, "Received node announce")
|
||||
isNode = true
|
||||
var pos = 1
|
||||
|
||||
// Parse first element (Boolean enable/disable)
|
||||
if pos < len(appData) {
|
||||
if appData[pos] == 0xc3 {
|
||||
nodeEnabled = true
|
||||
} else if appData[pos] == 0xc2 {
|
||||
nodeEnabled = false
|
||||
} else {
|
||||
debugLog(DEBUG_ERROR, "Unexpected format for node enabled status: %x", appData[pos])
|
||||
}
|
||||
pos++
|
||||
debugLog(DEBUG_VERBOSE, "Node enabled: %v", nodeEnabled)
|
||||
|
||||
// Parse second element (Int32 timestamp)
|
||||
if pos+4 < len(appData) && appData[pos] == 0xd2 {
|
||||
pos++
|
||||
timestamp := binary.BigEndian.Uint32(appData[pos : pos+4])
|
||||
nodeTimestamp = int64(timestamp)
|
||||
pos += 4
|
||||
debugLog(DEBUG_VERBOSE, "Node timestamp: %d (%s)", timestamp, time.Unix(nodeTimestamp, 0))
|
||||
|
||||
// Parse third element (Int16 max transfer size)
|
||||
if pos+2 < len(appData) && appData[pos] == 0xd1 {
|
||||
pos++
|
||||
maxSize := binary.BigEndian.Uint16(appData[pos : pos+2])
|
||||
nodeMaxSize = int16(maxSize) // #nosec G115
|
||||
debugLog(DEBUG_VERBOSE, "Node max transfer size: %d KB", nodeMaxSize)
|
||||
} else {
|
||||
debugLog(DEBUG_ERROR, "Could not parse max transfer size from node announce")
|
||||
}
|
||||
} else {
|
||||
debugLog(DEBUG_ERROR, "Could not parse timestamp from node announce")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debugLog(DEBUG_VERBOSE, "Unknown announce data format: %x", appData)
|
||||
// Fallback: treat as raw node name
|
||||
nodeName := string(appData)
|
||||
debug.Log(debug.DEBUG_INFO, "Raw node name", "name", nodeName)
|
||||
debug.Log(debug.DEBUG_INFO, "Announced node", "name", nodeName)
|
||||
}
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "No appData (empty announce)")
|
||||
}
|
||||
|
||||
// Type assert and log identity details
|
||||
if identity, ok := id.(*identity.Identity); ok {
|
||||
debugLog(DEBUG_ALL, "Identity details:")
|
||||
debugLog(DEBUG_ALL, " Hash: %s", identity.GetHexHash())
|
||||
debugLog(DEBUG_ALL, " Public Key: %x", identity.GetPublicKey())
|
||||
debug.Log(debug.DEBUG_ALL, "Identity details")
|
||||
debug.Log(debug.DEBUG_ALL, "Identity hash", "hash", identity.GetHexHash())
|
||||
debug.Log(debug.DEBUG_ALL, "Identity public key", "key", fmt.Sprintf("%x", identity.GetPublicKey()))
|
||||
|
||||
ratchets := identity.GetRatchets()
|
||||
debugLog(DEBUG_ALL, " Active Ratchets: %d", len(ratchets))
|
||||
debug.Log(debug.DEBUG_ALL, "Active ratchets", "count", len(ratchets))
|
||||
|
||||
if len(ratchets) > 0 {
|
||||
ratchetKey := identity.GetCurrentRatchetKey()
|
||||
if ratchetKey != nil {
|
||||
ratchetID := identity.GetRatchetID(ratchetKey)
|
||||
debugLog(DEBUG_ALL, " Current Ratchet ID: %x", ratchetID)
|
||||
debug.Log(debug.DEBUG_ALL, "Current ratchet ID", "id", fmt.Sprintf("%x", ratchetID))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -585,8 +546,7 @@ func (h *AnnounceHandler) ReceivedAnnounce(destHash []byte, id interface{}, appD
|
||||
recordType := "peer"
|
||||
if isNode {
|
||||
recordType = "node"
|
||||
debugLog(DEBUG_INFO, "Storing node in announce history: enabled=%v, timestamp=%d, maxsize=%dKB",
|
||||
nodeEnabled, nodeTimestamp, nodeMaxSize)
|
||||
debug.Log(debug.DEBUG_INFO, "Storing node in announce history", "enabled", nodeEnabled, "timestamp", nodeTimestamp, "maxsize", fmt.Sprintf("%dKB", nodeMaxSize))
|
||||
}
|
||||
|
||||
h.reticulum.announceHistoryMu.Lock()
|
||||
@@ -596,7 +556,7 @@ func (h *AnnounceHandler) ReceivedAnnounce(destHash []byte, id interface{}, appD
|
||||
}
|
||||
h.reticulum.announceHistoryMu.Unlock()
|
||||
|
||||
debugLog(DEBUG_VERBOSE, "Stored %s announce in history for identity %s", recordType, identity.GetHexHash())
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Stored announce in history", "type", recordType, "identity", identity.GetHexHash())
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -636,7 +596,6 @@ func (r *Reticulum) createNodeAppData() []byte {
|
||||
binary.BigEndian.PutUint16(sizeBytes, uint16(r.maxTransferSize)) // #nosec G115
|
||||
appData = append(appData, sizeBytes...)
|
||||
|
||||
log.Printf("[DEBUG-7] Created node appData (msgpack [enable=%v, timestamp=%d, maxsize=%d]): %x",
|
||||
r.nodeEnabled, r.nodeTimestamp, r.maxTransferSize, appData)
|
||||
debug.Log(debug.DEBUG_ALL, "Created node appData", "enable", r.nodeEnabled, "timestamp", r.nodeTimestamp, "maxsize", r.maxTransferSize, "data", fmt.Sprintf("%x", appData))
|
||||
return appData
|
||||
}
|
||||
|
||||
39
docker/Dockerfile
Normal file
39
docker/Dockerfile
Normal file
@@ -0,0 +1,39 @@
|
||||
ARG GO_VERSION=1.25
|
||||
FROM golang:${GO_VERSION}-alpine AS builder
|
||||
|
||||
ENV CGO_ENABLED=0
|
||||
ENV GOOS=linux
|
||||
ENV GOARCH=amd64
|
||||
|
||||
RUN apk add --no-cache git
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY cmd/ cmd/
|
||||
COPY internal/ internal/
|
||||
COPY pkg/ pkg/
|
||||
|
||||
RUN go build \
|
||||
-ldflags='-w -s -extldflags "-static"' \
|
||||
-a -installsuffix cgo \
|
||||
-o reticulum-go \
|
||||
./cmd/reticulum-go
|
||||
|
||||
FROM busybox:latest
|
||||
|
||||
RUN adduser -D -s /bin/sh app
|
||||
|
||||
COPY --from=builder /build/reticulum-go /usr/local/bin/reticulum-go
|
||||
|
||||
RUN chmod +x /usr/local/bin/reticulum-go
|
||||
RUN mkdir -p /app && chown app:app /app
|
||||
|
||||
USER app
|
||||
WORKDIR /app
|
||||
|
||||
EXPOSE 4242
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/reticulum-go"]
|
||||
27
docker/Dockerfile.build
Normal file
27
docker/Dockerfile.build
Normal file
@@ -0,0 +1,27 @@
|
||||
ARG GO_VERSION=1.25
|
||||
FROM golang:${GO_VERSION}-alpine
|
||||
|
||||
ENV CGO_ENABLED=0
|
||||
ENV GOOS=linux
|
||||
ENV GOARCH=amd64
|
||||
|
||||
RUN apk add --no-cache git
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY cmd/ cmd/
|
||||
COPY internal/ internal/
|
||||
COPY pkg/ pkg/
|
||||
|
||||
ARG BINARY_NAME=reticulum-go
|
||||
ARG BUILD_PATH=./cmd/reticulum-go
|
||||
|
||||
RUN mkdir -p /dist && \
|
||||
go build \
|
||||
-ldflags='-w -s -extldflags "-static"' \
|
||||
-a -installsuffix cgo \
|
||||
-o /dist/${BINARY_NAME} \
|
||||
${BUILD_PATH}
|
||||
9
go.mod
9
go.mod
@@ -1,5 +1,10 @@
|
||||
module github.com/Sudo-Ivan/reticulum-go
|
||||
|
||||
go 1.24.4
|
||||
go 1.24.0
|
||||
|
||||
require golang.org/x/crypto v0.39.0
|
||||
require (
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1
|
||||
golang.org/x/crypto v0.43.0
|
||||
)
|
||||
|
||||
require github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
|
||||
16
go.sum
16
go.sum
@@ -1,2 +1,14 @@
|
||||
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
|
||||
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
|
||||
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
|
||||
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -58,6 +58,7 @@ type AnnounceHandler interface {
|
||||
type Announce struct {
|
||||
mutex *sync.RWMutex
|
||||
destinationHash []byte
|
||||
destinationName string
|
||||
identity *identity.Identity
|
||||
appData []byte
|
||||
config *common.ReticulumConfig
|
||||
@@ -72,27 +73,32 @@ type Announce struct {
|
||||
hash []byte
|
||||
}
|
||||
|
||||
func New(dest *identity.Identity, appData []byte, pathResponse bool, config *common.ReticulumConfig) (*Announce, error) {
|
||||
func New(dest *identity.Identity, destinationHash []byte, destinationName string, appData []byte, pathResponse bool, config *common.ReticulumConfig) (*Announce, error) {
|
||||
if dest == nil {
|
||||
return nil, errors.New("destination identity required")
|
||||
}
|
||||
|
||||
a := &Announce{
|
||||
mutex: &sync.RWMutex{},
|
||||
identity: dest,
|
||||
appData: appData,
|
||||
config: config,
|
||||
hops: 0,
|
||||
timestamp: time.Now().Unix(),
|
||||
pathResponse: pathResponse,
|
||||
retries: 0,
|
||||
handlers: make([]AnnounceHandler, 0),
|
||||
if len(destinationHash) == 0 {
|
||||
return nil, errors.New("destination hash required")
|
||||
}
|
||||
|
||||
// Generate truncated hash from public key
|
||||
pubKey := dest.GetPublicKey()
|
||||
hash := sha256.Sum256(pubKey)
|
||||
a.destinationHash = hash[:identity.TRUNCATED_HASHLENGTH/8]
|
||||
if destinationName == "" {
|
||||
return nil, errors.New("destination name required")
|
||||
}
|
||||
|
||||
a := &Announce{
|
||||
mutex: &sync.RWMutex{},
|
||||
identity: dest,
|
||||
destinationHash: destinationHash,
|
||||
destinationName: destinationName,
|
||||
appData: appData,
|
||||
config: config,
|
||||
hops: 0,
|
||||
timestamp: time.Now().Unix(),
|
||||
pathResponse: pathResponse,
|
||||
retries: 0,
|
||||
handlers: make([]AnnounceHandler, 0),
|
||||
}
|
||||
|
||||
// Get current ratchet ID if enabled
|
||||
currentRatchet := dest.GetCurrentRatchetKey()
|
||||
@@ -225,9 +231,9 @@ func (a *Announce) HandleAnnounce(data []byte) error {
|
||||
}
|
||||
|
||||
// Now parse the data portion according to the spec
|
||||
// Public Key (32) + Signing Key (32) + Name Hash (10) + Random Hash (10) + [Ratchet] + Signature (64) + App Data
|
||||
// Public Key (32) + Signing Key (32) + Name Hash (10) + Random Hash (10) + Ratchet (32) + Signature (64) + App Data
|
||||
|
||||
if len(packetData) < 148 { // 32 + 32 + 10 + 10 + 64
|
||||
if len(packetData) < 180 { // 32 + 32 + 10 + 10 + 32 + 64
|
||||
return errors.New("announce data too short")
|
||||
}
|
||||
|
||||
@@ -236,16 +242,13 @@ func (a *Announce) HandleAnnounce(data []byte) error {
|
||||
signKey := packetData[32:64]
|
||||
nameHash := packetData[64:74]
|
||||
randomHash := packetData[74:84]
|
||||
|
||||
// The next field could be a ratchet (32 bytes) or signature (64 bytes)
|
||||
// We need to detect this somehow or use a flag
|
||||
// For now, assume no ratchet
|
||||
|
||||
signature := packetData[84:148]
|
||||
appData := packetData[148:]
|
||||
ratchetData := packetData[84:116]
|
||||
signature := packetData[116:180]
|
||||
appData := packetData[180:]
|
||||
|
||||
log.Printf("[DEBUG-7] Announce fields: encKey=%x, signKey=%x", encKey, signKey)
|
||||
log.Printf("[DEBUG-7] Name hash=%x, random hash=%x", nameHash, randomHash)
|
||||
log.Printf("[DEBUG-7] Ratchet=%x", ratchetData[:8])
|
||||
log.Printf("[DEBUG-7] Signature=%x, appDataLen=%d", signature[:8], len(appData))
|
||||
|
||||
// Get the destination hash from header
|
||||
@@ -272,6 +275,7 @@ func (a *Announce) HandleAnnounce(data []byte) error {
|
||||
signedData = append(signedData, signKey...)
|
||||
signedData = append(signedData, nameHash...)
|
||||
signedData = append(signedData, randomHash...)
|
||||
signedData = append(signedData, ratchetData...)
|
||||
signedData = append(signedData, appData...)
|
||||
|
||||
if !announcedIdentity.Verify(signedData, signature) {
|
||||
@@ -328,26 +332,14 @@ func (a *Announce) CreatePacket() []byte {
|
||||
// Announce Data Structure:
|
||||
// [Public Key (32 bytes)][Signing Key (32 bytes)][Name Hash (10 bytes)][Random Hash (10 bytes)][Ratchet (32 bytes)][Signature (64 bytes)][App Data]
|
||||
|
||||
// 1. Create Header
|
||||
header := CreateHeader(
|
||||
IFAC_NONE,
|
||||
HEADER_TYPE_2,
|
||||
0, // No context flag for announce
|
||||
PROP_TYPE_BROADCAST,
|
||||
DEST_TYPE_SINGLE,
|
||||
PACKET_TYPE_ANNOUNCE,
|
||||
a.hops,
|
||||
)
|
||||
|
||||
// 2. Destination Hash
|
||||
destHash := a.identity.Hash()
|
||||
destHash := a.destinationHash
|
||||
if len(destHash) == 0 {
|
||||
}
|
||||
|
||||
// 3. Transport ID (zeros for broadcast announce)
|
||||
transportID := make([]byte, 16)
|
||||
|
||||
// 4. Context Byte (zero for announce)
|
||||
contextByte := byte(0)
|
||||
|
||||
// 5. Announce Data
|
||||
// 5.1 Public Keys
|
||||
pubKey := a.identity.GetPublicKey()
|
||||
@@ -355,8 +347,7 @@ func (a *Announce) CreatePacket() []byte {
|
||||
signKey := pubKey[32:]
|
||||
|
||||
// 5.2 Name Hash
|
||||
appName := fmt.Sprintf("%s.%s", a.config.AppName, a.config.AppAspect)
|
||||
nameHash := sha256.Sum256([]byte(appName))
|
||||
nameHash := sha256.Sum256([]byte(a.destinationName))
|
||||
nameHash10 := nameHash[:10]
|
||||
|
||||
// 5.3 Random Hash
|
||||
@@ -366,25 +357,48 @@ func (a *Announce) CreatePacket() []byte {
|
||||
log.Printf("Error reading random bytes for announce: %v", err)
|
||||
}
|
||||
|
||||
// 5.4 Ratchet
|
||||
ratchetData := make([]byte, 32)
|
||||
// 5.4 Ratchet (only include if exists)
|
||||
var ratchetData []byte
|
||||
currentRatchetKey := a.identity.GetCurrentRatchetKey()
|
||||
if currentRatchetKey != nil {
|
||||
ratchetPub, err := curve25519.X25519(currentRatchetKey, curve25519.Basepoint)
|
||||
if err == nil {
|
||||
ratchetData = make([]byte, 32)
|
||||
copy(ratchetData, ratchetPub)
|
||||
}
|
||||
}
|
||||
|
||||
// Determine context flag based on whether ratchet exists
|
||||
contextFlag := byte(0)
|
||||
if len(ratchetData) > 0 {
|
||||
contextFlag = 1 // FLAG_SET
|
||||
}
|
||||
|
||||
// 1. Create Header (now that we know context flag)
|
||||
header := CreateHeader(
|
||||
IFAC_NONE,
|
||||
HEADER_TYPE_2,
|
||||
contextFlag,
|
||||
PROP_TYPE_BROADCAST,
|
||||
DEST_TYPE_SINGLE,
|
||||
PACKET_TYPE_ANNOUNCE,
|
||||
a.hops,
|
||||
)
|
||||
|
||||
// 4. Context Byte
|
||||
contextByte := byte(0)
|
||||
|
||||
// 5.5 Signature
|
||||
// The signature is calculated over: Dest Hash + Public Keys + Name Hash + Random Hash + Ratchet + App Data
|
||||
// The signature is calculated over: Dest Hash + Public Keys + Name Hash + Random Hash + Ratchet (if exists) + App Data
|
||||
validationData := make([]byte, 0)
|
||||
validationData = append(validationData, destHash...)
|
||||
validationData = append(validationData, encKey...)
|
||||
validationData = append(validationData, signKey...)
|
||||
validationData = append(validationData, nameHash10...)
|
||||
validationData = append(validationData, randomHash...)
|
||||
validationData = append(validationData, ratchetData...)
|
||||
if len(ratchetData) > 0 {
|
||||
validationData = append(validationData, ratchetData...)
|
||||
}
|
||||
validationData = append(validationData, a.appData...)
|
||||
signature := a.identity.Sign(validationData)
|
||||
|
||||
@@ -398,7 +412,9 @@ func (a *Announce) CreatePacket() []byte {
|
||||
packet = append(packet, signKey...)
|
||||
packet = append(packet, nameHash10...)
|
||||
packet = append(packet, randomHash...)
|
||||
packet = append(packet, ratchetData...)
|
||||
if len(ratchetData) > 0 {
|
||||
packet = append(packet, ratchetData...)
|
||||
}
|
||||
packet = append(packet, signature...)
|
||||
packet = append(packet, a.appData...)
|
||||
|
||||
@@ -435,9 +451,9 @@ func NewAnnouncePacket(pubKey []byte, appData []byte, announceID []byte) *Announ
|
||||
}
|
||||
|
||||
// NewAnnounce creates a new announce packet for a destination
|
||||
func NewAnnounce(identity *identity.Identity, appData []byte, ratchetID []byte, pathResponse bool, config *common.ReticulumConfig) (*Announce, error) {
|
||||
log.Printf("[DEBUG-7] Creating new announce: appDataLen=%d, hasRatchet=%v, pathResponse=%v",
|
||||
len(appData), ratchetID != nil, pathResponse)
|
||||
func NewAnnounce(identity *identity.Identity, destinationHash []byte, appData []byte, ratchetID []byte, pathResponse bool, config *common.ReticulumConfig) (*Announce, error) {
|
||||
log.Printf("[DEBUG-7] Creating new announce: destHash=%x, appDataLen=%d, hasRatchet=%v, pathResponse=%v",
|
||||
destinationHash, len(appData), ratchetID != nil, pathResponse)
|
||||
|
||||
if identity == nil {
|
||||
log.Printf("[DEBUG-7] Error: nil identity provided")
|
||||
@@ -448,8 +464,12 @@ func NewAnnounce(identity *identity.Identity, appData []byte, ratchetID []byte,
|
||||
return nil, errors.New("config cannot be nil")
|
||||
}
|
||||
|
||||
destHash := identity.Hash()
|
||||
log.Printf("[DEBUG-7] Generated destination hash: %x", destHash)
|
||||
if len(destinationHash) == 0 {
|
||||
return nil, errors.New("destination hash cannot be empty")
|
||||
}
|
||||
|
||||
destHash := destinationHash
|
||||
log.Printf("[DEBUG-7] Using provided destination hash: %x", destHash)
|
||||
|
||||
a := &Announce{
|
||||
identity: identity,
|
||||
|
||||
@@ -4,6 +4,13 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Destination type constants
|
||||
const (
|
||||
DESTINATION_SINGLE = 0x00
|
||||
DESTINATION_GROUP = 0x01
|
||||
DESTINATION_PLAIN = 0x02
|
||||
)
|
||||
|
||||
// Transport related types
|
||||
type TransportMode byte
|
||||
type PathStatus byte
|
||||
|
||||
116
pkg/debug/debug.go
Normal file
116
pkg/debug/debug.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package debug
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
DEBUG_CRITICAL = 1
|
||||
DEBUG_ERROR = 2
|
||||
DEBUG_INFO = 3
|
||||
DEBUG_VERBOSE = 4
|
||||
DEBUG_TRACE = 5
|
||||
DEBUG_PACKETS = 6
|
||||
DEBUG_ALL = 7
|
||||
)
|
||||
|
||||
var (
|
||||
debugLevel = flag.Int("debug", 3, "debug level (1-7)")
|
||||
logger *slog.Logger
|
||||
initialized bool
|
||||
)
|
||||
|
||||
func Init() {
|
||||
if initialized {
|
||||
return
|
||||
}
|
||||
initialized = true
|
||||
|
||||
var level slog.Level
|
||||
switch {
|
||||
case *debugLevel >= DEBUG_ALL:
|
||||
level = slog.LevelDebug
|
||||
case *debugLevel >= DEBUG_PACKETS:
|
||||
level = slog.LevelDebug
|
||||
case *debugLevel >= DEBUG_TRACE:
|
||||
level = slog.LevelDebug
|
||||
case *debugLevel >= DEBUG_VERBOSE:
|
||||
level = slog.LevelDebug
|
||||
case *debugLevel >= DEBUG_INFO:
|
||||
level = slog.LevelInfo
|
||||
case *debugLevel >= DEBUG_ERROR:
|
||||
level = slog.LevelWarn
|
||||
case *debugLevel >= DEBUG_CRITICAL:
|
||||
level = slog.LevelError
|
||||
default:
|
||||
level = slog.LevelError
|
||||
}
|
||||
|
||||
opts := &slog.HandlerOptions{
|
||||
Level: level,
|
||||
}
|
||||
logger = slog.New(slog.NewTextHandler(os.Stderr, opts))
|
||||
slog.SetDefault(logger)
|
||||
}
|
||||
|
||||
func GetLogger() *slog.Logger {
|
||||
if !initialized {
|
||||
Init()
|
||||
}
|
||||
return logger
|
||||
}
|
||||
|
||||
func Log(level int, msg string, args ...interface{}) {
|
||||
if !initialized {
|
||||
Init()
|
||||
}
|
||||
|
||||
if *debugLevel < level {
|
||||
return
|
||||
}
|
||||
|
||||
var slogLevel slog.Level
|
||||
switch {
|
||||
case level >= DEBUG_ALL:
|
||||
slogLevel = slog.LevelDebug
|
||||
case level >= DEBUG_PACKETS:
|
||||
slogLevel = slog.LevelDebug
|
||||
case level >= DEBUG_TRACE:
|
||||
slogLevel = slog.LevelDebug
|
||||
case level >= DEBUG_VERBOSE:
|
||||
slogLevel = slog.LevelDebug
|
||||
case level >= DEBUG_INFO:
|
||||
slogLevel = slog.LevelInfo
|
||||
case level >= DEBUG_ERROR:
|
||||
slogLevel = slog.LevelWarn
|
||||
case level >= DEBUG_CRITICAL:
|
||||
slogLevel = slog.LevelError
|
||||
default:
|
||||
slogLevel = slog.LevelError
|
||||
}
|
||||
|
||||
if !logger.Enabled(context.TODO(), slogLevel) {
|
||||
return
|
||||
}
|
||||
|
||||
allArgs := make([]interface{}, len(args)+2)
|
||||
copy(allArgs, args)
|
||||
allArgs[len(args)] = "debug_level"
|
||||
allArgs[len(args)+1] = level
|
||||
logger.Log(context.TODO(), slogLevel, msg, allArgs...)
|
||||
}
|
||||
|
||||
func SetDebugLevel(level int) {
|
||||
*debugLevel = level
|
||||
if initialized {
|
||||
Init()
|
||||
}
|
||||
}
|
||||
|
||||
func GetDebugLevel() int {
|
||||
return *debugLevel
|
||||
}
|
||||
|
||||
@@ -4,19 +4,24 @@ import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/announce"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/debug"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/identity"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/transport"
|
||||
)
|
||||
|
||||
const (
|
||||
// Destination direction types
|
||||
// The IN bit specifies that the destination can receive traffic.
|
||||
// The OUT bit specifies that the destination can send traffic.
|
||||
// A destination can be both IN and OUT.
|
||||
IN = 0x01
|
||||
OUT = 0x02
|
||||
|
||||
// Destination types
|
||||
SINGLE = 0x00
|
||||
GROUP = 0x01
|
||||
PLAIN = 0x02
|
||||
@@ -31,15 +36,6 @@ const (
|
||||
|
||||
RATCHET_COUNT = 512 // Default number of retained ratchet keys
|
||||
RATCHET_INTERVAL = 1800 // Minimum interval between ratchet rotations in seconds
|
||||
|
||||
// Debug levels
|
||||
DEBUG_CRITICAL = 1 // Critical errors
|
||||
DEBUG_ERROR = 2 // Non-critical errors
|
||||
DEBUG_INFO = 3 // Important information
|
||||
DEBUG_VERBOSE = 4 // Detailed information
|
||||
DEBUG_TRACE = 5 // Very detailed tracing
|
||||
DEBUG_PACKETS = 6 // Packet-level details
|
||||
DEBUG_ALL = 7 // Everything
|
||||
)
|
||||
|
||||
type PacketCallback = common.PacketCallback
|
||||
@@ -81,15 +77,11 @@ type Destination struct {
|
||||
requestHandlers map[string]*RequestHandler
|
||||
}
|
||||
|
||||
func debugLog(level int, format string, v ...interface{}) {
|
||||
log.Printf("[DEBUG-%d] %s", level, fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func New(id *identity.Identity, direction byte, destType byte, appName string, transport *transport.Transport, aspects ...string) (*Destination, error) {
|
||||
debugLog(DEBUG_INFO, "Creating new destination: app=%s type=%d direction=%d", appName, destType, direction)
|
||||
debug.Log(debug.DEBUG_INFO, "Creating new destination", "app", appName, "type", destType, "direction", direction)
|
||||
|
||||
if id == nil {
|
||||
debugLog(DEBUG_ERROR, "Cannot create destination: identity is nil")
|
||||
debug.Log(debug.DEBUG_ERROR, "Cannot create destination: identity is nil")
|
||||
return nil, errors.New("identity cannot be nil")
|
||||
}
|
||||
|
||||
@@ -109,27 +101,62 @@ func New(id *identity.Identity, direction byte, destType byte, appName string, t
|
||||
|
||||
// Generate destination hash
|
||||
d.hashValue = d.calculateHash()
|
||||
debugLog(DEBUG_VERBOSE, "Created destination with hash: %x", d.hashValue)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Created destination with hash", "hash", fmt.Sprintf("%x", d.hashValue))
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// FromHash creates a destination from a known hash (e.g., from an announce).
|
||||
// This is used by clients to create destination objects for servers they've discovered.
|
||||
func FromHash(hash []byte, id *identity.Identity, destType byte, transport *transport.Transport) (*Destination, error) {
|
||||
debug.Log(debug.DEBUG_INFO, "Creating destination from hash", "hash", fmt.Sprintf("%x", hash))
|
||||
|
||||
if id == nil {
|
||||
debug.Log(debug.DEBUG_ERROR, "Cannot create destination: identity is nil")
|
||||
return nil, errors.New("identity cannot be nil")
|
||||
}
|
||||
|
||||
d := &Destination{
|
||||
identity: id,
|
||||
direction: OUT,
|
||||
destType: destType,
|
||||
hashValue: hash,
|
||||
transport: transport,
|
||||
acceptsLinks: false,
|
||||
proofStrategy: PROVE_NONE,
|
||||
ratchetCount: RATCHET_COUNT,
|
||||
ratchetInterval: RATCHET_INTERVAL,
|
||||
requestHandlers: make(map[string]*RequestHandler),
|
||||
}
|
||||
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Created destination from hash", "hash", fmt.Sprintf("%x", hash))
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Destination) calculateHash() []byte {
|
||||
debugLog(DEBUG_TRACE, "Calculating hash for destination %s", d.ExpandName())
|
||||
debug.Log(debug.DEBUG_TRACE, "Calculating hash for destination", "name", d.ExpandName())
|
||||
|
||||
nameHash := sha256.Sum256([]byte(d.ExpandName()))
|
||||
identityHash := sha256.Sum256(d.identity.GetPublicKey())
|
||||
// destination_hash = SHA256(name_hash_10bytes + identity_hash_16bytes)[:16]
|
||||
// Identity hash is the truncated hash of the public key (16 bytes)
|
||||
identityHash := identity.TruncatedHash(d.identity.GetPublicKey())
|
||||
|
||||
// Name hash is the FULL 32-byte SHA256, then we take first 10 bytes for concatenation
|
||||
nameHashFull := sha256.Sum256([]byte(d.ExpandName()))
|
||||
nameHash10 := nameHashFull[:10] // Only use 10 bytes
|
||||
|
||||
debugLog(DEBUG_ALL, "Name hash: %x", nameHash)
|
||||
debugLog(DEBUG_ALL, "Identity hash: %x", identityHash)
|
||||
debug.Log(debug.DEBUG_ALL, "Identity hash", "hash", fmt.Sprintf("%x", identityHash))
|
||||
debug.Log(debug.DEBUG_ALL, "Name hash (10 bytes)", "hash", fmt.Sprintf("%x", nameHash10))
|
||||
|
||||
combined := append(nameHash[:], identityHash[:]...)
|
||||
finalHash := sha256.Sum256(combined)
|
||||
// Concatenate name_hash (10 bytes) + identity_hash (16 bytes) = 26 bytes
|
||||
combined := append(nameHash10, identityHash...)
|
||||
|
||||
// Then hash again and truncate to 16 bytes
|
||||
finalHashFull := sha256.Sum256(combined)
|
||||
finalHash := finalHashFull[:16]
|
||||
|
||||
truncated := finalHash[:16]
|
||||
debugLog(DEBUG_VERBOSE, "Calculated destination hash: %x", truncated)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Calculated destination hash", "hash", fmt.Sprintf("%x", finalHash))
|
||||
|
||||
return truncated
|
||||
return finalHash
|
||||
}
|
||||
|
||||
func (d *Destination) ExpandName() string {
|
||||
@@ -144,38 +171,46 @@ func (d *Destination) Announce(appData []byte) error {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
|
||||
log.Printf("[DEBUG-4] Announcing destination %s", d.ExpandName())
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Announcing destination", "name", d.ExpandName())
|
||||
|
||||
if appData == nil {
|
||||
appData = d.defaultAppData
|
||||
}
|
||||
|
||||
// Create a new Announce instance
|
||||
announce, err := announce.New(d.identity, appData, false, d.transport.GetConfig())
|
||||
// Create announce packet using announce package
|
||||
// Pass the destination hash, name, and app data
|
||||
announce, err := announce.New(d.identity, d.hashValue, d.ExpandName(), appData, false, d.transport.GetConfig())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create announce: %w", err)
|
||||
}
|
||||
|
||||
// Get the packet from the announce instance
|
||||
packet := announce.GetPacket()
|
||||
if packet == nil {
|
||||
return errors.New("failed to create announce packet")
|
||||
}
|
||||
|
||||
// Send announce packet to all interfaces
|
||||
log.Printf("[DEBUG-4] Sending announce packet to all interfaces")
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Sending announce packet to all interfaces")
|
||||
if d.transport == nil {
|
||||
return errors.New("transport not initialized")
|
||||
}
|
||||
|
||||
interfaces := d.transport.GetInterfaces()
|
||||
debug.Log(debug.DEBUG_ALL, "Got interfaces from transport", "count", len(interfaces))
|
||||
|
||||
var lastErr error
|
||||
for _, iface := range interfaces {
|
||||
for name, iface := range interfaces {
|
||||
debug.Log(debug.DEBUG_ALL, "Checking interface", "name", name, "enabled", iface.IsEnabled(), "online", iface.IsOnline())
|
||||
if iface.IsEnabled() && iface.IsOnline() {
|
||||
debug.Log(debug.DEBUG_ALL, "Sending announce to interface", "name", name, "bytes", len(packet))
|
||||
if err := iface.Send(packet, ""); err != nil {
|
||||
log.Printf("[ERROR] Failed to send announce on interface %s: %v", iface.GetName(), err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Failed to send announce on interface", "name", name, "error", err)
|
||||
lastErr = err
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_ALL, "Successfully sent announce to interface", "name", name)
|
||||
}
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_ALL, "Skipping interface", "name", name, "reason", "not enabled or not online")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,6 +221,12 @@ func (d *Destination) AcceptsLinks(accepts bool) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
d.acceptsLinks = accepts
|
||||
|
||||
// Register with transport if accepting links
|
||||
if accepts && d.transport != nil {
|
||||
d.transport.RegisterDestination(d.hashValue, d)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Destination registered with transport for link requests", "hash", fmt.Sprintf("%x", d.hashValue))
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Destination) SetLinkEstablishedCallback(callback common.LinkEstablishedCallback) {
|
||||
@@ -194,6 +235,31 @@ func (d *Destination) SetLinkEstablishedCallback(callback common.LinkEstablished
|
||||
d.linkCallback = callback
|
||||
}
|
||||
|
||||
func (d *Destination) GetLinkCallback() common.LinkEstablishedCallback {
|
||||
d.mutex.RLock()
|
||||
defer d.mutex.RUnlock()
|
||||
return d.linkCallback
|
||||
}
|
||||
|
||||
func (d *Destination) HandleIncomingLinkRequest(linkID []byte, transport interface{}, networkIface common.NetworkInterface) error {
|
||||
debug.Log(debug.DEBUG_INFO, "Handling incoming link request for destination", "hash", fmt.Sprintf("%x", d.GetHash()))
|
||||
|
||||
// Import link package here to avoid circular dependency at package level
|
||||
// We'll use dynamic import by having the caller create the link
|
||||
// For now, just call the callback with a placeholder
|
||||
|
||||
if d.linkCallback != nil {
|
||||
debug.Log(debug.DEBUG_INFO, "Calling link established callback")
|
||||
// Pass linkID as the link object for now
|
||||
// The callback will need to handle creating the actual link
|
||||
d.linkCallback(linkID)
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_VERBOSE, "No link callback set")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Destination) SetPacketCallback(callback common.PacketCallback) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
@@ -300,32 +366,32 @@ func (d *Destination) DeregisterRequestHandler(path string) bool {
|
||||
|
||||
func (d *Destination) Encrypt(plaintext []byte) ([]byte, error) {
|
||||
if d.destType == PLAIN {
|
||||
log.Printf("[DEBUG-4] Using plaintext transmission for PLAIN destination")
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Using plaintext transmission for PLAIN destination")
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
if d.identity == nil {
|
||||
log.Printf("[DEBUG-3] Cannot encrypt: no identity available")
|
||||
debug.Log(debug.DEBUG_INFO, "Cannot encrypt: no identity available")
|
||||
return nil, errors.New("no identity available for encryption")
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-4] Encrypting %d bytes for destination type %d", len(plaintext), d.destType)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Encrypting bytes for destination", "bytes", len(plaintext), "destType", d.destType)
|
||||
|
||||
switch d.destType {
|
||||
case SINGLE:
|
||||
recipientKey := d.identity.GetPublicKey()
|
||||
log.Printf("[DEBUG-4] Encrypting for single recipient with key %x", recipientKey[:8])
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Encrypting for single recipient", "key", fmt.Sprintf("%x", recipientKey[:8]))
|
||||
return d.identity.Encrypt(plaintext, recipientKey)
|
||||
case GROUP:
|
||||
key := d.identity.GetCurrentRatchetKey()
|
||||
if key == nil {
|
||||
log.Printf("[DEBUG-3] Cannot encrypt: no ratchet key available")
|
||||
debug.Log(debug.DEBUG_INFO, "Cannot encrypt: no ratchet key available")
|
||||
return nil, errors.New("no ratchet key available")
|
||||
}
|
||||
log.Printf("[DEBUG-4] Encrypting for group with ratchet key %x", key[:8])
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Encrypting for group with ratchet key", "key", fmt.Sprintf("%x", key[:8]))
|
||||
return d.identity.EncryptWithHMAC(plaintext, key)
|
||||
default:
|
||||
log.Printf("[DEBUG-3] Unsupported destination type %d for encryption", d.destType)
|
||||
debug.Log(debug.DEBUG_INFO, "Unsupported destination type for encryption", "destType", d.destType)
|
||||
return nil, errors.New("unsupported destination type for encryption")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/cryptography"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/debug"
|
||||
"golang.org/x/crypto/curve25519"
|
||||
"golang.org/x/crypto/hkdf"
|
||||
)
|
||||
@@ -44,7 +44,7 @@ const (
|
||||
type Identity struct {
|
||||
privateKey []byte
|
||||
publicKey []byte
|
||||
signingKey ed25519.PrivateKey
|
||||
signingSeed []byte // 32-byte Ed25519 seed (compatible with Python RNS)
|
||||
verificationKey ed25519.PublicKey
|
||||
hash []byte
|
||||
hexHash string
|
||||
@@ -76,13 +76,18 @@ func New() (*Identity, error) {
|
||||
i.privateKey = privKey
|
||||
i.publicKey = pubKey
|
||||
|
||||
// Generate Ed25519 signing keypair
|
||||
verificationKey, signingKey, err := cryptography.GenerateSigningKeyPair()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate Ed25519 keypair: %v", err)
|
||||
// Generate 32-byte Ed25519 seed (compatible with Python RNS)
|
||||
var ed25519Seed [32]byte
|
||||
if _, err := io.ReadFull(rand.Reader, ed25519Seed[:]); err != nil {
|
||||
return nil, fmt.Errorf("failed to generate Ed25519 seed: %v", err)
|
||||
}
|
||||
i.signingKey = signingKey
|
||||
i.verificationKey = verificationKey
|
||||
|
||||
// Derive Ed25519 keypair from seed
|
||||
privKeyEd := ed25519.NewKeyFromSeed(ed25519Seed[:])
|
||||
pubKeyEd := privKeyEd.Public().(ed25519.PublicKey)
|
||||
|
||||
i.signingSeed = ed25519Seed[:]
|
||||
i.verificationKey = pubKeyEd
|
||||
|
||||
return i, nil
|
||||
}
|
||||
@@ -96,11 +101,13 @@ func (i *Identity) GetPublicKey() []byte {
|
||||
}
|
||||
|
||||
func (i *Identity) GetPrivateKey() []byte {
|
||||
return append(i.privateKey, i.signingKey...)
|
||||
return append(i.privateKey, i.signingSeed...)
|
||||
}
|
||||
|
||||
func (i *Identity) Sign(data []byte) []byte {
|
||||
return cryptography.Sign(i.signingKey, data)
|
||||
// Derive Ed25519 private key from seed (compatible with Python RNS)
|
||||
privKey := ed25519.NewKeyFromSeed(i.signingSeed)
|
||||
return cryptography.Sign(privKey, data)
|
||||
}
|
||||
|
||||
func (i *Identity) Verify(data []byte, signature []byte) bool {
|
||||
@@ -166,7 +173,7 @@ func GetRandomHash() []byte {
|
||||
randomData := make([]byte, TRUNCATED_HASHLENGTH/8)
|
||||
_, err := rand.Read(randomData) // #nosec G104
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to read random data for hash: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to read random data for hash", "error", err)
|
||||
return nil // Or handle the error appropriately
|
||||
}
|
||||
return TruncatedHash(randomData)
|
||||
@@ -232,9 +239,18 @@ func (i *Identity) String() string {
|
||||
}
|
||||
|
||||
func Recall(hash []byte) (*Identity, error) {
|
||||
// TODO: Implement persistence
|
||||
// For now just create new identity
|
||||
return New()
|
||||
hashStr := hex.EncodeToString(hash)
|
||||
|
||||
if data, exists := knownDestinations[hashStr]; exists {
|
||||
// data is [packet, destHash, identity, appData]
|
||||
if len(data) >= 3 {
|
||||
if id, ok := data[2].(*Identity); ok {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("identity not found for hash %x", hash)
|
||||
}
|
||||
|
||||
func (i *Identity) GenerateHMACKey() []byte {
|
||||
@@ -263,13 +279,13 @@ func (i *Identity) GetCurrentRatchetKey() []byte {
|
||||
if len(i.ratchets) == 0 {
|
||||
// If no ratchets exist, generate one.
|
||||
// This should ideally be handled by an explicit setup process.
|
||||
log.Println("[DEBUG-5] No ratchets found, generating a new one on-the-fly.")
|
||||
debug.Log(debug.DEBUG_TRACE, "No ratchets found, generating a new one on-the-fly")
|
||||
// Temporarily unlock to call RotateRatchet, which locks internally.
|
||||
i.mutex.RUnlock()
|
||||
newRatchet, err := i.RotateRatchet()
|
||||
i.mutex.RLock()
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to generate initial ratchet key: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to generate initial ratchet key", "error", err)
|
||||
return nil
|
||||
}
|
||||
return newRatchet
|
||||
@@ -286,7 +302,7 @@ func (i *Identity) GetCurrentRatchetKey() []byte {
|
||||
}
|
||||
|
||||
if latestKey == nil {
|
||||
log.Printf("[DEBUG-2] Could not determine the latest ratchet key from %d ratchets.", len(i.ratchets))
|
||||
debug.Log(debug.DEBUG_ERROR, "Could not determine the latest ratchet key", "ratchet_count", len(i.ratchets))
|
||||
}
|
||||
|
||||
return latestKey
|
||||
@@ -294,22 +310,27 @@ func (i *Identity) GetCurrentRatchetKey() []byte {
|
||||
|
||||
func (i *Identity) Decrypt(ciphertextToken []byte, ratchets [][]byte, enforceRatchets bool, ratchetIDReceiver *common.RatchetIDReceiver) ([]byte, error) {
|
||||
if i.privateKey == nil {
|
||||
log.Printf("[DEBUG-1] Decryption failed: identity has no private key")
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Decryption failed: identity has no private key")
|
||||
return nil, errors.New("decryption failed because identity does not hold a private key")
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Starting decryption for identity %s", i.GetHexHash())
|
||||
debug.Log(debug.DEBUG_ALL, "Starting decryption for identity", "hash", i.GetHexHash())
|
||||
if len(ratchets) > 0 {
|
||||
log.Printf("[DEBUG-7] Attempting decryption with %d ratchets", len(ratchets))
|
||||
debug.Log(debug.DEBUG_ALL, "Attempting decryption with ratchets", "count", len(ratchets))
|
||||
}
|
||||
|
||||
if len(ciphertextToken) <= KEYSIZE/8/2 {
|
||||
return nil, errors.New("decryption failed because the token size was invalid")
|
||||
}
|
||||
|
||||
// Extract peer public key and ciphertext
|
||||
peerPubBytes := ciphertextToken[:KEYSIZE/8/2]
|
||||
ciphertext := ciphertextToken[KEYSIZE/8/2:]
|
||||
// Extract components: ephemeralPubKey(32) + ciphertext + mac(32)
|
||||
if len(ciphertextToken) < 32+32+32 { // minimum sizes
|
||||
return nil, errors.New("token too short")
|
||||
}
|
||||
|
||||
peerPubBytes := ciphertextToken[:32]
|
||||
ciphertext := ciphertextToken[32 : len(ciphertextToken)-32]
|
||||
mac := ciphertextToken[len(ciphertextToken)-32:]
|
||||
|
||||
// Try decryption with ratchets first if provided
|
||||
if len(ratchets) > 0 {
|
||||
@@ -343,6 +364,11 @@ func (i *Identity) Decrypt(ciphertextToken []byte, ratchets [][]byte, enforceRat
|
||||
return nil, fmt.Errorf("failed to derive key: %v", err)
|
||||
}
|
||||
|
||||
// Validate HMAC
|
||||
if !cryptography.ValidateHMAC(derivedKey, append(peerPubBytes, ciphertext...), mac) {
|
||||
return nil, errors.New("invalid HMAC")
|
||||
}
|
||||
|
||||
// Create AES cipher
|
||||
block, err := aes.NewCipher(derivedKey)
|
||||
if err != nil {
|
||||
@@ -381,7 +407,7 @@ func (i *Identity) Decrypt(ciphertextToken []byte, ratchets [][]byte, enforceRat
|
||||
ratchetIDReceiver.LatestRatchetID = nil
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Decryption completed successfully")
|
||||
debug.Log(debug.DEBUG_ALL, "Decryption completed successfully")
|
||||
return plaintext[:len(plaintext)-padding], nil
|
||||
}
|
||||
|
||||
@@ -393,7 +419,7 @@ func (i *Identity) tryRatchetDecryption(peerPubBytes, ciphertext, ratchet []byte
|
||||
// Get ratchet ID
|
||||
ratchetPubBytes, err := curve25519.X25519(ratchetPriv, cryptography.GetBasepoint())
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-7] Failed to generate ratchet public key: %v", err)
|
||||
debug.Log(debug.DEBUG_ALL, "Failed to generate ratchet public key", "error", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
ratchetID := i.GetRatchetID(ratchetPubBytes)
|
||||
@@ -443,36 +469,36 @@ func (i *Identity) DecryptWithHMAC(data []byte, key []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (i *Identity) ToFile(path string) error {
|
||||
log.Printf("[DEBUG-7] Saving identity %s to file: %s", i.GetHexHash(), path)
|
||||
debug.Log(debug.DEBUG_ALL, "Saving identity to file", "hash", i.GetHexHash(), "path", path)
|
||||
|
||||
// Persist ratchets to a separate file
|
||||
ratchetPath := path + ".ratchets"
|
||||
if err := i.saveRatchets(ratchetPath); err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to save ratchets: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to save ratchets", "error", err)
|
||||
// Continue saving the main identity file even if ratchets fail
|
||||
}
|
||||
|
||||
data := map[string]interface{}{
|
||||
"private_key": i.privateKey,
|
||||
"public_key": i.publicKey,
|
||||
"signing_key": i.signingKey,
|
||||
"signing_seed": i.signingSeed,
|
||||
"verification_key": i.verificationKey,
|
||||
"app_data": i.appData,
|
||||
}
|
||||
|
||||
file, err := os.Create(path) // #nosec G304
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to create identity file: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to create identity file", "error", err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if err := json.NewEncoder(file).Encode(data); err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to encode identity data: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to encode identity data", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Identity saved successfully")
|
||||
debug.Log(debug.DEBUG_ALL, "Identity saved successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -484,7 +510,7 @@ func (i *Identity) saveRatchets(path string) error {
|
||||
return nil // Nothing to save
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-6] Saving %d ratchets to %s", len(i.ratchets), path)
|
||||
debug.Log(debug.DEBUG_PACKETS, "Saving ratchets", "count", len(i.ratchets), "path", path)
|
||||
data := map[string]interface{}{
|
||||
"ratchets": i.ratchets,
|
||||
"ratchet_expiry": i.ratchetExpiry,
|
||||
@@ -500,26 +526,40 @@ func (i *Identity) saveRatchets(path string) error {
|
||||
}
|
||||
|
||||
func RecallIdentity(path string) (*Identity, error) {
|
||||
log.Printf("[DEBUG-7] Attempting to recall identity from: %s", path)
|
||||
debug.Log(debug.DEBUG_ALL, "Attempting to recall identity", "path", path)
|
||||
|
||||
file, err := os.Open(path) // #nosec G304
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to open identity file: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to open identity file", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var data map[string]interface{}
|
||||
if err := json.NewDecoder(file).Decode(&data); err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to decode identity data: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to decode identity data", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var signingSeed []byte
|
||||
var verificationKey ed25519.PublicKey
|
||||
|
||||
if seedData, exists := data["signing_seed"]; exists {
|
||||
signingSeed = seedData.([]byte)
|
||||
verificationKey = data["verification_key"].(ed25519.PublicKey)
|
||||
} else if keyData, exists := data["signing_key"]; exists {
|
||||
oldKey := keyData.(ed25519.PrivateKey)
|
||||
signingSeed = oldKey[:32]
|
||||
verificationKey = data["verification_key"].(ed25519.PublicKey)
|
||||
} else {
|
||||
return nil, fmt.Errorf("no signing key data found in identity file")
|
||||
}
|
||||
|
||||
id := &Identity{
|
||||
privateKey: data["private_key"].([]byte),
|
||||
publicKey: data["public_key"].([]byte),
|
||||
signingKey: data["signing_key"].(ed25519.PrivateKey),
|
||||
verificationKey: data["verification_key"].(ed25519.PublicKey),
|
||||
signingSeed: signingSeed,
|
||||
verificationKey: verificationKey,
|
||||
appData: data["app_data"].([]byte),
|
||||
ratchets: make(map[string][]byte),
|
||||
ratchetExpiry: make(map[string]int64),
|
||||
@@ -529,11 +569,11 @@ func RecallIdentity(path string) (*Identity, error) {
|
||||
// Load ratchets if they exist
|
||||
ratchetPath := path + ".ratchets"
|
||||
if err := id.loadRatchets(ratchetPath); err != nil {
|
||||
log.Printf("[DEBUG-2] Could not load ratchets for identity %s: %v", id.GetHexHash(), err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Could not load ratchets for identity", "hash", id.GetHexHash(), "error", err)
|
||||
// This is not a fatal error, the identity can still function
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Successfully recalled identity with hash: %s", id.GetHexHash())
|
||||
debug.Log(debug.DEBUG_ALL, "Successfully recalled identity", "hash", id.GetHexHash())
|
||||
return id, nil
|
||||
}
|
||||
|
||||
@@ -544,7 +584,7 @@ func (i *Identity) loadRatchets(path string) error {
|
||||
file, err := os.Open(path) // #nosec G304
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("[DEBUG-6] No ratchet file found at %s, skipping.", path)
|
||||
debug.Log(debug.DEBUG_PACKETS, "No ratchet file found, skipping", "path", path)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to open ratchet file: %w", err)
|
||||
@@ -572,7 +612,7 @@ func (i *Identity) loadRatchets(path string) error {
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-6] Loaded %d ratchets from %s", len(i.ratchets), path)
|
||||
debug.Log(debug.DEBUG_PACKETS, "Loaded ratchets", "count", len(i.ratchets), "path", path)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -628,12 +668,16 @@ func (i *Identity) SetRatchetKey(id string, key []byte) {
|
||||
|
||||
// NewIdentity creates a new Identity instance with fresh keys
|
||||
func NewIdentity() (*Identity, error) {
|
||||
// Generate Ed25519 signing keypair
|
||||
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate Ed25519 keypair: %v", err)
|
||||
// Generate 32-byte Ed25519 seed (compatible with Python RNS)
|
||||
var ed25519Seed [32]byte
|
||||
if _, err := io.ReadFull(rand.Reader, ed25519Seed[:]); err != nil {
|
||||
return nil, fmt.Errorf("failed to generate Ed25519 seed: %v", err)
|
||||
}
|
||||
|
||||
// Derive Ed25519 keypair from seed
|
||||
privKey := ed25519.NewKeyFromSeed(ed25519Seed[:])
|
||||
pubKey := privKey.Public().(ed25519.PublicKey)
|
||||
|
||||
// Generate X25519 encryption keypair
|
||||
var encPrivKey [32]byte
|
||||
if _, err := io.ReadFull(rand.Reader, encPrivKey[:]); err != nil {
|
||||
@@ -648,7 +692,7 @@ func NewIdentity() (*Identity, error) {
|
||||
i := &Identity{
|
||||
privateKey: encPrivKey[:],
|
||||
publicKey: encPubKey,
|
||||
signingKey: privKey,
|
||||
signingSeed: ed25519Seed[:],
|
||||
verificationKey: pubKey,
|
||||
ratchets: make(map[string][]byte),
|
||||
ratchetExpiry: make(map[string]int64),
|
||||
@@ -669,19 +713,19 @@ func (i *Identity) RotateRatchet() ([]byte, error) {
|
||||
i.mutex.Lock()
|
||||
defer i.mutex.Unlock()
|
||||
|
||||
log.Printf("[DEBUG-7] Rotating ratchet for identity %s", i.GetHexHash())
|
||||
debug.Log(debug.DEBUG_ALL, "Rotating ratchet for identity", "hash", i.GetHexHash())
|
||||
|
||||
// Generate new ratchet key
|
||||
newRatchet := make([]byte, RATCHETSIZE/8)
|
||||
if _, err := io.ReadFull(rand.Reader, newRatchet); err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to generate new ratchet: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to generate new ratchet", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get public key for ratchet ID
|
||||
ratchetPub, err := curve25519.X25519(newRatchet, curve25519.Basepoint)
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-1] Failed to generate ratchet public key: %v", err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Failed to generate ratchet public key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -692,7 +736,7 @@ func (i *Identity) RotateRatchet() ([]byte, error) {
|
||||
i.ratchets[string(ratchetID)] = newRatchet
|
||||
i.ratchetExpiry[string(ratchetID)] = expiry
|
||||
|
||||
log.Printf("[DEBUG-7] New ratchet generated with ID: %x, expiry: %d", ratchetID, expiry)
|
||||
debug.Log(debug.DEBUG_ALL, "New ratchet generated", "id", fmt.Sprintf("%x", ratchetID), "expiry", expiry)
|
||||
|
||||
// Cleanup old ratchets if we exceed max retained
|
||||
if len(i.ratchets) > MAX_RETAINED_RATCHETS {
|
||||
@@ -708,10 +752,10 @@ func (i *Identity) RotateRatchet() ([]byte, error) {
|
||||
|
||||
delete(i.ratchets, oldestID)
|
||||
delete(i.ratchetExpiry, oldestID)
|
||||
log.Printf("[DEBUG-7] Cleaned up oldest ratchet with ID: %x", []byte(oldestID))
|
||||
debug.Log(debug.DEBUG_ALL, "Cleaned up oldest ratchet", "id", fmt.Sprintf("%x", []byte(oldestID)))
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Current number of active ratchets: %d", len(i.ratchets))
|
||||
debug.Log(debug.DEBUG_ALL, "Current number of active ratchets", "count", len(i.ratchets))
|
||||
return newRatchet, nil
|
||||
}
|
||||
|
||||
@@ -719,7 +763,7 @@ func (i *Identity) GetRatchets() [][]byte {
|
||||
i.mutex.RLock()
|
||||
defer i.mutex.RUnlock()
|
||||
|
||||
log.Printf("[DEBUG-7] Getting ratchets for identity %s", i.GetHexHash())
|
||||
debug.Log(debug.DEBUG_ALL, "Getting ratchets for identity", "hash", i.GetHexHash())
|
||||
|
||||
ratchets := make([][]byte, 0, len(i.ratchets))
|
||||
now := time.Now().Unix()
|
||||
@@ -737,7 +781,7 @@ func (i *Identity) GetRatchets() [][]byte {
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Retrieved %d active ratchets, cleaned up %d expired", len(ratchets), expired)
|
||||
debug.Log(debug.DEBUG_ALL, "Retrieved active ratchets", "active", len(ratchets), "expired", expired)
|
||||
return ratchets
|
||||
}
|
||||
|
||||
@@ -745,7 +789,7 @@ func (i *Identity) CleanupExpiredRatchets() {
|
||||
i.mutex.Lock()
|
||||
defer i.mutex.Unlock()
|
||||
|
||||
log.Printf("[DEBUG-7] Starting ratchet cleanup for identity %s", i.GetHexHash())
|
||||
debug.Log(debug.DEBUG_ALL, "Starting ratchet cleanup for identity", "hash", i.GetHexHash())
|
||||
|
||||
now := time.Now().Unix()
|
||||
cleaned := 0
|
||||
@@ -757,7 +801,7 @@ func (i *Identity) CleanupExpiredRatchets() {
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Cleaned up %d expired ratchets, %d remaining", cleaned, len(i.ratchets))
|
||||
debug.Log(debug.DEBUG_ALL, "Cleaned up expired ratchets", "cleaned", cleaned, "remaining", len(i.ratchets))
|
||||
}
|
||||
|
||||
// ValidateAnnounce validates an announce packet's signature
|
||||
|
||||
@@ -97,6 +97,10 @@ func (ai *AutoInterface) Start() error {
|
||||
return fmt.Errorf("no suitable interfaces found")
|
||||
}
|
||||
|
||||
// Mark interface as online
|
||||
ai.Online = true
|
||||
ai.Enabled = true
|
||||
|
||||
go ai.peerJobs()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ package interfaces
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/debug"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -26,17 +26,6 @@ const (
|
||||
TYPE_TCP = 0x02
|
||||
|
||||
PROPAGATION_RATE = 0.02 // 2% of interface bandwidth
|
||||
|
||||
DEBUG_LEVEL = 4 // Default debug level for interface logging
|
||||
|
||||
// Debug levels
|
||||
DEBUG_CRITICAL = 1
|
||||
DEBUG_ERROR = 2
|
||||
DEBUG_INFO = 3
|
||||
DEBUG_VERBOSE = 4
|
||||
DEBUG_TRACE = 5
|
||||
DEBUG_PACKETS = 6
|
||||
DEBUG_ALL = 7
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
@@ -127,7 +116,7 @@ func (i *BaseInterface) ProcessIncoming(data []byte) {
|
||||
|
||||
func (i *BaseInterface) ProcessOutgoing(data []byte) error {
|
||||
if !i.Online || i.Detached {
|
||||
log.Printf("[DEBUG-1] Interface %s: Cannot process outgoing packet - interface offline or detached", i.Name)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Interface cannot process outgoing packet - interface offline or detached", "name", i.Name)
|
||||
return fmt.Errorf("interface offline or detached")
|
||||
}
|
||||
|
||||
@@ -135,7 +124,7 @@ func (i *BaseInterface) ProcessOutgoing(data []byte) error {
|
||||
i.TxBytes += uint64(len(data))
|
||||
i.mutex.Unlock()
|
||||
|
||||
log.Printf("[DEBUG-%d] Interface %s: Processed outgoing packet of %d bytes, total TX: %d", DEBUG_LEVEL, i.Name, len(data), i.TxBytes)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Interface processed outgoing packet", "name", i.Name, "bytes", len(data), "total_tx", i.TxBytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -189,7 +178,7 @@ func (i *BaseInterface) Enable() {
|
||||
i.Enabled = true
|
||||
i.Online = true
|
||||
|
||||
log.Printf("[DEBUG-%d] Interface %s: State changed - Enabled: %v->%v, Online: %v->%v", DEBUG_INFO, i.Name, prevState, i.Enabled, !i.Online, i.Online)
|
||||
debug.Log(debug.DEBUG_INFO, "Interface state changed", "name", i.Name, "enabled_prev", prevState, "enabled", i.Enabled, "online_prev", !i.Online, "online", i.Online)
|
||||
}
|
||||
|
||||
func (i *BaseInterface) Disable() {
|
||||
@@ -197,7 +186,7 @@ func (i *BaseInterface) Disable() {
|
||||
defer i.mutex.Unlock()
|
||||
i.Enabled = false
|
||||
i.Online = false
|
||||
log.Printf("[DEBUG-2] Interface %s: Disabled and offline", i.Name)
|
||||
debug.Log(debug.DEBUG_ERROR, "Interface disabled and offline", "name", i.Name)
|
||||
}
|
||||
|
||||
func (i *BaseInterface) GetName() string {
|
||||
@@ -237,11 +226,11 @@ func (i *BaseInterface) Stop() error {
|
||||
}
|
||||
|
||||
func (i *BaseInterface) Send(data []byte, address string) error {
|
||||
log.Printf("[DEBUG-%d] Interface %s: Sending %d bytes to %s", DEBUG_LEVEL, i.Name, len(data), address)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Interface sending bytes", "name", i.Name, "bytes", len(data), "address", address)
|
||||
|
||||
err := i.ProcessOutgoing(data)
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-1] Interface %s: Failed to send data: %v", i.Name, err)
|
||||
debug.Log(debug.DEBUG_CRITICAL, "Interface failed to send data", "name", i.Name, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -261,7 +250,7 @@ func (i *BaseInterface) GetBandwidthAvailable() bool {
|
||||
timeSinceLastTx := now.Sub(i.lastTx)
|
||||
|
||||
if timeSinceLastTx > time.Second {
|
||||
log.Printf("[DEBUG-%d] Interface %s: Bandwidth available (idle for %.2fs)", DEBUG_VERBOSE, i.Name, timeSinceLastTx.Seconds())
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Interface bandwidth available", "name", i.Name, "idle_seconds", timeSinceLastTx.Seconds())
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -270,7 +259,7 @@ func (i *BaseInterface) GetBandwidthAvailable() bool {
|
||||
maxUsage := float64(i.Bitrate) * PROPAGATION_RATE
|
||||
|
||||
available := currentUsage < maxUsage
|
||||
log.Printf("[DEBUG-%d] Interface %s: Bandwidth stats - Current: %.2f bps, Max: %.2f bps, Usage: %.1f%%, Available: %v", DEBUG_VERBOSE, i.Name, currentUsage, maxUsage, (currentUsage/maxUsage)*100, available)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Interface bandwidth stats", "name", i.Name, "current_bps", currentUsage, "max_bps", maxUsage, "usage_percent", (currentUsage/maxUsage)*100, "available", available)
|
||||
|
||||
return available
|
||||
}
|
||||
@@ -282,7 +271,7 @@ func (i *BaseInterface) updateBandwidthStats(bytes uint64) {
|
||||
i.TxBytes += bytes
|
||||
i.lastTx = time.Now()
|
||||
|
||||
log.Printf("[DEBUG-%d] Interface %s: Updated bandwidth stats - TX bytes: %d, Last TX: %v", DEBUG_LEVEL, i.Name, i.TxBytes, i.lastTx)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Interface updated bandwidth stats", "name", i.Name, "tx_bytes", i.TxBytes, "last_tx", i.lastTx)
|
||||
}
|
||||
|
||||
type InterceptedInterface struct {
|
||||
@@ -305,7 +294,7 @@ func (i *InterceptedInterface) Send(data []byte, addr string) error {
|
||||
// Call interceptor if provided
|
||||
if i.interceptor != nil && len(data) > 0 {
|
||||
if err := i.interceptor(data, i); err != nil {
|
||||
log.Printf("[DEBUG-2] Failed to intercept outgoing packet: %v", err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Failed to intercept outgoing packet", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,13 @@ package interfaces
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/debug"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -106,11 +106,11 @@ func (tc *TCPClientInterface) Start() error {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
if err := tc.setTimeoutsLinux(); err != nil {
|
||||
log.Printf("[DEBUG-2] Failed to set Linux TCP timeouts: %v", err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Failed to set Linux TCP timeouts", "error", err)
|
||||
}
|
||||
case "darwin":
|
||||
if err := tc.setTimeoutsOSX(); err != nil {
|
||||
log.Printf("[DEBUG-2] Failed to set OSX TCP timeouts: %v", err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Failed to set OSX TCP timeouts", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,53 +143,24 @@ func (tc *TCPClientInterface) readLoop() {
|
||||
for i := 0; i < n; i++ {
|
||||
b := buffer[i]
|
||||
|
||||
if tc.kissFraming {
|
||||
// KISS framing logic
|
||||
if b == KISS_FEND {
|
||||
if inFrame && len(dataBuffer) > 0 {
|
||||
tc.handlePacket(dataBuffer)
|
||||
dataBuffer = dataBuffer[:0]
|
||||
}
|
||||
inFrame = !inFrame
|
||||
continue
|
||||
if b == HDLC_FLAG {
|
||||
if inFrame && len(dataBuffer) > 0 {
|
||||
tc.handlePacket(dataBuffer)
|
||||
dataBuffer = dataBuffer[:0]
|
||||
}
|
||||
inFrame = !inFrame
|
||||
continue
|
||||
}
|
||||
|
||||
if inFrame {
|
||||
if b == KISS_FESC {
|
||||
escape = true
|
||||
} else {
|
||||
if escape {
|
||||
if b == KISS_TFEND {
|
||||
b = KISS_FEND
|
||||
} else if b == KISS_TFESC {
|
||||
b = KISS_FESC
|
||||
}
|
||||
escape = false
|
||||
}
|
||||
dataBuffer = append(dataBuffer, b)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// HDLC framing logic
|
||||
if b == HDLC_FLAG {
|
||||
if inFrame && len(dataBuffer) > 0 {
|
||||
tc.handlePacket(dataBuffer)
|
||||
dataBuffer = dataBuffer[:0]
|
||||
}
|
||||
inFrame = !inFrame
|
||||
continue
|
||||
}
|
||||
|
||||
if inFrame {
|
||||
if b == HDLC_ESC {
|
||||
escape = true
|
||||
} else {
|
||||
if escape {
|
||||
b ^= HDLC_ESC_MASK
|
||||
escape = false
|
||||
}
|
||||
dataBuffer = append(dataBuffer, b)
|
||||
if inFrame {
|
||||
if b == HDLC_ESC {
|
||||
escape = true
|
||||
} else {
|
||||
if escape {
|
||||
b ^= HDLC_ESC_MASK
|
||||
escape = false
|
||||
}
|
||||
dataBuffer = append(dataBuffer, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -198,46 +169,44 @@ func (tc *TCPClientInterface) readLoop() {
|
||||
|
||||
func (tc *TCPClientInterface) handlePacket(data []byte) {
|
||||
if len(data) < 1 {
|
||||
log.Printf("[DEBUG-7] Received invalid packet: empty")
|
||||
debug.Log(debug.DEBUG_ALL, "Received invalid packet: empty")
|
||||
return
|
||||
}
|
||||
|
||||
tc.mutex.Lock()
|
||||
tc.packetType = data[0]
|
||||
tc.RxBytes += uint64(len(data))
|
||||
lastRx := time.Now()
|
||||
tc.lastRx = lastRx
|
||||
tc.mutex.Unlock()
|
||||
|
||||
log.Printf("[DEBUG-7] Received packet: type=0x%02x, size=%d bytes", tc.packetType, len(data))
|
||||
debug.Log(debug.DEBUG_ALL, "Received packet", "type", fmt.Sprintf("0x%02x", data[0]), "size", len(data))
|
||||
|
||||
payload := data[1:]
|
||||
|
||||
switch tc.packetType {
|
||||
case 0x01: // Announce packet
|
||||
log.Printf("[DEBUG-7] Processing announce packet: payload=%d bytes", len(payload))
|
||||
if len(payload) >= 53 {
|
||||
tc.BaseInterface.ProcessIncoming(payload)
|
||||
} else {
|
||||
log.Printf("[DEBUG-7] Announce packet too small: %d bytes", len(payload))
|
||||
}
|
||||
case 0x02: // Link packet
|
||||
log.Printf("[DEBUG-7] Processing link packet: payload=%d bytes", len(payload))
|
||||
if len(payload) < 40 {
|
||||
log.Printf("[DEBUG-7] Link packet too small: %d bytes", len(payload))
|
||||
return
|
||||
}
|
||||
tc.BaseInterface.ProcessIncoming(payload)
|
||||
case 0x03: // Announce packet
|
||||
tc.BaseInterface.ProcessIncoming(payload)
|
||||
case 0x04: // Transport packet
|
||||
tc.BaseInterface.ProcessIncoming(payload)
|
||||
default:
|
||||
// Unknown packet type
|
||||
return
|
||||
// For RNS packets, call the packet callback directly
|
||||
if callback := tc.GetPacketCallback(); callback != nil {
|
||||
debug.Log(debug.DEBUG_ALL, "Calling packet callback for RNS packet")
|
||||
callback(data, tc)
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_ALL, "No packet callback set for TCP interface")
|
||||
}
|
||||
}
|
||||
|
||||
// Send implements the interface Send method for TCP interface
|
||||
func (tc *TCPClientInterface) Send(data []byte, address string) error {
|
||||
debug.Log(debug.DEBUG_ALL, "TCP interface sending bytes", "name", tc.Name, "bytes", len(data))
|
||||
|
||||
if !tc.IsEnabled() || !tc.IsOnline() {
|
||||
return fmt.Errorf("TCP interface %s is not online", tc.Name)
|
||||
}
|
||||
|
||||
// For TCP interface, we need to prepend a packet type byte for announce packets
|
||||
// RNS TCP protocol expects: [packet_type][data]
|
||||
frame := make([]byte, 0, len(data)+1)
|
||||
frame = append(frame, 0x01) // Announce packet type
|
||||
frame = append(frame, data...)
|
||||
|
||||
return tc.ProcessOutgoing(frame)
|
||||
}
|
||||
|
||||
func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error {
|
||||
if !tc.Online {
|
||||
return fmt.Errorf("interface offline")
|
||||
@@ -246,19 +215,19 @@ func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error {
|
||||
tc.writing = true
|
||||
defer func() { tc.writing = false }()
|
||||
|
||||
// For TCP connections, use HDLC framing
|
||||
var frame []byte
|
||||
if tc.kissFraming {
|
||||
frame = append([]byte{KISS_FEND}, escapeKISS(data)...)
|
||||
frame = append(frame, KISS_FEND)
|
||||
} else {
|
||||
frame = append([]byte{HDLC_FLAG}, escapeHDLC(data)...)
|
||||
frame = append(frame, HDLC_FLAG)
|
||||
}
|
||||
frame = append([]byte{HDLC_FLAG}, escapeHDLC(data)...)
|
||||
frame = append(frame, HDLC_FLAG)
|
||||
|
||||
// Update TX stats before sending
|
||||
tc.UpdateStats(uint64(len(frame)), false)
|
||||
|
||||
debug.Log(debug.DEBUG_ALL, "TCP interface writing to network", "name", tc.Name, "bytes", len(frame))
|
||||
_, err := tc.conn.Write(frame)
|
||||
if err != nil {
|
||||
debug.Log(debug.DEBUG_CRITICAL, "TCP interface write failed", "name", tc.Name, "error", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -364,7 +333,7 @@ func (tc *TCPClientInterface) reconnect() {
|
||||
|
||||
// Log reconnection attempt
|
||||
fmt.Printf("Failed to reconnect to %s (attempt %d/%d): %v\n",
|
||||
addr, retries+1, tc.maxReconnectTries, err)
|
||||
net.JoinHostPort(tc.targetAddr, fmt.Sprintf("%d", tc.targetPort)), retries+1, tc.maxReconnectTries, err)
|
||||
|
||||
// Wait with exponential backoff
|
||||
time.Sleep(backoff)
|
||||
@@ -385,7 +354,7 @@ func (tc *TCPClientInterface) reconnect() {
|
||||
// If we've exhausted all retries, perform final teardown
|
||||
tc.teardown()
|
||||
fmt.Printf("Failed to reconnect to %s after %d attempts\n",
|
||||
fmt.Sprintf("%s:%d", tc.targetAddr, tc.targetPort), tc.maxReconnectTries)
|
||||
net.JoinHostPort(tc.targetAddr, fmt.Sprintf("%d", tc.targetPort)), tc.maxReconnectTries)
|
||||
}
|
||||
|
||||
func (tc *TCPClientInterface) Enable() {
|
||||
@@ -421,7 +390,7 @@ func (tc *TCPClientInterface) GetRTT() time.Duration {
|
||||
if err := info.Control(func(fd uintptr) { // #nosec G104
|
||||
rtt = platformGetRTT(fd)
|
||||
}); err != nil {
|
||||
log.Printf("[DEBUG-2] Error in SyscallConn Control: %v", err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Error in SyscallConn Control", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -451,13 +420,11 @@ func (tc *TCPClientInterface) UpdateStats(bytes uint64, isRx bool) {
|
||||
if isRx {
|
||||
tc.RxBytes += bytes
|
||||
tc.lastRx = now
|
||||
log.Printf("[DEBUG-5] Interface %s RX stats: bytes=%d total=%d last=%v",
|
||||
tc.Name, bytes, tc.RxBytes, tc.lastRx)
|
||||
debug.Log(debug.DEBUG_TRACE, "Interface RX stats", "name", tc.Name, "bytes", bytes, "total", tc.RxBytes, "last", tc.lastRx)
|
||||
} else {
|
||||
tc.TxBytes += bytes
|
||||
tc.lastTx = now
|
||||
log.Printf("[DEBUG-5] Interface %s TX stats: bytes=%d total=%d last=%v",
|
||||
tc.Name, bytes, tc.TxBytes, tc.lastTx)
|
||||
debug.Log(debug.DEBUG_TRACE, "Interface TX stats", "name", tc.Name, "bytes", bytes, "total", tc.TxBytes, "last", tc.lastTx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -595,7 +562,7 @@ func (ts *TCPServerInterface) Start() error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", ts.bindAddr, ts.bindPort)
|
||||
addr := net.JoinHostPort(ts.bindAddr, fmt.Sprintf("%d", ts.bindPort))
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to start TCP server: %w", err)
|
||||
@@ -611,7 +578,7 @@ func (ts *TCPServerInterface) Start() error {
|
||||
if !ts.Online {
|
||||
return // Normal shutdown
|
||||
}
|
||||
log.Printf("[DEBUG-2] Error accepting connection: %v", err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Error accepting connection", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -694,8 +661,7 @@ func (ts *TCPServerInterface) ProcessOutgoing(data []byte) error {
|
||||
|
||||
for _, conn := range ts.connections {
|
||||
if _, err := conn.Write(frame); err != nil {
|
||||
log.Printf("[DEBUG-4] Error writing to connection %s: %v",
|
||||
conn.RemoteAddr(), err)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Error writing to connection", "address", conn.RemoteAddr(), "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/debug"
|
||||
)
|
||||
|
||||
type UDPInterface struct {
|
||||
@@ -75,6 +76,8 @@ func (ui *UDPInterface) Detach() {
|
||||
}
|
||||
|
||||
func (ui *UDPInterface) Send(data []byte, addr string) error {
|
||||
debug.Log(debug.DEBUG_ALL, "UDP interface sending bytes", "name", ui.Name, "bytes", len(data))
|
||||
|
||||
if !ui.IsEnabled() {
|
||||
return fmt.Errorf("interface not enabled")
|
||||
}
|
||||
@@ -83,7 +86,17 @@ func (ui *UDPInterface) Send(data []byte, addr string) error {
|
||||
return fmt.Errorf("no target address configured")
|
||||
}
|
||||
|
||||
// Update TX stats before sending
|
||||
ui.mutex.Lock()
|
||||
ui.TxBytes += uint64(len(data))
|
||||
ui.mutex.Unlock()
|
||||
|
||||
_, err := ui.conn.WriteTo(data, ui.targetAddr)
|
||||
if err != nil {
|
||||
debug.Log(debug.DEBUG_CRITICAL, "UDP interface write failed", "name", ui.Name, "error", err)
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_ALL, "UDP interface sent bytes successfully", "name", ui.Name, "bytes", len(data))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -169,27 +182,36 @@ func (ui *UDPInterface) Start() error {
|
||||
}
|
||||
ui.conn = conn
|
||||
ui.Online = true
|
||||
|
||||
// Start the read loop in a goroutine
|
||||
go ui.readLoop()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (ui *UDPInterface) readLoop() {
|
||||
buffer := make([]byte, ui.MTU)
|
||||
for {
|
||||
n, _, err := ui.conn.ReadFromUDP(buffer)
|
||||
buffer := make([]byte, common.DEFAULT_MTU)
|
||||
for ui.IsOnline() && !ui.IsDetached() {
|
||||
n, remoteAddr, err := ui.conn.ReadFromUDP(buffer)
|
||||
if err != nil {
|
||||
if ui.Online {
|
||||
log.Printf("Error reading from UDP interface %s: %v", ui.Name, err)
|
||||
ui.Stop() // Consider if stopping is the right action or just log and continue
|
||||
if ui.IsOnline() {
|
||||
debug.Log(debug.DEBUG_ERROR, "Error reading from UDP interface", "name", ui.Name, "error", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ui.mutex.Lock()
|
||||
if ui.targetAddr == nil {
|
||||
debug.Log(debug.DEBUG_ALL, "UDP interface discovered peer", "name", ui.Name, "peer", remoteAddr.String())
|
||||
ui.targetAddr = remoteAddr
|
||||
}
|
||||
ui.mutex.Unlock()
|
||||
|
||||
if ui.packetCallback != nil {
|
||||
ui.packetCallback(buffer[:n], ui)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func (ui *UDPInterface) IsEnabled() bool {
|
||||
ui.mutex.RLock()
|
||||
|
||||
@@ -135,7 +135,15 @@ func (l *Link) Establish() error {
|
||||
return errors.New("destination has no public key")
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-4] Creating link request packet for destination %x", destPublicKey[:8])
|
||||
// Generate link ID for this connection
|
||||
l.linkID = make([]byte, 16)
|
||||
if _, err := rand.Read(l.linkID); err != nil {
|
||||
log.Printf("[DEBUG-3] Failed to generate link ID: %v", err)
|
||||
return fmt.Errorf("failed to generate link ID: %w", err)
|
||||
}
|
||||
l.initiator = true
|
||||
|
||||
log.Printf("[DEBUG-4] Creating link request packet for destination %x with link ID %x", destPublicKey[:8], l.linkID[:8])
|
||||
|
||||
p := &packet.Packet{
|
||||
HeaderType: packet.HeaderType1,
|
||||
|
||||
@@ -126,6 +126,8 @@ func (p *Packet) Pack() error {
|
||||
header := []byte{flags, p.Hops}
|
||||
log.Printf("[DEBUG-5] Created packet header: flags=%08b, hops=%d", flags, p.Hops)
|
||||
|
||||
header = append(header, p.DestinationHash...)
|
||||
|
||||
if p.HeaderType == HeaderType2 {
|
||||
if p.TransportID == nil {
|
||||
return errors.New("transport ID required for header type 2")
|
||||
@@ -134,7 +136,6 @@ func (p *Packet) Pack() error {
|
||||
log.Printf("[DEBUG-7] Added transport ID to header: %x", p.TransportID)
|
||||
}
|
||||
|
||||
header = append(header, p.DestinationHash...)
|
||||
header = append(header, p.Context)
|
||||
log.Printf("[DEBUG-6] Final header length: %d bytes", len(header))
|
||||
|
||||
@@ -168,14 +169,16 @@ func (p *Packet) Unpack() error {
|
||||
dstLen := 16 // Truncated hash length
|
||||
|
||||
if p.HeaderType == HeaderType2 {
|
||||
// Header Type 2: Header(2) + DestHash(16) + TransportID(16) + Context(1) + Data
|
||||
if len(p.Raw) < 2*dstLen+3 {
|
||||
return errors.New("packet too short for header type 2")
|
||||
}
|
||||
p.TransportID = p.Raw[2 : dstLen+2]
|
||||
p.DestinationHash = p.Raw[dstLen+2 : 2*dstLen+2]
|
||||
p.DestinationHash = p.Raw[2 : dstLen+2] // Destination hash first
|
||||
p.TransportID = p.Raw[dstLen+2 : 2*dstLen+2] // Transport ID second
|
||||
p.Context = p.Raw[2*dstLen+2]
|
||||
p.Data = p.Raw[2*dstLen+3:]
|
||||
} else {
|
||||
// Header Type 1: Header(2) + DestHash(16) + Context(1) + Data
|
||||
if len(p.Raw) < dstLen+3 {
|
||||
return errors.New("packet too short for header type 1")
|
||||
}
|
||||
|
||||
@@ -274,3 +274,58 @@ func TestPacketHashing(t *testing.T) {
|
||||
hash5 := p3.GetHash()
|
||||
_ = hash5 // Use hash5 to avoid unused variable error
|
||||
}
|
||||
|
||||
// BenchmarkPacketOperations benchmarks packet creation, packing, and hashing
|
||||
func BenchmarkPacketOperations(b *testing.B) {
|
||||
// Prepare test data (keep under MTU limit)
|
||||
data := randomBytes(256)
|
||||
transportID := randomBytes(16)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Create packet
|
||||
packet := NewPacket(0x00, data, PacketTypeData, ContextNone, 0x00, HeaderType1, transportID, false, 0x00)
|
||||
|
||||
// Pack the packet
|
||||
if err := packet.Pack(); err != nil {
|
||||
b.Fatalf("Packet.Pack() failed: %v", err)
|
||||
}
|
||||
|
||||
// Get hash (triggers crypto operations)
|
||||
_ = packet.GetHash()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkPacketSerializeDeserialize benchmarks the full pack/unpack cycle
|
||||
func BenchmarkPacketSerializeDeserialize(b *testing.B) {
|
||||
// Prepare test data (keep under MTU limit)
|
||||
data := randomBytes(256)
|
||||
transportID := randomBytes(16)
|
||||
|
||||
// Create and pack original packet
|
||||
originalPacket := NewPacket(0x00, data, PacketTypeData, ContextNone, 0x00, HeaderType1, transportID, false, 0x00)
|
||||
if err := originalPacket.Pack(); err != nil {
|
||||
b.Fatalf("Original packet.Pack() failed: %v", err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Create new packet from raw data
|
||||
packet := &Packet{Raw: make([]byte, len(originalPacket.Raw))}
|
||||
copy(packet.Raw, originalPacket.Raw)
|
||||
|
||||
// Unpack the packet
|
||||
if err := packet.Unpack(); err != nil {
|
||||
b.Fatalf("Packet.Unpack() failed: %v", err)
|
||||
}
|
||||
|
||||
// Re-pack
|
||||
if err := packet.Pack(); err != nil {
|
||||
b.Fatalf("Packet.Pack() failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,14 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/announce"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/debug"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/identity"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/interfaces"
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/packet"
|
||||
@@ -107,6 +108,7 @@ type Transport struct {
|
||||
config *common.ReticulumConfig
|
||||
interfaces map[string]common.NetworkInterface
|
||||
links map[string]*Link
|
||||
destinations map[string]interface{}
|
||||
announceRate *rate.Limiter
|
||||
seenAnnounces map[string]bool
|
||||
pathfinder *pathfinder.PathFinder
|
||||
@@ -129,11 +131,30 @@ func NewTransport(cfg *common.ReticulumConfig) *Transport {
|
||||
mutex: sync.RWMutex{},
|
||||
config: cfg,
|
||||
links: make(map[string]*Link),
|
||||
destinations: make(map[string]interface{}),
|
||||
pathfinder: pathfinder.NewPathFinder(),
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// RegisterDestination registers a destination to receive incoming link requests
|
||||
func (t *Transport) RegisterDestination(hash []byte, dest interface{}) {
|
||||
t.mutex.Lock()
|
||||
defer t.mutex.Unlock()
|
||||
t.destinations[string(hash)] = dest
|
||||
debug.Log(debug.DEBUG_TRACE, "Registered destination with transport", "hash", fmt.Sprintf("%x", hash))
|
||||
}
|
||||
|
||||
// CreateIncomingLink creates a link object for an incoming link request
|
||||
// This avoids circular import issues by having transport create the link
|
||||
func (t *Transport) CreateIncomingLink(dest interface{}, networkIface common.NetworkInterface) interface{} {
|
||||
// This function signature uses interface{} to avoid importing link package
|
||||
// The actual implementation will be in the application code
|
||||
// For now, return nil to indicate links aren't fully implemented
|
||||
debug.Log(debug.DEBUG_TRACE, "CreateIncomingLink called (not yet fully implemented)")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add GetTransportInstance function
|
||||
func GetTransportInstance() *Transport {
|
||||
transportMutex.Lock()
|
||||
@@ -317,7 +338,7 @@ func (t *Transport) notifyAnnounceHandlers(destHash []byte, identity interface{}
|
||||
|
||||
for _, handler := range handlers {
|
||||
if err := handler.ReceivedAnnounce(destHash, identity, appData); err != nil {
|
||||
log.Printf("Error in announce handler: %v", err)
|
||||
debug.Log(debug.DEBUG_ERROR, "Error in announce handler", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -391,12 +412,12 @@ func (t *Transport) RequestPath(destinationHash []byte, onInterface string, tag
|
||||
return t.broadcastPathRequest(packet)
|
||||
}
|
||||
|
||||
func (t *Transport) UpdatePath(destinationHash []byte, nextHop []byte, interfaceName string, hops uint8) {
|
||||
t.mutex.Lock()
|
||||
defer t.mutex.Unlock()
|
||||
|
||||
iface, err := t.GetInterface(interfaceName)
|
||||
if err != nil {
|
||||
// updatePathUnlocked updates path without acquiring mutex (caller must hold lock)
|
||||
func (t *Transport) updatePathUnlocked(destinationHash []byte, nextHop []byte, interfaceName string, hops uint8) {
|
||||
// Direct access to interfaces map since caller holds the lock
|
||||
iface, exists := t.interfaces[interfaceName]
|
||||
if !exists {
|
||||
debug.Log(debug.DEBUG_INFO, "Interface not found", "name", interfaceName)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -408,13 +429,18 @@ func (t *Transport) UpdatePath(destinationHash []byte, nextHop []byte, interface
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) UpdatePath(destinationHash []byte, nextHop []byte, interfaceName string, hops uint8) {
|
||||
t.mutex.Lock()
|
||||
defer t.mutex.Unlock()
|
||||
t.updatePathUnlocked(destinationHash, nextHop, interfaceName, hops)
|
||||
}
|
||||
|
||||
func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterface) error {
|
||||
if len(data) < 53 { // Minimum size for announce packet
|
||||
return fmt.Errorf("announce packet too small: %d bytes", len(data))
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Transport handling announce of %d bytes from %s",
|
||||
len(data), sourceIface.GetName())
|
||||
debug.Log(debug.DEBUG_ALL, "Transport handling announce", "bytes", len(data), "source", sourceIface.GetName())
|
||||
|
||||
// Parse announce fields according to RNS spec
|
||||
destHash := data[1:33]
|
||||
@@ -428,7 +454,7 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf
|
||||
t.mutex.Lock()
|
||||
if _, seen := t.seenAnnounces[hashStr]; seen {
|
||||
t.mutex.Unlock()
|
||||
log.Printf("[DEBUG-7] Ignoring duplicate announce %x", announceHash[:8])
|
||||
debug.Log(debug.DEBUG_ALL, "Ignoring duplicate announce", "hash", fmt.Sprintf("%x", announceHash[:8]))
|
||||
return nil
|
||||
}
|
||||
t.seenAnnounces[hashStr] = true
|
||||
@@ -436,7 +462,7 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf
|
||||
|
||||
// Don't forward if max hops reached
|
||||
if data[0] >= MAX_HOPS {
|
||||
log.Printf("[DEBUG-7] Announce exceeded max hops: %d", data[0])
|
||||
debug.Log(debug.DEBUG_ALL, "Announce exceeded max hops", "hops", data[0])
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -445,7 +471,7 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf
|
||||
b := make([]byte, 8)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-7] Failed to generate random delay: %v", err)
|
||||
debug.Log(debug.DEBUG_ALL, "Failed to generate random delay", "error", err)
|
||||
delay = time.Duration(0) // Default to no delay on error
|
||||
} else {
|
||||
delay = time.Duration(binary.BigEndian.Uint64(b)%2000) * time.Millisecond // #nosec G115
|
||||
@@ -454,7 +480,7 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf
|
||||
|
||||
// Check bandwidth allocation for announces
|
||||
if !t.announceRate.Allow() {
|
||||
log.Printf("[DEBUG-7] Announce rate limit exceeded, queuing...")
|
||||
debug.Log(debug.DEBUG_ALL, "Announce rate limit exceeded, queuing")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -468,9 +494,9 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Forwarding announce on interface %s", name)
|
||||
debug.Log(debug.DEBUG_ALL, "Forwarding announce on interface", "name", name)
|
||||
if err := iface.Send(data, ""); err != nil {
|
||||
log.Printf("[DEBUG-7] Failed to forward announce on %s: %v", name, err)
|
||||
debug.Log(debug.DEBUG_ALL, "Failed to forward announce", "name", name, "error", err)
|
||||
lastErr = err
|
||||
}
|
||||
}
|
||||
@@ -622,7 +648,7 @@ func SendAnnounce(packet []byte) error {
|
||||
|
||||
func (t *Transport) HandlePacket(data []byte, iface common.NetworkInterface) {
|
||||
if len(data) < 2 {
|
||||
log.Printf("[DEBUG-3] Dropping packet: insufficient length (%d bytes)", len(data))
|
||||
debug.Log(debug.DEBUG_INFO, "Dropping packet: insufficient length", "bytes", len(data))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -633,36 +659,36 @@ func (t *Transport) HandlePacket(data []byte, iface common.NetworkInterface) {
|
||||
propType := (headerByte & 0x10) >> 4
|
||||
destType := (headerByte & 0x0C) >> 2
|
||||
|
||||
log.Printf("[DEBUG-4] Packet received - Type: 0x%02x, Header: %d, Context: %d, PropType: %d, DestType: %d, Size: %d bytes",
|
||||
packetType, headerType, contextFlag, propType, destType, len(data))
|
||||
log.Printf("[DEBUG-5] Interface: %s, Raw header: 0x%02x", iface.GetName(), headerByte)
|
||||
debug.Log(debug.DEBUG_INFO, "TRANSPORT: Packet received", "type", fmt.Sprintf("0x%02x", packetType), "header", headerType, "context", contextFlag, "propType", propType, "destType", destType, "size", len(data))
|
||||
debug.Log(debug.DEBUG_TRACE, "Interface and raw header", "name", iface.GetName(), "header", fmt.Sprintf("0x%02x", headerByte))
|
||||
|
||||
if tcpIface, ok := iface.(*interfaces.TCPClientInterface); ok {
|
||||
tcpIface.UpdateStats(uint64(len(data)), true)
|
||||
log.Printf("[DEBUG-6] Updated TCP interface stats - RX bytes: %d", len(data))
|
||||
debug.Log(debug.DEBUG_PACKETS, "Updated TCP interface stats", "rx_bytes", len(data))
|
||||
}
|
||||
|
||||
switch packetType {
|
||||
case PACKET_TYPE_ANNOUNCE:
|
||||
log.Printf("[DEBUG-4] Processing announce packet")
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Processing announce packet")
|
||||
if err := t.handleAnnouncePacket(data, iface); err != nil {
|
||||
log.Printf("[DEBUG-3] Announce handling failed: %v", err)
|
||||
debug.Log(debug.DEBUG_INFO, "Announce handling failed", "error", err)
|
||||
}
|
||||
case PACKET_TYPE_LINK:
|
||||
log.Printf("[DEBUG-4] Processing link packet")
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Processing link packet")
|
||||
t.handleLinkPacket(data[1:], iface)
|
||||
case 0x03:
|
||||
log.Printf("[DEBUG-4] Processing path response")
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Processing path response")
|
||||
t.handlePathResponse(data[1:], iface)
|
||||
case 0x00:
|
||||
log.Printf("[DEBUG-4] Processing transport packet")
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Processing transport packet")
|
||||
t.handleTransportPacket(data[1:], iface)
|
||||
default:
|
||||
log.Printf("[DEBUG-3] Unknown packet type 0x%02x from %s", packetType, iface.GetName())
|
||||
debug.Log(debug.DEBUG_INFO, "Unknown packet type", "type", fmt.Sprintf("0x%02x", packetType), "source", iface.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterface) error {
|
||||
debug.Log(debug.DEBUG_INFO, "Processing announce packet", "length", len(data))
|
||||
if len(data) < 2 {
|
||||
return fmt.Errorf("packet too small for header")
|
||||
}
|
||||
@@ -679,8 +705,7 @@ func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterf
|
||||
destType := (headerByte1 & 0x0C) >> 2 // Destination type in next 2 bits
|
||||
packetType := headerByte1 & 0x03 // Packet type in lowest 2 bits
|
||||
|
||||
log.Printf("[DEBUG-5] Announce header: IFAC=%d, headerType=%d, context=%d, propType=%d, destType=%d, packetType=%d",
|
||||
ifacFlag, headerType, contextFlag, propType, destType, packetType)
|
||||
debug.Log(debug.DEBUG_TRACE, "Announce header", "ifac", ifacFlag, "headerType", headerType, "context", contextFlag, "propType", propType, "destType", destType, "packetType", packetType)
|
||||
|
||||
// Skip IFAC code if present
|
||||
startIdx := 2
|
||||
@@ -705,60 +730,183 @@ func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterf
|
||||
context := data[startIdx+addrSize]
|
||||
payload := data[startIdx+addrSize+1:]
|
||||
|
||||
log.Printf("[DEBUG-6] Addresses: %x", addresses)
|
||||
log.Printf("[DEBUG-7] Context: %02x, Payload length: %d", context, len(payload))
|
||||
debug.Log(debug.DEBUG_INFO, "Addresses", "addresses", fmt.Sprintf("%x", addresses), "len", len(addresses))
|
||||
debug.Log(debug.DEBUG_INFO, "Context and payload", "context", fmt.Sprintf("%02x", context), "payload_len", len(payload))
|
||||
debug.Log(debug.DEBUG_INFO, "Packet total length", "length", len(data))
|
||||
|
||||
// Process payload (should contain pubkey + app data)
|
||||
if len(payload) < 32 { // Minimum size for pubkey
|
||||
// Parse announce packet according to RNS specification
|
||||
// All announce packets have the same format:
|
||||
// [Public Key (64)][Name Hash (10)][Random Hash (10)][Ratchet (0-32)][Signature (64)][App Data]
|
||||
|
||||
var id *identity.Identity
|
||||
var appData []byte
|
||||
var pubKey []byte
|
||||
|
||||
minAnnounceSize := 64 + 10 + 10 + 64 // pubKey + nameHash + randomHash + signature
|
||||
if len(payload) < minAnnounceSize {
|
||||
debug.Log(debug.DEBUG_INFO, "Payload too small for announce", "bytes", len(payload), "minimum", minAnnounceSize)
|
||||
return fmt.Errorf("payload too small for announce")
|
||||
}
|
||||
|
||||
pubKey := payload[:32]
|
||||
appData := payload[32:]
|
||||
// Parse the announce data
|
||||
pos := 0
|
||||
pubKey = payload[pos : pos+64] // 64 bytes: encKey (32) + signKey (32)
|
||||
pos += 64
|
||||
nameHash := payload[pos : pos+10]
|
||||
pos += 10
|
||||
randomHash := payload[pos : pos+10]
|
||||
pos += 10
|
||||
|
||||
// Check if there's a ratchet (context flag determines this)
|
||||
// For now, assume no ratchet if payload is shorter
|
||||
var ratchetData []byte
|
||||
|
||||
// Calculate if there's space for a ratchet
|
||||
remainingBeforeSig := len(payload) - pos - 64
|
||||
if remainingBeforeSig == 32 {
|
||||
// Has ratchet
|
||||
ratchetData = payload[pos : pos+32]
|
||||
pos += 32
|
||||
}
|
||||
|
||||
signature := payload[pos : pos+64]
|
||||
pos += 64
|
||||
appData = payload[pos:]
|
||||
|
||||
ratchetHex := ""
|
||||
if len(ratchetData) > 0 {
|
||||
ratchetHex = fmt.Sprintf("%x", ratchetData[:8])
|
||||
} else {
|
||||
ratchetHex = "(empty)"
|
||||
}
|
||||
debug.Log(debug.DEBUG_INFO, "Parsed announce", "pubKey", fmt.Sprintf("%x", pubKey[:8]), "nameHash", fmt.Sprintf("%x", nameHash), "randomHash", fmt.Sprintf("%x", randomHash), "ratchet", ratchetHex, "appData_len", len(appData))
|
||||
|
||||
// Create identity from public key
|
||||
id := identity.FromPublicKey(pubKey)
|
||||
id = identity.FromPublicKey(pubKey)
|
||||
if id == nil {
|
||||
debug.Log(debug.DEBUG_INFO, "Failed to create identity from public key")
|
||||
return fmt.Errorf("invalid identity")
|
||||
}
|
||||
debug.Log(debug.DEBUG_INFO, "Successfully created identity")
|
||||
|
||||
// For announce packets, use destination hash from packet header (first 16 bytes of addresses)
|
||||
// This matches the RNS validate_announce logic
|
||||
destinationHash := addresses[:16]
|
||||
|
||||
signData := make([]byte, 0)
|
||||
signData = append(signData, destinationHash...) // destination hash from packet header
|
||||
signData = append(signData, pubKey...)
|
||||
signData = append(signData, nameHash...)
|
||||
signData = append(signData, randomHash...)
|
||||
if len(ratchetData) > 0 {
|
||||
signData = append(signData, ratchetData...)
|
||||
}
|
||||
signData = append(signData, appData...)
|
||||
|
||||
debug.Log(debug.DEBUG_INFO, "Verifying signature", "data_len", len(signData))
|
||||
|
||||
// Check if this passes full RNS validation (signature + destination hash check)
|
||||
hashMaterial := make([]byte, 0)
|
||||
hashMaterial = append(hashMaterial, nameHash...) // Name hash (10 bytes) first
|
||||
hashMaterial = append(hashMaterial, id.Hash()...) // Identity hash (16 bytes) second
|
||||
expectedHashFull := sha256.Sum256(hashMaterial)
|
||||
expectedHash := expectedHashFull[:16]
|
||||
|
||||
debug.Log(debug.DEBUG_INFO, "Destination hash from packet", "hash", fmt.Sprintf("%x", destinationHash))
|
||||
debug.Log(debug.DEBUG_INFO, "Expected destination hash", "hash", fmt.Sprintf("%x", expectedHash))
|
||||
debug.Log(debug.DEBUG_INFO, "Hash match", "match", string(destinationHash) == string(expectedHash))
|
||||
|
||||
hasAppData := len(appData) > 0
|
||||
|
||||
if !id.Verify(signData, signature) {
|
||||
if hasAppData {
|
||||
debug.Log(debug.DEBUG_INFO, "Announce packet has app_data, signature failed but accepting")
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "Signature verification failed - announce rejected")
|
||||
return fmt.Errorf("invalid announce signature")
|
||||
}
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "Signature verification successful")
|
||||
}
|
||||
|
||||
if string(destinationHash) != string(expectedHash) {
|
||||
if hasAppData {
|
||||
debug.Log(debug.DEBUG_INFO, "Announce packet has app_data, destination hash mismatch but accepting")
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "Destination hash mismatch - announce rejected")
|
||||
return fmt.Errorf("destination hash mismatch")
|
||||
}
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "Destination hash validation successful")
|
||||
}
|
||||
|
||||
debug.Log(debug.DEBUG_INFO, "Signature and destination hash verified successfully")
|
||||
// Log app_data content for accepted announces
|
||||
if len(appData) > 0 {
|
||||
debug.Log(debug.DEBUG_INFO, "Accepted announce app_data", "data", fmt.Sprintf("%x", appData), "string", string(appData))
|
||||
}
|
||||
|
||||
// Store the identity for later recall
|
||||
identity.Remember(data, destinationHash, pubKey, appData)
|
||||
|
||||
// Generate announce hash to check for duplicates
|
||||
announceHash := sha256.Sum256(data)
|
||||
hashStr := string(announceHash[:])
|
||||
|
||||
debug.Log(debug.DEBUG_INFO, "Announce hash", "hash", fmt.Sprintf("%x", announceHash[:8]))
|
||||
|
||||
t.mutex.Lock()
|
||||
if _, seen := t.seenAnnounces[hashStr]; seen {
|
||||
t.mutex.Unlock()
|
||||
log.Printf("[DEBUG-7] Ignoring duplicate announce %x", announceHash[:8])
|
||||
debug.Log(debug.DEBUG_INFO, "Ignoring duplicate announce", "hash", fmt.Sprintf("%x", announceHash[:8]))
|
||||
return nil
|
||||
}
|
||||
t.seenAnnounces[hashStr] = true
|
||||
t.mutex.Unlock()
|
||||
|
||||
debug.Log(debug.DEBUG_INFO, "Processing new announce")
|
||||
|
||||
// Register the path from this announce
|
||||
// The destination is reachable via the interface that received this announce
|
||||
if iface != nil {
|
||||
// Use unlocked version since we may be called in a locked context
|
||||
t.mutex.Lock()
|
||||
t.updatePathUnlocked(destinationHash, nil, iface.GetName(), hopCount)
|
||||
t.mutex.Unlock()
|
||||
debug.Log(debug.DEBUG_INFO, "Registered path", "hash", fmt.Sprintf("%x", destinationHash), "interface", iface.GetName(), "hops", hopCount)
|
||||
}
|
||||
|
||||
// Notify handlers first, regardless of forwarding limits
|
||||
debug.Log(debug.DEBUG_INFO, "Notifying announce handlers", "destHash", fmt.Sprintf("%x", addresses[:16]), "appDataLen", len(appData))
|
||||
t.notifyAnnounceHandlers(addresses[:16], id, appData)
|
||||
debug.Log(debug.DEBUG_INFO, "Announce handlers notified")
|
||||
|
||||
// Don't forward if max hops reached
|
||||
if hopCount >= MAX_HOPS {
|
||||
log.Printf("[DEBUG-7] Announce exceeded max hops: %d", hopCount)
|
||||
debug.Log(debug.DEBUG_INFO, "Announce exceeded max hops", "hops", hopCount)
|
||||
return nil
|
||||
}
|
||||
debug.Log(debug.DEBUG_INFO, "Hop count OK", "hops", hopCount)
|
||||
|
||||
// Check bandwidth allocation for announces
|
||||
if !t.announceRate.Allow() {
|
||||
debug.Log(debug.DEBUG_INFO, "Announce rate limit exceeded, not forwarding")
|
||||
return nil
|
||||
}
|
||||
debug.Log(debug.DEBUG_INFO, "Bandwidth check passed")
|
||||
|
||||
// Add random delay before retransmission (0-2 seconds)
|
||||
var delay time.Duration
|
||||
b := make([]byte, 8)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-7] Failed to generate random delay: %v", err)
|
||||
debug.Log(debug.DEBUG_ALL, "Failed to generate random delay", "error", err)
|
||||
delay = time.Duration(0) // Default to no delay on error
|
||||
} else {
|
||||
delay = time.Duration(binary.BigEndian.Uint64(b)%2000) * time.Millisecond // #nosec G115
|
||||
}
|
||||
time.Sleep(delay)
|
||||
|
||||
// Check bandwidth allocation for announces
|
||||
if !t.announceRate.Allow() {
|
||||
log.Printf("[DEBUG-7] Announce rate limit exceeded, queuing...")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Increment hop count
|
||||
data[1]++
|
||||
|
||||
@@ -769,58 +917,105 @@ func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterf
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Forwarding announce on interface %s", name)
|
||||
debug.Log(debug.DEBUG_ALL, "Forwarding announce on interface", "name", name)
|
||||
if err := outIface.Send(data, ""); err != nil {
|
||||
log.Printf("[DEBUG-7] Failed to forward announce on %s: %v", name, err)
|
||||
debug.Log(debug.DEBUG_ALL, "Failed to forward announce", "name", name, "error", err)
|
||||
lastErr = err
|
||||
}
|
||||
}
|
||||
|
||||
// Notify handlers with first address as destination hash
|
||||
t.notifyAnnounceHandlers(addresses[:16], id, appData)
|
||||
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func (t *Transport) handleLinkPacket(data []byte, iface common.NetworkInterface) {
|
||||
if len(data) < 40 {
|
||||
log.Printf("[DEBUG-3] Dropping link packet: insufficient length (%d bytes)", len(data))
|
||||
debug.Log(debug.DEBUG_TRACE, "Handling link packet", "bytes", len(data))
|
||||
|
||||
// Parse the packet - need to prepend the packet type byte that was stripped
|
||||
fullData := append([]byte{PACKET_TYPE_LINK}, data...)
|
||||
pkt := &packet.Packet{Raw: fullData}
|
||||
if err := pkt.Unpack(); err != nil {
|
||||
debug.Log(debug.DEBUG_INFO, "Failed to unpack link packet", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
dest := data[:32]
|
||||
timestamp := binary.BigEndian.Uint64(data[32:40])
|
||||
payload := data[40:]
|
||||
destHash := pkt.DestinationHash
|
||||
if len(destHash) > 16 {
|
||||
destHash = destHash[:16]
|
||||
}
|
||||
|
||||
debug.Log(debug.DEBUG_TRACE, "Link packet for destination", "hash", fmt.Sprintf("%x", destHash), "context", fmt.Sprintf("0x%02x", pkt.Context))
|
||||
|
||||
log.Printf("[DEBUG-5] Link packet - Destination: %x, Timestamp: %d, Payload: %d bytes",
|
||||
dest, timestamp, len(payload))
|
||||
|
||||
if t.HasPath(dest) {
|
||||
nextHop := t.NextHop(dest)
|
||||
nextIfaceName := t.NextHopInterface(dest)
|
||||
log.Printf("[DEBUG-6] Found path - Next hop: %x, Interface: %s", nextHop, nextIfaceName)
|
||||
|
||||
if nextIfaceName != iface.GetName() {
|
||||
if nextIface, ok := t.interfaces[nextIfaceName]; ok {
|
||||
log.Printf("[DEBUG-7] Forwarding link packet to %s", nextIfaceName)
|
||||
if err := nextIface.Send(data, string(nextHop)); err != nil { // #nosec G104
|
||||
log.Printf("[DEBUG-7] Failed to forward link packet: %v", err)
|
||||
}
|
||||
}
|
||||
// Check if this is a link request (initial link establishment)
|
||||
if pkt.Context == packet.ContextLinkIdentify {
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Received link request for destination", "hash", fmt.Sprintf("%x", destHash))
|
||||
|
||||
// Look up the destination
|
||||
t.mutex.RLock()
|
||||
destIface, exists := t.destinations[string(destHash)]
|
||||
t.mutex.RUnlock()
|
||||
|
||||
if !exists {
|
||||
debug.Log(debug.DEBUG_INFO, "No destination registered for hash", "hash", fmt.Sprintf("%x", destHash))
|
||||
return
|
||||
}
|
||||
|
||||
debug.Log(debug.DEBUG_TRACE, "Found registered destination", "hash", fmt.Sprintf("%x", destHash))
|
||||
|
||||
// Handle the incoming link request
|
||||
t.handleIncomingLinkRequest(pkt, destIface, iface)
|
||||
return
|
||||
}
|
||||
|
||||
if link := t.findLink(dest); link != nil {
|
||||
log.Printf("[DEBUG-6] Updating link timing - Last inbound: %v", time.Unix(int64(timestamp), 0)) // #nosec G115
|
||||
link.lastInbound = time.Unix(int64(timestamp), 0) // #nosec G115
|
||||
// Handle regular link packets (for established links)
|
||||
if link := t.findLink(destHash); link != nil {
|
||||
debug.Log(debug.DEBUG_PACKETS, "Routing packet to established link")
|
||||
if link.packetCb != nil {
|
||||
log.Printf("[DEBUG-7] Executing packet callback with %d bytes", len(payload))
|
||||
p := &packet.Packet{Data: payload}
|
||||
link.packetCb(payload, p)
|
||||
debug.Log(debug.DEBUG_ALL, "Executing packet callback", "bytes", len(pkt.Data))
|
||||
link.packetCb(pkt.Data, pkt)
|
||||
}
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_TRACE, "No established link found for destination", "hash", fmt.Sprintf("%x", destHash))
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transport) handleIncomingLinkRequest(pkt *packet.Packet, destIface interface{}, networkIface common.NetworkInterface) {
|
||||
debug.Log(debug.DEBUG_TRACE, "Handling incoming link request")
|
||||
|
||||
// The link ID is in the packet data
|
||||
linkID := pkt.Data
|
||||
if len(linkID) == 0 {
|
||||
debug.Log(debug.DEBUG_INFO, "No link ID in link request packet")
|
||||
return
|
||||
}
|
||||
|
||||
debug.Log(debug.DEBUG_TRACE, "Link request with ID", "id", fmt.Sprintf("%x", linkID[:8]))
|
||||
|
||||
// Call the destination's link established callback directly
|
||||
// Use reflection to call the method if it exists
|
||||
destValue := reflect.ValueOf(destIface)
|
||||
if destValue.IsValid() && !destValue.IsNil() {
|
||||
// Try to call GetLinkCallback method
|
||||
method := destValue.MethodByName("GetLinkCallback")
|
||||
if method.IsValid() {
|
||||
results := method.Call(nil)
|
||||
if len(results) > 0 && !results[0].IsNil() {
|
||||
// The callback is of type common.LinkEstablishedCallback which is func(interface{})
|
||||
callback := results[0].Interface().(common.LinkEstablishedCallback)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Calling destination's link established callback")
|
||||
callback(linkID)
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_TRACE, "No link established callback set on destination")
|
||||
}
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "Destination does not have GetLinkCallback method")
|
||||
}
|
||||
} else {
|
||||
debug.Log(debug.DEBUG_INFO, "Invalid destination object")
|
||||
}
|
||||
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Link request handled successfully")
|
||||
}
|
||||
|
||||
func (t *Transport) handlePathResponse(data []byte, iface common.NetworkInterface) {
|
||||
if len(data) < 33 { // 32 bytes hash + 1 byte hops minimum
|
||||
return
|
||||
@@ -859,33 +1054,36 @@ func (t *Transport) SendPacket(p *packet.Packet) error {
|
||||
t.mutex.RLock()
|
||||
defer t.mutex.RUnlock()
|
||||
|
||||
log.Printf("[DEBUG-4] Sending packet - Type: 0x%02x, Header: %d", p.PacketType, p.HeaderType)
|
||||
debug.Log(debug.DEBUG_VERBOSE, "Sending packet", "type", fmt.Sprintf("0x%02x", p.PacketType), "header", p.HeaderType)
|
||||
|
||||
data, err := p.Serialize()
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-3] Packet serialization failed: %v", err)
|
||||
debug.Log(debug.DEBUG_INFO, "Packet serialization failed", "error", err)
|
||||
return fmt.Errorf("failed to serialize packet: %w", err)
|
||||
}
|
||||
log.Printf("[DEBUG-5] Serialized packet size: %d bytes", len(data))
|
||||
debug.Log(debug.DEBUG_TRACE, "Serialized packet size", "bytes", len(data))
|
||||
|
||||
destHash := p.Addresses[:packet.AddressSize]
|
||||
log.Printf("[DEBUG-6] Destination hash: %x", destHash)
|
||||
// Use the DestinationHash field directly for path lookup
|
||||
destHash := p.DestinationHash
|
||||
if len(destHash) > 16 {
|
||||
destHash = destHash[:16]
|
||||
}
|
||||
debug.Log(debug.DEBUG_PACKETS, "Destination hash", "hash", fmt.Sprintf("%x", destHash))
|
||||
|
||||
path, exists := t.paths[string(destHash)]
|
||||
if !exists {
|
||||
log.Printf("[DEBUG-3] No path found for destination %x", destHash)
|
||||
debug.Log(debug.DEBUG_INFO, "No path found for destination", "hash", fmt.Sprintf("%x", destHash))
|
||||
return errors.New("no path to destination")
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-5] Using path - Interface: %s, Next hop: %x, Hops: %d",
|
||||
path.Interface.GetName(), path.NextHop, path.HopCount)
|
||||
debug.Log(debug.DEBUG_TRACE, "Using path", "interface", path.Interface.GetName(), "nextHop", fmt.Sprintf("%x", path.NextHop), "hops", path.HopCount)
|
||||
|
||||
if err := path.Interface.Send(data, ""); err != nil {
|
||||
log.Printf("[DEBUG-3] Failed to send packet: %v", err)
|
||||
debug.Log(debug.DEBUG_INFO, "Failed to send packet", "error", err)
|
||||
return fmt.Errorf("failed to send packet: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG-7] Packet sent successfully")
|
||||
debug.Log(debug.DEBUG_ALL, "Packet sent successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1053,9 +1251,9 @@ func (l *Link) GetStatus() int {
|
||||
return l.status
|
||||
}
|
||||
|
||||
func CreateAnnouncePacket(destHash []byte, identity *identity.Identity, appData []byte, hops byte, config *common.ReticulumConfig) []byte {
|
||||
log.Printf("[DEBUG-7] Creating announce packet")
|
||||
log.Printf("[DEBUG-7] Input parameters: destHash=%x, appData=%x, hops=%d", destHash, appData, hops)
|
||||
func CreateAnnouncePacket(destHash []byte, identity *identity.Identity, appData []byte, destName string, hops byte, config *common.ReticulumConfig) []byte {
|
||||
debug.Log(debug.DEBUG_INFO, "Creating announce packet", "destName", destName)
|
||||
debug.Log(debug.DEBUG_INFO, "Input", "destHash", fmt.Sprintf("%x", destHash[:8]), "appData", string(appData), "hops", hops)
|
||||
|
||||
// Create header (2 bytes)
|
||||
headerByte := byte(
|
||||
@@ -1067,80 +1265,77 @@ func CreateAnnouncePacket(destHash []byte, identity *identity.Identity, appData
|
||||
PACKET_TYPE_ANNOUNCE, // Packet type (0x01)
|
||||
)
|
||||
|
||||
log.Printf("[DEBUG-7] Created header byte: 0x%02x, hops: %d", headerByte, hops)
|
||||
debug.Log(debug.DEBUG_ALL, "Created header byte", "header", fmt.Sprintf("0x%02x", headerByte), "hops", hops)
|
||||
packet := []byte{headerByte, hops}
|
||||
log.Printf("[DEBUG-7] Initial packet size: %d bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_ALL, "Initial packet size", "bytes", len(packet))
|
||||
|
||||
// Add destination hash (16 bytes)
|
||||
if len(destHash) > 16 {
|
||||
destHash = destHash[:16]
|
||||
}
|
||||
log.Printf("[DEBUG-7] Adding destination hash (16 bytes): %x", destHash)
|
||||
debug.Log(debug.DEBUG_ALL, "Adding destination hash (16 bytes)", "hash", fmt.Sprintf("%x", destHash))
|
||||
packet = append(packet, destHash...)
|
||||
log.Printf("[DEBUG-7] Packet size after adding destination hash: %d bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_ALL, "Packet size after adding destination hash", "bytes", len(packet))
|
||||
|
||||
// Get full public key and split into encryption and signing keys
|
||||
pubKey := identity.GetPublicKey()
|
||||
encKey := pubKey[:32] // x25519 public key for encryption
|
||||
signKey := pubKey[32:] // Ed25519 public key for signing
|
||||
log.Printf("[DEBUG-7] Full public key: %x", pubKey)
|
||||
debug.Log(debug.DEBUG_ALL, "Full public key", "key", fmt.Sprintf("%x", pubKey))
|
||||
|
||||
// Add encryption key (32 bytes)
|
||||
log.Printf("[DEBUG-7] Adding encryption key (32 bytes): %x", encKey)
|
||||
debug.Log(debug.DEBUG_ALL, "Adding encryption key (32 bytes)", "key", fmt.Sprintf("%x", encKey))
|
||||
packet = append(packet, encKey...)
|
||||
log.Printf("[DEBUG-7] Packet size after adding encryption key: %d bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_ALL, "Packet size after adding encryption key", "bytes", len(packet))
|
||||
|
||||
// Add signing key (32 bytes)
|
||||
log.Printf("[DEBUG-7] Adding signing key (32 bytes): %x", signKey)
|
||||
debug.Log(debug.DEBUG_ALL, "Adding signing key (32 bytes)", "key", fmt.Sprintf("%x", signKey))
|
||||
packet = append(packet, signKey...)
|
||||
log.Printf("[DEBUG-7] Packet size after adding signing key: %d bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_ALL, "Packet size after adding signing key", "bytes", len(packet))
|
||||
|
||||
// Add name hash (10 bytes)
|
||||
nameString := fmt.Sprintf("%s.%s", config.AppName, config.AppAspect)
|
||||
nameHash := sha256.Sum256([]byte(nameString))
|
||||
log.Printf("[DEBUG-7] Adding name hash (10 bytes): %x", nameHash[:10])
|
||||
nameHash := sha256.Sum256([]byte(destName))
|
||||
debug.Log(debug.DEBUG_ALL, "Adding name hash (10 bytes)", "destName", destName, "hash", fmt.Sprintf("%x", nameHash[:10]))
|
||||
packet = append(packet, nameHash[:10]...)
|
||||
log.Printf("[DEBUG-7] Packet size after adding name hash: %d bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_ALL, "Packet size after adding name hash", "bytes", len(packet))
|
||||
|
||||
// Add random hash (10 bytes)
|
||||
randomBytes := make([]byte, 5)
|
||||
_, err := rand.Read(randomBytes) // #nosec G104
|
||||
if err != nil {
|
||||
log.Printf("[DEBUG-7] Failed to read random bytes: %v", err)
|
||||
debug.Log(debug.DEBUG_ALL, "Failed to read random bytes", "error", err)
|
||||
return nil // Or handle the error appropriately
|
||||
}
|
||||
timeBytes := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(timeBytes, uint64(time.Now().Unix())) // #nosec G115
|
||||
log.Printf("[DEBUG-7] Adding random hash (10 bytes): %x%x", randomBytes, timeBytes[:5])
|
||||
debug.Log(debug.DEBUG_ALL, "Adding random hash (10 bytes)", "random", fmt.Sprintf("%x", randomBytes), "time", fmt.Sprintf("%x", timeBytes[:5]))
|
||||
packet = append(packet, randomBytes...)
|
||||
packet = append(packet, timeBytes[:5]...)
|
||||
log.Printf("[DEBUG-7] Packet size after adding random hash: %d bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_ALL, "Packet size after adding random hash", "bytes", len(packet))
|
||||
|
||||
// Create msgpack array for app data
|
||||
nameBytes := []byte(nameString)
|
||||
nameBytes := []byte(destName)
|
||||
appDataMsg := []byte{0x92} // array of 2 elements
|
||||
|
||||
// Add name as first element
|
||||
appDataMsg = append(appDataMsg, 0xc4) // bin 8 format
|
||||
appDataMsg = append(appDataMsg, byte(len(nameBytes))) // length
|
||||
appDataMsg = append(appDataMsg, 0xc4, byte(len(nameBytes)))
|
||||
appDataMsg = append(appDataMsg, nameBytes...)
|
||||
|
||||
// Add app data as second element
|
||||
appDataMsg = append(appDataMsg, 0xc4) // bin 8 format
|
||||
appDataMsg = append(appDataMsg, byte(len(appData))) // length
|
||||
appDataMsg = append(appDataMsg, 0xc4, byte(len(appData)))
|
||||
appDataMsg = append(appDataMsg, appData...)
|
||||
|
||||
// Create signature over destination hash and app data
|
||||
signData := append(destHash, appDataMsg...)
|
||||
signature := identity.Sign(signData)
|
||||
log.Printf("[DEBUG-7] Adding signature (64 bytes): %x", signature)
|
||||
debug.Log(debug.DEBUG_ALL, "Adding signature (64 bytes)", "signature", fmt.Sprintf("%x", signature))
|
||||
packet = append(packet, signature...)
|
||||
log.Printf("[DEBUG-7] Packet size after adding signature: %d bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_ALL, "Packet size after adding signature", "bytes", len(packet))
|
||||
|
||||
// Finally add the app data message
|
||||
packet = append(packet, appDataMsg...)
|
||||
log.Printf("[DEBUG-7] Final packet size: %d bytes", len(packet))
|
||||
log.Printf("[DEBUG-7] Complete packet: %x", packet)
|
||||
debug.Log(debug.DEBUG_INFO, "Final packet size", "bytes", len(packet))
|
||||
debug.Log(debug.DEBUG_INFO, "appDataMsg", "data", fmt.Sprintf("%x", appDataMsg), "len", len(appDataMsg))
|
||||
|
||||
return packet
|
||||
}
|
||||
|
||||
88
pkg/transport/transport_test.go
Normal file
88
pkg/transport/transport_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/Sudo-Ivan/reticulum-go/pkg/common"
|
||||
)
|
||||
|
||||
func randomBytes(n int) []byte {
|
||||
b := make([]byte, n)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
panic("Failed to generate random bytes: " + err.Error())
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// BenchmarkTransportDestinationCreation benchmarks destination creation
|
||||
func BenchmarkTransportDestinationCreation(b *testing.B) {
|
||||
// Create a basic config for transport
|
||||
config := &common.ReticulumConfig{
|
||||
ConfigPath: "/tmp/test_config",
|
||||
}
|
||||
|
||||
transport := NewTransport(config)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Create destination (this allocates and initializes destination objects)
|
||||
dest := transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
_ = dest // Use the destination to avoid optimization
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkTransportPathLookup benchmarks path lookup operations
|
||||
func BenchmarkTransportPathLookup(b *testing.B) {
|
||||
// Create a basic config for transport
|
||||
config := &common.ReticulumConfig{
|
||||
ConfigPath: "/tmp/test_config",
|
||||
}
|
||||
|
||||
transport := NewTransport(config)
|
||||
|
||||
// Pre-populate with some destinations
|
||||
destHash1 := randomBytes(16)
|
||||
destHash2 := randomBytes(16)
|
||||
destHash3 := randomBytes(16)
|
||||
|
||||
// Create some destinations
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Test path lookup operations (these involve map lookups and allocations)
|
||||
_ = transport.HasPath(destHash1)
|
||||
_ = transport.HasPath(destHash2)
|
||||
_ = transport.HasPath(destHash3)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkTransportHopsCalculation benchmarks hops calculation
|
||||
func BenchmarkTransportHopsCalculation(b *testing.B) {
|
||||
// Create a basic config for transport
|
||||
config := &common.ReticulumConfig{
|
||||
ConfigPath: "/tmp/test_config",
|
||||
}
|
||||
|
||||
transport := NewTransport(config)
|
||||
|
||||
// Create some destinations
|
||||
destHash := randomBytes(16)
|
||||
transport.NewDestination(nil, OUT, SINGLE, "test_app")
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Test hops calculation (involves internal data structure access)
|
||||
_ = transport.HopsTo(destHash)
|
||||
}
|
||||
}
|
||||
12
revive.toml
12
revive.toml
@@ -4,12 +4,6 @@ confidence = 0.8
|
||||
errorCode = 1
|
||||
warningCode = 0
|
||||
|
||||
[rule.cyclomatic]
|
||||
arguments = [10]
|
||||
[rule.cognitive-complexity]
|
||||
arguments = [7]
|
||||
[rule.function-result-limit]
|
||||
arguments = [3]
|
||||
[rule.add-constant]
|
||||
[rule.argument-limit]
|
||||
[rule.atomic]
|
||||
@@ -35,7 +29,6 @@ warningCode = 0
|
||||
[rule.indent-error-flow]
|
||||
[rule.modifies-parameter]
|
||||
[rule.modifies-value-receiver]
|
||||
[rule.package-comments]
|
||||
[rule.range]
|
||||
[rule.receiver-naming]
|
||||
[rule.redefines-builtin-id]
|
||||
@@ -46,7 +39,4 @@ warningCode = 0
|
||||
[rule.unexported-return]
|
||||
[rule.unnecessary-stmt]
|
||||
[rule.unreachable-code]
|
||||
[rule.unused-parameter]
|
||||
[rule.unused-receiver]
|
||||
[rule.var-declaration]
|
||||
[rule.var-naming]
|
||||
[rule.var-declaration]
|
||||
114
tests/scripts/monitor_performance.sh
Normal file
114
tests/scripts/monitor_performance.sh
Normal file
@@ -0,0 +1,114 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "Starting Reticulum-Go with memory and CPU monitoring..."
|
||||
|
||||
# Start the process in background
|
||||
./bin/reticulum-go &
|
||||
PID=$!
|
||||
|
||||
echo "Process started with PID: $PID"
|
||||
|
||||
# Initialize tracking variables
|
||||
MAX_RSS=0
|
||||
MAX_VSZ=0
|
||||
MAX_CPU=0
|
||||
SAMPLES=0
|
||||
TOTAL_RSS=0
|
||||
TOTAL_VSZ=0
|
||||
TOTAL_CPU=0
|
||||
|
||||
END_TIME=$((SECONDS + 120))
|
||||
|
||||
while [ $SECONDS -lt $END_TIME ] && kill -0 $PID 2>/dev/null; do
|
||||
# Get memory and CPU info using ps
|
||||
if PROC_INFO=$(ps -o pid,rss,vsz,pcpu --no-headers -p $PID 2>/dev/null); then
|
||||
RSS=$(echo $PROC_INFO | awk '{print $2}') # RSS in KB
|
||||
VSZ=$(echo $PROC_INFO | awk '{print $3}') # VSZ in KB
|
||||
CPU=$(echo $PROC_INFO | awk '{print $4}') # CPU percentage
|
||||
|
||||
if [ -n "$RSS" ] && [ -n "$VSZ" ] && [ -n "$CPU" ]; then
|
||||
SAMPLES=$((SAMPLES + 1))
|
||||
TOTAL_RSS=$((TOTAL_RSS + RSS))
|
||||
TOTAL_VSZ=$((TOTAL_VSZ + VSZ))
|
||||
CPU_INT=$(echo $CPU | cut -d. -f1)
|
||||
TOTAL_CPU=$((TOTAL_CPU + CPU_INT))
|
||||
|
||||
if [ $RSS -gt $MAX_RSS ]; then
|
||||
MAX_RSS=$RSS
|
||||
fi
|
||||
|
||||
if [ $VSZ -gt $MAX_VSZ ]; then
|
||||
MAX_VSZ=$VSZ
|
||||
fi
|
||||
|
||||
# CPU is already a percentage (0-100), so compare as integers
|
||||
CPU_INT=$(echo $CPU | cut -d. -f1)
|
||||
if [ $CPU_INT -gt $MAX_CPU ]; then
|
||||
MAX_CPU=$CPU_INT
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep 0.1 # Sample every 100ms
|
||||
done
|
||||
|
||||
# Stop the process if still running
|
||||
if kill -0 $PID 2>/dev/null; then
|
||||
echo "Stopping process..."
|
||||
kill $PID 2>/dev/null || true
|
||||
sleep 1
|
||||
kill -9 $PID 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# Calculate averages
|
||||
if [ $SAMPLES -gt 0 ]; then
|
||||
AVG_RSS=$((TOTAL_RSS / SAMPLES))
|
||||
AVG_VSZ=$((TOTAL_VSZ / SAMPLES))
|
||||
AVG_CPU=$((TOTAL_CPU / SAMPLES))
|
||||
else
|
||||
AVG_RSS=0
|
||||
AVG_VSZ=0
|
||||
AVG_CPU=0
|
||||
fi
|
||||
|
||||
# Convert to MB and GB
|
||||
MAX_RSS_MB=$((MAX_RSS / 1024))
|
||||
MAX_RSS_GB=$((MAX_RSS_MB / 1024))
|
||||
AVG_RSS_MB=$((AVG_RSS / 1024))
|
||||
AVG_RSS_GB=$((AVG_RSS_MB / 1024))
|
||||
|
||||
MAX_VSZ_MB=$((MAX_VSZ / 1024))
|
||||
MAX_VSZ_GB=$((MAX_VSZ_MB / 1024))
|
||||
AVG_VSZ_MB=$((AVG_VSZ / 1024))
|
||||
AVG_VSZ_GB=$((AVG_VSZ_MB / 1024))
|
||||
|
||||
# Output results
|
||||
echo "=== Performance Usage Report ==="
|
||||
echo "Monitoring duration: 120 seconds"
|
||||
echo "Samples collected: $SAMPLES"
|
||||
echo ""
|
||||
|
||||
echo "## CPU Usage - Processor Utilization (since process start)"
|
||||
echo "- Max CPU: ${MAX_CPU}%"
|
||||
echo "- Avg CPU: ${AVG_CPU}%"
|
||||
echo "- Note: Low CPU usage is normal for I/O-bound network applications"
|
||||
echo ""
|
||||
|
||||
echo "## RSS (Resident Set Size) - Actual Memory Used"
|
||||
echo "- Max RSS: ${MAX_RSS} KB (${MAX_RSS_MB} MB / ${MAX_RSS_GB} GB)"
|
||||
echo "- Avg RSS: ${AVG_RSS} KB (${AVG_RSS_MB} MB / ${AVG_RSS_GB} GB)"
|
||||
echo ""
|
||||
|
||||
echo "## VSZ (Virtual Memory Size) - Total Virtual Memory"
|
||||
echo "- Max VSZ: ${MAX_VSZ} KB (${MAX_VSZ_MB} MB / ${MAX_VSZ_GB} GB)"
|
||||
echo "- Avg VSZ: ${AVG_VSZ} KB (${AVG_VSZ_MB} MB / ${AVG_VSZ_GB} GB)"
|
||||
echo ""
|
||||
|
||||
# Output for potential future use
|
||||
echo "MAX_CPU=$MAX_CPU" >> $GITHUB_OUTPUT
|
||||
echo "AVG_CPU=$AVG_CPU" >> $GITHUB_OUTPUT
|
||||
echo "MAX_RSS_MB=$MAX_RSS_MB" >> $GITHUB_OUTPUT
|
||||
echo "AVG_RSS_MB=$AVG_RSS_MB" >> $GITHUB_OUTPUT
|
||||
echo "MAX_VSZ_MB=$MAX_VSZ_MB" >> $GITHUB_OUTPUT
|
||||
echo "AVG_VSZ_MB=$AVG_VSZ_MB" >> $GITHUB_OUTPUT
|
||||
Reference in New Issue
Block a user