Skip to content

Commit

Permalink
start server src and spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jkthorne committed Nov 7, 2018
1 parent 0564bc4 commit 1364661
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions .crystal-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.27.0
20 changes: 20 additions & 0 deletions spec/socks/server_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "../spec_helper"

describe Socks::Server do
it "bind" do
begin
bind_port = rand(8000..10000)
server = HTTP::Server.new{|ctx| ctx.response << "yolo"}
server.bind_socks("127.0.0.1", bind_port, "127.0.0.1", SSH_PORT)

spawn { server.try &.listen }

client = TCPSocket.new("127.0.0.1", SSH_PORT)
client << "GET / HTTP 1.1\n\n"
client.gets.should eq "yolo"
ensure
client.try &.close
server.try &.close
end
end
end
2 changes: 1 addition & 1 deletion spec/socks_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe Socks do
it "bind" do
begin
bind_port = rand(8000..10000)
client = TCPSocket.new("localhost", SSH_PORT)
socket = Socks.new(host_addr: "127.0.0.1", host_port: SSH_PORT,
addr: "127.0.0.1", port: bind_port,
command: Socks::COMMAND::BIND)
Expand All @@ -76,7 +77,6 @@ describe Socks do
client << "pong\n"
}

client = TCPSocket.new("localhost", SSH_PORT)
client << "ping\n"
response = client.gets
end
Expand Down
4 changes: 3 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "spec"
require "../src/socks"

require "http/server"
require "../src/socks/server.cr"
require "../src/socks/client.cr"
require "../src/socks/udp.cr"

SSH_PORT = rand(8000..10000)
SSH_PROCESS = Process.new("ssh", ["-D", SSH_PORT.to_s, "-C", "-N", "127.0.0.1"])
Expand Down
3 changes: 0 additions & 3 deletions src/socks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,3 @@ require "./connection_request.cr"
require "./connection_response.cr"
require "./request.cr"
require "./reply.cr"

require "./socks/client.cr"
require "./socks/udp.cr"
13 changes: 13 additions & 0 deletions src/socks/server.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "../socks.cr"
require "http/server"

class HTTP::Server
def bind_socks(addr : String, port : Int32, host_addr : String, host_port : Int32)
tcp_server = Socks.new(addr, port, host_addr, host_port, command: Socks::COMMAND::BIND)

bind(tcp_server)

tcp_server.local_address
end
end

0 comments on commit 1364661

Please sign in to comment.