Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .github/workflows/build-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
SECRET:
required: true
workflow_dispatch:
pull_request:
paths:
- backend/**

env:
SECRET: ${{ secrets.SECRET }}
Expand All @@ -26,7 +29,9 @@ jobs:
- name: "Install packages"
run: apk update && apk add --no-cache libpcap-dev musl-dev gcc go

- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
sparse-checkout: backend

- name: "Create output path"
working-directory: "${{env.BACKEND_DIR}}"
Expand All @@ -47,10 +52,12 @@ jobs:
go build -ldflags '-linkmode external -extldflags "-static"' -o ../output/backend-linux-64

- name: "Upload build"
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: backend-linux
path: "${{env.BACKEND_DIR}}/output/*"
retention-days: 3
compression-level: 9

build-backend-windows:
name: "Build backend for windows"
Expand Down Expand Up @@ -90,10 +97,12 @@ jobs:
go build -o ..\output\backend-windows-64.exe

- name: "Upload build"
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: backend-windows
path: "${{env.BACKEND_DIR}}\\output\\*"
retention-days: 3
compression-level: 9

build-backend-mac:
name: "Build backend for macOS"
Expand Down Expand Up @@ -142,7 +151,9 @@ jobs:
go build -o ../output/backend-macos-m1-64

- name: "Upload build"
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: backend-macos
path: "${{env.BACKEND_DIR}}/output/*"
retention-days: 3
compression-level: 9
14 changes: 12 additions & 2 deletions .github/workflows/build-ethernet-view.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name: Build ethernet view
on:
workflow_call:
workflow_dispatch:
pull_request:
paths:
- ethernet-view/**
- common-front/**

jobs:
build-ethernet-view:
Expand All @@ -14,7 +18,11 @@ jobs:
COMMON_DIR: ./common-front

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
sparse-checkout: |
ethernet-view
common-front

- name: "Install common front dependencies"
working-directory: "${{env.COMMON_DIR}}"
Expand All @@ -36,7 +44,9 @@ jobs:


- name: "Upload build"
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ethernet-view
path: "${{env.FRONTEND_DIR}}/static/*"
retention-days: 3
compression-level: 9
45 changes: 45 additions & 0 deletions .github/workflows/test-backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Test backend

on:
push:
paths:
- backend/**
pull_request:
paths:
- backend/**
workflow_dispatch:

env:
SECRET: ${{ secrets.SECRET }}

jobs:
test-backend:
name: "Test backend"
runs-on: ubuntu-latest

env:
BACKEND_DIR: ./backend

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 1.21
cache: false


- name: Install Dependencies
run: |
sudo apt-get update && sudo apt-get install -y libpcap-dev

- name: "Load secret"
working-directory: "${{env.BACKEND_DIR}}"
run: |
./load-secret.sh "$SECRET"

- name: Test with Go
working-directory: "${{env.BACKEND_DIR}}"
run: go test -v -timeout 30s ./...
39 changes: 30 additions & 9 deletions backend/pkg/transport/session/buffer.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
package session

import "io"

type Buffer struct {
data chan byte
data chan byte
closed chan struct{}
}

func NewBuffer(size int) *Buffer {
return &Buffer{
data: make(chan byte, size),
data: make(chan byte, size),
closed: make(chan struct{}),
}
}

func (buffer *Buffer) Read(b []byte) (n int, err error) {
n = 0
ok := true
loop:
for i := range b {
b[i], ok = <-buffer.data
n++
if !ok {
break loop
select {
case b[i], ok = <-buffer.data:
if !ok {
err = io.EOF
return
}
n++
default:
return
}
}

return n, nil
return
}

func (buffer *Buffer) Write(b []byte) (n int, err error) {
select {
case _, ok := <-buffer.closed:
if !ok {
return 0, io.ErrClosedPipe
}
default:
}

n = 0
for i := range b {
buffer.data <- b[i]
Expand All @@ -34,3 +49,9 @@ func (buffer *Buffer) Write(b []byte) (n int, err error) {

return n, nil
}

func (buffer *Buffer) Close() error {
close(buffer.data)
close(buffer.closed)
return nil
}
42 changes: 32 additions & 10 deletions backend/pkg/transport/session/sniffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"sync"
"testing"
"time"

"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network"
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/session"
Expand Down Expand Up @@ -216,19 +217,40 @@ func TestSnifferDemux(t *testing.T) {
go func(socket network.Socket, reader io.Reader) {
defer wg.Done()
buf := make([]byte, 65536)
timeout := time.After(time.Millisecond)
readChan := make(chan []byte)
for {
n, err := reader.Read(buf)
if err != nil {
go func() {
for {
n, err := reader.Read(buf)
if err != nil {
close(readChan)
return
}
if n > 0 {
readChan <- buf[:n]
return
}
}
}()

select {
case <-timeout:
return
case buf, ok := <-readChan:
if !ok {
return
}
mapMx.Lock()
data := string(buf)
prev, ok := outputMap[socket]
if ok {
data = prev + data
}
outputMap[socket] = data
mapMx.Unlock()
}
mapMx.Lock()
data := string(buf[:n])
prev, ok := outputMap[socket]
if ok {
data = prev + data
}
outputMap[socket] = data
mapMx.Unlock()

}
}(socket, reader)
}
Expand Down
Loading