the hard reset

This commit is contained in:
Yessiest 2024-04-29 15:37:32 +04:00
parent c151429e85
commit abe6521fd6
8 changed files with 0 additions and 1059 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "hyde"]
path = hyde
url = git@adastra7.net:Yessiest/hyde

View File

@ -1,14 +0,0 @@
# The behavior of RuboCop can be controlled via the .rubocop.yml
# configuration file. It makes it possible to enable/disable
# certain cops (checks) and to alter their behavior if they accept
# any parameters. The file can be placed either in your home
# directory or in some project directory.
#
# RuboCop will start looking for the configuration file in the directory
# where the inspected file is and continue its way up to the root directory.
#
# See https://docs.rubocop.org/rubocop/configuration
require: standard
inherit_gem:
standard: config/base.yml

View File

@ -1,22 +0,0 @@
---
include:
- "**/*.rb"
exclude:
- spec/**/*
- test/**/*
- vendor/**/*
- ".bundle/**/*"
require: []
domains: []
reporters:
- rubocop
- require_not_found
formatter:
rubocop:
cops: safe
except: []
only: []
extra_args: []
require_paths: []
plugins: []
max_files: 5000

View File

@ -1,9 +0,0 @@
# frozen_string_literal: true
source "https://rubygems.org"
# gem "rails"
gem 'webrick'
gem 'mime-types'
gem 'webrick-websocket'

185
client.rb
View File

@ -1,185 +0,0 @@
#!/usr/bin/ruby
require 'readline'
require 'net/http'
require 'json'
require 'websocket-client-simple'
require 'uri'
puts "Connecting to server #{ARGV[0]} on port #{ARGV[1]}"
def get(path)
where = URI("http://#{ARGV[0]}:#{ARGV[1]}/#{path}")
JSON.parse(Net::HTTP.get(where))
end
def post(path,data)
where = URI("http://#{ARGV[0]}:#{ARGV[1]}/#{path}")
Net::HTTP.post(where,data.to_json)
end
version = get("version")["version"]
puts "Server reported version: #{version}"
print "Nickname> "
nickname = $stdin.gets.strip
puts "Trying to log in..."
res = get("/user/exists?protocol_id=#{"heimdall-"+nickname}")
puts "Account exists! exiting" if res["exists"]
return if res["exists"]
puts "Creating account..."
test = post("/user/new",{username: nickname, protocol_id: "heimdall-"+nickname})
unless test.kind_of? Net::HTTPOK then
puts "Something went wrong! exiting"
exit
end
puts "Your id is: heimdall-#{nickname}"
puts "Establishing websocket connection..."
ws = WebSocket::Client::Simple.connect "ws://#{ARGV[0]}:#{ARGV[2]}"
ws.on :message do |msg|
data = JSON.parse(msg.data)
if data.has_key? "websocket" then
WEBSOCKET_UID = uid = data["websocket"]
response = post("/user/listen",{
websocket: uid,
protocol_id: "heimdall-"+nickname
})
unless response.kind_of? Net::HTTPOK then
puts "Something went wrong when initiating listening to user! Check server logs for info."
end
elsif data.has_key? "error" then
puts "ERROR: #{data["error"]}"
elsif data.has_key? "room" then
puts "[#{data["room"]["name"]}] #{data["user"]["username"]}: #{data["content"]}"
else
puts "#{data["user"]["username"]}: #{data["content"]}"
end
end
ws.on :open do |msg|
puts "Websocket connection established"
end
at_exit do
ws.close
end
target = nil
target_type = nil
at_exit do
post("/user/delete",{
"protocol_id"=> "heimdall-"+nickname
})
end
while buf = Readline.readline("", true)
if buf == "/help" then
puts "Commands:"
puts "/help - this message"
puts "/send <protcol_id> - direct messages to somebody"
puts "/exit - quit program"
puts "/find <username> - find a username by pattern"
puts "/find-protoid <protoid> - find by a protocol id"
puts "/username <username> - set your username (does not change your protocol id"
puts "/create-room <roomname> - create a room"
puts "/join <room id> - join a room"
puts "/leave <room> - leave a room"
puts "/send-room <room> - send messages to a room"
next
end
if buf == "/exit" then
post("/user/delete",{
"protocol_id"=> "heimdall-"+nickname
})
exit
end
if buf.match(/^\/send .*$/) then
target = buf.match(/^\/send ([^\s]*)$/)[1]
target_type = "user"
next
end
if buf.match(/^\/send-room .*$/) then
target = buf.match(/^\/send-room ([^\s]*)$/)[1]
target_type = "room"
next
end
if buf.match(/^\/join .*$/) and defined? WEBSOCKET_UID then
target = buf.match(/^\/join ([^\s]*)$/)[1]
if get("/room/exists?protocol_id=#{target}")["exists"] then
target_type = "room"
post("/room/listen", {
websocket: WEBSOCKET_UID,
protocol_id: target
})
else
target = nil
end
next
end
if buf.match(/^\/leave .*$/) and defined? WEBSOCKET_UID then
target = buf.match(/^\/leave ([^\s]*)$/)[1]
if get("/room/exists?protocol_id=#{target}")["exists"] then
post("/room/unlisten",{
websocket: WEBSOCKET_UID,
protocol_id: target
})
if target_type == "room" then
target = nil
end
end
end
if buf.match(/^\/find .*$/) then
uname = (buf.match /^\/find (.*)$/)[1]
users = get("/user/find/by-name?username=#{uname}")["results"]
puts "Found #{users.length} results: "
users.each { |x| puts x[0] }
next
end
if buf.match(/^\/find-protoid .*$/) then
pid = (buf.match /^\/find-protoid (.*)$/)[1]
users = get("/user/find/by-protoid?protocol_id=#{pid}")["results"]
puts "Found #{users.length} results: "
users.each { |x| puts x[0] }
next
end
if buf.match(/^\/username .*$/) then
uname = (buf.match /^\/username (.*)$/)[1]
post("/user/modify",{
data: {
username: uname
},
protocol_id: "heimdall-"+nickname
})
next
end
if buf.match(/^\/create-room \S+$/) then
name = (buf.match /^\/create-room (\S+)$/)[1]
post("/room/new",{
protocol_id: "heimdall-room-"+name,
name: name
})
puts("Room id: heimdall-room-#{name}")
end
if target and target_type then
if target_type == "user" then
post("/user/send", {
"to" => target,
"content" => buf,
"from" => "heimdall-"+nickname
})
elsif target_type == "room" then
post("/room/send", {
"to" => target,
"content" => buf,
"from" => "heimdall-"+nickname
})
end
end
end

