Skip to content

Commit

Permalink
ZBProxy 2.0 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
layou233 committed Sep 11, 2021
1 parent 18bf3ec commit a093157
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 103 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
# Include amd64 on all platforms.
goos: [windows, freebsd, openbsd, linux, dragonfly, darwin]
goos: [console, freebsd, openbsd, linux, dragonfly, darwin]
goarch: [amd64, 386]
exclude:
# Exclude i386 on darwin and dragonfly.
Expand All @@ -30,7 +30,7 @@ jobs:
goarm: 5
# END Linux ARM 5 6 7
# Windows ARM 7
- goos: windows
- goos: console
goarch: arm
goarm: 7
# BEGIN Other architectures
Expand Down Expand Up @@ -71,24 +71,21 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: ^1.16.4

- name: Handle for Non-Windows Build
if: ${{ env.GOOS != 'windows' }}
run: yes | cp ./windows/kernel32.go.nonwindows ./windows/kernel32.go

- name: Build
run: go build -v -o ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}

- name: Handle for Windows Build
if: ${{ env.GOOS == 'windows' }}
run: mv ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }} ZBLite-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}.exe

- name: Upload a Non-Windows Build Artifact
uses: actions/[email protected]
if: ${{ env.GOOS != 'windows' }}
with:
name: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}
path: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}

- name: Upload a Windows Build Artifact
uses: actions/[email protected]
if: ${{ env.GOOS == 'windows' }}
with:
name: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}.exe
path: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}
path: ZBProxy-${{ matrix.goos }}-${{ matrix.goarch }}-${{ matrix.goarm }}.exe
10 changes: 10 additions & 0 deletions console/console_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !windows
// +build !windows

package console

import "fmt"

func SetTitle(title string) {
fmt.Printf("\033]0;%s\007", title)
}
5 changes: 4 additions & 1 deletion windows/kernel32.go → console/console_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package windows
//go:build windows
// +build windows

package console

