diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7dda791..e4d7fdc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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. @@ -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 @@ -71,13 +71,10 @@ 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/upload-artifact@v2.2.4 @@ -85,10 +82,10 @@ jobs: 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/upload-artifact@v2.2.4 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 diff --git a/console/console_other.go b/console/console_other.go new file mode 100644 index 0000000..c7267a0 --- /dev/null +++ b/console/console_other.go @@ -0,0 +1,10 @@ +//go:build !windows +// +build !windows + +package console + +import "fmt" + +func SetTitle(title string) { + fmt.Printf("\033]0;%s\007", title) +} diff --git a/windows/kernel32.go b/console/console_windows.go similarity index 80% rename from windows/kernel32.go rename to console/console_windows.go index acd591f..9c939c7 100644 --- a/windows/kernel32.go +++ b/console/console_windows.go @@ -1,4 +1,7 @@ -package windows +//go:build windows +// +build windows + +package console import ( "syscall" diff --git a/main.go b/main.go index 3b1b457..bab8fc9 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "ZBProxy/windows" + "ZBProxy/console" "fmt" "log" "net" @@ -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(` ______ _____ _____ _____ _____ __ __ __ __ |___ / | _ \ | _ \ | _ \ / _ \ \ \ / / \ \ / / / / | |_| | | |_| | | |_| | | | | | \ \/ / \ \/ / @@ -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 { diff --git a/transfer.go b/transfer.go index f9541ea..3a2629e 100644 --- a/transfer.go +++ b/transfer.go @@ -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 diff --git a/version.go b/version.go index 25c5169..f94bfea 100644 --- a/version.go +++ b/version.go @@ -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()) diff --git a/windows/kernel32.go.nonwindows b/windows/kernel32.go.nonwindows deleted file mode 100644 index 163da3a..0000000 --- a/windows/kernel32.go.nonwindows +++ /dev/null @@ -1,6 +0,0 @@ -package windows - -func SetTitle(title string) { - if title == "" { - } -}