From 67164f89f39c9a6b1f1d2d8c6affb8545c1f6172 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Tue, 16 Mar 2021 21:00:17 +0300 Subject: [PATCH] Pull request: all: add native endianness, imp Makefile Merge in DNS/adguard-home from fix-some to master Squashed commit of the following: commit 190e9a88d9c0f2bfc597aa61b41dae8b8686158e Author: Ainar Garipov Date: Tue Mar 16 20:50:02 2021 +0300 all: add native endianness, imp Makefile --- Makefile | 15 --------------- internal/aghos/endian_big.go | 10 ++++++++++ internal/aghos/endian_little.go | 10 ++++++++++ internal/home/authglinet.go | 19 ++----------------- internal/home/authglinet_test.go | 11 +++-------- scripts/make/go-lint.sh | 18 ++++++++++++++---- 6 files changed, 39 insertions(+), 44 deletions(-) create mode 100644 internal/aghos/endian_big.go create mode 100644 internal/aghos/endian_little.go diff --git a/Makefile b/Makefile index da6aa534..9288427d 100644 --- a/Makefile +++ b/Makefile @@ -82,18 +82,3 @@ go-check: go-tools go-lint go-test openapi-lint: ; cd ./openapi/ && $(YARN) test openapi-show: ; cd ./openapi/ && $(YARN) start - -# TODO(a.garipov): Remove the legacy targets once the build -# infrastructure stops using them. -dependencies: - @ echo "use make deps instead" - @ $(MAKE) deps -docker-multi-arch: - @ echo "use make build-docker instead" - @ $(MAKE) build-docker -go-install-tools: - @ echo "use make go-tools instead" - @ $(MAKE) go-tools -release: - @ echo "use make build-release instead" - @ $(MAKE) build-release diff --git a/internal/aghos/endian_big.go b/internal/aghos/endian_big.go new file mode 100644 index 00000000..e7c5e71e --- /dev/null +++ b/internal/aghos/endian_big.go @@ -0,0 +1,10 @@ +// +build mips mips64 + +// This file is an adapted version of github.com/josharian/native. + +package aghos + +import "encoding/binary" + +// NativeEndian is the native endianness of this system. +var NativeEndian = binary.BigEndian diff --git a/internal/aghos/endian_little.go b/internal/aghos/endian_little.go new file mode 100644 index 00000000..2785de4e --- /dev/null +++ b/internal/aghos/endian_little.go @@ -0,0 +1,10 @@ +// +build amd64 386 arm arm64 mipsle mips64le ppc64le + +// This file is an adapted version of github.com/josharian/native. + +package aghos + +import "encoding/binary" + +// NativeEndian is the native endianness of this system. +var NativeEndian = binary.LittleEndian diff --git a/internal/home/authglinet.go b/internal/home/authglinet.go index 28d8ddac..a77294d8 100644 --- a/internal/home/authglinet.go +++ b/internal/home/authglinet.go @@ -8,9 +8,9 @@ import ( "net/http" "os" "time" - "unsafe" "github.com/AdguardTeam/AdGuardHome/internal/aghio" + "github.com/AdguardTeam/AdGuardHome/internal/aghos" "github.com/AdguardTeam/golibs/log" ) @@ -66,16 +66,6 @@ func glCheckToken(sess string) bool { return now <= (tokenDate + glTokenTimeoutSeconds) } -// TODO(a.garipov): Replace with a smaller version of -// https://github.com/josharian/native. -func archIsLittleEndian() bool { - var i int32 = 0x01020304 - u := unsafe.Pointer(&i) - pb := (*byte)(u) - b := *pb - return (b == 0x04) -} - // MaxFileSize is a maximum file length in bytes. const MaxFileSize = 1024 * 1024 @@ -104,12 +94,7 @@ func glGetTokenDate(file string) uint32 { } buf := bytes.NewBuffer(bs) - var order binary.ByteOrder = binary.BigEndian - if archIsLittleEndian() { - order = binary.LittleEndian - } - - err = binary.Read(buf, order, &dateToken) + err = binary.Read(buf, aghos.NativeEndian, &dateToken) if err != nil { log.Error("binary.Read: %s", err) diff --git a/internal/home/authglinet_test.go b/internal/home/authglinet_test.go index 6f53c181..e3a71be4 100644 --- a/internal/home/authglinet_test.go +++ b/internal/home/authglinet_test.go @@ -1,12 +1,12 @@ package home import ( - "encoding/binary" "io/ioutil" "net/http" "testing" "time" + "github.com/AdguardTeam/AdGuardHome/internal/aghos" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -20,19 +20,14 @@ func TestAuthGL(t *testing.T) { }) glFilePrefix = dir + "/gl_token_" - putFunc := binary.BigEndian.PutUint32 - if archIsLittleEndian() { - putFunc = binary.LittleEndian.PutUint32 - } - data := make([]byte, 4) - putFunc(data, 1) + aghos.NativeEndian.PutUint32(data, 1) require.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644)) assert.False(t, glCheckToken("test")) data = make([]byte, 4) - putFunc(data, uint32(time.Now().UTC().Unix()+60)) + aghos.NativeEndian.PutUint32(data, uint32(time.Now().UTC().Unix()+60)) require.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644)) r, _ := http.NewRequest(http.MethodGet, "http://localhost/", nil) diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh index 7aabc7ed..ccd21486 100644 --- a/scripts/make/go-lint.sh +++ b/scripts/make/go-lint.sh @@ -66,10 +66,20 @@ method_const() { # underscores is a simple check against Go filenames with underscores. underscores() { - git ls-files '*_*.go' | { grep -F -e '_darwin.go' \ - -e '_freebsd.go' -e '_linux.go' -e '_others.go' \ - -e '_test.go' -e '_unix.go' -e '_windows.go' \ - -v || exit 0; } + git ls-files '*_*.go' | { + grep -F\ + -e '_big.go'\ + -e '_darwin.go'\ + -e '_freebsd.go'\ + -e '_linux.go'\ + -e '_little.go'\ + -e '_others.go'\ + -e '_test.go'\ + -e '_unix.go'\ + -e '_windows.go' \ + -v\ + || exit 0 + } }