1
hyde

@ -1 +0,0 @@
Subproject commit fd76422b760e6c045581bcb11842ee42221d06e0

221
proto.rb
View File

@ -1,221 +0,0 @@
UIDS = {}
module Heimdall
VERSION = "0.99 beta"
attr_reader :VERSION
class ProtocolError < StandardError
end
class UID
def initialize
@UID_prefix = "abstract" if not @UID_prefix
id = (1..32).map { |x| (rand()*10).floor }.join
while UIDS.has_key? id do
id = (1..32).map { |x| (rand()*10).floor }.join
end
UIDS[@UID_prefix+id] = self
@UID = id
end
attr_reader :UID
end
class UserCache
def initialize
@users = {}
end
def sync(data)
data.each { |userdata|
if not @users[userdata["protocol_id"]] then
new_user = User.new userdata
@users[new_user.protoid] = new_user
else
@users[userdata["protocol_id"]].modify(userdata)
end
}
end
def add(user)
@users[user.protoid] = user
end
def get(protoid)
raise ProtocolError, "user not found" if not @users[protoid]
return @users[protoid]
end
def search(name,n,&block)
@users.select(&block).take(n)
end
def search_by_name(name, n = 10)
search(name,n) { |k,v| v.username.match? name }
end
def search_by_protoid(name,n = 10)
search(name,n) { |k,v| k.match? name }
end
def filter(&block)
@users.filter &block
end
def delete(protoid)
@users.delete protoid
end
def bulk_delete(protoid_list)
protoid_list.each { |x| @users.delete x }
end
end
class RoomCache < UserCache
def get(protoid)
raise ProtocolError, "room not found" if not @users[protoid]
return @users[protoid]
end
def sync(data)
data.each { |userdata|
if not @users[userdata["protocol_id"]] then
new_user = Room.new userdata
@users[new_user.protoid] = new_user
else
@users[userdata["protocol_id"]].modify(userdata)
end
}
end
end
class RoomFilter
def initialize(cache, room_protoid)
@cache = cache
@filter = room_protoid
end
def get(protoid)
raise ProtocolError, "user not found" if not _filter[protoid]
return _filter[protoid]
end
private
def _filter
@cache.filter { |k,v|
v.protoid == @filter
}
end
end
class User < UID
def initialize(data)
@username = data["username"]
@protoid = data["protocol_id"]
@UID_prefix = "user"
@channel = DirectChannel.new
super()
end
def to_card()
return {
"username" => @username,
"protoid" => @protoid
}
end
def modify(data)
@username = data["username"]
end
attr_reader :username
attr_reader :protoid
attr_reader :channel
end
class Room < UID
def initialize(data)
@name = data["name"]
@protoid = data["protocol_id"]
@UID_prefix = "room"
@channel = RoomChannel.new
@users = {}
super()
end
def to_card()
return {
"name" => @name,
"protoid" => @protoid
}
end
def modify(data)
@name = data["name"]
end
attr_reader :users
attr_reader :username
attr_reader :protoid
attr_reader :channel
end
class Channel < UID
def initialize
@UID_prefix = "channel"
@messages = MsgStack.new
@read = 0
super()
end
def send(msg)
@messages.push(msg)
@read = @read+1
end
def send_silent(msg)
@messages.push(msg)
end
def get(n = 1)
raise Heimdall::ProtocolError, "Invalid number of messages" if n < 0
return @messages.pull(n)
end
def read()
messages = @messages.pull(@read)
@read = 0
return messages
end
end
class DirectChannel < Channel
def initialize
@UID_prefix = "dchannel"
super()
end
end
class RoomChannel < Channel
def initialize
@UID_prefix = "schannel"
super()
end
undef read
def get(n = 1)
@read = 0
super(n)
end
end
class Message < UID
def initialize(data)
@content = data["content"]
@from = data["from"]
@to = data["to"]
@UID_prefix = "message"
super()
end
def to_struct
return {
"content" => @content,
"from" => @from,
"to" => @to
}
end
attr_reader :content
attr_reader :from
attr_reader :to
end
class MsgStack < UID
def initialize
@UID_prefix = "msgstack"
@messages = []
super()
end
def push(msg)
@messages.append(msg)
end
def pull(n)
@messages.last n
end
end
end

