diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-27 14:55:37 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-02-02 16:28:11 +0700 |
| commit | 6c7b070156c030de11c2a4b46ea6101f7e6142c0 (patch) | |
| tree | c30408af336412ca0f2a65a78a7093fe6205521a /lib | |
| parent | 93149aeb09a1641be37d6fc62846a244d957b81a (diff) | |
| download | pakakeh.go-6c7b070156c030de11c2a4b46ea6101f7e6142c0.tar.xz | |
lib/dns: minimize use of append
In handleDoHRequest, we did not need to call append, just copy it as is.
In DoTClient and TCPClient, since we already do make, use direct copy
instead of another append.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/dns/dot_client.go | 10 | ||||
| -rw-r--r-- | lib/dns/server.go | 2 | ||||
| -rw-r--r-- | lib/dns/tcp_client.go | 10 |
3 files changed, 11 insertions, 11 deletions
diff --git a/lib/dns/dot_client.go b/lib/dns/dot_client.go index a3eea4af..870a1395 100644 --- a/lib/dns/dot_client.go +++ b/lib/dns/dot_client.go @@ -6,7 +6,6 @@ package dns import ( "crypto/tls" - "encoding/binary" "errors" "fmt" "net" @@ -162,12 +161,13 @@ func (cl *DoTClient) Write(msg []byte) (n int, err error) { } var ( - lenmsg = len(msg) - packet = make([]byte, 0, 2+lenmsg) + lenmsg = uint16(len(msg)) + packet = make([]byte, 2+lenmsg) ) - packet = binary.BigEndian.AppendUint16(packet, uint16(lenmsg)) - packet = append(packet, msg...) + packet[0] = byte(lenmsg >> 8) + packet[1] = byte(lenmsg) + copy(packet[2:], msg) n, err = cl.conn.Write(packet) if err != nil { diff --git a/lib/dns/server.go b/lib/dns/server.go index 32fd8241..2090d383 100644 --- a/lib/dns/server.go +++ b/lib/dns/server.go @@ -476,7 +476,7 @@ func (srv *Server) handleDoHRequest(raw []byte, w http.ResponseWriter) { req.kind = connTypeDoH req.writer = cl - req.message.packet = append(req.message.packet[:0], raw...) + req.message.packet = raw err = req.message.UnpackHeaderQuestion() if err != nil { diff --git a/lib/dns/tcp_client.go b/lib/dns/tcp_client.go index 402540f6..da745892 100644 --- a/lib/dns/tcp_client.go +++ b/lib/dns/tcp_client.go @@ -5,7 +5,6 @@ package dns import ( - "encoding/binary" "errors" "fmt" "io" @@ -172,12 +171,13 @@ func (cl *TCPClient) Write(msg []byte) (n int, err error) { } var ( - lenmsg = len(msg) - packet = make([]byte, 0, 2+lenmsg) + lenmsg = uint16(len(msg)) + packet = make([]byte, 2+lenmsg) ) - packet = binary.BigEndian.AppendUint16(packet, uint16(lenmsg)) - packet = append(packet, msg...) + packet[0] = byte(lenmsg >> 8) + packet[1] = byte(lenmsg) + copy(packet[2:], msg) n, err = cl.conn.Write(packet) if err != nil { |
