From ac0df188f345e77b6bda8772bcd763a0f884041e Mon Sep 17 00:00:00 2001 From: Dylan McKay Date: Sun, 1 May 2016 19:51:05 +1200 Subject: [PATCH] Support SOCKS proxies This adds support for SOCKS proxies (which SSH tunnelling provides). --- Gemfile | 1 + jenkins_api_client.gemspec | 3 +++ lib/jenkins_api_client/client.rb | 13 ++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 30e7919c..65c8f640 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ gem "thor", ">= 0.16.0" gem "json", ">= 0" gem "terminal-table", ">= 1.4.0" gem "mixlib-shellout", ">= 1.1.0" +gem 'socksify', ">= 1.7.0" group :development do gem "bundler", ">= 1.0" diff --git a/jenkins_api_client.gemspec b/jenkins_api_client.gemspec index ecf8badc..aa237c17 100644 --- a/jenkins_api_client.gemspec +++ b/jenkins_api_client.gemspec @@ -102,6 +102,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency(%q, [">= 0"]) s.add_runtime_dependency(%q, [">= 1.4.0"]) s.add_runtime_dependency(%q, [">= 1.1.0"]) + s.add_runtime_dependency(%q, [">= 1.7.0"]) s.add_development_dependency(%q, [">= 1.0"]) s.add_development_dependency(%q, [">= 1.6.4"]) s.add_development_dependency(%q, ["~> 2.14.1"]) @@ -115,6 +116,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 1.4.0"]) s.add_dependency(%q, [">= 1.1.0"]) + s.add_dependency(%q, [">= 1.7.0"]) s.add_dependency(%q, [">= 1.0"]) s.add_dependency(%q, [">= 1.6.4"]) s.add_dependency(%q, ["~> 2.14.1"]) @@ -129,6 +131,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 1.4.0"]) s.add_dependency(%q, [">= 1.1.0"]) + s.add_dependency(%q, [">= 1.7.0"]) s.add_dependency(%q, [">= 1.0"]) s.add_dependency(%q, [">= 1.6.4"]) s.add_dependency(%q, ["~> 2.14.1"]) diff --git a/lib/jenkins_api_client/client.rb b/lib/jenkins_api_client/client.rb index 9c89e2e7..4d86630d 100644 --- a/lib/jenkins_api_client/client.rb +++ b/lib/jenkins_api_client/client.rb @@ -29,6 +29,7 @@ require 'mixlib/shellout' require 'uri' require 'logger' +require 'socksify/http' # The main module that contains the Client class and all subclasses that # communicate with the Jenkins's Remote Access API. @@ -53,6 +54,7 @@ class Client "server_port", "proxy_ip", "proxy_port", + "proxy_protocol", "jenkins_path", "username", "password", @@ -86,6 +88,7 @@ class Client # :/user//configure # @option args [String] :proxy_ip the proxy IP address # @option args [String] :proxy_port the proxy port + # @option args [String] :proxy_protocol the proxy protocol ('socks' or 'http' (defaults to HTTP) # @option args [String] :jenkins_path ("/") the optional context path for Jenkins # @option args [Boolean] :ssl (false) indicates if Jenkins is accessible over HTTPS # @option args [Boolean] :follow_redirects this argument causes the client to follow a redirect (jenkins can @@ -146,6 +149,7 @@ def initialize(args) @http_open_timeout = DEFAULT_HTTP_OPEN_TIMEOUT unless @http_open_timeout @http_read_timeout = DEFAULT_HTTP_READ_TIMEOUT unless @http_read_timeout @ssl ||= false + @proxy_protocol ||= 'http' # Setting log options if @logger @@ -292,7 +296,14 @@ def make_http_request(request, follow_redirect = @follow_redirects) request['Cookie'] = @cookies if @cookies if @proxy_ip - http = Net::HTTP::Proxy(@proxy_ip, @proxy_port).new(@server_ip, @server_port) + case @proxy_protocol + when 'http' + http = Net::HTTP::Proxy(@proxy_ip, @proxy_port).new(@server_ip, @server_port) + when 'socks' + http = Net::HTTP::SOCKSProxy(@proxy_ip, @proxy_port).start(@server_ip, @server_port) + else + raise "unknwon proxy protocol: '#{@proxy_protocol}'" + end else http = Net::HTTP.new(@server_ip, @server_port) end