Skip to content

Commit

Permalink
Merge pull request #69 from loopholelabs/staging
Browse files Browse the repository at this point in the history
Release v0.2.1
  • Loading branch information
ShivanshVij committed Mar 4, 2022
2 parents e77c7c0 + dca6df1 commit 908d072
Show file tree
Hide file tree
Showing 25 changed files with 177 additions and 633 deletions.
2 changes: 1 addition & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 0.1
cli:
version: 0.7.0-beta
version: 0.8.0-beta
lint:
enabled:
- gitleaks@7.6.1
Expand Down
21 changes: 18 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v0.2.1] - 2022-03-02 (Beta)

### Fixes

- The `Server.ConnContext` function now runs after a TLS Handshake is completed - this means if you'd like to access
the `tls.ConnectionState` of a `frisbee.Conn` in the `ConnContext` it is now feasible to do so

### Changes

- Frisbee packets now use a `*packet.Content` object to store byte slices. This struct will append data when the `Write`
function is called, and can be reset using the `Reset` function. Putting a packet back in a pool automatically calls
the `Reset` function on the content as well.
- Moving the `packet` and `metadata` packages into their own repo and referencing them

## [v0.2.0] - 2022-02-22 (Beta)

### Fixes
Expand Down Expand Up @@ -108,7 +122,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Initial Release of Frisbee

[unreleased]: https://github.com/loopholelabs/frisbee/compare/v0.2.0...HEAD
[unreleased]: https://github.com/loopholelabs/frisbee/compare/v0.2.1...HEAD
[v0.2.1]: https://github.com/loopholelabs/frisbee/compare/v0.2.0...v0.2.1
[v0.2.0]: https://github.com/loopholelabs/frisbee/compare/v0.1.6...v0.2.0
[v0.1.6]: https://github.com/loopholelabs/frisbee/compare/v0.1.5...v0.1.6
[v0.1.5]: https://github.com/loopholelabs/frisbee/compare/v0.1.4...v0.1.5
Expand Down
37 changes: 28 additions & 9 deletions async.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package frisbee

