This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
19 changed files
with
301 additions
and
47 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
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 |
---|---|---|
|
@@ -35,3 +35,4 @@ | |
!/app/assets/builds/.keep | ||
|
||
/node_modules | ||
.env |
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# :nocov: | ||
ActiveAdmin.register ClientAddress do | ||
menu parent: 'Remote', priority: 1 | ||
|
||
permit_params :client_id, :value, :active | ||
|
||
index do | ||
selectable_column | ||
id_column | ||
column :client | ||
column :value | ||
column :active | ||
column :created_at | ||
column :updated_at | ||
actions | ||
end | ||
end | ||
# :nocov: |
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# :nocov: | ||
ActiveAdmin.register ServerAddress do | ||
menu parent: 'Remote', priority: 1 | ||
|
||
permit_params :server_id, :value, :active | ||
|
||
index do | ||
selectable_column | ||
id_column | ||
column :server | ||
column :value | ||
column :active | ||
column :created_at | ||
column :updated_at | ||
actions | ||
end | ||
end | ||
# :nocov: |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
RemotePage = Struct.new(:title, :blurb, :content, :url) | ||
RemotePage = Struct.new(:title, :blurb, :content, :url) do | ||
def to_partial_path | ||
'pages/page' | ||
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
class RequestUrl | ||
attr_reader :address | ||
|
||
def initialize | ||
# TODO: Make this dynamic, distributed | ||
server = Server.first | ||
@address = server&.server_addresses&.first | ||
end | ||
|
||
def url | ||
return unless scheme && address | ||
|
||
"#{scheme.name}://#{address.value}#{port}/api/search" | ||
end | ||
|
||
def scheme | ||
name = Rails.env.production? ? 'https' : 'http' | ||
Scheme.find_by(name:) | ||
rescue StandardError | ||
logger.error("Invalid scheme name '#{name}'") | ||
end | ||
|
||
def port | ||
Rails.env.production? ? '' : ':3000' | ||
end | ||
|
||
def self.url | ||
RequestUrl.new.url | ||
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 |
---|---|---|
@@ -1,36 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module RemoteSearchService | ||
include Headers | ||
class RemoteSearchService | ||
attr_reader :term, :url | ||
|
||
def self.search(term) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize | ||
server = Server.first | ||
return Page.none unless server | ||
|
||
address = server.server_addresses.first | ||
return Page.none unless address | ||
|
||
url = "#{scheme.name}://#{address.value}#{port}/api/search?q=#{term}" | ||
def initialize(term) | ||
@term = term | ||
@url = RequestUrl.url | ||
end | ||
|
||
response = HTTParty.get(url, { headers: }) | ||
json = JSON.parse(response.body) | ||
def search | ||
return [] unless json && json['pages'] | ||
|
||
pages = [] | ||
json['pages'].each do |page| | ||
pages << RemotePage.new(title: page['title'], blurb: page['blurb'], content: page['content'], url: page['url']) | ||
json['pages'].map do |page| | ||
RemotePage.new(title: page['title'], blurb: page['blurb'], content: page['content'], url: page['url']) | ||
end | ||
end | ||
|
||
pages | ||
private | ||
|
||
def request | ||
RequestEncoderService.new(term).encode | ||
# rescue StandardError | ||
# Rails.logger.error('Failed to encode request') | ||
# nil | ||
end | ||
|
||
class << self | ||
def scheme | ||
name = Rails.env.production? ? 'https' : 'http' | ||
Scheme.find_by(name:) | ||
end | ||
def response | ||
r = request | ||
HTTParty.post(url, body: r[:body], headers: r[:headers]) | ||
# rescue StandardError | ||
# Rails.logger.error('Remote request failed') | ||
# nil | ||
end | ||
|
||
def port | ||
Rails.env.production? ? '' : ':3000' | ||
end | ||
def json | ||
JSON.parse(response.body) | ||
# rescue StandardError | ||
# Rails.logger.error('Failed to parse response body') | ||
# nil | ||
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
class RequestDecoderService | ||
attr_reader :request | ||
|
||
def initialize(request) | ||
@request = request | ||
end | ||
|
||
def decode | ||
client = find_client_by_ip_addr | ||
return {} unless client | ||
|
||
decode_request(client) | ||
end | ||
|
||
private | ||
|
||
def decode_request(client) | ||
params = request.params | ||
return {} unless params[:data] && params[:algorithm] | ||
|
||
begin | ||
data = JWT.decode(params[:data], client.pub_key, true, { algorithm: params[:algorithm] }) | ||
{ q: data.first['q'] } | ||
rescue JWT::DecodeError | ||
{} | ||
end | ||
end | ||
|
||
def find_client_by_ip_addr | ||
client_address = ClientAddress.find_by(value: request.remote_ip) | ||
return unless client_address&.active? | ||
|
||
client = client_address.client | ||
client if client.active? | ||
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class RequestEncoderService | ||
include Headers | ||
|
||
attr_reader :term, :private_key, :algorithm | ||
|
||
def initialize(term, private_key: nil, algorithm: nil) | ||
@term = term | ||
@private_key = private_key || ENV.fetch('RSA_PRIVATE_KEY', nil) | ||
@algorithm = algorithm || ENV.fetch('RSA_ALGO', nil) | ||
end | ||
|
||
def encode | ||
Rails.logger.error('Invalid RSA private key value') unless private_key | ||
Rails.logger.error('Invalid RSA algorithm value') unless algorithm | ||
|
||
return unless private_key && algorithm | ||
|
||
body = { data:, algorithm: }.to_json | ||
headers = self.headers.merge({ 'Content-Type' => 'application/json' }) | ||
|
||
{ body:, headers: } | ||
end | ||
|
||
private | ||
|
||
def pkey | ||
OpenSSL::PKey::RSA.new(private_key) | ||
# rescue StandardError | ||
# logger.error('Invalid RSA private key value') | ||
end | ||
|
||
def data | ||
JWT.encode({ q: term }, pkey, algorithm) | ||
# rescue StandardError | ||
# logger.error('Failed to encode JWT data') | ||
end | ||
end |
File renamed without changes.
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 |
---|---|---|
@@ -1,9 +1,6 @@ | ||
<% if @pages&.any? %> | ||
<p>Remote search results for "<%= params[:q] %>":</p> | ||
|
||
<% @pages.each do |page| %> | ||
<%= render partial: 'shared/page', locals: { page: page } %> | ||
<% end %> | ||
<%= render @pages %> | ||
<% else %> | ||
<p>No remote search results found for "<%= params[:q] %>".</p> | ||
<% 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
Oops, something went wrong.