Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if udp fails, fallback to tcp connection #256

Merged
merged 2 commits into from
Mar 14, 2025
Merged

Conversation

ShubhamRasal
Copy link
Contributor

@ShubhamRasal ShubhamRasal commented Mar 14, 2025


import (
	"fmt"
	"net"
	"time"

	"github.com/miekg/dns"
	"golang.org/x/net/proxy"
)

func main() {
	// Create SOCKS5 proxy dialer
	proxyAddr := "localhost:37367"
	auth := &proxy.Auth{
		User:     "abc",
		Password: "abc",
	}
	fmt.Printf("Attempting to connect to SOCKS5 proxy at %s\n", proxyAddr)

	dialer, err := proxy.SOCKS5("tcp", proxyAddr, auth, nil)
	if err != nil {
		fmt.Printf("Failed to create SOCKS5 dialer: %v\n", err)
		return
	}

	// Try TCP first (this should work)
	fmt.Println("\nTesting TCP connection:")
	testConnection(dialer, "tcp", "8.8.8.8:53")

	// Try UDP (this will fail with "network not implemented")
	fmt.Println("\nTesting UDP connection:")
	testConnection(dialer, "udp", "8.8.8.8:53")

	// Demonstrate a working DNS query over TCP
	fmt.Println("\nAttempting DNS query over TCP:")
	performDNSQuery(dialer)
}

func testConnection(dialer proxy.Dialer, network, address string) {
	fmt.Printf("Attempting to dial %s via %s\n", address, network)

	conn, err := dialer.Dial(network, address)
	if err != nil {
		fmt.Printf("Dial error: %v\n", err)
		return
	}
	defer conn.Close()

	fmt.Printf("Successfully connected to %s via %s\n", address, network)
}


var question = "google.com."

func performDNSQuery(dialer proxy.Dialer) {
	// Create a DNS client
	client := &dns.Client{
		Net: "tcp", // Force TCP
		Dialer: &net.Dialer{
			Timeout: 5 * time.Second,
		},
	}

	// Create a DNS message
	m := new(dns.Msg)
	m.SetQuestion(question, dns.TypeA)
	m.RecursionDesired = true

	// Try to connect to Google's DNS server through the proxy
	dnsServer := "8.8.8.8:53"
	fmt.Printf("Attempting DNS query for google.com to %s\n", dnsServer)

	// Create a connection through the proxy
	conn, err := dialer.Dial("tcp", dnsServer)
	if err != nil {
		fmt.Printf("Failed to connect to DNS server: %v\n", err)
		return
	}
	defer conn.Close()

	// Create DNS connection
	dnsConn := &dns.Conn{Conn: conn}
	defer dnsConn.Close()

	// Send the query
	fmt.Println("Sending DNS query...")
	response, _, err := client.ExchangeWithConn(m, dnsConn)
	if err != nil {
		fmt.Printf("DNS query failed: %v\n", err)
		return
	}

	// Print results
	fmt.Println("\nDNS Response:")
	for _, answer := range response.Answer {
		fmt.Printf("Answer: %v\n", answer)
	}
}

- incase of proxy, dail function fails with `network not implemented` error
@ShubhamRasal ShubhamRasal requested a review from Mzack9999 March 14, 2025 09:36
@Mzack9999 Mzack9999 merged commit 0ba1620 into main Mar 14, 2025
5 checks passed
@Mzack9999 Mzack9999 deleted the proxy-fallback-tcp branch March 14, 2025 19:22
@ShubhamRasal ShubhamRasal self-assigned this Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants