Skip to content

Commit e54fb00

Browse files
author
dushixiang
committed
使用channel优化代码逻辑,复用函数
1 parent 5b80279 commit e54fb00

File tree

1 file changed

+44
-69
lines changed

1 file changed

+44
-69
lines changed

4dnat.go

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"net"
77
"os"
8+
"strings"
89
"sync"
910
"time"
1011
)
@@ -67,82 +68,44 @@ func copyIO(src, dest net.Conn, wg *sync.WaitGroup) {
6768
func listener(listenPort0, listenPort1 string) {
6869
ln0 := listen(listenPort0)
6970
ln1 := listen(listenPort1)
70-
fmt.Printf("[#] 4dnat listen port on: [%s] [%s]\n", listenPort0, listenPort1)
71+
fmt.Printf("[#] 4dnat listen port on: [%s %s],[%s %s]\n", ln0.Addr().Network(), ln0.Addr().String(), ln1.Addr().Network(), ln1.Addr().String())
7172

7273
for true {
7374
cc := make(chan net.Conn, 2)
7475

75-
go loopAccept(cc, listenPort0, ln0)
76-
go loopAccept(cc, listenPort1, ln1)
76+
go accept(cc, ln0)
77+
go accept(cc, ln1)
7778

7879
conn0, conn1 := <-cc, <-cc
7980
mutualCopyIO(conn0, conn1)
8081
}
81-
82-
}
83-
84-
func loopAccept(cc chan net.Conn, listenPort string, ln net.Listener) {
85-
for true {
86-
fmt.Printf("[#] 4dnat waiting for client to connect port [%s]\n", listenPort)
87-
c, err := accept(ln)
88-
if err != nil {
89-
continue
90-
}
91-
cc <- c
92-
break
93-
}
9482
}
9583

9684
func forward(listenPort string, targetAddress string) {
9785
ln := listen(listenPort)
98-
fmt.Printf("[#] 4dnat listen on: [%s] forward to: [%s]\n", listenPort, targetAddress)
99-
for {
100-
conn0, err := accept(ln)
101-
if err != nil {
102-
continue
103-
}
104-
go func() {
105-
fmt.Printf("[#] try to connect: [%s] \n", targetAddress)
106-
// after server accept will be connect the target address,if failed will be retry.
107-
for true {
108-
conn1, err := dial(targetAddress)
109-
if err != nil {
110-
fmt.Printf("[#] retry to connect: [%s] after [%d] second\n", targetAddress, RetryInterval)
111-
time.Sleep(time.Duration(RetryInterval) * time.Second)
112-
continue
113-
}
114-
115-
go mutualCopyIO(conn0, conn1)
116-
break
117-
}
118-
}()
86+
fmt.Printf("[#] 4dnat listen on: [%s %s] forward to: [%s]\n", ln.Addr().Network(), ln.Addr().String(), targetAddress)
87+
for true {
88+
cc := make(chan net.Conn, 2)
89+
90+
go accept(cc, ln)
91+
go dial(cc, targetAddress)
92+
93+
conn0, conn1 := <-cc, <-cc
94+
go mutualCopyIO(conn0, conn1)
11995
}
12096
}
12197

12298
func agent(targetAddress0 string, targetAddress1 string) {
123-
12499
fmt.Printf("[#] 4dnat agent with: [%s] [%s]\n", targetAddress0, targetAddress1)
125-
126-
var conn0 net.Conn = nil
127100
for {
128-
if conn0 == nil {
129-
conn, err0 := dial(targetAddress0)
130-
if err0 != nil {
131-
time.Sleep(time.Duration(RetryInterval) * time.Second)
132-
continue
133-
}
134-
conn0 = conn
135-
}
101+
cc := make(chan net.Conn, 2)
136102

137-
conn1, err1 := dial(targetAddress1)
138-
if err1 != nil {
139-
time.Sleep(time.Duration(RetryInterval) * time.Second)
140-
continue
141-
}
103+
go dial(cc, targetAddress0)
104+
go dial(cc, targetAddress1)
142105

106+
conn0, conn1 := <-cc, <-cc
143107
mutualCopyIO(conn0, conn1)
144108
}
145-
146109
}
147110

148111
func mutualCopyIO(conn0, conn1 net.Conn) {
@@ -153,28 +116,40 @@ func mutualCopyIO(conn0, conn1 net.Conn) {
153116
wg.Wait()
154117
}
155118

156-
func dial(targetAddress string) (net.Conn, error) {
157-
conn, err := net.Dial("tcp", targetAddress)
158-
if err != nil {
159-
fmt.Printf("[x] connect [%s] error [%s].\n", targetAddress, err.Error())
160-
return conn, err
119+
func dial(cc chan net.Conn, targetAddress string) {
120+
for true {
121+
conn, err := net.Dial("tcp", targetAddress)
122+
if err != nil {
123+
fmt.Printf("[x] connect [%s] error [%s].\n", targetAddress, err.Error())
124+
fmt.Printf("[#] retry to connect: [%s] after [%d] second\n", targetAddress, RetryInterval)
125+
time.Sleep(time.Duration(RetryInterval) * time.Second)
126+
continue
127+
}
128+
fmt.Printf("[+] [%s]->[%s] connected to target.\n", conn.LocalAddr().String(), targetAddress)
129+
cc <- conn
130+
break
161131
}
162-
fmt.Printf("[+] [%s]->[%s] connected to target.\n", conn.LocalAddr().String(), targetAddress)
163-
return conn, err
164132
}
165133

166-
func accept(ln net.Listener) (net.Conn, error) {
167-
conn, err := ln.Accept()
168-
if err != nil {
169-
fmt.Printf("[x] accept error [%s].\n", err.Error())
170-
return nil, err
134+
func accept(cc chan net.Conn, ln net.Listener) {
135+
for true {
136+
fmt.Printf("[#] 4dnat waiting for client to connect [%s %s]\n", ln.Addr().Network(), ln.Addr().String())
137+
c, err := ln.Accept()
138+
if err != nil {
139+
fmt.Printf("[x] accept error [%s].\n", err.Error())
140+
continue
141+
}
142+
fmt.Printf("[+] [%s]<-[%s] new client connected.\n", c.LocalAddr().String(), c.RemoteAddr().String())
143+
cc <- c
144+
break
171145
}
172-
fmt.Printf("[+] [%s]<-[%s] new client connected.\n", conn.LocalAddr().String(), conn.RemoteAddr().String())
173-
return conn, nil
174146
}
175147

176148
func listen(listenPort string) net.Listener {
177-
ln, err := net.Listen("tcp", ":"+listenPort)
149+
if !strings.Contains(listenPort, ":") {
150+
listenPort = "0.0.0.0:" + listenPort
151+
}
152+
ln, err := net.Listen("tcp", listenPort)
178153
if err != nil {
179154
fmt.Printf("[x] listen error [%s].\n", err.Error())
180155
os.Exit(0)

0 commit comments

Comments
 (0)