Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MYST-41 Session generation and exchange #47

Merged
merged 7 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cmd/mysterium_client/command_run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
dto_discovery "github.com/mysterium/node/service_discovery/dto"
"io"
"time"
"encoding/json"
vpn_session "github.com/mysterium/node/openvpn/session"
)

type CommandRun struct {
Expand Down Expand Up @@ -38,13 +40,13 @@ func (cmd *CommandRun) Run(options CommandOptions) (err error) {
return err
}

vpnConfigString, err := sender.Request(communication.GET_CONNECTION_CONFIG, "")
vpnSession, err := getVpnSession(sender, string(proposal.Id))
if err != nil {
return err
}

vpnConfig, err := openvpn.NewClientConfigFromString(
vpnConfigString,
vpnSession.Config,
options.DirectoryRuntime+"/client.ovpn",
)
if err != nil {
Expand Down Expand Up @@ -75,3 +77,14 @@ func (cmd *CommandRun) Kill() {
cmd.communicationClient.Stop()
cmd.vpnClient.Stop()
}

func getVpnSession(sender communication.Sender, proposalId string) (session vpn_session.VpnSession, err error) {
sessionJson, err := sender.Request(communication.SESSION_CREATE, proposalId)
if err != nil {
return
}

err = json.Unmarshal([]byte(sessionJson), &session)

return
}
22 changes: 19 additions & 3 deletions cmd/mysterium_server/command_run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
dto_discovery "github.com/mysterium/node/service_discovery/dto"
"io"
"time"
vpn_session "github.com/mysterium/node/openvpn/session"
"encoding/json"
)

type CommandRun struct {
Expand Down Expand Up @@ -46,9 +48,23 @@ func (cmd *CommandRun) Run(options CommandOptions) (err error) {
}

handleDialog := func(sender communication.Sender, receiver communication.Receiver) {
receiver.Respond(communication.GET_CONNECTION_CONFIG, func(request string) (response string) {
config, _ := buildVpnClientConfig(vpnServerIp, options.DirectoryConfig)
return config
receiver.Respond(communication.SESSION_CREATE, func(proposalId string) (response string) {
config, err := buildVpnClientConfig(vpnServerIp, options.DirectoryConfig)
if err != nil {
return "Failed to create VPN config."
}

vpnSession := vpn_session.NewVpnSession(config)
if err != nil {
return "Failed to create session."
}

session, err := json.Marshal(vpnSession)
if err != nil {
return "Failed to serialize VPN session."
}

return string(session)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing acceptance:
Node responds “session_rejected” with “reasonType”, “reasonMessage” (when Client's proposalId mismatches with Node's current proposal)

})
}

Expand Down
4 changes: 2 additions & 2 deletions communication/message_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package communication

const (
// Client is trying to establish new dialog with Node
DIALOG_CREATE = RequestType("dialog-create")
GET_CONNECTION_CONFIG = RequestType("get-connection-config")
DIALOG_CREATE = RequestType("dialog-create")
SESSION_CREATE = RequestType("session-create")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest defining structs for message types

type SessionCreate struct {
proposalId, serviceStartParams
}

we also need response types defined here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message types do have their own structs:) These constants are basically topics for the communication channel, so we still need them.

)
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import:
version: ^2.6.0
- package: github.com/nats-io/go-nats
version: ^1.3.0
- package: github.com/satori/go.uuid
version: ~1.1.0
testImport:
- package: github.com/stretchr/testify
subpackages:
Expand Down
18 changes: 18 additions & 0 deletions openvpn/session/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package session

import (
node_session "github.com/mysterium/node/session"
)

var manager node_session.Manager

func NewVpnSession(config string) (session VpnSession) {
id := manager.Create()

session = VpnSession{
Id: id,
Config: config,
}

return
}
8 changes: 8 additions & 0 deletions openvpn/session/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package session

import "github.com/mysterium/node/session"

type VpnSession struct {
Id session.SessionId `json:"id"`
Config string `json:"config"`
}
13 changes: 13 additions & 0 deletions session/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package session

import (
"github.com/satori/go.uuid"
)

type SessionId string

func GenerateSessionId() (sid SessionId) {
sid = SessionId(uuid.NewV4().String())

return
}
12 changes: 12 additions & 0 deletions session/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package session

import (
"testing"
"github.com/stretchr/testify/assert"
)

func TestSessionIdLength(t *testing.T) {
sid := GenerateSessionId()

assert.Len(t, sid, 36)
}
13 changes: 13 additions & 0 deletions session/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package session

type Manager struct {
Sessions []SessionId
}

func (manager *Manager) Create() (sid SessionId) {
sid = GenerateSessionId()

manager.Sessions = append(manager.Sessions, sid)

return sid
}
17 changes: 17 additions & 0 deletions session/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package session

import (
"testing"
"github.com/stretchr/testify/assert"
)

func TestManagerHasSessionsStored(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not look like unit.
Is it like integration test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, testing the manager is kind of pointless in this case.. the only use case for unit tests would be to freeze the interface methods, does that make sense though?

manager := Manager{}
length := 10

for i := 0; i < length; i++ {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like loop with overhead.
You can:

  • create SID
  • test private function sessionIsUnique()

manager.Create()
}

assert.Len(t, manager.Sessions, length)
}