/
Controller.go
182 lines (153 loc) · 3.86 KB
/
Controller.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package dnet
import (
"errors"
"fmt"
"net/rpc"
"time"
// "crypto/rsa"
//"crypto/tls"
//"errors"
"net"
"golang.org/x/crypto/ssh"
"github.com/AnimusPEXUS/gologger"
"github.com/AnimusPEXUS/dnet/common_types"
)
// This is for cases when DNet (as a Controller), whises to access module
// instance or theyr some other parts. In this case, DNet controller will be
// presented as DNET_UNIVERSAL_APPLICATION_NAME
const DNET_UNIVERSAL_APPLICATION_NAME = "localDNet"
type Controller struct {
application_controller common_types.ApplicationControllerI
logger *gologger.Logger
}
func NewController(
application_controller common_types.ApplicationControllerI,
logger *gologger.Logger,
) (
*Controller,
error,
) {
if application_controller == nil {
return nil, errors.New("application_controller must be specified")
}
ret := new(Controller)
ret.application_controller = application_controller
logger.Info("DNet Controller initiation completed without errors")
return ret, nil
}
func (self *Controller) TestOutgoingConnection(conn net.Conn) error {
repl := &struct{ Text string }{}
cli := rpc.NewClient(conn)
err := cli.Call(
"Node.IsReallyDNetNode",
&struct{ Text string }{Text: "Hi!"},
repl,
)
if err != nil {
fmt.Println("debug error TestOutgoingConnection", err)
return err
}
if repl.Text != "yes it is" {
return errors.New("RPC challange failed")
}
return nil
}
func (self *Controller) ServeConnection(
who *common_types.Address,
conn net.Conn,
) {
config := &ssh.ServerConfig{
NoClientAuth: true,
MaxAuthTries: 2,
PasswordCallback: self._ISSHH_PasswordCallback,
PublicKeyCallback: self._ISSHH_PublicKeyCallback,
KeyboardInteractiveCallback: self._ISSHH_KeyboardInteractiveCallback,
AuthLogCallback: self._ISSHH_AuthLogCallback,
ServerVersion: "",
}
// ssh_conn, new_chan, request, err := ssh.NewServerConn(conn, config)
_, _, _, err := ssh.NewServerConn(conn, config)
if err != nil {
fmt.Println("error", err)
}
return
}
func (self *Controller) _ISSHH_PasswordCallback(
conn ssh.ConnMetadata,
password []byte,
) (
*ssh.Permissions,
error,
) {
ret := &ssh.Permissions{}
return ret, nil
}
func (self *Controller) _ISSHH_PublicKeyCallback(
conn ssh.ConnMetadata,
key ssh.PublicKey,
) (
*ssh.Permissions,
error,
) {
ret := &ssh.Permissions{}
return ret, nil
}
func (self *Controller) _ISSHH_KeyboardInteractiveCallback(
conn ssh.ConnMetadata,
client ssh.KeyboardInteractiveChallenge,
) (
*ssh.Permissions,
error,
) {
ret := &ssh.Permissions{}
return ret, nil
}
func (self *Controller) _ISSHH_AuthLogCallback(
conn ssh.ConnMetadata,
method string,
err error,
) {
fmt.Println("authentication attempted", conn, method, err)
}
func (self *Controller) PossiblyNodeDiscoveredNotificationReceptor(
module_name *common_types.ModuleName,
address common_types.NetworkAddress,
) error {
tracker_module_rpc_client, err := self.application_controller.GetInnodeRPC(
common_types.ModuleNameNewF(DNET_UNIVERSAL_APPLICATION_NAME),
common_types.ModuleNameNewF("builtin_address_tracker"),
)
if err != nil {
return err
}
dnet_address := &common_types.Address{}
var res bool
t := time.Now().UTC()
tracker_module_rpc_client.Call(
"RPC.NoteRecord",
&struct {
DnetAddrStr string
DiscoveredDate *time.Time
NetworkName string
NetworkAddress common_types.NetworkAddress
}{
DnetAddrStr: dnet_address.String(),
DiscoveredDate: &t,
NetworkName: module_name.Value(),
NetworkAddress: address,
},
&res,
)
return nil
}
func (self *Controller) GetInnodeRPC(calling_app_name *common_types.ModuleName) (
*rpc.Client,
error,
) {
pipe1, pipe2 := net.Pipe()
serv := rpc.NewServer()
serv.RegisterName("DNET", NewInnodeRPC(self, calling_app_name))
serv.ServeConn(pipe1)
ret := rpc.NewClient(pipe2)
return ret, nil
}