From b831060d6eb89719871d2c2cb45d78e97202231f Mon Sep 17 00:00:00 2001
From: Preston <1033514+boypt@users.noreply.github.com>
Date: Thu, 23 Dec 2021 09:34:16 +0800
Subject: [PATCH] fix http tracker arg position (#700)

Co-authored-by: Bot Git <bot@example.com>
---
 tracker/http/http.go | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tracker/http/http.go b/tracker/http/http.go
index f45cc7c2..3eed2d80 100644
--- a/tracker/http/http.go
+++ b/tracker/http/http.go
@@ -23,7 +23,7 @@ import (
 var vars = expvar.NewMap("tracker/http")
 
 func setAnnounceParams(_url *url.URL, ar *AnnounceRequest, opts AnnounceOpt) {
-	q := _url.Query()
+	q := url.Values{}
 
 	q.Set("key", strconv.FormatInt(int64(ar.Key), 10))
 	q.Set("info_hash", string(ar.InfoHash[:]))
@@ -64,7 +64,14 @@ func setAnnounceParams(_url *url.URL, ar *AnnounceRequest, opts AnnounceOpt) {
 	doIp("ipv6", opts.ClientIp6)
 	// We're operating purely on query-escaped strings, where + would have already been encoded to
 	// %2B, and + has no other special meaning. See https://github.com/anacrolix/torrent/issues/534.
-	_url.RawQuery = strings.ReplaceAll(q.Encode(), "+", "%20")
+	qstr := strings.ReplaceAll(q.Encode(), "+", "%20")
+
+	// Some private trackers require the original query param to be in the first position.
+	if _url.RawQuery != "" {
+		_url.RawQuery += "&" + qstr
+	} else {
+		_url.RawQuery = qstr
+	}
 }
 
 type AnnounceOpt struct {
-- 
2.51.0