@@ -10,11 +10,10 @@ package main
10
10
import (
11
11
"fmt"
12
12
"log"
13
- "net"
14
13
"os"
15
14
"time"
16
15
17
- mqtt "github.com/eclipse/paho.mqtt.golang "
16
+ "github.com/acep-uaf/data-diode/utility "
18
17
"github.com/urfave/cli/v2"
19
18
"gopkg.in/yaml.v2"
20
19
)
@@ -40,147 +39,11 @@ type Configuration struct {
40
39
}
41
40
}
42
41
43
- func newClient (ip string , port int ) {
44
- // Create a socket
45
-
46
- client , err := net .DialTimeout ("tcp" , fmt .Sprintf ("%s:%d" , ip , port ), time .Second )
47
-
48
- if err != nil {
49
- fmt .Println (">> Error establishing connection to the diode input side: " , err .Error ())
50
- log .Fatal (err )
51
- }
52
- defer client .Close ()
53
-
54
- numberOfSends := 1
55
-
56
- for {
57
- sendMessage := fmt .Sprintf ("This is TCP passthrough test message number: %d" , numberOfSends )
58
- _ , err := client .Write ([]byte (sendMessage ))
59
- if err != nil {
60
- fmt .Println (">> Error sending message to the diode input side: " , err .Error ())
61
- log .Fatal (err )
62
- break
63
- }
64
-
65
- // if string(response) == "OK\r\n" {
66
- // fmt.Println(">> Message sent successfully!")
67
- // }
68
-
69
- numberOfSends ++
70
-
71
- time .Sleep (1 * time .Second )
72
- }
73
- }
74
-
75
- func newServer (ip string , port int ) {
76
- // Begin listening for incoming connections
77
-
78
- server , err := net .Listen ("tcp" , fmt .Sprintf ("%s:%d" , ip , port ))
79
-
80
- if err != nil {
81
- fmt .Println (">> Error listening for incoming connections: " , err .Error ())
82
- return
83
- }
84
- defer server .Close ()
85
-
86
- fmt .Printf (">> Server listening on %s:%d\n " , ip , port )
87
-
88
- for {
89
- // Wait for connection
90
- connection , err := server .Accept ()
91
-
92
- if err != nil {
93
- fmt .Println (">> Error accepting connection: " , err .Error ())
94
- return
95
- }
96
-
97
- fmt .Println ("Connected to client IP:" , connection .RemoteAddr ().String ())
98
-
99
- go communicationHandler (connection )
100
-
101
- }
102
-
103
- }
104
-
105
- func communicationHandler (connection net.Conn ) {
106
-
107
- defer connection .Close ()
108
-
109
- // Buffer for incoming data (holding recieved data)
110
- buffer := make ([]byte , 10240 )
111
-
112
- for {
113
- // Read incoming data into buffer
114
- bytesRead , err := connection .Read (buffer )
115
- if err != nil {
116
- fmt .Println (">> Error reading: " , err .Error ())
117
- break
118
- }
119
-
120
- if bytesRead > 0 {
121
- fmt .Println (">> Message recieved: " , string (buffer [:bytesRead ]))
122
- }
123
-
124
- if bytesRead < 10240 {
125
- break
126
- }
127
- }
128
-
129
- }
130
-
131
- func sampleMetrics () {
42
+ func sampleMetrics (server string , port int ) {
132
43
fmt .Println (">> Local time: " , time .Now ())
133
44
fmt .Println (">> UTC time: " , time .Now ().UTC ())
134
- }
135
-
136
- func demoRepublisher (server string , port int , topic string , message string ) {
137
- fmt .Println (">> MQTT" )
138
- fmt .Println (">> Broker: " , server )
139
- fmt .Println (">> Port: " , port )
140
-
141
- // Source: https://github.com/eclipse/paho.mqtt.golang/blob/master/cmd/simple/main.go
142
- var example mqtt.MessageHandler = func (client mqtt.Client , msg mqtt.Message ) {
143
- fmt .Printf (">> Topic: %s\n " , msg .Topic ())
144
- fmt .Printf (">> Message: %s\n " , msg .Payload ())
145
- }
146
-
147
- mqtt .DEBUG = log .New (os .Stdout , "" , 0 )
148
- mqtt .ERROR = log .New (os .Stdout , "" , 0 )
149
-
150
- // Initial Connection
151
- opts := mqtt .NewClientOptions ().AddBroker (fmt .Sprintf ("tcp://%s:%d" , server , port ))
152
- opts .SetKeepAlive (2 * time .Second )
153
- opts .SetDefaultPublishHandler (example )
154
- opts .SetPingTimeout (1 * time .Second )
155
-
156
- // Create and start a client using the above ClientOptions
157
- client := mqtt .NewClient (opts )
158
- if token := client .Connect (); token .Wait () && token .Error () != nil {
159
- panic (token .Error ())
160
- }
161
-
162
- // Subscribe to a topic
163
- if token := client .Subscribe (topic , 0 , nil ); token .Wait () && token .Error () != nil {
164
- fmt .Println (token .Error ())
165
- os .Exit (1 )
166
- }
167
-
168
- // Publish to a topic
169
- token := client .Publish (topic , 0 , false , message )
170
- token .Wait ()
171
-
172
- time .Sleep (6 * time .Second )
173
-
174
- // Disconnect from the broker
175
- if token := client .Unsubscribe (topic ); token .Wait () && token .Error () != nil {
176
- fmt .Println (token .Error ())
177
- os .Exit (1 )
178
- }
179
-
180
- client .Disconnect (250 )
181
-
182
- time .Sleep (1 * time .Second )
183
-
45
+ fmt .Println (">> Value: " , utility .Value ())
46
+ // utility.Client(server, port)
184
47
}
185
48
186
49
func main () {
@@ -222,7 +85,7 @@ func main() {
222
85
Usage : "Input side of the data diode" ,
223
86
Action : func (cCtx * cli.Context ) error {
224
87
fmt .Println ("----- INPUT -----" )
225
- newClient (diodeInputSideIP , diodeTCPPassthroughPort )
88
+ utility . Client (diodeInputSideIP , diodeTCPPassthroughPort )
226
89
return nil
227
90
},
228
91
},
@@ -232,7 +95,18 @@ func main() {
232
95
Usage : "Output side of the data diode" ,
233
96
Action : func (sCtx * cli.Context ) error {
234
97
fmt .Println ("----- OUTPUT -----" )
235
- newServer (targetTCPServerIP , targetTCPServerPort )
98
+ utility .Server (targetTCPServerIP , targetTCPServerPort )
99
+ return nil
100
+ },
101
+ },
102
+ {
103
+ Name : "test" ,
104
+ Aliases : []string {"t" },
105
+ Usage : "Testing state synchronization via diode I/O" ,
106
+ Action : func (tCtx * cli.Context ) error {
107
+ fmt .Println ("----- TEST -----" )
108
+ example := utility .Checksum ()
109
+ fmt .Printf (">> Checksum: %x\n " , example )
236
110
return nil
237
111
},
238
112
},
@@ -252,7 +126,7 @@ func main() {
252
126
Usage : "System benchmark analysis + report performance metrics" ,
253
127
Action : func (bCtx * cli.Context ) error {
254
128
fmt .Println ("----- BENCHMARKS -----" )
255
- sampleMetrics ()
129
+ sampleMetrics (utility . CONN_HOST , 3333 )
256
130
return nil
257
131
},
258
132
},
@@ -262,7 +136,7 @@ func main() {
262
136
Usage : "MQTT (TCP stream) demo" ,
263
137
Action : func (mCtx * cli.Context ) error {
264
138
fmt .Println ("----- MQTT -----" )
265
- demoRepublisher (mqttBrokerIP , mqttBrokerPort , mqttBrokerTopic , mqttBrokerMessage )
139
+ utility . Republisher (mqttBrokerIP , mqttBrokerPort , mqttBrokerTopic , mqttBrokerMessage )
266
140
return nil
267
141
},
268
142
},
0 commit comments