forked from witchfindertr/rconn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
137 lines (118 loc) · 3.05 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
package main
import (
"net"
"os"
)
func main() {
initLogger()
if len(os.Args) < 2 || (os.Args[1] != "-s" && os.Args[1] != "-c") {
Log.Infof("Usage %s: [-s remote_port local_port | -c remote_addr remote_port local_addr local_port]", os.Args[0])
os.Exit(1)
}
if os.Args[1] == "-s" {
remoteListen, err := net.Listen("tcp", "0.0.0.0:"+os.Args[2])
if err != nil {
Log.Errorf("Error listening on remote port: %s", err.Error())
os.Exit(1)
}
defer remoteListen.Close()
localListen, err := net.Listen("tcp", "0.0.0.0:"+os.Args[3])
if err != nil {
Log.Errorf("Error listening on local port: %s", err.Error())
os.Exit(1)
}
defer localListen.Close()
statusRemote := make(chan bool)
statusLocal := make(chan bool)
for {
remoteConn, err := remoteListen.Accept()
if err != nil {
Log.Errorf("Error connecting on remote port: %s", err.Error())
os.Exit(1)
}
localConn, err := localListen.Accept()
if err != nil {
Log.Errorf("Error connecting on local port: %s", err.Error())
os.Exit(1)
}
go pipeSocket(remoteConn, localConn, statusRemote)
go pipeSocket(localConn, remoteConn, statusLocal)
}
// for {
// status := <-statusLocal
// if !status {
// localConn, err = localListen.Accept()
// if err != nil {
// Log.Errorf("Error connecting on local port: %s", err.Error())
// os.Exit(1)
// }
// }
// go pipeSocket(false, statusLocal)
// }
}
if os.Args[1] == "-c" {
statusRemote := make(chan bool)
//statusLocal := make(chan bool)
for {
remoteConn, err := net.Dial("tcp", os.Args[2]+":"+os.Args[3])
if err != nil {
Log.Errorf("Error dialing to remote port: %s", err.Error())
continue
//os.Exit(1)
}
go pipeSocket(remoteConn, nil, statusRemote)
//go pipeSocket(localConn, remoteConn, statusLocal)
}
// for {
// status := <-statusLocal
// if !status {
// localConn, err = net.Dial("tcp", os.Args[4]+":"+os.Args[5])
// if err != nil {
// Log.Errorf("Error dialing to local port: %s", err.Error())
// os.Exit(1)
// }
// }
// go pipeSocket(false, statusLocal)
// }
}
}
func pipeSocket(readConn net.Conn, writeConn net.Conn, status chan<- bool) {
Log.Info("Started")
for {
//buf := make([]byte, 0x10000)
buf := make([]byte, 0x1000)
var err error
var read, write int
defer readConn.Close()
read, err = readConn.Read(buf)
if err != nil {
Log.Errorf("Read error: %s", err.Error())
status <- false
return
}
Log.Info("Read: ", read)
if writeConn == nil {
writeConn, err = net.Dial("tcp", os.Args[4]+":"+os.Args[5])
if err != nil {
Log.Errorf("Error dialing to local port: %s", err.Error())
return
//os.Exit(1)
}
go pipeSocket(writeConn, readConn, status)
}
defer writeConn.Close()
var start, written int
for written < read {
write, err = writeConn.Write(buf[start:read])
if err != nil {
Log.Errorf("Write error: %s", err.Error())
status <- false
return
}
Log.Info("Write: ", write)
written += write
start += write
}
}
Log.Info("Finished")
}