diff --git a/relay/auth/hmac/token.go b/relay/auth/hmac/token.go index e886bc7ae..a5fa30a14 100644 --- a/relay/auth/hmac/token.go +++ b/relay/auth/hmac/token.go @@ -8,7 +8,6 @@ import ( "encoding/gob" "fmt" "strconv" - "sync" "time" log "github.com/sirupsen/logrus" @@ -40,7 +39,6 @@ func unmarshalToken(payload []byte) (Token, error) { // TimedHMAC generates token with TTL and using pre-shared secret known to TURN server type TimedHMAC struct { - mux sync.Mutex secret string timeToLive time.Duration } diff --git a/relay/client/dialer/ws/ws.go b/relay/client/dialer/ws/ws.go index 3a2bf48c8..d53ced58e 100644 --- a/relay/client/dialer/ws/ws.go +++ b/relay/client/dialer/ws/ws.go @@ -23,11 +23,12 @@ func Dial(address string) (net.Conn, error) { HTTPClient: httpClientNbDialer(), } - wsConn, _, err := websocket.Dial(context.Background(), wsURL, opts) + wsConn, resp, err := websocket.Dial(context.Background(), wsURL, opts) if err != nil { log.Errorf("failed to dial to Relay server '%s': %s", wsURL, err) return nil, err } + _ = resp.Body.Close() conn := NewConn(wsConn) return conn, nil diff --git a/relay/client/manager.go b/relay/client/manager.go index d1a26d705..a71cdf2c3 100644 --- a/relay/client/manager.go +++ b/relay/client/manager.go @@ -18,7 +18,7 @@ var ( errRelayClientNotConnected = fmt.Errorf("relay client not connected") ) -// RelayTrack hold the relay clients for the foregin relay servers. +// RelayTrack hold the relay clients for the foreign relay servers. // With the mutex can ensure we can open new connection in case the relay connection has been established with // the relay server. type RelayTrack struct { diff --git a/relay/messages/id.go b/relay/messages/id.go index d80c09414..531eefa25 100644 --- a/relay/messages/id.go +++ b/relay/messages/id.go @@ -18,7 +18,9 @@ var ( func HashID(peerID string) ([]byte, string) { idHash := sha256.Sum256([]byte(peerID)) idHashString := string(prefix) + base64.StdEncoding.EncodeToString(idHash[:]) - prefixedHash := append(prefix, idHash[:]...) + var prefixedHash []byte + prefixedHash = append(prefixedHash, prefix...) + prefixedHash = append(prefixedHash, idHash[:]...) return prefixedHash, idHashString } diff --git a/relay/server/listener/quic/conn.go b/relay/server/listener/quic/conn.go index 12d22bb5e..5414f9eee 100644 --- a/relay/server/listener/quic/conn.go +++ b/relay/server/listener/quic/conn.go @@ -7,19 +7,19 @@ import ( log "github.com/sirupsen/logrus" ) -type QuicConn struct { +type Conn struct { quic.Stream qConn quic.Connection } func NewConn(stream quic.Stream, qConn quic.Connection) net.Conn { - return &QuicConn{ + return &Conn{ Stream: stream, qConn: qConn, } } -func (q QuicConn) Write(b []byte) (n int, err error) { +func (q Conn) Write(b []byte) (n int, err error) { n, err = q.Stream.Write(b) if n != len(b) { log.Errorf("failed to write out the full message") @@ -27,10 +27,10 @@ func (q QuicConn) Write(b []byte) (n int, err error) { return } -func (q QuicConn) LocalAddr() net.Addr { +func (q Conn) LocalAddr() net.Addr { return q.qConn.LocalAddr() } -func (q QuicConn) RemoteAddr() net.Addr { +func (q Conn) RemoteAddr() net.Addr { return q.qConn.RemoteAddr() } diff --git a/relay/server/listener/quic/listener.go b/relay/server/listener/quic/listener.go index 8244bb2e2..55107e7ba 100644 --- a/relay/server/listener/quic/listener.go +++ b/relay/server/listener/quic/listener.go @@ -18,8 +18,7 @@ import ( ) type Listener struct { - address string - onAcceptFn func(conn net.Conn) + address string listener *quic.Listener quit chan struct{} @@ -88,7 +87,7 @@ func (l *Listener) acceptLoop(acceptFn func(conn net.Conn)) { // Setup a bare-bones TLS config for the server func generateTLSConfig() *tls.Config { - key, err := rsa.GenerateKey(rand.Reader, 1024) + key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { panic(err) } diff --git a/relay/server/listener/tcp/listener.go b/relay/server/listener/tcp/listener.go index bd96e3b30..fb26b0fc4 100644 --- a/relay/server/listener/tcp/listener.go +++ b/relay/server/listener/tcp/listener.go @@ -10,7 +10,7 @@ import ( ) // Listener -// Is it just demo code. It does not work in real life environment because the TCP is a streaming protocol, adn +// Is it just demo code. It does not work in real life environment because the TCP is a streaming protocol, and // it does not handle framing. type Listener struct { address string diff --git a/relay/server/listener/udp/conn.go b/relay/server/listener/udp/conn.go index 24b6d4640..46c7580d5 100644 --- a/relay/server/listener/udp/conn.go +++ b/relay/server/listener/udp/conn.go @@ -6,21 +6,21 @@ import ( "time" ) -type UDPConn struct { +type Conn struct { *net.UDPConn addr *net.UDPAddr msgChannel chan []byte } -func NewConn(conn *net.UDPConn, addr *net.UDPAddr) *UDPConn { - return &UDPConn{ +func NewConn(conn *net.UDPConn, addr *net.UDPAddr) *Conn { + return &Conn{ UDPConn: conn, addr: addr, msgChannel: make(chan []byte), } } -func (u *UDPConn) Read(b []byte) (n int, err error) { +func (u *Conn) Read(b []byte) (n int, err error) { msg, ok := <-u.msgChannel if !ok { return 0, io.EOF @@ -30,39 +30,39 @@ func (u *UDPConn) Read(b []byte) (n int, err error) { return n, nil } -func (u *UDPConn) Write(b []byte) (n int, err error) { +func (u *Conn) Write(b []byte) (n int, err error) { return u.UDPConn.WriteTo(b, u.addr) } -func (u *UDPConn) Close() error { +func (u *Conn) Close() error { //TODO implement me //panic("implement me") return nil } -func (u *UDPConn) LocalAddr() net.Addr { +func (u *Conn) LocalAddr() net.Addr { return u.UDPConn.LocalAddr() } -func (u *UDPConn) RemoteAddr() net.Addr { +func (u *Conn) RemoteAddr() net.Addr { return u.addr } -func (u *UDPConn) SetDeadline(t time.Time) error { +func (u *Conn) SetDeadline(t time.Time) error { //TODO implement me panic("implement me") } -func (u *UDPConn) SetReadDeadline(t time.Time) error { +func (u *Conn) SetReadDeadline(t time.Time) error { //TODO implement me panic("implement me") } -func (u *UDPConn) SetWriteDeadline(t time.Time) error { +func (u *Conn) SetWriteDeadline(t time.Time) error { //TODO implement me panic("implement me") } -func (u *UDPConn) onNewMsg(b []byte) { +func (u *Conn) onNewMsg(b []byte) { u.msgChannel <- b } diff --git a/relay/server/listener/udp/listener.go b/relay/server/listener/udp/listener.go index 31b6098e5..a895cdc32 100644 --- a/relay/server/listener/udp/listener.go +++ b/relay/server/listener/udp/listener.go @@ -11,7 +11,7 @@ import ( type Listener struct { address string - conns map[string]*UDPConn + conns map[string]*Conn onAcceptFn func(conn net.Conn) listener *net.UDPConn @@ -24,7 +24,7 @@ type Listener struct { func NewListener(address string) listener.Listener { return &Listener{ address: address, - conns: make(map[string]*UDPConn), + conns: make(map[string]*Conn), } } diff --git a/relay/server/relay.go b/relay/server/relay.go index e3aefb06a..cb35c6e17 100644 --- a/relay/server/relay.go +++ b/relay/server/relay.go @@ -66,7 +66,7 @@ func (r *Relay) Accept(conn net.Conn) { } func (r *Relay) Close(ctx context.Context) { - log.Infof("closeing connection with all peers") + log.Infof("close connection with all peers") r.closeMu.Lock() wg := sync.WaitGroup{} peers := r.store.Peers()