Version 6 of Socks proxy

Updated 2006-11-19 22:28:32 by MJ

Describe SOCKS 4/5 proxy [L1 ]


MJ - When connecting to a SOCKS4 proxy one request-response pair has to be handled before using the socket as a normal socket. The following proc should come close in handling this request response pair. I was unable to test this with a real SOCKS4 proxy, but ethereal at least recognizes the request as a valid SOCKS4 request.

 # connect to host port through the SOCKS4 proxy. authenticate with username (default empty)
 # returns a socket that can be used to send and receive traffic from the remote host
 proc connect_through_proxy {proxy_host proxy_port ip port {username {}}} {
        set connect_request \x04\x01
        append connect_request [binary format S $port]        
        append connect_request [binary format c4 [split $ip .]]
        append connect_request $username
        append connect_request \x00

        set s [socket $proxy_host $proxy_port]
        fconfigure $s -translation binary -buffering none
        puts -nonewline $s $connect_request

        set response [read $s 8]
        if {[string index $response 1] ne "\x5a"} {
                error "connection request rejected by proxy, reason [format 0x%X [string index $response 1]]"
        } else {
                return $s
        }
 }

 set s [connect_through_proxy phost pport ip port]
 # s can now be used as a normal socket

[Category Networking]