Fix varinterval panic if request IH is not provided

This commit is contained in:
Širhoe Biazhkovič
2021-11-26 19:21:49 +03:00
committed by Lawrence, Rendall
parent 9122aefdd7
commit 239a642bfc
6 changed files with 13 additions and 13 deletions
+2 -2
View File
@@ -148,7 +148,7 @@ func RootRunCmdFunc(cmd *cobra.Command, _ []string) error {
return err return err
} }
quit := make(chan os.Signal) quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
reload := makeReloadChan() reload := makeReloadChan()
@@ -209,7 +209,7 @@ func RootPreRunCmdFunc(cmd *cobra.Command, _ []string) error {
// RootPostRunCmdFunc handles clean up of any state initialized by command line // RootPostRunCmdFunc handles clean up of any state initialized by command line
// flags. // flags.
func RootPostRunCmdFunc(cmd *cobra.Command, args []string) error { func RootPostRunCmdFunc(_ *cobra.Command, _ []string) error {
return nil return nil
} }
+1 -1
View File
@@ -10,7 +10,7 @@ import (
) )
func makeReloadChan() <-chan os.Signal { func makeReloadChan() <-chan os.Signal {
reload := make(chan os.Signal) reload := make(chan os.Signal, 1)
signal.Notify(reload, syscall.SIGUSR1) signal.Notify(reload, syscall.SIGUSR1)
return reload return reload
} }
+1 -1
View File
@@ -172,7 +172,7 @@ func NewFrontend(logic frontend.TrackerLogic, provided Config) (*Frontend, error
} }
} }
if cfg.HTTPSAddr == "" || f.tlsCfg == nil { if (cfg.HTTPSAddr == "") != (f.tlsCfg == nil) {
return nil, errors.New("must specify both https_addr, tls_cert_path and tls_key_path") return nil, errors.New("must specify both https_addr, tls_cert_path and tls_key_path")
} }
+2 -4
View File
@@ -21,8 +21,7 @@ func TestWriteError(t *testing.T) {
for _, tt := range table { for _, tt := range table {
t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) { t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
r := httptest.NewRecorder() r := httptest.NewRecorder()
err := WriteError(r, bittorrent.ClientError(tt.reason)) WriteError(r, bittorrent.ClientError(tt.reason))
require.Nil(t, err)
require.Equal(t, r.Body.String(), tt.expected) require.Equal(t, r.Body.String(), tt.expected)
}) })
} }
@@ -38,8 +37,7 @@ func TestWriteStatus(t *testing.T) {
for _, tt := range table { for _, tt := range table {
t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) { t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
r := httptest.NewRecorder() r := httptest.NewRecorder()
err := WriteError(r, bittorrent.ClientError(tt.reason)) WriteError(r, bittorrent.ClientError(tt.reason))
require.Nil(t, err)
require.Equal(t, r.Body.String(), tt.expected) require.Equal(t, r.Body.String(), tt.expected)
}) })
} }
+6 -4
View File
@@ -10,8 +10,10 @@ import (
// AnnounceRequest. // AnnounceRequest.
// //
// Calling DeriveEntropyFromRequest multiple times yields the same values. // Calling DeriveEntropyFromRequest multiple times yields the same values.
func DeriveEntropyFromRequest(req *bittorrent.AnnounceRequest) (uint64, uint64) { func DeriveEntropyFromRequest(req *bittorrent.AnnounceRequest) (v0 uint64, v1 uint64) {
v0 := binary.BigEndian.Uint64([]byte(req.InfoHash[:8])) + binary.BigEndian.Uint64([]byte(req.InfoHash[8:16])) if len(req.InfoHash) >= bittorrent.InfoHashV1Len {
v1 := binary.BigEndian.Uint64(req.Peer.ID[:8]) + binary.BigEndian.Uint64(req.Peer.ID[8:16]) v0 = binary.BigEndian.Uint64([]byte(req.InfoHash[:8])) + binary.BigEndian.Uint64([]byte(req.InfoHash[8:16]))
return v0, v1 }
v1 = binary.BigEndian.Uint64(req.Peer.ID[:8]) + binary.BigEndian.Uint64(req.Peer.ID[8:16])
return
} }
+1 -1
View File
@@ -50,7 +50,7 @@ func TestHandleAnnounce(t *testing.T) {
require.NotNil(t, h) require.NotNil(t, h)
ctx := context.Background() ctx := context.Background()
req := &bittorrent.AnnounceRequest{} req := &bittorrent.AnnounceRequest{InfoHash: "1234567890ABCDEF0000"}
resp := &bittorrent.AnnounceResponse{} resp := &bittorrent.AnnounceResponse{}
nCtx, err := h.HandleAnnounce(ctx, req, resp) nCtx, err := h.HandleAnnounce(ctx, req, resp)