Skip to content

Commit b76cd7a

Browse files
committed
Release 1.0
1 parent 99cb0bb commit b76cd7a

File tree

4 files changed

+153
-55
lines changed

4 files changed

+153
-55
lines changed

main.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,54 @@ import (
1212
var onlineConnections = 0
1313

1414
const (
15-
version = "1.0-SNAPSHOT"
16-
17-
serverAddr = "mc.hypixel.net"
18-
serverPort uint16 = 25565 // this must be uint16 (unsigned short) to be compatible with the protocol
19-
localPort uint16 = 25565
15+
ServerAddr = "mc.hypixel.net"
16+
ServerPort uint16 = 25565 // this must be uint16 (unsigned short) to be compatible with the protocol
17+
LocalPort uint16 = 25565
18+
MotdDescription = ""
19+
MotdFavicon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAeZQTFRF/+IAzrcHKyodHx8fR0IZ89cC2MAFKikdw64IIyMeXFQX/eAAxa8IIiIfrpwLISAfgHQRnYwOuKQKaF4VJyYecGUUj4AQ99sBUUsYMS4d38YE+98AWVEXrJkM7dMCQDsaPzsb9NgCTkgYuqYJ3sYEUEoYODQcJiUe0LkHW1MX+94B178GMC4dcmgTwq0I5s0DpJINoZAN69shjLi/i7i/ZZfEU4fHVIfHeqnB0NFOZqr/f7PWY1sVtcd7n7+g+NwBm76nv8tq7tMCRkEagLPU4Nc06M4DPjob4McE49cvgLTUxMxjnL6lpMCYi6B7s8d/ssZ/a4iJQmWPZaj7NUdcXnNvYJ7sMkVcu8pxW5XdLjtMNjMcssaBVovNKTI9qMORUYG+JCgtnr6hTHiu+N8MlbuxR26f7twci7fBQ2SPfXES5dgsgbTRPVuA29U8ebLgxMxdiHoQ0tFLb63w0dFMOFFverHe3NU6PVl9hLTN5tkpQWKLj7m78d0XRWqZmb2q++EGSnOnICIkTny0rsSHqpgMJSoxUoTCuch1KTNAV43QZHlxSXKkcY6IdrDl+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FuULbS8VhCc/mKmM78ZQOKzqpIX4ln0lsZvBmHRQrMDCzVOAJEWtihbKD+pjMgymROuxsTI4yeFADonvA1ywCR3KFgmc8LVhCCIOxQu80DlEm8kvtHYepwdU42Yyfwbja+zb20v8VT4jfYfTXskk4+wbR0AAAAASUVORK5CYII="
2020
)
2121

22+
var IsChangeDescription = MotdDescription != ""
23+
var IsChangeFavicon = (len(MotdFavicon) > 22) && (MotdFavicon[:22] == "data:image/png;base64,")
24+
2225
func main() {
23-
windows.SetTitle(fmt.Sprintf("ZBProxy %v | Loading...", version))
24-
fmt.Printf("Welcome to ZBProxy %s!\n\n", version)
26+
windows.SetTitle(fmt.Sprintf("ZBProxy %v | Loading...", Version))
27+
fmt.Println(` ______ _____ _____ _____ _____ __ __ __ __
28+
|___ / | _ \ | _ \ | _ \ / _ \ \ \ / / \ \ / /
29+
/ / | |_| | | |_| | | |_| | | | | | \ \/ / \ \/ /
30+
/ / | _ { | ___/ | _ / | | | | } { \ /
31+
/ /__ | |_| | | | | | \ \ | |_| | / /\ \ / /
32+
/_____| |_____/ |_| |_| \_\ \_____/ /_/ \_\ /_/`)
33+
fmt.Printf("Welcome to ZBProxy %s!\n\n", Version)
34+
go CheckUpdate()
2535
fmt.Println("Your current settings:")
26-
fmt.Println("serverAddr=" + serverAddr)
27-
fmt.Printf("serverPort=%d\n", serverPort)
28-
fmt.Printf("localPort=%d\n", localPort)
36+
fmt.Println("ServerAddr=" + ServerAddr)
37+
fmt.Printf("ServerPort=%d\n", ServerPort)
38+
fmt.Printf("LocalPort=%d\n", LocalPort)
2939

30-
log.Printf("Starting listening on local port %d...", localPort)
31-
listen, err1 := net.Listen("tcp", fmt.Sprintf(":%d", localPort))
40+
log.Printf("Starting listening on local port %d...", LocalPort)
41+
listen, err1 := net.Listen("tcp", fmt.Sprintf(":%d", LocalPort))
3242
if err1 != nil {
33-
log.Printf("Unable to listen on port %d.\n", localPort)
43+
log.Printf("Unable to listen on port %d.\n", LocalPort)
3444
log.Printf("Caution: %v\n", err1.Error())
3545
log.Printf("The program will exit in 5 seconds.")
3646
time.Sleep(5 * time.Second)
3747
os.Exit(1)
3848
}
3949
defer listen.Close()
4050
for {
41-
time.Sleep(time.Second)
51+
//time.Sleep(time.Second)
4252
windows.SetTitle(
43-
fmt.Sprintf("ZBProxy %v | Online Connections: %v", version, onlineConnections/2))
53+
fmt.Sprintf("ZBProxy %v | Online Players: %v", Version, onlineConnections/2))
4454
fromConn, err2 := listen.Accept()
4555
if err2 != nil {
4656
continue
4757
}
48-
serverIp, err3 := net.ResolveIPAddr("ip4", serverAddr)
58+
serverIp, err3 := net.ResolveIPAddr("ip4", ServerAddr)
4959
if err3 != nil {
50-
log.Printf("Can't resolve hostname: %v", serverAddr)
60+
log.Printf("Can't resolve hostname: %v", ServerAddr)
5161
continue
5262
}
53-
go forDial(fromConn, fmt.Sprintf("%s:%d", serverIp.String(), serverPort))
63+
go forDial(fromConn, fmt.Sprintf("%s:%d", serverIp.String(), ServerPort), 0)
5464
}
5565
}

