/
command.go
105 lines (84 loc) · 3.16 KB
/
command.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
package run
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/mysterium/node/client_connection"
"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"
)
//NewCommand function created new client command with options passed from commandline
func NewCommand(options CommandOptions) *CommandRun {
return NewCommandWith(
options,
server.NewClient(),
)
}
//NewCommandWith does the same as NewCommand with possibility to override mysterium api client for external communication
func NewCommandWith(
options CommandOptions,
mysteriumClient server.Client,
) *CommandRun {
nats_discovery.Bootstrap()
openvpn.Bootstrap()
keystoreInstance := keystore.NewKeyStore(options.DirectoryKeystore, keystore.StandardScryptN, keystore.StandardScryptP)
identityManager := identity.NewIdentityManager(keystoreInstance)
dialogEstablisherFactory := func(myIdentity identity.Identity) communication.DialogEstablisher {
return nats_dialog.NewDialogEstablisher(myIdentity, identity.NewSigner(keystoreInstance, myIdentity))
}
signerFactory := func(id identity.Identity) identity.Signer {
return identity.NewSigner(keystoreInstance, id)
}
statsKeeper := &bytescount.SessionStatsKeeper{}
vpnClientFactory := client_connection.ConfigureVpnClientFactory(mysteriumClient, options.DirectoryRuntime, signerFactory, statsKeeper)
connectionManager := client_connection.NewManager(mysteriumClient, dialogEstablisherFactory, vpnClientFactory)
router := tequilapi.NewAPIRouter()
tequilapi_endpoints.AddRoutesForIdentities(router, identityManager, mysteriumClient, signerFactory)
ipResolver := ip.NewResolver()
tequilapi_endpoints.AddRoutesForConnection(router, connectionManager, ipResolver, statsKeeper)
tequilapi_endpoints.AddRoutesForProposals(router, mysteriumClient)
httpAPIServer := tequilapi.NewServer(options.TequilapiAddress, options.TequilapiPort, router)
return &CommandRun{
connectionManager,
httpAPIServer,
}
}
//CommandRun represent entry point for MysteriumVpn client with top level components
type CommandRun struct {
connectionManager client_connection.Manager
httpApiServer tequilapi.APIServer
}
//Run starts Tequilapi service - does not block
func (cmd *CommandRun) Run() 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 *CommandRun) Wait() error {
return cmd.httpApiServer.Wait()
}
//Kill stops tequilapi service
func (cmd *CommandRun) Kill() error {
err := cmd.connectionManager.Disconnect()
if err != nil {
return err
}
cmd.httpApiServer.Stop()
fmt.Printf("Api stopped\n")
return nil
}