@@ -22,105 +22,119 @@ func forDial(fromConn net.Conn, forAddr string, retryTimes uint8) {
2222 toConn .Close ()
2323 return
2424 }
25- log .Printf ("[Transfer started] %s -> %s" , fromConn .LocalAddr ().String (), toConn .RemoteAddr ().String ())
25+ log .Printf ("[Transfer started] %s -> %s" , fromConn .RemoteAddr ().String (), toConn .RemoteAddr ().String ())
2626 go transfer (fromConn , toConn , 4096 , true )
2727 go transfer (toConn , fromConn , 4096 , false )
2828}
2929
3030func transfer (f , t net.Conn , n int , isFrom2to bool ) {
31- firstConn , secondConn := true , false
31+ // firstConn, secondConn := true, false
3232 onlineConnections ++
3333 defer func () { onlineConnections -- }()
3434 defer f .Close ()
3535 defer t .Close ()
3636
3737 var buf = make ([]byte , n )
38- for {
38+
39+ if isFrom2to {
3940 count , err := f .Read (buf )
4041 if err != nil {
41- break
42+ log .Printf ("err: %s" , err .Error ())
43+ return
4244 }
43- if firstConn {
44- packetLength , startIndex := DecodeVarint (buf , 0 )
45- //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 )
45+ packetLength , startIndex := DecodeVarint (buf , 0 )
46+ //log.Println(buf[:count])
47+ //log.Println(packetLength)
48+ if buf [startIndex ] == 0 {
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+ var addressLength , protocolVersionLength int
55+ if (buf [2 ] & 0b10000000 ) == 0 {
56+ addressLength , _ = DecodeVarint (buf , 3 )
57+ protocolVersionLength = 1
58+ } else {
59+ addressLength , _ = DecodeVarint (buf , 4 )
60+ protocolVersionLength = 2
61+ }
62+ //log.Println(addressLength)
63+ newPacketLength := packetLength + len (ServerAddr ) - addressLength
64+ buf = bytes .Join ([][]byte {
65+ EncodeVarint (newPacketLength ),
66+ buf [startIndex : startIndex + 1 + protocolVersionLength ], // includes Packet ID and Protocol Version
67+ {(byte )(len (ServerAddr ))},
68+ []byte (ServerAddr ),
69+ {byte (ServerPort >> 8 ), byte (ServerPort & 0xff )}, // uint16 to []byte aka []uint8
70+ buf [3 + addressLength + 2 + protocolVersionLength :], // 2 is the length of 2* unsigned short (uint16)
71+ }, make ([]byte , 0 ))
72+ count += newPacketLength - packetLength
73+ } //else { //TODO(MOTD FUNCTION NOT FINISHED YET)
74+ /*
75+ Server respond the ping request that requested by client.
76+ And all the motd information is included in this packet.
77+ We can rewrite it in order to change the look of the server title.
78+ */ /*
79+ jsonLength, jsonStartIndex := DecodeVarint(buf, startIndex+1)
80+ jsonStartIndex += startIndex + 1
81+ motdJson := string(buf[jsonStartIndex:count])
82+ log.Printf("origin data,%v", motdJson)
83+ motdJsonLength := len(motdJson)
84+ motdDescriptionIndex := strings.Index(motdJson, `description":`)
85+ motdFaviconIndex := strings.Index(motdJson, `favicon":`)
86+ if IsChangeDescription && IsChangeFavicon {
87+ motdJson = strings.Join([]string{
88+ motdJson[:motdDescriptionIndex-1],
89+ `description":"`,
90+ MotdDescription,
91+ `","favicon":"`,
92+ MotdFavicon,
93+ `"}`,
94+ }, "")
95+ } else if IsChangeDescription {
96+ motdJson = strings.Join([]string{
97+ motdJson[:motdDescriptionIndex-1],
98+ `description":"`,
99+ MotdDescription,
100+ `","`,
101+ motdJson[motdFaviconIndex:],
102+ }, "")
103+ } else { // IsChangeFavicon
104+ motdJson = strings.Join([]string{
105+ motdJson[:motdFaviconIndex-1],
106+ `favicon":"`,
107+ MotdFavicon,
108+ `"}`,
109+ }, "")
110+ }
111+ lengthDiscrepancy := len(motdJson) - motdJsonLength
112+ newPacketLengthArray := EncodeVarint(packetLength + lengthDiscrepancy)
57113 buf = bytes.Join([][]byte{
58114 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)
115+ {0},
116+ EncodeVarint(jsonLength + lengthDiscrepancy),
117+ []byte(motdJson),
64118 }, 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- }
116- firstConn = false
117- secondConn = true
118- } else if secondConn {
119- //log.Println(buf)
120- defer func () { log .Printf ("[Closed] %s -> %s" , f .RemoteAddr ().String (), t .RemoteAddr ().String ()) }()
121- secondConn = false
119+ count += len(newPacketLengthArray) - startIndex + lengthDiscrepancy
120+ }
121+ }*/
122+
123+ //log.Println(buf[:count])
124+ _ , err = t .Write (buf [:count ])
125+ if err != nil {
126+ log .Printf ("err: %s" , err .Error ())
127+ return
128+ }
129+ }
130+ defer func () { log .Printf ("[Closed] %s -> %s" , f .RemoteAddr ().String (), t .RemoteAddr ().String ()) }()
131+
132+ for {
133+ count , err := f .Read (buf )
134+ if err != nil {
135+ break
122136 }
123- count , err = t .Write (buf [:count ])
137+ _ , err = t .Write (buf [:count ])
124138 if err != nil {
125139 log .Printf ("err: %s" , err .Error ())
126140 break
0 commit comments