import (
"bufio"
"context"
"crypto/tls"
"encoding/binary"
"github.com/loopholelabs/frisbee/pkg/metadata"
"github.com/loopholelabs/frisbee/pkg/packet"
"github.com/loopholelabs/frisbee/pkg/queue"
"github.com/loopholelabs/frisbee/internal/queue"
"github.com/loopholelabs/packet"
"github.com/loopholelabs/packet/pkg/metadata"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"go.uber.org/atomic"
Expand Down Expand Up @@ -124,7 +125,25 @@ func (c *Async) ConnectionState() (tls.ConnectionState, error) {
if tlsConn, ok := c.conn.(*tls.Conn); ok {
return tlsConn.ConnectionState(), nil
}
return tls.ConnectionState{}, NotTLSConnectionError
return emptyState, NotTLSConnectionError
}

// Handshake performs the tls.Handshake() of a *tls.Conn
// if the connection is not *tls.Conn then the NotTLSConnectionError is returned
func (c *Async) Handshake() error {
if tlsConn, ok := c.conn.(*tls.Conn); ok {
return tlsConn.Handshake()
}
return NotTLSConnectionError
}

// HandshakeContext performs the tls.HandshakeContext() of a *tls.Conn
// if the connection is not *tls.Conn then the NotTLSConnectionError is returned
func (c *Async) HandshakeContext(ctx context.Context) error {
if tlsConn, ok := c.conn.(*tls.Conn); ok {
return tlsConn.HandshakeContext(ctx) //trunk-ignore(golangci-lint/typecheck)
}
return NotTLSConnectionError
}

// LocalAddr returns the local address of the underlying net.Conn
Expand All @@ -146,7 +165,7 @@ func (c *Async) CloseChannel() <-chan struct{} {
//
// If packet.Metadata.ContentLength == 0, then the content array must be nil. Otherwise, it is required that packet.Metadata.ContentLength == len(content).
func (c *Async) WritePacket(p *packet.Packet) error {
if int(p.Metadata.ContentLength) != len(p.Content) {
if int(p.Metadata.ContentLength) != len(p.Content.B) {
return InvalidContentLength
}

Expand Down Expand Up @@ -185,7 +204,7 @@ func (c *Async) WritePacket(p *packet.Packet) error {
return c.closeWithError(err)
}
}
_, err = c.writer.Write(p.Content[:p.Metadata.ContentLength])
_, err = c.writer.Write(p.Content.B[:p.Metadata.ContentLength])
if err != nil {
c.Unlock()
if c.closed.Load() {
Expand Down Expand Up @@ -468,7 +487,7 @@ func (c *Async) readLoop() {
default:
if p.Metadata.ContentLength > 0 {
if n-index < int(p.Metadata.ContentLength) {
min := int(p.Metadata.ContentLength) - p.Write(buf[index:n])
min := int(p.Metadata.ContentLength) - p.Content.Write(buf[index:n])
n = 0
for cap(buf) < min {
buf = append(buf[:cap(buf)], 0)
Expand All @@ -493,10 +512,10 @@ func (c *Async) readLoop() {
break
}
}
p.Content = append(p.Content, buf[:min]...)
p.Content.Write(buf[:min])
index = min
} else {
index += p.Write(buf[index : index+int(p.Metadata.ContentLength)])
index += p.Content.Write(buf[index : index+int(p.Metadata.ContentLength)])
}
}
err = c.incoming.Push(p)
Expand Down
34 changes: 19 additions & 15 deletions async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package frisbee

import (
"crypto/rand"
"github.com/loopholelabs/frisbee/pkg/packet"
"github.com/loopholelabs/packet"
"github.com/loopholelabs/testing/conn/pair"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"io/ioutil"
"net"
"runtime"
"testing"
"time"
)
Expand Down Expand Up @@ -56,12 +57,12 @@ func TestNewAsync(t *testing.T) {
assert.Equal(t, uint16(64), p.Metadata.Id)
assert.Equal(t, uint16(32), p.Metadata.Operation)
assert.Equal(t, uint32(0), p.Metadata.ContentLength)
assert.Equal(t, 0, len(p.Content))
assert.Equal(t, 0, len(p.Content.B))

data := make([]byte, packetSize)
_, _ = rand.Read(data)

p.Write(data)
p.Content.Write(data)
p.Metadata.ContentLength = packetSize

err = writerConn.WritePacket(p)
Expand All @@ -75,8 +76,8 @@ func TestNewAsync(t *testing.T) {
assert.Equal(t, uint16(64), p.Metadata.Id)
assert.Equal(t, uint16(32), p.Metadata.Operation)
assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength)
assert.Equal(t, len(data), len(p.Content))
assert.Equal(t, data, p.Content)
assert.Equal(t, len(data), len(p.Content.B))
assert.Equal(t, data, p.Content.B)

packet.Put(p)

Expand Down Expand Up @@ -108,8 +109,9 @@ func TestAsyncLargeWrite(t *testing.T) {
for i := 0; i < testSize; i++ {
randomData[i] = make([]byte, packetSize)
_, _ = rand.Read(randomData[i])
p.Write(randomData[i])
p.Content.Write(randomData[i])
err := writerConn.WritePacket(p)
p.Content.Reset()
assert.NoError(t, err)
}
packet.Put(p)
Expand All @@ -121,8 +123,8 @@ func TestAsyncLargeWrite(t *testing.T) {
assert.Equal(t, uint16(64), p.Metadata.Id)
assert.Equal(t, uint16(32), p.Metadata.Operation)
assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength)
assert.Equal(t, len(randomData[i]), len(p.Content))
assert.Equal(t, randomData[i], p.Content)
assert.Equal(t, len(randomData[i]), len(p.Content.B))
assert.Equal(t, randomData[i], p.Content.B)
packet.Put(p)
}

Expand Down Expand Up @@ -152,7 +154,7 @@ func TestAsyncRawConn(t *testing.T) {
p := packet.Get()
p.Metadata.Id = 64
p.Metadata.Operation = 32
p.Write(randomData)
p.Content.Write(randomData)
p.Metadata.ContentLength = packetSize

for i := 0; i < testSize; i++ {
Expand All @@ -169,8 +171,8 @@ func TestAsyncRawConn(t *testing.T) {
assert.Equal(t, uint16(64), p.Metadata.Id)
assert.Equal(t, uint16(32), p.Metadata.Operation)
assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength)
assert.Equal(t, len(randomData), len(p.Content))
assert.Equal(t, randomData, p.Content)
assert.Equal(t, packetSize, len(p.Content.B))
assert.Equal(t, randomData, p.Content.B)
}

rawReaderConn := readerConn.Raw()
Expand Down Expand Up @@ -223,7 +225,7 @@ func TestAsyncReadClose(t *testing.T) {
assert.Equal(t, uint16(64), p.Metadata.Id)
assert.Equal(t, uint16(32), p.Metadata.Operation)
assert.Equal(t, uint32(0), p.Metadata.ContentLength)
assert.Equal(t, 0, len(p.Content))
assert.Equal(t, 0, len(p.Content.B))

err = readerConn.conn.Close()
assert.NoError(t, err)
Expand Down Expand Up @@ -271,7 +273,7 @@ func TestAsyncWriteClose(t *testing.T) {
assert.Equal(t, uint16(64), p.Metadata.Id)
assert.Equal(t, uint16(32), p.Metadata.Operation)
assert.Equal(t, uint32(0), p.Metadata.ContentLength)
assert.Equal(t, 0, len(p.Content))
assert.Equal(t, 0, len(p.Content.B))

err = writerConn.WritePacket(p)
assert.NoError(t, err)
Expand Down Expand Up @@ -322,7 +324,7 @@ func TestAsyncTimeout(t *testing.T) {
assert.Equal(t, uint16(64), p.Metadata.Id)
assert.Equal(t, uint16(32), p.Metadata.Operation)
assert.Equal(t, uint32(0), p.Metadata.ContentLength)
assert.Equal(t, 0, len(p.Content))
assert.Equal(t, 0, len(p.Content.B))

time.Sleep(defaultDeadline * 5)

Expand All @@ -341,9 +343,11 @@ func TestAsyncTimeout(t *testing.T) {

_, err = readerConn.ReadPacket()
assert.ErrorIs(t, err, ConnectionClosed)

err = readerConn.Error()
if err == nil {
time.Sleep(defaultDeadline * 5)
runtime.Gosched()
time.Sleep(defaultDeadline * 10)
}
assert.Error(t, readerConn.Error())

Expand Down
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package frisbee

import (
"context"
"github.com/loopholelabs/frisbee/pkg/packet"
"github.com/loopholelabs/packet"
"github.com/rs/zerolog"
"go.uber.org/atomic"
"net"
Expand Down Expand Up @@ -167,7 +167,7 @@ func (c *Client) handlePacket(ctx context.Context, conn *Async, p *packet.Packet
packetCtx = c.PacketContext(packetCtx, p)
}
outgoing, action := handlerFunc(packetCtx, p)
if outgoing != nil && outgoing.Metadata.ContentLength == uint32(len(outgoing.Content)) {
if outgoing != nil && outgoing.Metadata.ContentLength == uint32(len(outgoing.Content.B)) {
err := conn.WritePacket(outgoing)
if outgoing != p {
packet.Put(outgoing)
Expand Down
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package frisbee
import (
"context"
"crypto/rand"
"github.com/loopholelabs/frisbee/pkg/metadata"
"github.com/loopholelabs/frisbee/pkg/packet"
"github.com/loopholelabs/packet"
"github.com/loopholelabs/packet/pkg/metadata"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestClientRaw(t *testing.T) {

p := packet.Get()
p.Metadata.Operation = metadata.PacketPing
p.Write(data)
p.Content.Write(data)
p.Metadata.ContentLength = packetSize

for q := 0; q < testSize; q++ {
Expand Down Expand Up @@ -169,7 +169,7 @@ func BenchmarkThroughputClient(b *testing.B) {
p := packet.Get()

p.Metadata.Operation = metadata.PacketPing
p.Write(data)
p.Content.Write(data)
p.Metadata.ContentLength = packetSize

b.Run("test", func(b *testing.B) {
Expand Down Expand Up @@ -250,7 +250,7 @@ func BenchmarkThroughputResponseClient(b *testing.B) {
p := packet.Get()
p.Metadata.Operation = metadata.PacketPing

p.Write(data)
p.Content.Write(data)
p.Metadata.ContentLength = packetSize

b.Run("test", func(b *testing.B) {
Expand Down
15 changes: 11 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package frisbee

import (
"context"
"crypto/tls"
"github.com/loopholelabs/frisbee/pkg/packet"
"github.com/loopholelabs/packet"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"net"
Expand All @@ -30,10 +31,14 @@ import (
const DefaultBufferSize = 1 << 19

var (
defaultLogger = zerolog.New(os.Stdout)
defaultLogger = zerolog.New(os.Stdout)

defaultDeadline = time.Second
emptyTime = time.Time{}
pastTime = time.Unix(1, 0)

emptyTime = time.Time{}
pastTime = time.Unix(1, 0)

emptyState = tls.ConnectionState{}
)

var (
Expand All @@ -45,6 +50,8 @@ type Conn interface {
LocalAddr() net.Addr
RemoteAddr() net.Addr
ConnectionState() (tls.ConnectionState, error)
Handshake() error
HandshakeContext(context.Context) error
SetDeadline(time.Time) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
Expand Down
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
//
// import (
// "github.com/loopholelabs/frisbee"
// "github.com/loopholelabs/frisbee/pkg/packet"
// "github.com/loopholelabs/packet"
// "github.com/rs/zerolog/log"
// "os"
// "os/signal"
Expand Down Expand Up @@ -77,7 +77,7 @@
// import (
// "fmt"
// "github.com/loopholelabs/frisbee"
// "github.com/loopholelabs/frisbee/pkg/packet"
// "github.com/loopholelabs/packet"
// "github.com/rs/zerolog/log"
// "os"
// "os/signal"
Expand Down
Loading

0 comments on commit 908d072

Please sign in to comment.