Skip to content

Commit

Permalink
Add metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
junbeomlee authored and owljoa committed Oct 13, 2018
1 parent b336195 commit 36df626
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 52 deletions.
34 changes: 17 additions & 17 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type GrpcOpts struct {
}

// 서버와 연결 요청. 실패시 err. handshake 과정을 거침.
func Dial(serverIp string, clientOpts ClientOpts, grpcOpts GrpcOpts, crypto bifrost.Crypto) (bifrost.Connection, error) {
func Dial(serverIp string, metaData map[string]string, clientOpts ClientOpts, grpcOpts GrpcOpts, crypto bifrost.Crypto) (bifrost.Connection, error) {

var opts []grpc.DialOption //required options

Expand All @@ -61,13 +61,13 @@ func Dial(serverIp string, clientOpts ClientOpts, grpcOpts GrpcOpts, crypto bifr
return nil, err
}

serverPubKey, err := handShake(streamWrapper, clientOpts, crypto.KeyRecoverer)
serverPubKey, metaData, err := handShake(streamWrapper, metaData, clientOpts, crypto.KeyRecoverer)

if err != nil {
return nil, err
}

conn, err := bifrost.NewConnection(serverIp, serverPubKey, streamWrapper, crypto)
conn, err := bifrost.NewConnection(serverIp, metaData, serverPubKey, streamWrapper, crypto)

if err != nil {
return nil, err
Expand All @@ -77,34 +77,34 @@ func Dial(serverIp string, clientOpts ClientOpts, grpcOpts GrpcOpts, crypto bifr
}

// handshake 함수, return : serverPubKey, err
func handShake(streamWrapper bifrost.StreamWrapper, clientOpts ClientOpts, keyRecoverer bifrost.KeyRecoverer) (bifrost.Key, error) {
func handShake(streamWrapper bifrost.StreamWrapper, metaData map[string]string, clientOpts ClientOpts, keyRecoverer bifrost.KeyRecoverer) (bifrost.Key, map[string]string, error) {

err := waitServer(streamWrapper)

if err != nil {
iLogger.Infof(nil, "[Bifrost] Waiting server failed [%s]", err.Error())
streamWrapper.Close()
return nil, err
return nil, nil, err
}

err = sendInfo(streamWrapper, clientOpts)
err = sendInfo(streamWrapper, clientOpts, metaData)
if err != nil {
iLogger.Infof(nil, "[Bifrost] Send info failed [%s]", err.Error())
streamWrapper.Close()
return nil, err
return nil, nil, err
}

serverPubKey, err := getServerInfo(streamWrapper, keyRecoverer)
serverPubKey, metaD, err := getServerInfo(streamWrapper, keyRecoverer)

if err != nil {
iLogger.Infof(nil, "[Bifrost] Get server info failed [%s]", err.Error())
streamWrapper.Close()
return nil, err
return nil, nil, err
}

iLogger.Info(nil, "[Bifrost] Handshake success")

return serverPubKey, nil
return serverPubKey, metaD, nil
}

// handshake 첫번째 과정 함수. server 의 request peer info 메세지를 기다린다.
Expand All @@ -121,8 +121,8 @@ func waitServer(streamWrapper bifrost.StreamWrapper) error {
}

// handShake 두번째 과정 함수. client 의 peer info 메세지를 server 에게 전달한다.
func sendInfo(streamWrapper bifrost.StreamWrapper, clientOpts ClientOpts) error {
env, err := bifrost.BuildResponsePeerInfo(clientOpts.PubKey)
func sendInfo(streamWrapper bifrost.StreamWrapper, clientOpts ClientOpts, metaData map[string]string) error {
env, err := bifrost.BuildResponsePeerInfo(clientOpts.PubKey, metaData)

if err != nil {
return err
Expand All @@ -136,26 +136,26 @@ func sendInfo(streamWrapper bifrost.StreamWrapper, clientOpts ClientOpts) error
}

// handShake 세번째 과정 함수. server 의 peer info 메세지를 기다린다(Get 한다).
func getServerInfo(streamWrapper bifrost.StreamWrapper, keyRecoverer bifrost.KeyRecoverer) (bifrost.Key, error) {
func getServerInfo(streamWrapper bifrost.StreamWrapper, keyRecoverer bifrost.KeyRecoverer) (bifrost.Key, map[string]string, error) {
env, err := bifrost.RecvWithTimeout(3*time.Second, streamWrapper)

if err != nil {
return nil, err
return nil, nil, err
}

peerInfo := &bifrost.PeerInfo{}

err = json.Unmarshal(env.Payload, peerInfo)

if err != nil {
return nil, err
return nil, nil, err
}

serverPubKey, err := keyRecoverer.RecoverKeyFromByte(peerInfo.PubKeyBytes, peerInfo.IsPrivate)

if err != nil {
return nil, err
return nil, nil, err
}

return serverPubKey, nil
return serverPubKey, peerInfo.MetaData, nil
}
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDial(t *testing.T) {
time.Sleep(3 * time.Second)

// when
testConn, err := client.Dial(serverIP, clientOpt, grpcOpt, mocks.NewMockCrypto())
testConn, err := client.Dial(serverIP, nil, clientOpt, grpcOpt, mocks.NewMockCrypto())
go func() {
defer testConn.Close()
if err := testConn.Start(); err != nil {
Expand Down
11 changes: 10 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PeerInfo struct {
PubKeyBytes []byte
IsPrivate bool
KeyGenOpt string
MetaData map[string]string
}

type innerMessage struct {
Expand Down Expand Up @@ -62,10 +63,11 @@ type GrpcConnection struct {
readChannel chan *pb.Envelope
stopChannel chan struct{}
sync.RWMutex
metaData map[string]string
Crypto
}

func NewConnection(ip string, peerKey Key, streamWrapper StreamWrapper, crypto Crypto) (Connection, error) {
func NewConnection(ip string, metaData map[string]string, peerKey Key, streamWrapper StreamWrapper, crypto Crypto) (Connection, error) {

if streamWrapper == nil || peerKey == nil {
return nil, errors.New("fail to create connection streamWrapper or peerKey is nil")
Expand All @@ -85,15 +87,22 @@ func NewConnection(ip string, peerKey Key, streamWrapper StreamWrapper, crypto C
readChannel: make(chan *pb.Envelope, 200),
stopChannel: make(chan struct{}, 1),
Crypto: crypto,
metaData: metaData,
}, nil
}

func (conn *GrpcConnection) GetMetaData() map[string]string {
return conn.metaData
}

func (conn *GrpcConnection) GetIP() Address {
return conn.ip
}

func (conn *GrpcConnection) GetPeerKey() Key {
return conn.peerKey
}

func (conn *GrpcConnection) GetID() ConnID {
return conn.ID
}
Expand Down
8 changes: 4 additions & 4 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestGrpcConnection_Send(t *testing.T) {
crypto.Signer.(*mocks.MockECDSASigner).KeyID = keyOpts.PubKey.ID()
crypto.Signer.(*mocks.MockECDSASigner).KeyDirPath = "./.test_private_key"

conn, err := bifrost.NewConnection("127.0.0.1:1234", keyOpts.PubKey, mockStreamWrapper, crypto)
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)

mockStreamWrapper.SendCallBack = func(envelope *pb.Envelope) {
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestGrpcConnection_GetPeerKey(t *testing.T) {
mockStreamWrapper := mocks.MockStreamWrapper{}
crypto := mocks.NewMockCrypto()

conn, err := bifrost.NewConnection("127.0.0.1:1234", keyOpts.PubKey, mockStreamWrapper, crypto)
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)

go func() {
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestGrpcConnection_Close(t *testing.T) {
}
crypto := mocks.NewMockCrypto()

conn, err := bifrost.NewConnection("127.0.0.1:1234", keyOpts.PubKey, mockStreamWrapper, crypto)
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)

go func() {
Expand All @@ -151,7 +151,7 @@ func TestGrpcConnection_GetIP(t *testing.T) {
mockStreamWrapper := mocks.MockStreamWrapper{}
crypto := mocks.NewMockCrypto()

conn, err := bifrost.NewConnection("127.0.0.1:1234", keyOpts.PubKey, mockStreamWrapper, crypto)
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)
ipAddr := conn.GetIP()
assert.Equal(t, bifrost.Address{IP: "127.0.0.1:1234"}, ipAddr)
Expand Down
2 changes: 1 addition & 1 deletion example/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
recoverer := mocks.MockECDSAKeyRecoverer{}
crypto := bifrost.Crypto{Signer: &signer, Verifier: &verifier, KeyRecoverer: &recoverer}

conn, err := client.Dial(serverIp, clientOpt, grpcOpt, crypto)
conn, err := client.Dial(serverIp, nil, clientOpt, grpcOpt, crypto)
if err != nil {
iLogger.Fatalf(nil, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion example/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
recoverer := mocks.MockECDSAKeyRecoverer{}
crypto := bifrost.Crypto{Verifier: &verifier, Signer: &signer, KeyRecoverer: &recoverer}

s := server.New(keyPair, crypto)
s := server.New(keyPair, crypto, nil)

s.OnConnection(OnConnection)
s.OnError(OnError)
Expand Down
2 changes: 1 addition & 1 deletion mocks/mock_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewMockConnection(targetIP string) (bifrost.Connection, error) {

}

conn, err := bifrost.NewConnection(targetIP, keyOpts.PubKey, mockStreamWrapper, mockCrypto)
conn, err := bifrost.NewConnection(targetIP, nil, keyOpts.PubKey, mockStreamWrapper, mockCrypto)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion mocks/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func NewMockServer() *server.Server {
mockCrypto := NewMockCrypto()
mockCrypto.Signer.(*MockECDSASigner).KeyID = keyOpt.PubKey.ID()

s := server.New(keyOpt, mockCrypto)
s := server.New(keyOpt, mockCrypto, nil)

return s
}
Expand Down
42 changes: 22 additions & 20 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Server struct {
pubKey bifrost.Key
ip string
lis net.Listener
metaData map[string]string
bifrost.Crypto
}

Expand All @@ -36,13 +37,13 @@ func (s Server) BifrostStream(streamServer pb.StreamService_BifrostStreamServer)
_, cf := context.WithCancel(context.Background())
streamWrapper := bifrost.NewServerStreamWrapper(streamServer, cf)

peerKey, err := s.handShake(streamWrapper)
peerKey, metaData, err := s.handShake(streamWrapper)

if err != nil {
return err
}

conn, err := bifrost.NewConnection(ip, peerKey, streamWrapper, s.Crypto)
conn, err := bifrost.NewConnection(ip, metaData, peerKey, streamWrapper, s.Crypto)

if s.onConnectionHandler != nil {
s.onConnectionHandler(conn)
Expand All @@ -51,32 +52,32 @@ func (s Server) BifrostStream(streamServer pb.StreamService_BifrostStreamServer)
return nil
}

func (s Server) handShake(streamWrapper bifrost.StreamWrapper) (bifrost.Key, error) {
func (s Server) handShake(streamWrapper bifrost.StreamWrapper) (bifrost.Key, map[string]string, error) {

err := requestInfo(streamWrapper)

if err != nil {
streamWrapper.Close()
return nil, err
return nil, nil, err
}

peerKey, err := s.getClientInfo(streamWrapper)
peerKey, metaData, err := s.getClientInfo(streamWrapper)

if err != nil {
streamWrapper.Close()
return nil, err
return nil, nil, err
}

err = s.sendInfo(streamWrapper)
err = s.sendInfo(streamWrapper, metaData)

if err != nil {
streamWrapper.Close()
return nil, err
return nil, nil, err
}

iLogger.Info(nil, "[Bifrost] Handshake success")

return peerKey, nil
return peerKey, metaData, nil
}

func requestInfo(streamWrapper bifrost.StreamWrapper) error {
Expand All @@ -89,9 +90,9 @@ func requestInfo(streamWrapper bifrost.StreamWrapper) error {
return nil
}

func (s Server) sendInfo(streamWrapper bifrost.StreamWrapper) error {
func (s Server) sendInfo(streamWrapper bifrost.StreamWrapper, metaData map[string]string) error {

envelope, err := bifrost.BuildResponsePeerInfo(s.pubKey)
envelope, err := bifrost.BuildResponsePeerInfo(s.pubKey, metaData)

if err != nil {
return errors.New("fail to build info")
Expand All @@ -104,32 +105,32 @@ func (s Server) sendInfo(streamWrapper bifrost.StreamWrapper) error {
return nil
}

func (s Server) getClientInfo(streamWrapper bifrost.StreamWrapper) (bifrost.Key, error) {
func (s Server) getClientInfo(streamWrapper bifrost.StreamWrapper) (bifrost.Key, map[string]string, error) {

env, err := bifrost.RecvWithTimeout(3*time.Second, streamWrapper)

if err != nil {
return nil, err
return nil, nil, err
}

if env.GetType() != pb.Envelope_RESPONSE_PEERINFO {
iLogger.Info(nil, "[Bifrost] Invaild message type")
return nil, errors.New("invalid message type")
return nil, nil, errors.New("invalid message type")
}

peerInfo := &bifrost.PeerInfo{}

err = json.Unmarshal(env.Payload, peerInfo)
if err != nil {
return nil, err
return nil, nil, err
}

pubKey, err := s.Crypto.RecoverKeyFromByte(peerInfo.PubKeyBytes, peerInfo.IsPrivate)
if err != nil {
return nil, err
return nil, nil, err
}

return pubKey, nil
return pubKey, peerInfo.MetaData, nil
}

func (s Server) validateRequestPeerInfo(envelope *pb.Envelope) (bool, string, bifrost.Key) {
Expand Down Expand Up @@ -182,10 +183,11 @@ func extractRemoteAddress(stream pb.StreamService_BifrostStreamServer) string {
type OnConnectionHandler func(connection bifrost.Connection)
type OnErrorHandler func(err error)

func New(key bifrost.KeyOpts, crypto bifrost.Crypto) *Server {
func New(key bifrost.KeyOpts, crypto bifrost.Crypto, metaData map[string]string) *Server {
return &Server{
pubKey: key.PubKey,
Crypto: crypto,
pubKey: key.PubKey,
Crypto: crypto,
metaData: metaData,
}
}

Expand Down
6 changes: 3 additions & 3 deletions server/server_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestServer_OnConnection(t *testing.T) {

crypto := bifrost.Crypto{}

mockServer := New(keyOpt, crypto)
mockServer := New(keyOpt, crypto, nil)

// when
mockServer.OnConnection(func(conn bifrost.Connection) {
Expand All @@ -54,7 +54,7 @@ func TestServer_OnError(t *testing.T) {

crypto := bifrost.Crypto{}

mockServer := New(keyOpt, crypto)
mockServer := New(keyOpt, crypto, nil)

// when
mockServer.OnError(func(err error) {
Expand All @@ -81,7 +81,7 @@ func TestServer_validateRequestPeerInfo_whenInValidPeerInfo(t *testing.T) {

crypto := bifrost.Crypto{}

mockServer := New(keyOpt, crypto)
mockServer := New(keyOpt, crypto, nil)

payload, _ := json.Marshal(peerInfo)

Expand Down
Loading

0 comments on commit 36df626

Please sign in to comment.