-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
245 lines (218 loc) · 5.4 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"github.com/spf13/viper"
)
var log_show = false
var rdr_ip = ""
var rdr_port = ""
// Handles TC connection and perform synchorinization:
// TCP -> Stdout and Stdin -> TCP
func tcp_con_handle(con net.Conn) {
chan_to_stdout := stream_copy(con, os.Stdout)
chan_to_remote := stream_copy(os.Stdin, con)
select {
case <-chan_to_stdout:
log.Println("Remote connection is closed")
case <-chan_to_remote:
log.Println("Local program is terminated")
}
}
// Performs copy operation between streams: os and tcp streams
func stream_copy(src io.Reader, dst io.Writer) <-chan int {
buf := make([]byte, 1024)
sync_channel := make(chan int)
go func() {
defer func() {
if con, ok := dst.(net.Conn); ok {
con.Close()
log.Printf("Connection from %v is closed\n", con.RemoteAddr())
}
sync_channel <- 0 // Notify that processing is finished
}()
for {
var nBytes int
var err error
nBytes, err = src.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("Read error: %s\n", err)
}
break
}
// _, err = dst.Write(buf[0:nBytes])
// if err != nil {
// log.Fatalf("Write error: %s\n", err)
// }
log.Println(buf[0:nBytes])
sHexnya := ""
for j := 0; j < nBytes; j++ {
h := ""
if buf[j] == 0 {
h = "00"
} else {
h = fmt.Sprintf("%x", buf[j])
}
sHexnya = sHexnya + " " + h
}
log.Println(sHexnya)
}
}()
return sync_channel
}
// Accept data from UPD connection and copy it to the stream
func accept_from_udp_to_stream(src net.Conn, dst io.Writer) <-chan net.Addr {
buf := make([]byte, 1024)
sync_channel := make(chan net.Addr)
con, ok := src.(*net.UDPConn)
if !ok {
log.Printf("Input must be UDP connection")
return sync_channel
}
go func() {
var remoteAddr net.Addr
for {
var nBytes int
var err error
var addr net.Addr
nBytes, addr, err = con.ReadFromUDP(buf)
if err != nil {
if err != io.EOF {
log.Printf("Read error: %s\n", err)
}
break
}
if remoteAddr == nil && remoteAddr != addr {
remoteAddr = addr
sync_channel <- remoteAddr
}
_, err = dst.Write(buf[0:nBytes])
if err != nil {
log.Fatalf("Write error: %s\n", err)
}
}
}()
log.Println("Exit write_from_udp_to_stream")
return sync_channel
}
// Put input date from the stream to UDP connection
func put_from_stream_to_udp(src io.Reader, dst net.Conn, remoteAddr net.Addr) <-chan net.Addr {
buf := make([]byte, 1024)
sync_channel := make(chan net.Addr)
go func() {
for {
var nBytes int
var err error
nBytes, err = src.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("Read error: %s\n", err)
}
break
}
log.Println("Write to the remote address:", remoteAddr)
if con, ok := dst.(*net.UDPConn); ok && remoteAddr != nil {
_, err = con.WriteTo(buf[0:nBytes], remoteAddr)
}
if err != nil {
log.Fatalf("Write error: %s\n", err)
}
}
}()
return sync_channel
}
// Handle UDP connection
func udp_con_handle(con net.Conn) {
in_channel := accept_from_udp_to_stream(con, os.Stdout)
log.Println("Waiting for remote connection")
remoteAddr := <-in_channel
log.Println("Connected from", remoteAddr)
out_channel := put_from_stream_to_udp(os.Stdin, con, remoteAddr)
select {
case <-in_channel:
log.Println("Remote connection is closed")
case <-out_channel:
log.Println("Local program is terminated")
}
}
func wrLog(msg string) {
if log_show {
log.Print(msg)
}
}
func main() {
viper.AddConfigPath("./config")
viper.SetConfigName("config") // Register config file name (no extension)
viper.SetConfigType("json") // Look for specific type
viper.ReadInConfig()
rdr_ip = viper.GetString("target_reader.ip")
rdr_port = viper.GetString("target_reader.port")
log_show = viper.GetBool("log.verbose")
isUdp := viper.GetBool("target_reader.is_udp")
//var sourcePort string
var destinationPort string
var isListen bool = false
var host string
host = rdr_ip
destinationPort = rdr_port
//arguments := os.Args
wrLog("Connected to " + rdr_ip + " PORT " + rdr_port)
log.Println("Hostname:", host)
log.Println("Port:", destinationPort)
if !isUdp {
log.Println("Work with TCP protocol")
if isListen {
listener, err := net.Listen("tcp", destinationPort)
if err != nil {
log.Fatalln(err)
}
log.Println("Listening on", destinationPort)
con, err := listener.Accept()
if err != nil {
log.Fatalln(err)
}
log.Println("Connect from", con.RemoteAddr())
tcp_con_handle(con)
} else if host != "" {
con, err := net.Dial("tcp", host+":"+destinationPort)
if err != nil {
log.Fatalln(err)
}
log.Println("Connected to", host+":"+destinationPort)
tcp_con_handle(con)
} else {
flag.Usage()
}
} else {
log.Println("Work with UDP protocol")
if isListen {
addr, err := net.ResolveUDPAddr("udp", destinationPort)
if err != nil {
log.Fatalln(err)
}
con, err := net.ListenUDP("udp", addr)
if err != nil {
log.Fatalln(err)
}
log.Println("Has been resolved UDP address:", addr)
log.Println("Listening on", destinationPort)
udp_con_handle(con)
} else if host != "" {
addr, err := net.ResolveUDPAddr("udp", host+":"+destinationPort)
if err != nil {
log.Fatalln(err)
}
log.Println("Has been resolved UDP address:", addr)
con, err := net.DialUDP("udp", nil, addr)
if err != nil {
log.Fatalln(err)
}
udp_con_handle(con)
}
}
}