604
server.rb
View File

@ -1,604 +0,0 @@
require_relative "proto"
require_relative "hyde/hyde"
require 'webrick/websocket'
require "json"
Users = Heimdall::UserCache.new
Rooms = Heimdall::RoomCache.new
SocketsMap = {}
NotifyList = {}
def _require_keys(dict,key_dict)
raise KeyError, "not a dict" unless dict.kind_of? Hash
key_dict.each_pair { |k,v|
unless (dict.has_key? k.to_s) and (dict[k.to_s].kind_of? v) then
raise KeyError, "key #{k} of type #{v} required"
end
}
end
def _send_json(res,data,code: 200)
res.body = JSON::fast_generate(data)
res['Content-Type'] = "application/json"
res.status = code
end
def _throw_error(res,error)
_send_json(res,{
error: "#{error}"
},code: 400)
end
def _parse_json(body,key_dict)
data = JSON::Parser.new(body).parse
_require_keys(data,key_dict)
return data
end
server = Hyde::Server.new Port: 8000 do
path "user" do
path "find" do
index ["by-name"]
get "by-name" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query, {
username: String
})
_send_json(res, {
"results": Users.search_by_name(req.query['username']).map { |x| x[1].to_card }
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
end
end
get "by-protoid" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query, {
protocol_id: String
})
_send_json(res, {
"results": Users.search_by_protoid(req.query['protocol_id']).map { |x| x[1].to_card; x }
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
end
end
end
post "sync" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
data: Array
})
Users.sync(data["data"])
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "bulk-delete" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
users: Array
})
Users.bulk_delete(data["users"])
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
get "exists" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query, {
protocol_id: String
})
_send_json(res,{
exists: (Users.get(req.query["protocol_id"]) != nil)
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue Heimdall::ProtocolError => protoerr
_send_json(res, {
exists: false
})
end
end
post "new" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body,{
username: String,
protocol_id: String
})
new_user = Heimdall::User.new(data)
Users.add(new_user)
_send_json(res,{
status: true
})
NotifyList[new_user.UID] = []
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue ProtocolError => protoerr
_throw_error(res,protoerr)
end
end
post "modify" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
data: Hash,
protocol_id: String
})
user = Users.get(data["protocol_id"])
user.modify(data["data"])
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue ProtocolError => protoerr
_throw_error(res,protoerr)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "send" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
content: String,
from: String,
to: String
})
new_message = Heimdall::Message.new(data)
user = Users.get(new_message.to)
if NotifyList[user.UID].length != 0 then
NotifyList[user.UID].each { |sockid|
sock = SocketsMap[sockid]
msg = new_message.to_struct
msg["user"] = Users.get(msg["from"]).to_card
sock.puts(JSON::fast_generate(msg))
}
user.channel.send_silent(new_message)
else
user.channel.send(new_message)
end
_send_json(res,{
status: true
})
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue Heimdall::ProtocolError => protoerr
_throw_error(res,protoerr)
end
end
get "get" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query,{
n: Integer,
protocol_id: String
})
number = req.query[:n]
id = req.query["protocol_id"]
user = Users.get(id)
messages = user.channel.get(number)
_send_json(res, {
messages: messages.map { |x|
x = x.to_struct
x["user"] = Users.get(x["from"]).to_card
x
}
})
rescue Heimdall::ProtocolError => protoerr
_throw_error(res,protoerr)
end
end
get "read" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query,{
protocol_id: String
})
id = req.query["protocol_id"]
user = Users.get(id)
messages = user.channel.read
_send_json(res, {
messages: messages.map { |x|
x = x.to_struct
x["user"] = Users.get(x["from"]).to_card
x
}
})
rescue Heimdall::ProtocolError => protoerr
_throw_error(res,protoerr)
end
end
post "listen" do |ctx|
req,res = ctx.request, ctx.response
begin
data = _parse_json(req.body, {
websocket: String,
protocol_id: String
})
uid = Users.get(data["protocol_id"]).UID
raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
NotifyList[uid].append data["websocket"]
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "unlisten" do |ctx|
req,res = ctx.request, ctx.response
begin
data = _parse_json(req.body, {
websocket: String,
protocol_id: String
})
uid = Users.get(data["protocol_id"]).UID
raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
NotfiyList[uid].delete data["websocket"]
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "delete" do |ctx|
req,res = ctx.request, ctx.response
begin
data = _parse_json(req.body,{
protocol_id: String
})
id = data["protocol_id"]
_send_json(res, {
status: true
})
user = Users.get(id)
NotifyList.delete(user.UID)
Users.delete(id)
rescue Heimdall::ProtocolError => protoerr
_throw_error(res,protoerr)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
end
path "room" do
path "find" do
index ["by-name"]
get "by-name" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query, {
username: String
})
_send_json(res, {
"results": Rooms.search_by_name(req.query['username']).map { |x| x[1].to_card }
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
end
end
get "by-protoid" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query, {
protocol_id: String
})
_send_json(res, {
"results": Rooms.search_by_protoid(req.query['protocol_id']).map { |x| x[1].to_card; x }
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
end
end
end
post "new" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
name: String,
protocol_id: String
})
new_room = Heimdall::Room.new(data)
Rooms.add(new_room)
_send_json(res, {
status: true
})
NotifyList[new_room.UID] = []
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "send" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
content: String,
from: String,
to: String
})
new_message = Heimdall::Message.new(data)
room = Rooms.get(new_message.to)
if NotifyList[room.UID].length != 0 then
NotifyList[room.UID].each { |sockid|
sock = SocketsMap[sockid]
msg = new_message.to_struct
msg["user"] = Users.get(msg["from"]).to_card
msg["room"] = room.to_card
sock.puts(JSON::fast_generate(msg))
}
room.channel.send_silent(new_message)
else
room.channel.send(new_message)
end
_send_json(res,{
status: true
})
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue Heimdall::ProtocolError => protoerr
_throw_error(res,protoerr)
end
end
get "get" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query,{
n: Integer,
protocol_id: String
})
number = req.query[:n]
id = req.query["protocol_id"]
room = Rooms.get(id)
messages = room.channel.get(number)
_send_json(res, {
messages: messages.map { |x|
x = x.to_struct
x["user"] = Users.get(x["from"]).to_card
x["room"] = room.to_card
x
}
})
rescue Heimdall::ProtocolError => protoerr
_throw_error(res,protoerr)
end
end
post "listen" do |ctx|
req,res = ctx.request, ctx.response
begin
data = _parse_json(req.body, {
websocket: String,
protocol_id: String
})
uid = Rooms.get(data["protocol_id"]).UID
raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
NotifyList[uid].append data["websocket"]
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "unlisten" do |ctx|
req,res = ctx.request, ctx.response
begin
data = _parse_json(req.body, {
websocket: String,
protocol_id: String
})
uid = Rooms.get(data["protocol_id"]).UID
raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
NotfiyList[uid].delete data["websocket"]
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "modify" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
data: Hash,
protocol_id: String
})
room = Rooms.get(data["protocol_id"])
room.modify(data["data"])
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue ProtocolError => protoerr
_throw_error(res,protoerr)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "delete" do |ctx|
req,res = ctx.request, ctx.response
begin
data = _parse_json(req.body,{
protocol_id: String
})
id = data["protocol_id"]
_send_json(res, {
status: true
})
room = Rooms.get(id)
NotifyList.delete(room.UID)
Rooms.delete(id)
rescue Heimdall::ProtocolError => protoerr
_throw_error(res,protoerr)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "sync" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
data: Array
})
Rooms.sync(data["data"])
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
post "bulk-delete" do |ctx|
req,res = ctx.request,ctx.response
begin
data = _parse_json(req.body, {
users: Array
})
Rooms.bulk_delete(data["users"])
_send_json(res, {
status: true
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue JSON::ParserError => jsonerror
_throw_error(res,jsonerror)
end
end
get "exists" do |ctx|
req,res = ctx.request,ctx.response
begin
_require_keys(req.query, {
protocol_id: String
})
_send_json(res,{
exists: (Rooms.get(req.query["protocol_id"]) != nil)
})
rescue KeyError => keyerror
_throw_error(res,keyerror)
rescue Heimdall::ProtocolError => protoerr
_send_json(res, {
exists: false
})
end
end
end
get "version" do |ctx|
ctx.response.body = "{\"version\":\"#{Heimdall::VERSION}\"}"
ctx.response['Content-Type'] = "application/json"
end
end
at_exit do
server.shutdown
end
class WebsocketUID < Heimdall::UID
def initialize
@UID_prefix = "websocket"
super
end
end
class EventServlet < WEBrick::Websocket::Servlet
def socket_open(sock)
@UID = WebsocketUID.new
@connected_listeners = []
SocketsMap[@UID.UID] = sock
sock.puts(JSON::fast_generate({
websocket: @UID.UID.to_s
}))
end
def socket_close(sock)
SocketsMap.delete @UID.UID
NotifyList.each do |k,v|
if v.include? @UID.UID
v.delete @UID.UID
end
end
end
def socket_text(sock,text)
# do nothing
end
end
Thread.new do
websocket_server = WEBrick::Websocket::HTTPServer.new Port:8001
websocket_server.mount "/", EventServlet
websocket_server.start
at_exit do
websocket_server.shutdown
end
end
server.start