84 lines
4.0 KiB
Ruby
84 lines
4.0 KiB
Ruby
class ApiScoringController < ApiController
|
|
|
|
respond_to :json
|
|
before_filter :api_signed_in_user
|
|
|
|
def work # clientid; returns another clientid
|
|
client_id = params[:clientid]
|
|
if client_id.nil? then render :json => {message: 'client_id not specified'}, :status => 400; return end
|
|
|
|
c = Connection.where(client_id: client_id).first
|
|
if c.nil? then render :json => {message: 'connection not found'}, :status => 404; return end
|
|
if !c.user.id.eql?(current_user.id) then render :json => {message: 'user does not own client_id'}, :status => 403; return end
|
|
|
|
# todo this method is a stub
|
|
result_client_id = client_id+'peer'
|
|
|
|
render :json => {:clientid => result_client_id}, :status => 200
|
|
end
|
|
|
|
def worklist # clientid; returns a list of clientid
|
|
client_id = params[:clientid]
|
|
if client_id.nil? then render :json => {message: 'client_id not specified'}, :status => 400; return end
|
|
|
|
c = Connection.where(client_id: client_id).first
|
|
if c.nil? then render :json => {message: 'connection not found'}, :status => 404; return end
|
|
if !c.user.id.eql?(current_user.id) then render :json => {message: 'user does not own client_id'}, :status => 403; return end
|
|
|
|
# todo this method is a stub
|
|
result_client_ids = [client_id+'peer1', client_id+'peer2']
|
|
|
|
render :json => {:clientids => result_client_ids}, :status => 200
|
|
end
|
|
|
|
def record # aclientid, aAddr, bclientid, bAddr, score returns nothing
|
|
|
|
aclient_id = params[:aclientid]
|
|
aip_address = params[:aAddr]
|
|
bclient_id = params[:bclientid]
|
|
bip_address = params[:bAddr]
|
|
score = params[:score]
|
|
|
|
if aclient_id.nil? then render :json => {message: 'aclient_id not specified'}, :status => 400; return end
|
|
if aip_address.nil? then render :json => {message: 'aAddr not specified'}, :status => 400; return end
|
|
if bclient_id.nil? then render :json => {message: 'bclient_id not specified'}, :status => 400; return end
|
|
if bip_address.nil? then render :json => {message: 'bAddr not specified'}, :status => 400; return end
|
|
if score.nil? then render :json => {message: 'score not specified'}, :status => 400; return end
|
|
|
|
aaddr = JamRuby::JamIsp.ip_to_num(aip_address)
|
|
if aaddr.nil? then render :json => {message: 'aAddr not valid ip_address'}, :status => 400; return end
|
|
|
|
baddr = JamRuby::JamIsp.ip_to_num(bip_address)
|
|
if baddr.nil? then render :json => {message: 'bAddr not valid ip_address'}, :status => 400; return end
|
|
|
|
if aaddr == baddr then render :json => {message: 'aAddr and bAddr are the same'}, :status => 403; return end
|
|
|
|
if !score.is_a? Numeric then render :json => {message: 'score not valid numeric'}, :status => 400; return end
|
|
|
|
aconn = Connection.where(client_id: aclient_id).first
|
|
if aconn.nil? then render :json => {message: 'a\'s session not found'}, :status => 404; return end
|
|
if aaddr != aconn.addr then render :json => {message: 'a\'s session addr does not match aAddr'}, :status => 403; return end
|
|
if !current_user.id.eql?(aconn.user.id) then render :json => {message: 'a\' session not owned by user'}, :status => 403; return end
|
|
|
|
bconn = Connection.where(client_id: bclient_id).first
|
|
if bconn.nil? then render :json => {message: 'b\'s session not found'}, :status => 404; return end
|
|
if baddr != bconn.addr then render :json => {message: 'b\'s session addr does not match bAddr'}, :status => 403; return end
|
|
|
|
if score < 0 or score > 999 then render :json => {message: 'score < 0 or score > 999'}, :status => 403; return end
|
|
|
|
aloc = JamRuby::GeoIpBlocks.lookup(aaddr)
|
|
aisp = JamRuby::JamIsp.lookup(aaddr)
|
|
if aisp.nil? or aloc.nil? then render :json => {message: 'a\'s location or isp not found'}, :status => 404; return end
|
|
alocidispid = aloc.locid*1000000+aisp.coid;
|
|
|
|
bloc = JamRuby::GeoIpBlocks.lookup(baddr)
|
|
bisp = JamRuby::JamIsp.lookup(baddr)
|
|
blocidispid = bloc.locid*1000000+bisp.coid
|
|
|
|
JamRuby::Score.createx(alocidispid, aclient_id, aaddr, blocidispid, bclient_id, baddr, score.ceil, nil)
|
|
|
|
render :json => {}, :status => 200
|
|
end
|
|
|
|
end
|