mic.git


mic

~ proxy for JA4-TLS fingerprinting evasion
git clone https://github.com/djnnvx/mic.git
log | files | refs | archive | README | LICENSE

https_test.go (2864B)

      1 package proxy
      2 
      3 import (
      4 	"net"
      5 	"testing"
      6 	"time"
      7 )
      8 
      9 func TestParseAuthority(t *testing.T) {
     10 	cases := []struct {
     11 		name         string
     12 		in           string
     13 		wantServer   string
     14 		wantHostPort string
     15 	}{
     16 		{"hostname with explicit port", "example.com:443", "example.com", "example.com:443"},
     17 		{"hostname with non-443 port", "localhost:3000", "localhost", "localhost:3000"},
     18 		{"hostname with high port", "api.internal:8443", "api.internal", "api.internal:8443"},
     19 		{"bare hostname defaults to 443", "example.com", "example.com", "example.com:443"},
     20 		{"bare localhost defaults to 443", "localhost", "localhost", "localhost:443"},
     21 
     22 		{"subdomain with port", "www.example.com:443", "www.example.com", "www.example.com:443"},
     23 		{"subdomain with non-443 port", "api.example.com:8080", "api.example.com", "api.example.com:8080"},
     24 		{"deep subdomain with port", "a.b.c.example.com:9000", "a.b.c.example.com", "a.b.c.example.com:9000"},
     25 		{"bare subdomain defaults to 443", "www.example.com", "www.example.com", "www.example.com:443"},
     26 		{"bare deep subdomain defaults to 443", "a.b.c.example.com", "a.b.c.example.com", "a.b.c.example.com:443"},
     27 		{"hyphenated hostname with port", "my-host-1.example.com:8443", "my-host-1.example.com", "my-host-1.example.com:8443"},
     28 
     29 		{"IPv4 with explicit port", "127.0.0.1:8080", "127.0.0.1", "127.0.0.1:8080"},
     30 		{"IPv4 with 443", "10.0.0.1:443", "10.0.0.1", "10.0.0.1:443"},
     31 		{"bare IPv4 defaults to 443", "10.0.0.1", "10.0.0.1", "10.0.0.1:443"},
     32 
     33 		{"IPv6 bracketed with port", "[::1]:443", "::1", "[::1]:443"},
     34 		{"IPv6 bracketed with non-443 port", "[fe80::1]:8080", "fe80::1", "[fe80::1]:8080"},
     35 		{"IPv6 bracketed without port", "[::1]", "::1", "[::1]:443"},
     36 		{"IPv6 full address bracketed with port", "[2001:db8::1]:9000", "2001:db8::1", "[2001:db8::1]:9000"},
     37 	}
     38 
     39 	for _, tc := range cases {
     40 		t.Run(tc.name, func(t *testing.T) {
     41 			gotServer, gotHostPort := parseAuthority(tc.in)
     42 			if gotServer != tc.wantServer {
     43 				t.Errorf("server: got %q, want %q", gotServer, tc.wantServer)
     44 			}
     45 			if gotHostPort != tc.wantHostPort {
     46 				t.Errorf("hostPort: got %q, want %q", gotHostPort, tc.wantHostPort)
     47 			}
     48 		})
     49 	}
     50 }
     51 
     52 func TestHttpsHandler_SilentPeerTimesOut(t *testing.T) {
     53 	orig := headerReadTimeout
     54 	headerReadTimeout = 200 * time.Millisecond
     55 	defer func() { headerReadTimeout = orig }()
     56 
     57 	ln, err := net.Listen("tcp", "127.0.0.1:0")
     58 	if err != nil {
     59 		t.Fatalf("net.Listen: %v", err)
     60 	}
     61 	defer ln.Close()
     62 
     63 	done := make(chan struct{})
     64 	go func() {
     65 		conn, err := ln.Accept()
     66 		if err != nil {
     67 			return
     68 		}
     69 		HttpsHandler(conn, &Proxy{})
     70 		close(done)
     71 	}()
     72 
     73 	client, err := net.Dial("tcp", ln.Addr().String())
     74 	if err != nil {
     75 		t.Fatalf("net.Dial: %v", err)
     76 	}
     77 	defer client.Close()
     78 
     79 	select {
     80 	case <-done:
     81 	case <-time.After(3 * time.Second):
     82 		t.Fatal("HttpsHandler did not return on a peer that sent nothing")
     83 	}
     84 }