Skip to content

Commit

Permalink
refactor: pass logger to tls (#59)
Browse files Browse the repository at this point in the history
# Checklist

* [x] I have read the contribution guidelines
* [x] Iff you changed code related to services, or inter-service
communication, make sure you update the diagrams in `ARCHITECTURE.md`.
* [x] Reference issue for this pull request: NA

# Description

Pass a logger to the TLSBio instead of using a global logger.
  • Loading branch information
ainghazal committed Jan 31, 2024
1 parent c4c9b5a commit 2e2be95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
43 changes: 23 additions & 20 deletions internal/tlssession/tlsbio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package tlssession

import (
"bytes"
"log"
"net"
"sync"
"time"

"github.com/ooni/minivpn/internal/model"
)

// tlsBio allows to use channels to read and write
Expand All @@ -14,70 +15,72 @@ type tlsBio struct {
directionDown chan<- []byte
directionUp <-chan []byte
hangup chan any
logger model.Logger
readBuffer *bytes.Buffer
}

// newTLSBio creates a new tlsBio
func newTLSBio(directionUp <-chan []byte, directionDown chan<- []byte) *tlsBio {
func newTLSBio(logger model.Logger, directionUp <-chan []byte, directionDown chan<- []byte) *tlsBio {
return &tlsBio{
closeOnce: sync.Once{},
directionDown: directionDown,
directionUp: directionUp,
hangup: make(chan any),
logger: logger,
readBuffer: &bytes.Buffer{},
}
}

func (c *tlsBio) Close() error {
c.closeOnce.Do(func() {
close(c.hangup)
func (t *tlsBio) Close() error {
t.closeOnce.Do(func() {
close(t.hangup)
})
return nil
}

func (c *tlsBio) Read(data []byte) (int, error) {
func (t *tlsBio) Read(data []byte) (int, error) {
for {
count, _ := c.readBuffer.Read(data)
count, _ := t.readBuffer.Read(data)
if count > 0 {
log.Printf("[tlsbio] received %d bytes", len(data))
t.logger.Debugf("[tlsbio] received %d bytes", len(data))
return count, nil
}
select {
case extra := <-c.directionUp:
c.readBuffer.Write(extra)
case <-c.hangup:
case extra := <-t.directionUp:
t.readBuffer.Write(extra)
case <-t.hangup:
return 0, net.ErrClosed
}
}
}

func (c *tlsBio) Write(data []byte) (int, error) {
log.Printf("[tlsbio] requested to write %d bytes", len(data))
func (t *tlsBio) Write(data []byte) (int, error) {
t.logger.Debugf("[tlsbio] requested to write %d bytes", len(data))
select {
case c.directionDown <- data:
case t.directionDown <- data:
return len(data), nil
case <-c.hangup:
case <-t.hangup:
return 0, net.ErrClosed
}
}

func (c *tlsBio) LocalAddr() net.Addr {
func (t *tlsBio) LocalAddr() net.Addr {
return &tlsBioAddr{}
}

func (c *tlsBio) RemoteAddr() net.Addr {
func (t *tlsBio) RemoteAddr() net.Addr {
return &tlsBioAddr{}
}

func (c *tlsBio) SetDeadline(t time.Time) error {
func (t *tlsBio) SetDeadline(tt time.Time) error {
return nil
}

func (c *tlsBio) SetReadDeadline(t time.Time) error {
func (t *tlsBio) SetReadDeadline(tt time.Time) error {
return nil
}

func (c *tlsBio) SetWriteDeadline(t time.Time) error {
func (t *tlsBio) SetWriteDeadline(tt time.Time) error {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/tlssession/tlssession.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (ws *workersState) worker() {
// tlsAuth runs the TLS auth algorithm
func (ws *workersState) tlsAuth() error {
// create the BIO to use channels as a socket
conn := newTLSBio(ws.tlsRecordUp, ws.tlsRecordDown)
conn := newTLSBio(ws.logger, ws.tlsRecordUp, ws.tlsRecordDown)
defer conn.Close()

// we construct the certCfg from options, that has access to the certificate material
Expand Down

0 comments on commit 2e2be95

Please sign in to comment.