Skip to content

Commit

Permalink
支持根据响应更新 cookie
Browse files Browse the repository at this point in the history
合并urbanadventurer/WhatWeb#281

支持根据响应更新 cookie
  • Loading branch information
winezer0 committed May 10, 2023
1 parent 0b82820 commit b213f7d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,21 @@ def open_url(options)
end

@headers['set-cookie'] = res.get_fields('set-cookie').join("\n") unless @headers['set-cookie'].nil?
# update cookies
if $UPDATE_COOKIES and not @headers['set-cookie'].nil?
res.get_fields('set-cookie').each do |raw_cookie|
cookie = raw_cookie.split(';')[0]
cookie_name = cookie.split('=')[0]
cookie_pattern = Regexp.new('%s=.*?(?=$|;)' % cookie_name)
if not $CUSTOM_HEADERS.has_key?('Cookie')
$CUSTOM_HEADERS['Cookie'] = cookie
elsif $CUSTOM_HEADERS['Cookie'].match(cookie_pattern)
$CUSTOM_HEADERS['Cookie'].sub!(cookie_pattern, cookie)
else
$CUSTOM_HEADERS['Cookie'] += '; %s' % cookie
end
end
end

@status = res.code.to_i
puts @uri.to_s + " [#{status}]" if $verbose > 1
Expand Down
1 change: 1 addition & 0 deletions lib/whatweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
$USER_AGENT = "WhatWeb/#{WhatWeb::VERSION}"
$AGGRESSION = 1
$FOLLOW_REDIRECT = 'always'
$UPDATE_COOKIES = false
$USE_PROXY = false
$PROXY_HOST = nil
$PROXY_PORT = 8080
Expand Down
2 changes: 1 addition & 1 deletion lib/whatweb/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
# You should have received a copy of the GNU General Public License
# along with WhatWeb. If not, see <http://www.gnu.org/licenses/>.
module WhatWeb
VERSION = '0.5.5.14'.freeze
VERSION = '0.5.5.15'.freeze
end
4 changes: 4 additions & 0 deletions whatweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def usage_full
--user, -u=<user:password>\tHTTP basic authentication.
--cookie, -c=COOKIES\t\tUse cookies, e.g. 'name=value; name2=value2'.
--cookie-jar=FILE\t\tRead cookies from a file.
--update-cookies\t\tUpdate cookies according to responses.
PROXY:
--proxy, -P\t\t\t<hostname[:port]> Set proxy hostname and port.
Expand Down Expand Up @@ -347,6 +348,7 @@ def error(s)
['--header', '-H', GetoptLong::REQUIRED_ARGUMENT],
['--cookie', '-c', GetoptLong::REQUIRED_ARGUMENT],
['--cookie-jar', GetoptLong::REQUIRED_ARGUMENT],
['--update-cookies', GetoptLong::NO_ARGUMENT],
['--user', '-u', GetoptLong::REQUIRED_ARGUMENT],
['--wait', GetoptLong::REQUIRED_ARGUMENT],
['--debug', GetoptLong::NO_ARGUMENT],
Expand Down Expand Up @@ -530,6 +532,8 @@ def error(s)
rescue
raise("Could not read cookies from #{arg}.")
end
when '--update-cookies'
$UPDATE_COOKIES = true
when '-u', '--user'
unless arg.include?(':')
raise("Incorrect credentials format.")
Expand Down

0 comments on commit b213f7d

Please sign in to comment.