Skip to content

Commit a093157

Browse files
committed
ZBProxy 2.0 Release
1 parent 18bf3ec commit a093157

File tree

7 files changed

+121
-103
lines changed

7 files changed

+121
-103
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
matrix:
1111
# Include amd64 on all platforms.
12-
goos: [windows, freebsd, openbsd, linux, dragonfly, darwin]
12+
goos: [console, freebsd, openbsd, linux, dragonfly, darwin]
1313
goarch: [amd64, 386]
1414
exclude:
1515
# Exclude i386 on darwin and dragonfly.
@@ -30,7 +30,7 @@ jobs:
3030
goarm: 5
3131
# END Linux ARM 5 6 7
3232
# Windows ARM 7
33-
- goos: windows
33+
- goos: console
3434
goarch: arm
3535
goarm: 7
3636
# BEGIN Other architectures
@@ -71,24 +71,21 @@ jobs:
7171
uses: actions/setup-go@v2
7272
with:
7373
go-version: ^1.16.4
74-
75-
- name: Handle for Non-Windows Build
76-
if: ${{ env.GOOS != 'windows' }}
77-
run: yes | cp ./windows/kernel32.go.nonwindows ./windows/kernel32.go
78-
79-
- name: Build
80-
run: go build -v -o ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}
74+
75+
- name: Handle for Windows Build
76+
if: ${{ env.GOOS == 'windows' }}
77+
run: mv ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }} ZBLite-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}.exe
8178

8279
- name: Upload a Non-Windows Build Artifact
8380
uses: actions/[email protected]
8481
if: ${{ env.GOOS != 'windows' }}
8582
with:
8683
name: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}
8784
path: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}
88-
85+
8986
- name: Upload a Windows Build Artifact
9087
uses: actions/[email protected]
9188
if: ${{ env.GOOS == 'windows' }}
9289
with:
9390
name: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}.exe
94-
path: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}
91+
path: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}.exe

console/console_other.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package console
5+
6+
import "fmt"
7+
8+
func SetTitle(title string) {
9+
fmt.Printf("\033]0;%s\007", title)
10+
}

windows/kernel32.go renamed to console/console_windows.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package windows
1+
//go:build windows
2+
// +build windows
3+
4+
package console
25

36
import (
47
"syscall"

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"ZBProxy/windows"
4+
"ZBProxy/console"
55
"fmt"
66
"log"
77
"net"
@@ -23,7 +23,7 @@ var IsChangeDescription = MotdDescription != ""
2323
var IsChangeFavicon = (len(MotdFavicon) > 22) && (MotdFavicon[:22] == "data:image/png;base64,")
2424

2525
func main() {
26-
windows.SetTitle(fmt.Sprintf("ZBProxy %v | Loading...", Version))
26+
console.SetTitle(fmt.Sprintf("ZBProxy %v | Loading...", Version))
2727
fmt.Println(` ______ _____ _____ _____ _____ __ __ __ __
2828
|___ / | _ \ | _ \ | _ \ / _ \ \ \ / / \ \ / /
2929
/ / | |_| | | |_| | | |_| | | | | | \ \/ / \ \/ /
@@ -49,7 +49,7 @@ func main() {
4949
defer listen.Close()
5050
for {
5151
time.Sleep(time.Second)
52-
windows.SetTitle(
52+
console.SetTitle(
5353
fmt.Sprintf("ZBProxy %v | Online Players: %v", Version, onlineConnections/2))
5454
fromConn, err2 := listen.Accept()
5555
if err2 != nil {

transfer.go

Lines changed: 95 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3030
func 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

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99
)
1010

11-
const Version = "1.0"
11+
const Version = "2.0"
1212

1313
func printErr(err error) {
1414
log.Printf("Error to check for update, caution: %v.", err.Error())

windows/kernel32.go.nonwindows

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)