import (
"syscall"
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

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

func main() {
windows.SetTitle(fmt.Sprintf("ZBProxy %v | Loading...", Version))
console.SetTitle(fmt.Sprintf("ZBProxy %v | Loading...", Version))
fmt.Println(` ______ _____ _____ _____ _____ __ __ __ __
|___ / | _ \ | _ \ | _ \ / _ \ \ \ / / \ \ / /
/ / | |_| | | |_| | | |_| | | | | | \ \/ / \ \/ /
Expand All @@ -49,7 +49,7 @@ func main() {
defer listen.Close()
for {
time.Sleep(time.Second)
windows.SetTitle(
console.SetTitle(
fmt.Sprintf("ZBProxy %v | Online Players: %v", Version, onlineConnections/2))
fromConn, err2 := listen.Accept()
if err2 != nil {
Expand Down
176 changes: 95 additions & 81 deletions transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,105 +22,119 @@ func forDial(fromConn net.Conn, forAddr string, retryTimes uint8) {
toConn.Close()
return
}
log.Printf("[Transfer started] %s -> %s", fromConn.LocalAddr().String(), toConn.RemoteAddr().String())
log.Printf("[Transfer started] %s -> %s", fromConn.RemoteAddr().String(), toConn.RemoteAddr().String())
go transfer(fromConn, toConn, 4096, true)
go transfer(toConn, fromConn, 4096, false)
}

func transfer(f, t net.Conn, n int, isFrom2to bool) {
firstConn, secondConn := true, false
// firstConn, secondConn := true, false
onlineConnections++
defer func() { onlineConnections-- }()
defer f.Close()
defer t.Close()

var buf = make([]byte, n)
for {

if isFrom2to {
count, err := f.Read(buf)
if err != nil {
break
log.Printf("err: %s", err.Error())
return
}
if firstConn {
packetLength, startIndex := DecodeVarint(buf, 0)
//log.Println(buf)
//log.Println(packetLength)
if buf[startIndex] == 0 {
if isFrom2to {
/*
Client first post data to the server.
And the server address is included in this packet.
In this situation, we need to locale the server address and change it.
*/
addressLength, _ := DecodeVarint(buf, 3)
//log.Println(addressLength)
newPacketLengthArray := EncodeVarint(packetLength + len(ServerAddr) - addressLength)
packetLength, startIndex := DecodeVarint(buf, 0)
//log.Println(buf[:count])
//log.Println(packetLength)
if buf[startIndex] == 0 {
/*
Client first post data to the server.
And the server address is included in this packet.
In this situation, we need to locale the server address and change it.
*/
var addressLength, protocolVersionLength int
if (buf[2] & 0b10000000) == 0 {
addressLength, _ = DecodeVarint(buf, 3)
protocolVersionLength = 1
} else {
addressLength, _ = DecodeVarint(buf, 4)
protocolVersionLength = 2
}
//log.Println(addressLength)
newPacketLength := packetLength + len(ServerAddr) - addressLength
buf = bytes.Join([][]byte{
EncodeVarint(newPacketLength),
buf[startIndex : startIndex+1+protocolVersionLength], // includes Packet ID and Protocol Version
{(byte)(len(ServerAddr))},
[]byte(ServerAddr),
{byte(ServerPort >> 8), byte(ServerPort & 0xff)}, // uint16 to []byte aka []uint8
buf[3+addressLength+2+protocolVersionLength:], // 2 is the length of 2* unsigned short (uint16)
}, make([]byte, 0))
count += newPacketLength - packetLength
} //else { //TODO(MOTD FUNCTION NOT FINISHED YET)
/*
Server respond the ping request that requested by client.
And all the motd information is included in this packet.
We can rewrite it in order to change the look of the server title.
*/ /*
jsonLength, jsonStartIndex := DecodeVarint(buf, startIndex+1)
jsonStartIndex += startIndex + 1
motdJson := string(buf[jsonStartIndex:count])
log.Printf("origin data,%v", motdJson)
motdJsonLength := len(motdJson)
motdDescriptionIndex := strings.Index(motdJson, `description":`)
motdFaviconIndex := strings.Index(motdJson, `favicon":`)
if IsChangeDescription && IsChangeFavicon {
motdJson = strings.Join([]string{
motdJson[:motdDescriptionIndex-1],
`description":"`,
MotdDescription,
`","favicon":"`,
MotdFavicon,
`"}`,
}, "")
} else if IsChangeDescription {
motdJson = strings.Join([]string{
motdJson[:motdDescriptionIndex-1],
`description":"`,
MotdDescription,
`","`,
motdJson[motdFaviconIndex:],
}, "")
} else { // IsChangeFavicon
motdJson = strings.Join([]string{
motdJson[:motdFaviconIndex-1],
`favicon":"`,
MotdFavicon,
`"}`,
}, "")
}
lengthDiscrepancy := len(motdJson) - motdJsonLength
newPacketLengthArray := EncodeVarint(packetLength + lengthDiscrepancy)
buf = bytes.Join([][]byte{
newPacketLengthArray,
buf[startIndex : startIndex+2], // includes Packet ID and Protocol Version
{(byte)(len(ServerAddr))},
[]byte(ServerAddr),
{byte(ServerPort >> 8), byte(ServerPort & 0xff)}, // uint16 to []byte aka []uint8
buf[3+addressLength+2+1:], // 2 is the length of 2* unsigned short (uint16)
{0},
EncodeVarint(jsonLength + lengthDiscrepancy),
[]byte(motdJson),
}, make([]byte, 0))
count += len(ServerAddr) - addressLength + packetLength - len(newPacketLengthArray)
} //else { //TODO(MOTD FUNCTION NOT FINISHED YET)
/*
Server respond the ping request that requested by client.
And all the motd information is included in this packet.
We can rewrite it in order to change the look of the server title.
*/ /*
jsonLength, jsonStartIndex := DecodeVarint(buf, startIndex+1)
jsonStartIndex += startIndex + 1
motdJson := string(buf[jsonStartIndex:count])
log.Printf("origin data,%v", motdJson)
motdJsonLength := len(motdJson)
motdDescriptionIndex := strings.Index(motdJson, `description":`)
motdFaviconIndex := strings.Index(motdJson, `favicon":`)
if IsChangeDescription && IsChangeFavicon {
motdJson = strings.Join([]string{
motdJson[:motdDescriptionIndex-1],
`description":"`,
MotdDescription,
`","favicon":"`,
MotdFavicon,
`"}`,
}, "")
} else if IsChangeDescription {
motdJson = strings.Join([]string{
motdJson[:motdDescriptionIndex-1],
`description":"`,
MotdDescription,
`","`,
motdJson[motdFaviconIndex:],
}, "")
} else { // IsChangeFavicon
motdJson = strings.Join([]string{
motdJson[:motdFaviconIndex-1],
`favicon":"`,
MotdFavicon,
`"}`,
}, "")
}
lengthDiscrepancy := len(motdJson) - motdJsonLength
newPacketLengthArray := EncodeVarint(packetLength + lengthDiscrepancy)
buf = bytes.Join([][]byte{
newPacketLengthArray,
{0},
EncodeVarint(jsonLength + lengthDiscrepancy),
[]byte(motdJson),
}, make([]byte, 0))
count += len(newPacketLengthArray) - startIndex + lengthDiscrepancy
}
*/
}
firstConn = false
secondConn = true
} else if secondConn {
//log.Println(buf)
defer func() { log.Printf("[Closed] %s -> %s", f.RemoteAddr().String(), t.RemoteAddr().String()) }()
secondConn = false
count += len(newPacketLengthArray) - startIndex + lengthDiscrepancy
}
}*/

//log.Println(buf[:count])
_, err = t.Write(buf[:count])
if err != nil {
log.Printf("err: %s", err.Error())
return
}
}
defer func() { log.Printf("[Closed] %s -> %s", f.RemoteAddr().String(), t.RemoteAddr().String()) }()

for {
count, err := f.Read(buf)
if err != nil {
break
}
count, err = t.Write(buf[:count])
_, err = t.Write(buf[:count])
if err != nil {
log.Printf("err: %s", err.Error())
break
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

const Version = "1.0"
const Version = "2.0"

func printErr(err error) {
log.Printf("Error to check for update, caution: %v.", err.Error())
Expand Down
6 changes: 0 additions & 6 deletions windows/kernel32.go.nonwindows

This file was deleted.

0 comments on commit a093157

Please sign in to comment.