/
factory.go
102 lines (92 loc) · 3.6 KB
/
factory.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
package server
import (
"github.com/ethereum/go-ethereum/accounts/keystore"
identity_handler "github.com/mysterium/node/cmd/commands/server/identity"
"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/location"
"github.com/mysterium/node/nat"
"github.com/mysterium/node/openvpn"
"github.com/mysterium/node/openvpn/middlewares/server/auth"
"github.com/mysterium/node/openvpn/middlewares/state"
openvpn_session "github.com/mysterium/node/openvpn/session"
"github.com/mysterium/node/server"
"github.com/mysterium/node/service_discovery/dto"
"github.com/mysterium/node/session"
"path/filepath"
)
// NewCommand function creates new server command by given options
func NewCommand(options CommandOptions) *Command {
return NewCommandWith(
options,
server.NewClient(options.DiscoveryAPIAddress),
ip.NewResolver(),
nat.NewService(),
)
}
// NewCommandWith function creates new client command by given options + injects given dependencies
func NewCommandWith(
options CommandOptions,
mysteriumClient server.Client,
ipResolver ip.Resolver,
natService nat.NATService,
) *Command {
keystoreDirectory := filepath.Join(options.DirectoryData, "keystore")
keystoreInstance := keystore.NewKeyStore(keystoreDirectory, keystore.StandardScryptN, keystore.StandardScryptP)
createSigner := func(id identity.Identity) identity.Signer {
return identity.NewSigner(keystoreInstance, id)
}
identityHandler := identity_handler.NewHandler(
identity.NewIdentityManager(keystoreInstance),
mysteriumClient,
identity.NewIdentityCache(keystoreDirectory, "remember.json"),
createSigner,
)
var locationDetector location.Detector
if options.LocationCountry != "" {
locationDetector = location.NewDetectorFake(options.LocationCountry)
} else if options.LocationDatabase != "" {
locationDetector = location.NewDetector(filepath.Join(options.DirectoryConfig, options.LocationDatabase))
} else {
locationDetector = location.NewDetector(filepath.Join(options.DirectoryConfig, defaultLocationDatabase))
}
return &Command{
identityLoader: func() (identity.Identity, error) {
return identity_handler.LoadIdentity(identityHandler, options.Identity, options.Passphrase)
},
createSigner: createSigner,
locationDetector: locationDetector,
ipResolver: ipResolver,
mysteriumClient: mysteriumClient,
natService: natService,
dialogWaiterFactory: func(myID identity.Identity) communication.DialogWaiter {
return nats_dialog.NewDialogWaiter(
nats_discovery.NewAddressGenerate(options.BrokerAddress, myID),
identity.NewSigner(keystoreInstance, myID),
)
},
sessionManagerFactory: func(vpnServerIP string) session.Manager {
clientConfigGenerator := openvpn.NewClientConfigGenerator(options.DirectoryRuntime, vpnServerIP)
return openvpn_session.NewManager(
clientConfigGenerator,
&session.UUIDGenerator{},
)
},
vpnServerFactory: func(manager session.Manager, serviceLocation dto.Location, providerID identity.Identity, callback state.Callback) *openvpn.Server {
serverConfigGenerator := openvpn.NewServerConfigGenerator(options.DirectoryRuntime, serviceLocation, providerID)
sessionValidator := openvpn_session.NewSessionValidator(
manager.FindSession,
identity.NewExtractor(),
)
return openvpn.NewServer(
serverConfigGenerator,
options.DirectoryRuntime,
auth.NewMiddleware(sessionValidator),
state.NewMiddleware(callback),
)
},
}
}