From 13646612f1028060e290f3d139736a7e78017eb0 Mon Sep 17 00:00:00 2001 From: Jack Thorne Date: Wed, 7 Nov 2018 09:34:14 -0600 Subject: [PATCH] start server src and spec --- .crystal-version | 1 + spec/socks/server_spec.cr | 20 ++++++++++++++++++++ spec/socks_spec.cr | 2 +- spec/spec_helper.cr | 4 +++- src/socks.cr | 3 --- src/socks/server.cr | 13 +++++++++++++ 6 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .crystal-version create mode 100644 spec/socks/server_spec.cr create mode 100644 src/socks/server.cr diff --git a/.crystal-version b/.crystal-version new file mode 100644 index 0000000..1b58cc1 --- /dev/null +++ b/.crystal-version @@ -0,0 +1 @@ +0.27.0 diff --git a/spec/socks/server_spec.cr b/spec/socks/server_spec.cr new file mode 100644 index 0000000..aac067b --- /dev/null +++ b/spec/socks/server_spec.cr @@ -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 diff --git a/spec/socks_spec.cr b/spec/socks_spec.cr index c57e588..4f3ef5d 100644 --- a/spec/socks_spec.cr +++ b/spec/socks_spec.cr @@ -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) @@ -76,7 +77,6 @@ describe Socks do client << "pong\n" } - client = TCPSocket.new("localhost", SSH_PORT) client << "ping\n" response = client.gets end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index ce87b56..7e01b6c 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -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"]) diff --git a/src/socks.cr b/src/socks.cr index 012da5d..de4ee24 100644 --- a/src/socks.cr +++ b/src/socks.cr @@ -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" diff --git a/src/socks/server.cr b/src/socks/server.cr new file mode 100644 index 0000000..6ca29bc --- /dev/null +++ b/src/socks/server.cr @@ -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 +