@@ -8,25 +8,30 @@ import (
88)
99
1010func 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}
0 commit comments