* implement authentication via token for relaysrv Make replaysrv check for a token before allowing clients to join. The token can be set via the replay-uri. * fix formatting * key composite literal * do not error out if auth material is provided but not needed * remove unused method receiver * clean up unused parameter in functions * cleaner token handling, disable joining the pool if token is set. * Keep backwards compatibility with older clients. In prior versions of the protocol JoinRelayRequest did not have a token field. Trying to unmarshal such a request will result in an error. Return an empty JoinRelayRequest, that is a request without token, instead. Co-authored-by: entity0xfe <entity0xfe@my.domain>
This commit is contained in:
@@ -27,7 +27,8 @@ type staticClient struct {
|
||||
messageTimeout time.Duration
|
||||
connectTimeout time.Duration
|
||||
|
||||
conn *tls.Conn
|
||||
conn *tls.Conn
|
||||
token string
|
||||
}
|
||||
|
||||
func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *staticClient {
|
||||
@@ -38,6 +39,8 @@ func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan pro
|
||||
|
||||
messageTimeout: time.Minute * 2,
|
||||
connectTimeout: timeout,
|
||||
|
||||
token: uri.Query().Get("token"),
|
||||
}
|
||||
c.commonClient = newCommonClient(invitations, c.serve, c.String())
|
||||
return c
|
||||
@@ -173,7 +176,7 @@ func (c *staticClient) disconnect() {
|
||||
}
|
||||
|
||||
func (c *staticClient) join() error {
|
||||
if err := protocol.WriteMessage(c.conn, protocol.JoinRelayRequest{}); err != nil {
|
||||
if err := protocol.WriteMessage(c.conn, protocol.JoinRelayRequest{Token: c.token}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -31,9 +31,12 @@ type header struct {
|
||||
|
||||
type Ping struct{}
|
||||
type Pong struct{}
|
||||
type JoinRelayRequest struct{}
|
||||
type RelayFull struct{}
|
||||
|
||||
type JoinRelayRequest struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
type JoinSessionRequest struct {
|
||||
Key []byte // max:32
|
||||
}
|
||||
|
||||
@@ -137,40 +137,6 @@ func (*Pong) UnmarshalXDRFrom(_ *xdr.Unmarshaller) error {
|
||||
|
||||
/*
|
||||
|
||||
JoinRelayRequest Structure:
|
||||
(contains no fields)
|
||||
|
||||
|
||||
struct JoinRelayRequest {
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
func (JoinRelayRequest) XDRSize() int {
|
||||
return 0
|
||||
}
|
||||
func (JoinRelayRequest) MarshalXDR() ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (JoinRelayRequest) MustMarshalXDR() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (JoinRelayRequest) MarshalXDRInto(_ *xdr.Marshaller) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*JoinRelayRequest) UnmarshalXDR(_ []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*JoinRelayRequest) UnmarshalXDRFrom(_ *xdr.Unmarshaller) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
RelayFull Structure:
|
||||
(contains no fields)
|
||||
|
||||
@@ -205,6 +171,57 @@ func (*RelayFull) UnmarshalXDRFrom(_ *xdr.Unmarshaller) error {
|
||||
|
||||
/*
|
||||
|
||||
JoinRelayRequest Structure:
|
||||
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
/ /
|
||||
\ Token (length + padded data) \
|
||||
/ /
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|
||||
|
||||
struct JoinRelayRequest {
|
||||
string Token<>;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
func (o JoinRelayRequest) XDRSize() int {
|
||||
return 4 + len(o.Token) + xdr.Padding(len(o.Token))
|
||||
}
|
||||
|
||||
func (o JoinRelayRequest) MarshalXDR() ([]byte, error) {
|
||||
buf := make([]byte, o.XDRSize())
|
||||
m := &xdr.Marshaller{Data: buf}
|
||||
return buf, o.MarshalXDRInto(m)
|
||||
}
|
||||
|
||||
func (o JoinRelayRequest) MustMarshalXDR() []byte {
|
||||
bs, err := o.MarshalXDR()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bs
|
||||
}
|
||||
|
||||
func (o JoinRelayRequest) MarshalXDRInto(m *xdr.Marshaller) error {
|
||||
m.MarshalString(o.Token)
|
||||
return m.Error
|
||||
}
|
||||
|
||||
func (o *JoinRelayRequest) UnmarshalXDR(bs []byte) error {
|
||||
u := &xdr.Unmarshaller{Data: bs}
|
||||
return o.UnmarshalXDRFrom(u)
|
||||
}
|
||||
func (o *JoinRelayRequest) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
|
||||
o.Token = u.UnmarshalString()
|
||||
return u.Error
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
JoinSessionRequest Structure:
|
||||
|
||||
0 1 2 3
|
||||
|
||||
@@ -17,6 +17,7 @@ var (
|
||||
ResponseSuccess = Response{0, "success"}
|
||||
ResponseNotFound = Response{1, "not found"}
|
||||
ResponseAlreadyConnected = Response{2, "already connected"}
|
||||
ResponseWrongToken = Response{3, "wrong token"}
|
||||
ResponseUnexpectedMessage = Response{100, "unexpected message"}
|
||||
)
|
||||
|
||||
@@ -107,6 +108,14 @@ func ReadMessage(r io.Reader) (interface{}, error) {
|
||||
return msg, err
|
||||
case messageTypeJoinRelayRequest:
|
||||
var msg JoinRelayRequest
|
||||
|
||||
// In prior versions of the protocol JoinRelayRequest did not have a
|
||||
// token field. Trying to unmarshal such a request will result in
|
||||
// an error, return msg with an empty token instead.
|
||||
if header.messageLength == 0 {
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
err := msg.UnmarshalXDR(buf)
|
||||
return msg, err
|
||||
case messageTypeJoinSessionRequest:
|
||||
|
||||
Reference in New Issue
Block a user