-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,9 @@ 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. | ||
|
||
```crystal | ||
|
@@ -75,6 +78,49 @@ if response.success? | |
end | ||
``` | ||
|
||
#### Basic Client | ||
|
||
`Socks::Client` functions almost like the Crystal (HTTP::Client)[https://crystal-lang.org/api/latest/HTTP/Client.html] | ||
|
||
```crystal | ||
client = Socks::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 | ||
|
||
`Socks::UDP` functions almost like the Crystal (UDPSocket)[https://crystal-lang.org/api/latest/UDPSocket.html] | ||
|
||
```crystal | ||
server = UDPSocket.new | ||
server.bind "localhost", 9999 | ||
client = Socks::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 `Socks::Client` almost like the Crystal (HTTP::Client)[https://crystal-lang.org/api/latest/HTTP/Client.html] | ||
|
||
```crystal | ||
client = Socks::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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "../spec_helper" | ||
|
||
describe Socks::Client do | ||
it "connect" do | ||
begin | ||
server = HTTP::Server.new do |context| | ||
context.response.content_type = "text/plain" | ||
if context.request.path == "/ping" | ||
context.response << "pong" | ||
end | ||
end | ||
address = server.bind_unused_port "127.0.0.1" | ||
spawn { server.try &.listen } | ||
|
||
client = Socks::Client.new("127.0.0.1", address.port, host_addr: "127.0.0.1", host_port: SSH_PORT) | ||
response = client.get("/ping") | ||
response.try &.body.should eq "pong" | ||
ensure | ||
server.try &.close | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require "../spec_helper" | ||
|
||
describe Socks::UDP do | ||
it "associate" do | ||
begin | ||
udp_port = rand(8000..10000) | ||
server = UDPSocket.new | ||
server.bind "localhost", udp_port | ||
|
||
|
||
client = Socks::UDP.new(host_addr: "127.0.0.1", host_port: SSH_PORT) | ||
client.connect("localhost", udp_port) | ||
|
||
client.send("yolo") | ||
message, client_addr = server.receive | ||
|
||
message.should eq "yolo" | ||
ensure | ||
client.try &.close | ||
server.try &.close | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "../socks.cr" | ||
|
||
class Socks::UDP < UDPSocket | ||
def initialize(*args, @host_addr : String, @host_port : Int32) | ||
super(*args) | ||
end | ||
end |