Giter Club home page Giter Club logo

sox's Introduction

Sox

Sox is a library for creating SOCKS5 clients and servers. Sox is a network proxy and can proxy connections like HTTP requests or ssh connections.

Documentation

For more documentation on the specs, this implementation is based on please read documents at ./docs/specs.

For more documentation on the implementations of SOCKS, please read documents at ./docs/.

FEATURES

  • SOCKS5
    • addr type
      • IPv4 connection
      • IPv6 connection
      • Domain connection
    • Authentication
      • unauthentication
      • GSS connection
      • username and password
      • IANA unimplented in ssh
    • command types
      • connect
      • bind
      • udp associate
    • reply / response
      • reply server messages
      • connection response server messages
  • SOCKS4
  • SOCKS5 server

Installation

Add this to your application's shard.yml:

dependencies:
  sox:
    github: wontruefree/sox

Usage

A SOCKS Client so to make a connection you have to have a corresponding SOCKS server. The easiest one to use is ssh.

To start up a local ssh SOCKS server you can connect to yourself.

# Setup SOCKS on localhost
ssh -D 1080 -C -N 127.0.0.1

Have a local SOCKS server directing its connection to a remote host.

# Setup SOCKS connecting to remote host
ssh -D 1080 -C -N [email protected]

Basic Usage

Basic Socket

To open a SOCKS connection and send a basic HTTP request and get a response.

require "sox"

socket = Sox.new(addr: "52.85.89.35")
request = HTTP::Request.new("GET", "/", HTTP::Headers{"Host" => "crystal-lang.org"})

request.to_io(socket)
socket.flush

response = HTTP::Client::Response.from_io(socket)
if response.success?
  puts "Got to crystal through SOCKS5!!!"
end

Basic Client

Sox::Client functions almost like the Crystal (HTTP::Client)[https://crystal-lang.org/api/latest/HTTP/Client.html]

client = Sox::Client.new("www.example.com", host_addr: "127.0.0.1", host_port: 1080)
response = client.get("/")
puts response.status_code      # => 200
puts response.body.lines.first # => "<!doctype html>"
client.close

Basic UDP

Sox::UDP functions almost like the Crystal (UDPSocket)[https://crystal-lang.org/api/latest/UDPSocket.html]

server = UDPSocket.new
server.bind "localhost", 9999

client = Sox::UDP.new(host_addr: "127.0.0.1", host_port: 1080)
client.connect "localhost", 9999

client.send "message"
message, client_addr = server.receive

message     # => "message"
client_addr # => Socket::IPAddress(127.0.0.1:50516)

client.close
server.close

you can use Sox::Client almost like the Crystal (HTTP::Client)[https://crystal-lang.org/api/latest/HTTP/Client.html]

client = Sox::Client.new("www.example.com", host_addr: "127.0.0.1", host_port: 1080)
response = client.get("/")
puts response.status_code      # => 200
puts response.body.lines.first # => "<!doctype html>"
client.close

Remote server connnection

To open a connection to a remote SOCKS5 Server.

require "sox"

socket = Sox.new(host_addr: "gateway.com", addr: "52.85.89.35")
request = HTTP::Request.new("GET", "/", HTTP::Headers{"Host" => "crystal-lang.org"})

request.to_io(socket)
socket.flush

response = HTTP::Client::Response.from_io?(socket)
if response.success?
  puts "Got to crystal through SOCKS5!!!"
end

Connection using none default ports

Sometimes you connect to web servers or remote SOCKS servers on ports that are not default. This is built into the top level interface no having to deal with requests or connection requests directly.

require "sox"

socket = Sox.new(host_addr: "gateway.com" host_port: 8010, addr: "52.85.89.35", port: 3000)
request = HTTP::Request.new("GET", "/", HTTP::Headers{"Host" => "crystal-lang.org"})

request.to_io(socket)
socket.flush

response = HTTP::Client::Response.from_io?(socket)
if response.success?
  puts "Got to crystal through SOCKS5!!!"
end

Specs

Before running specs you should add your public key to the authorized keys. Please read below if you have not previously set up a socks server for testing. Although I like Unit Test Spec SOCKS only use tools in the stdlib so this is written in spec style syntax.

Runnning specs

To run the test suite use the spec command built into crystal.

crystal spec

Setup SOCKS Server

enable key based authentication for testing

# enable key based localhost authentication
cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys

TOR

There are some basic tests for a commonly used SOCKS5 server tor.

Debian

Install tor via apt.

sudo apt install tor

Optional: Start tor at login and keep tor running in the background.

systemctl start tor

MAC

Use homebrew to install tor on your mac.

brew install tor

Optional: Use brew services to keep tor running in the background.

brew services start tor

sox's People

Contributors

jkthorne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.