transfer.go

Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
3430
func 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])

varint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func EncodeVarint(x int) []byte {
3232

3333
// DecodeVarint decodes the Varint and returns a int typed value.
3434
// This will read bytes from the given index and read most at 5 bytes.
35-
func DecodeVarint(buf []byte, index int) int {
36-
numRead, result := 0, 0
35+
func DecodeVarint(buf []byte, index int) (result int, numRead int) {
36+
numRead, result = 0, 0
3737
var read byte
3838
_tk := true // Simulate do-while
3939
for ((read & 0b10000000) != 0) || _tk {
@@ -47,7 +47,7 @@ func DecodeVarint(buf []byte, index int) int {
4747
break
4848
}
4949
}
50-
return result
50+
return
5151
}
5252

5353
/*

version.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"strings"
9+
)
10+
11+
const Version = "1.0"
12+
13+
func printErr(err error) {
14+
log.Printf("Error to check for update, caution: %v.", err.Error())
15+
log.Println(`You can check it yourself at https://github.com/layou233/ZBProxy/releases`)
16+
}
17+
18+
func CheckUpdate() {
19+
resp, err := http.Get(`https://raw.githubusercontent.com/layou233/ZBProxy/master/version.go`)
20+
if err != nil {
21+
printErr(err)
22+
return
23+
}
24+
defer resp.Body.Close()
25+
body, err := ioutil.ReadAll(resp.Body)
26+
if err != nil {
27+
printErr(err)
28+
return
29+
}
30+
if strings.Contains(string(body), Version) {
31+
fmt.Println("Your ZBProxy is up-to-date. Have fun!")
32+
} else {
33+
fmt.Println("Your ZBProxy is out of date! Check for the latest version at https://github.com/layou233/ZBProxy/releases")
34+
}
35+
}

0 commit comments

Comments
 (0)