@@ -10,13 +10,117 @@ package main
10
10
import (
11
11
"fmt"
12
12
"log"
13
+ "net"
13
14
"os"
15
+ "time"
14
16
15
17
"github.com/urfave/cli/v2"
16
18
"rsc.io/quote"
17
19
)
18
20
19
- func main () {
21
+ func newClient (diodeInputSideIP string , diodeTcpPassthroughPort int ) {
22
+ // Create a socket
23
+
24
+ client , err := net .DialTimeout ("tcp" , fmt .Sprintf ("%s:%d" , diodeInputSideIP , diodeTcpPassthroughPort ), time .Second )
25
+
26
+ if err != nil {
27
+ fmt .Println (">> Error establishing connection to the diode input side: " , err .Error ())
28
+ log .Fatal (err )
29
+ }
30
+ defer client .Close ()
31
+
32
+ numberOfSends := 1
33
+
34
+ for {
35
+ sendMessage := fmt .Sprintf ("This is TCP passthrough test message number: %d" , numberOfSends )
36
+ _ , err := client .Write ([]byte (sendMessage ))
37
+ if err != nil {
38
+ fmt .Println (">> Error sending message to the diode input side: " , err .Error ())
39
+ log .Fatal (err )
40
+ break
41
+ }
42
+
43
+ // if string(response) == "OK\r\n" {
44
+ // fmt.Println(">> Message sent successfully!")
45
+ // }
46
+
47
+ numberOfSends ++
48
+
49
+ time .Sleep (1 * time .Second )
50
+ }
51
+ }
52
+
53
+ func newServer (targetTcpServerIP string , targetTcpServerPort int ) {
54
+ // Begin listening for incoming connections
55
+
56
+ server , err := net .Listen ("tcp" , fmt .Sprintf ("%s:%d" , targetTcpServerIP , targetTcpServerPort ))
57
+
58
+ if err != nil {
59
+ fmt .Println (">> Error listening for incoming connections: " , err .Error ())
60
+ return
61
+ }
62
+ defer server .Close ()
63
+
64
+ fmt .Printf (">> Server listening on %s:%d\n " , targetTcpServerIP , targetTcpServerPort )
65
+
66
+ for {
67
+ // Wait for connection
68
+ connection , err := server .Accept ()
69
+
70
+ if err != nil {
71
+ fmt .Println (">> Error accepting connection: " , err .Error ())
72
+ return
73
+ }
74
+
75
+ fmt .Println ("Connected to client IP:" , connection .RemoteAddr ().String ())
76
+
77
+ go communicationHandler (connection )
78
+
79
+ }
80
+
81
+ }
82
+
83
+ func communicationHandler (connection net.Conn ) {
84
+
85
+ defer connection .Close ()
86
+
87
+ // Buffer for incoming data (holding recieved data)
88
+ buffer := make ([]byte , 10240 )
89
+
90
+ for {
91
+ // Read incoming data into buffer
92
+ bytesRead , err := connection .Read (buffer )
93
+ if err != nil {
94
+ fmt .Println (">> Error reading: " , err .Error ())
95
+ break
96
+ }
97
+
98
+ if bytesRead > 0 {
99
+ fmt .Println (">> Message recieved: " , string (buffer [:bytesRead ]))
100
+ }
101
+
102
+ if bytesRead < 10240 {
103
+ break
104
+ }
105
+ }
106
+
107
+ }
108
+
109
+ func sampleMetrics () {
110
+ fmt .Println (">> Local time: " , time .Now ())
111
+ fmt .Println (">> UTC time: " , time .Now ().UTC ())
112
+ }
113
+
114
+ func main () {
115
+
116
+ // Configuration Options
117
+
118
+ diodeInputSideIP := "192.168.1.99"
119
+ diodeTcpPassthroughPort := 50000
120
+
121
+ targetTcpServerIP := "192.168.1.20"
122
+ targetTcpServerPort := 503
123
+
20
124
app := & cli.App {
21
125
Name : "diode" ,
22
126
Usage : "A command line tool for interacting with data diodes." ,
@@ -30,7 +134,8 @@ func main () {
30
134
Aliases : []string {"c" },
31
135
Usage : "Input side of the data diode" ,
32
136
Action : func (cCtx * cli.Context ) error {
33
- fmt .Println (">> INPUT" )
137
+ fmt .Println ("----- INPUT -----" )
138
+ newClient (diodeInputSideIP , diodeTcpPassthroughPort )
34
139
return nil
35
140
},
36
141
},
@@ -39,7 +144,8 @@ func main () {
39
144
Aliases : []string {"s" },
40
145
Usage : "Output side of the data diode" ,
41
146
Action : func (sCtx * cli.Context ) error {
42
- fmt .Println (">> OUTPUT" )
147
+ fmt .Println ("----- OUTPUT -----" )
148
+ newServer (targetTcpServerIP , targetTcpServerPort )
43
149
return nil
44
150
},
45
151
},
@@ -48,7 +154,11 @@ func main () {
48
154
Aliases : []string {"d" },
49
155
Usage : "Debug diagnostics via configuration settings" ,
50
156
Action : func (dCtx * cli.Context ) error {
51
- fmt .Println (">> DIAGNOSTICS" )
157
+ fmt .Println ("----- DIAGNOSTICS -----" )
158
+ input := fmt .Sprintf ("%s:%d" , diodeInputSideIP , diodeTcpPassthroughPort )
159
+ output := fmt .Sprintf ("%s:%d" , targetTcpServerIP , targetTcpServerPort )
160
+ fmt .Println (">> Client: " , input )
161
+ fmt .Println (">> Server: " , output )
52
162
return nil
53
163
},
54
164
},
@@ -57,16 +167,17 @@ func main () {
57
167
Aliases : []string {"b" },
58
168
Usage : "System benchmark analysis + report performance metrics" ,
59
169
Action : func (bCtx * cli.Context ) error {
60
- fmt .Println (">> BENCHMARKS" )
170
+ fmt .Println ("----- BENCHMARKS -----" )
171
+ sampleMetrics ()
61
172
return nil
62
173
},
63
174
},
64
175
{
65
- Name : "version" ,
176
+ Name : "version" ,
66
177
Aliases : []string {"v" },
67
- Usage : "Print the version of the diode CLI" ,
178
+ Usage : "Print the version of the diode CLI" ,
68
179
Action : func (vCtx * cli.Context ) error {
69
- fmt .Println (">> diode version 0.0.1 " )
180
+ fmt .Println (">> diode version 0.0.2 " )
70
181
return nil
71
182
},
72
183
},
0 commit comments