/
command_client.go
124 lines (102 loc) · 3.59 KB
/
command_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package client
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/mysterium/node/client/connection"
node_cmd "github.com/mysterium/node/cmd"
"github.com/mysterium/node/communication"
nats_dialog "github.com/mysterium/node/communication/nats/dialog"
nats_discovery "github.com/mysterium/node/communication/nats/discovery"
"github.com/mysterium/node/identity"
"github.com/mysterium/node/ip"
"github.com/mysterium/node/openvpn"
"github.com/mysterium/node/openvpn/middlewares/client/bytescount"
"github.com/mysterium/node/server"
"github.com/mysterium/node/tequilapi"
tequilapi_endpoints "github.com/mysterium/node/tequilapi/endpoints"
"path/filepath"
"time"
)
// NewCommand function creates new client command by given options
func NewCommand(options CommandOptions) *Command {
return NewCommandWith(
options,
server.NewClient(options.DiscoveryAPIAddress),
)
}
// NewCommandWith does the same as NewCommand with possibility to override mysterium api client for external communication
func NewCommandWith(
options CommandOptions,
mysteriumClient server.Client,
) *Command {
nats_discovery.Bootstrap()
openvpn.Bootstrap()
keystoreDirectory := filepath.Join(options.DirectoryData, "keystore")
keystoreInstance := keystore.NewKeyStore(keystoreDirectory, keystore.StandardScryptN, keystore.StandardScryptP)
identityManager := identity.NewIdentityManager(keystoreInstance)
dialogEstablisherFactory := func(myID identity.Identity) communication.DialogEstablisher {
return nats_dialog.NewDialogEstablisher(myID, identity.NewSigner(keystoreInstance, myID))
}
signerFactory := func(id identity.Identity) identity.Signer {
return identity.NewSigner(keystoreInstance, id)
}
statsKeeper := bytescount.NewSessionStatsKeeper(time.Now)
vpnClientFactory := connection.ConfigureVpnClientFactory(
mysteriumClient,
options.DirectoryConfig,
options.DirectoryRuntime,
signerFactory,
statsKeeper,
)
connectionManager := connection.NewManager(mysteriumClient, dialogEstablisherFactory, vpnClientFactory, statsKeeper)
router := tequilapi.NewAPIRouter()
httpAPIServer := tequilapi.NewServer(options.TequilapiAddress, options.TequilapiPort, router)
command := &Command{
connectionManager: connectionManager,
httpAPIServer: httpAPIServer,
}
tequilapi_endpoints.AddRoutesForIdentities(router, identityManager, mysteriumClient, signerFactory)
tequilapi_endpoints.AddRoutesForConnection(router, connectionManager, ip.NewResolver(), statsKeeper)
tequilapi_endpoints.AddRoutesForProposals(router, mysteriumClient)
tequilapi_endpoints.AddRouteForStop(router, node_cmd.NewApplicationStopper(command.Kill), time.Second)
return command
}
//Command represent entrypoint for Mysterium client with top level components
type Command struct {
connectionManager connection.Manager
httpAPIServer tequilapi.APIServer
}
// Start starts Tequilapi service - does not block
func (cmd *Command) Start() error {
err := cmd.httpAPIServer.StartServing()
if err != nil {
return err
}
port, err := cmd.httpAPIServer.Port()
if err != nil {
return err
}
fmt.Printf("Api started on: %d\n", port)
return nil
}
// Wait blocks until tequilapi service is stopped
func (cmd *Command) Wait() error {
return cmd.httpAPIServer.Wait()
}
// Kill stops tequilapi service
func (cmd *Command) Kill() error {
err := cmd.connectionManager.Disconnect()
if err != nil {
switch err {
case connection.ErrNoConnection:
fmt.Println("No active connection - proceeding")
default:
return err
}
} else {
fmt.Println("Connection closed")
}
cmd.httpAPIServer.Stop()
fmt.Printf("Api stopped\n")
return nil
}