A simple SOCKS 5 client written in Tcl that supports both the BIND and CONNECT commands.
- Support for username/password authentication only
- No support for
UDP ASSOCIATE
command
This program is released under the BSD license.
The socks5
package can be used to SOCKS enable the http
package. This is useful for passing all http requests through the Tor network, for example. Assuming you have Tor running on your local system and listening for connections on port 9050, the following sample code demonstrates how to enable http use of Tor.
package require Tcl 8.5
package require socks5
package require tls 1.6
package require http 2.7
proc httpsConnect {args} {
# Separate options from mandatory arguments
set opts [lrange $args 0 end-2]
lassign [lrange $args end-1 end] host port
# Establish connection with SOCKS 5
set sock [::socks5::connect {*}$opts $host $port]
# Enable SSL on open socket (make sure we're using tls rather
# than ssl3)
::tls::import $sock -tls1 1
return $sock
}
http::register http 80 ::socks5::connect
http::register https 443 ::httpsConnect
socks5::configure -proxy localhost -proxyport 9050
# Retrieve check.torproject.org to confirm we are using Tor
set token [http::geturl "https://check.torproject.org" -channel stdout]
http::cleanup $token
The socks5 library is distributed as a Tcl package and can be loaded using the package require
command:
package require socks5 0.1
Usage: socks5::configure <args>
Use socks5::configure
to configure the library for operations. The following options are available:
Option | Description |
---|---|
-proxy |
IP or hostname for the proxy server |
-proxyport |
Port number on which the proxy server operates (default: 1080) |
-bindtimeout |
Timeout (in milliseconds) when waiting for an incoming connection via socks5::bind |
-username |
Username (for username/password authentication) (default: ) |
-password |
Password (for username/password authentication) (default: ) |
Example:
socks5::configure -proxy localhost -proxyport 1080 -bindtimeout 2000
Usage: socks5::connect ?-async? ?-myaddr addr? ?-myport port? <host> <port>
Establishes a connection with a remote host through the configured proxy.
Example:
socks5::connect www.google.com 80
Usage: socks5::bind ?-myaddr addr? ?-myport port? <host> <port> <command>
Requests the proxy open a TCP port for listening and wait for an incoming connection from the specified host and port.
The command
parameter will be evaluated at the global scope when the incoming connection is established. Two parameters will be appended. The first indicates the type of response from the proxy, while the second depends on the response. The following table identifies expected response codes.
Response | Argument | Description |
---|---|---|
ok |
channel handle | Indicates an incoming connection was esablished. The channel handle may be used to communicate with the remote host. |
timeout |
error message | Indicates the -bindtimeout expired while waiting for an incoming connection. |
error |
error message | Indicates some sort of error occurred with the proxy. The error message provides further details. |
Example:
proc handle_connect {result arg} {
if {$result eq "ok"} {
puts "connection established via channel $arg"
} elseif {$result eq "timeout"} {
puts "timeout expired while waiting for connection"
} elseif {$result eq "error"} {
puts "error from proxy while waiting for connection: $arg"
}
}
socks5::bind ftp.cdrom.com 21 handle_connect