Add anti-buffering relay headers and optional randomized query suffixes

This commit is contained in:
Amin.MasterkinG
2026-04-21 15:28:09 +03:30
parent bf5c0ef06e
commit 74ff27ac78
7 changed files with 172 additions and 1 deletions
+9
View File
@@ -107,6 +107,8 @@ func (s *Server) Run(ctx context.Context) error {
}
func (s *Server) handleRelay(w http.ResponseWriter, r *http.Request) {
applyRelayResponseHeaders(w.Header())
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
@@ -167,6 +169,13 @@ func (s *Server) handleRelay(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(encrypted)
}
func applyRelayResponseHeaders(header http.Header) {
header.Set("Cache-Control", "no-store, no-cache, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "0")
header.Set("X-Accel-Buffering", "no")
}
func (s *Server) processBatch(batch protocol.Batch) (protocol.Batch, error) {
session := s.getOrCreateSession(batch.ClientSessionKey)
now := time.Now()
+22
View File
@@ -3,6 +3,7 @@ package server
import (
"errors"
"net"
"net/http/httptest"
"sync"
"testing"
"time"
@@ -319,6 +320,27 @@ func TestSOCKSStateCloseUpstreamClearsConnectionSnapshot(t *testing.T) {
}
}
func TestHandleRelayAppliesAntiBufferingHeaders(t *testing.T) {
srv := New(config.Config{}, nil)
request := httptest.NewRequest("GET", "/relay", nil)
recorder := httptest.NewRecorder()
srv.handleRelay(recorder, request)
if got := recorder.Header().Get("X-Accel-Buffering"); got != "no" {
t.Fatalf("expected X-Accel-Buffering=no, got %q", got)
}
if got := recorder.Header().Get("Cache-Control"); got != "no-store, no-cache, must-revalidate" {
t.Fatalf("unexpected Cache-Control header: %q", got)
}
if got := recorder.Header().Get("Pragma"); got != "no-cache" {
t.Fatalf("unexpected Pragma header: %q", got)
}
if got := recorder.Header().Get("Expires"); got != "0" {
t.Fatalf("unexpected Expires header: %q", got)
}
}
func testDataPacket(clientSessionKey string, socksID uint64, sequence uint64, payload string) protocol.Packet {
packet := protocol.NewPacket(clientSessionKey, protocol.PacketTypeSOCKSData)
packet.SOCKSID = socksID