This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.rb
146 lines (111 loc) · 3.45 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env ruby
#MCMyAuth - Custom Minecraft User Authentication
#Copyright (C) 2011 Anton Pirogov
#Licensed under the GPL version 3
require 'rubygems'
require 'sinatra'
require 'erb'
require 'digest/md5'
require './config' if File.exists?('./config.rb')
#set defaults if not present in config
$mainpage ||= '/users'
$servername ||= '<$servername not set>'
$serveraddress ||= '<$serveraddress not set>'
$admincontact ||= '<$admincontact not set>'
$mcma_version = 1 #MCMyAuth program version
$current_launcher_version = 1
$pwpath = 'passwords.txt' #File where password md5s are stored
$userpws = Hash.new
$sessions = Hash.new
$serverids = Hash.new
def md5(str)
Digest::MD5.hexdigest str
end
def load_users
userhashs = Hash.new
return Hash.new if File.exists?($pwpath)==false
lines = File.open($pwpath,'r').readlines.map{|l| l.chomp}
lines.each{|l| userhashs[l.split('=')[0]] = l.split('=')[1] }
$userpws = userhashs
end
def save_users
File.rename($pwpath, $pwpath+'.bak') if File.exists?($pwpath)
f = File.open($pwpath,'w')
$userpws.each do |k,v|
f.puts k+"="+v
end
f.close
File.delete($pwpath+'.bak') if File.exists?($pwpath+'.bak')
return true
end
#render a message using erb and providing a back link
def msg(str)
erb str + '<br /><a href="'+$mainpage+'">back</a>'
end
# Following is called from the website
get $mainpage do
erb :index
end
post $mainpage+'/register' do
name = params[:name]
pwd = params[:pwd]
return msg 'You have to set a nickname!' if name==''
return msg 'User already exists!' if $userpws[name] != nil
return msg 'You have to set a password!' if pwd==''
return msg 'Passwords do not match!' if pwd != params[:pwdrep]
$userpws[name] = md5 pwd
save_users
msg 'User successfully registered!<br /><br />You can login using the MCMyAuth launcher with Server=<b>'+$serveraddress+'</b> and your new account!'
end
post $mainpage+'/changepw' do
name = params[:name]
pwd = params[:pwd]
return msg 'Wrong nickname or password!' if $userpws[name].nil? || md5(pwd) != $userpws[name]
return msg 'Passwords do not match!' if params[:newpwd] != params[:pwdrep]
$userpws[name] = md5 params[:newpwd]
save_users
msg 'Password changed!'
end
post $mainpage+'/unregister' do
name = params[:name]
pwd = params[:pwd]
return msg 'Wrong nickname or password!' if $userpws[name].nil? || md5(pwd) != $userpws[name]
$userpws.delete(name)
save_users
msg 'Account deleted!'
end
# Called by client
get '/game/joinserver.jsp' do
name = params[:user]
serverid = params[:serverId].to_s
return 'User not registered!' if $userpws[name].nil?
return 'User not authenticated!' if $sessions[name] != params[:sessionId]
#if hash passed -> name verification
if serverid != ""
$serverids[name] = serverid
end
'ok'
end
# Called by server
get '/game/checkserver.jsp' do
name = params[:user]
serverid = params[:serverId]
return 'Failed to verify username!' if $userpws[name].nil? || serverid != $serverids[name]
'YES'
end
# Called by the launcher
post '/login' do
name = params[:user]
pwd = params[:password]
version = params[:version]
return 'Wrong nickname or password!' if $userpws[name].nil? || md5(pwd) != $userpws[name]
return 'Outdated launcher!' if version.to_i < $current_launcher_version
#generate new session id
sid = ''
16.times{ sid += rand(16).to_s(16) }
$sessions[name] = sid
#return nick + sessionid
return params[:user]+':'+sid
end
#Init user hash
load_users