Skip to content

Commit 8fbfcf8

Browse files
committed
Bug fix
1 parent 0743304 commit 8fbfcf8

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

handler/client.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,30 @@ import (
88
)
99

1010
func PublicPort(localPort, remotePort int) {
11+
fmt.Println("Go Public client started.")
1112
conn, err := net.Dial("tcp", common.ClientConfig.Host+":"+strconv.Itoa(common.ClientConfig.Port))
1213
if err != nil {
1314
fmt.Println("Failed to connect to server:", err.Error())
1415
return
1516
}
16-
fmt.Println("Connected to server with remote port", remotePort)
17+
fmt.Printf("Connected to %s:%d\n", common.ClientConfig.Host, common.ClientConfig.Port)
1718
err = sendHelloPacket(conn, remotePort)
1819
if err != nil {
1920
fmt.Println("Failed to send hello:", err.Error())
2021
return
2122
}
22-
fmt.Println("Hello packet sent")
23-
buf := make([]byte, ConnPacketSize)
23+
fmt.Printf("Ready to public local port %d to %s:%d\n", localPort, common.ClientConfig.Host, remotePort)
2424
for {
25-
_, err := conn.Read(buf)
25+
buf := make([]byte, ConnPacketSize) // do not move buf outside this for loop, it brings a bug
26+
n, err := conn.Read(buf)
2627
if err != nil {
27-
fmt.Println(err.Error())
28+
fmt.Println("Failed to read connection packet:", err.Error())
29+
fmt.Println("Abort.")
2830
return
2931
}
32+
if n != ConnPacketSize {
33+
fmt.Println("Warning: mismatched packet size.")
34+
}
3035
// Should be a connection packet
3136
localConn, err := net.Dial("tcp", "localhost:"+strconv.Itoa(localPort))
3237
if err != nil {
@@ -38,13 +43,15 @@ func PublicPort(localPort, remotePort int) {
3843
fmt.Println("Failed to connect to remote server:", err.Error())
3944
continue
4045
}
46+
fmt.Printf("Connection established: %s <-> %s\n", localConn.LocalAddr().String(), remoteConn.LocalAddr().String())
4147
go func() {
4248
_, err := remoteConn.Write(buf)
4349
if err != nil {
4450
fmt.Println("Failed to send connection packet:", err.Error())
51+
return
4552
}
53+
go forward(localConn, remoteConn)
54+
go forward(remoteConn, localConn)
4655
}()
47-
go forward(localConn, remoteConn)
48-
go forward(remoteConn, localConn)
4956
}
5057
}

handler/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func ServeForever() {
11-
fmt.Println("Go Public server started.")
11+
fmt.Printf("Go Public server started at port %d.\n", common.ServerConfig.Port)
1212
addr := net.TCPAddr{
1313
IP: nil,
1414
Port: common.ServerConfig.Port,
@@ -19,7 +19,7 @@ func ServeForever() {
1919
os.Exit(1)
2020
}
2121
defer listener.Close()
22-
fmt.Printf("Server listening on port %d, waiting for connections...\n", common.ServerConfig.Port)
22+
fmt.Printf("Waiting for connections...\n")
2323
for {
2424
conn, err := listener.Accept()
2525
if err != nil {
@@ -68,7 +68,7 @@ func handleHelloPacket(conn net.Conn, buf []byte) {
6868
println(err.Error())
6969
continue
7070
}
71-
fmt.Printf("[%d] Connection %p established.\n", port, userConn)
71+
//fmt.Printf("[%d] Connection %p established.\n", port, userConn)
7272
token := common.GenerateToken()
7373
store.add(token, &userConn)
7474
err = SendConnPacket(conn, token)
@@ -82,10 +82,12 @@ func handleHelloPacket(conn net.Conn, buf []byte) {
8282
func handleConnPacket(conn net.Conn, buf []byte) {
8383
uuid := common.Bytes2Token(buf[1 : 1+tokenSize])
8484
userConn := store.get(uuid)
85+
store.remove(uuid)
8586
if userConn == nil {
8687
fmt.Println("Invalid UUID:", uuid)
8788
return
8889
}
90+
fmt.Printf("Connection established: %s <-> %s\n", conn.RemoteAddr().String(), (*userConn).RemoteAddr().String())
8991
go forward(*userConn, conn)
9092
forward(conn, *userConn)
9193
}

0 commit comments

Comments
 (0)