@@ -4,33 +4,29 @@ import (
44 "bytes"
55 "log"
66 "net"
7+ "time"
78)
89
9- func forDial (fromConn net.Conn , forAddr string ) {
10- toConn , err := net .Dial ("tcp" , forAddr )
10+ func forDial (fromConn net.Conn , forAddr string , retryTimes uint8 ) {
11+ defer func () {
12+ if p := recover (); p != nil {
13+ log .Printf ("panic: %s\n " , p )
14+ if retryTimes < 5 {
15+ forDial (fromConn , forAddr , retryTimes + 1 )
16+ }
17+ }
18+ }()
19+ toConn , err := net .DialTimeout ("tcp" , forAddr , 10 * time .Second )
1120 if err != nil {
12- log .Printf ("[Bad Connection] %s to %s" , fromConn .LocalAddr ().String (), toConn .RemoteAddr ().String ())
21+ log .Printf ("[Bad Connection] %s -> %s" , fromConn .LocalAddr ().String (), toConn .RemoteAddr ().String ())
1322 toConn .Close ()
1423 return
1524 }
16- log .Printf ("[Transfer started] %s to %s" , fromConn .LocalAddr ().String (), toConn .RemoteAddr ().String ())
17- go transfer (fromConn , toConn , 1024 , true )
18- go transfer (toConn , fromConn , 1024 , false )
25+ log .Printf ("[Transfer started] %s -> %s" , fromConn .LocalAddr ().String (), toConn .RemoteAddr ().String ())
26+ go transfer (fromConn , toConn , 4096 , true )
27+ go transfer (toConn , fromConn , 4096 , false )
1928}
2029
21- /*
22- func toDial(fromConn net.Conn) {
23- toConn, err := net.Dial("tcp", fromConn.RemoteAddr().String())
24- if err != nil {
25- log.Printf("[Closed] %s to %s", fromConn.LocalAddr().String(), toConn.RemoteAddr().String())
26- toConn.Close()
27- return
28- }
29- log.Printf("%s to %s", fromConn.LocalAddr().String(), toConn.RemoteAddr().String())
30- go transfer(fromConn, toConn, 1024, true)
31- go transfer(toConn, fromConn, 1024, false)
32- }*/
33-
3430func transfer (f , t net.Conn , n int , isFrom2to bool ) {
3531 firstConn , secondConn := true , false
3632 onlineConnections ++
@@ -45,26 +41,83 @@ func transfer(f, t net.Conn, n int, isFrom2to bool) {
4541 break
4642 }
4743 if firstConn {
48- if isFrom2to && buf [1 ] == 0 {
49- addressLength := DecodeVarint (buf , 3 )
50- //log.Println(addressLength)
51- buf = bytes .Join ([][]byte {
52- buf [:3 ],
53- {(byte )(len (serverAddr ))},
54- []byte (serverAddr ),
55- {byte (serverPort >> 8 ), byte (serverPort & 0xff )}, // uint16 to []byte aka []uint8
56- buf [3 + addressLength + 2 + 1 :], // 2 is the length of 2* unsigned short (uint16)
57- }, make ([]byte , 0 ))
58- buf [0 ] = (byte )((int )(buf [0 ]) + len (serverAddr ) - addressLength )
59- count += len (serverAddr ) - addressLength
60- }
44+ packetLength , startIndex := DecodeVarint (buf , 0 )
6145 //log.Println(buf)
46+ //log.Println(packetLength)
47+ if buf [startIndex ] == 0 {
48+ if isFrom2to {
49+ /*
50+ Client first post data to the server.
51+ And the server address is included in this packet.
52+ In this situation, we need to locale the server address and change it.
53+ */
54+ addressLength , _ := DecodeVarint (buf , 3 )
55+ //log.Println(addressLength)
56+ newPacketLengthArray := EncodeVarint (packetLength + len (ServerAddr ) - addressLength )
57+ buf = bytes .Join ([][]byte {
58+ newPacketLengthArray ,
59+ buf [startIndex : startIndex + 2 ], // includes Packet ID and Protocol Version
60+ {(byte )(len (ServerAddr ))},
61+ []byte (ServerAddr ),
62+ {byte (ServerPort >> 8 ), byte (ServerPort & 0xff )}, // uint16 to []byte aka []uint8
63+ buf [3 + addressLength + 2 + 1 :], // 2 is the length of 2* unsigned short (uint16)
64+ }, make ([]byte , 0 ))
65+ count += len (ServerAddr ) - addressLength + packetLength - len (newPacketLengthArray )
66+ } //else { //TODO(MOTD FUNCTION NOT FINISHED YET)
67+ /*
68+ Server respond the ping request that requested by client.
69+ And all the motd information is included in this packet.
70+ We can rewrite it in order to change the look of the server title.
71+ */ /*
72+ jsonLength, jsonStartIndex := DecodeVarint(buf, startIndex+1)
73+ jsonStartIndex += startIndex + 1
74+ motdJson := string(buf[jsonStartIndex:count])
75+ log.Printf("origin data,%v", motdJson)
76+ motdJsonLength := len(motdJson)
77+ motdDescriptionIndex := strings.Index(motdJson, `description":`)
78+ motdFaviconIndex := strings.Index(motdJson, `favicon":`)
79+ if IsChangeDescription && IsChangeFavicon {
80+ motdJson = strings.Join([]string{
81+ motdJson[:motdDescriptionIndex-1],
82+ `description":"`,
83+ MotdDescription,
84+ `","favicon":"`,
85+ MotdFavicon,
86+ `"}`,
87+ }, "")
88+ } else if IsChangeDescription {
89+ motdJson = strings.Join([]string{
90+ motdJson[:motdDescriptionIndex-1],
91+ `description":"`,
92+ MotdDescription,
93+ `","`,
94+ motdJson[motdFaviconIndex:],
95+ }, "")
96+ } else { // IsChangeFavicon
97+ motdJson = strings.Join([]string{
98+ motdJson[:motdFaviconIndex-1],
99+ `favicon":"`,
100+ MotdFavicon,
101+ `"}`,
102+ }, "")
103+ }
104+ lengthDiscrepancy := len(motdJson) - motdJsonLength
105+ newPacketLengthArray := EncodeVarint(packetLength + lengthDiscrepancy)
106+ buf = bytes.Join([][]byte{
107+ newPacketLengthArray,
108+ {0},
109+ EncodeVarint(jsonLength + lengthDiscrepancy),
110+ []byte(motdJson),
111+ }, make([]byte, 0))
112+ count += len(newPacketLengthArray) - startIndex + lengthDiscrepancy
113+ }
114+ */
115+ }
62116 firstConn = false
63117 secondConn = true
64118 } else if secondConn {
65- log .Printf ("[ATTENTION] A new player has joined the game." )
66119 //log.Println(buf)
67- defer func () { log .Printf ("[Closed] %s to %s" , f .RemoteAddr ().String (), t .RemoteAddr ().String ()) }()
120+ defer func () { log .Printf ("[Closed] %s -> %s" , f .RemoteAddr ().String (), t .RemoteAddr ().String ()) }()
68121 secondConn = false
69122 }
70123 count , err = t .Write (buf [:count ])
0 commit comments