aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/sh_setup.lua
blob: 9c44e5711f58713f1edeae42116afb9f1bd22b47 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
--[[
    Some values that need to be setup by the server owner
]]

local aes = include("aes.lua")
local ECBMode = include("lockbox/ecb.lua")
local ZeroPadding = include("lockbox/padding.lua")
local Array = include("lockbox/array.lua")
local Stream = include("lockbox/stream.lua")
print("sh_setup included aes successfully")

local valuesneeded = {
    ["mysql DB host"] = "String",
    ["mysql DB dbname"] = "String",
    ["mysql DB uname"] = "String",
    ["mysql DB pass"] = "String",
    ["mysql should encrypt pass"] = "Bool",
    ["mysql encrypt password"] = "String",
    ["world default server ip:port"] = "String",
}

ART.Config = ART.Config or {}

if SERVER then
    util.AddNetworkString( "ART_CONFIG_WRITE" )
    local function ReadConfig(encryptkey)
        encryptkey = encryptkey or ""
        local ftext = file.Read("artery/config.txt", "DATA")
        if ftext == nil then
            print("Failed to read Config file, if this is a new setup, use art_setup to get started.")
            return
        end
        local tbl = string.Explode("\n",ftext,false)
        local strtbl = {}
        for k,v in pairs(tbl) do
            local ltext = v:Explode(":",false)
            strtbl[ltext[1]] = ltext[2]
        end
        for k,v in pairs(valuesneeded) do
            local tfunc = "to" .. v:lower()
            ART.Config[k] = _G[tfunc](strtbl[k])
        end
        if ART.Config["mysql should encrypt pass"] then
            if encryptkey == "" then
                print("Failed to retrive MySQL database password, please enter it with the \"artery_dbpasswordkey\" command.")
                return
            end
            ART.Config["mysql DB pass"] = aes.decrypt(lockstream.fromString(encryptkey),lockstream.fromString(ART.Config["mysql DB pass"]))
        end
    end

    ReadConfig()

    net.Receive( "ART_CONFIG_MYSQLPASS", function(len,ply)
        if not ply:IsAdmin() then
            return
        end
    end)

    net.Receive( "ART_CONFIG_WRITE", function(len,ply)
        print("Received write signal")
        if not ply:IsAdmin() then return end
        print("You're an admin!")
        for k,v in pairs(valuesneeded) do
            local ftype = "Read" .. v
            ART.Config[k] = net[ftype]()
        end
        if ART.Config["mysql should encrypt pass"] then
            local key = ART.Config["mysql encrypt password"]
            local block = ART.Config["mysql DB pass"]
            local akey = Array.fromString(key)
            local ablock = Array.fromString(block)
            local skey = Stream.fromString(key)
            local sblock = Stream.fromString(block)
            --print("sblock:" .. sblock)
            --print("skey:" .. skey)
            local cipher = ECBMode.Cipher().setKey(akey).setBlockCipher(aes).setPadding(ZeroPadding)
            local ciphertxt = cipher.init().update(sblock).finish().asHex()
            local decipher = ECBMode.Decipher().setKey(akey).setBlockCipher(aes).setPadding(ZeroPadding)
            local deciphertxt = decipher.init().update(Stream.fromHex(ciphertxt)).finish().asHex()
            print("Cyphertext of " .. block .. " is " .. ciphertxt)
            print("Deciphertext of " .. ciphertxt .. " is " .. deciphertxt)
            ART.Config["mysql DB pass"] = ciphertxt
        end
        local ftext = {}
        for k,v in pairs(ART.Config) do
            ftext[#ftext + 1] = k .. "=" .. tostring(v)
        end
        local wtext = table.concat(ftext,"\n")
        print("Writeing:" .. wtext)
        file.Write("artery/config.txt",wtext)
    end)

    net.Receive( "ART_CONFIG_REQUEST", function(len,ply)
        if not ply:IsAdmin() then return end
        for k,v in pairs(valuesneeded) do
            local ftype = "Write" .. v
            print("Calling " .. ftype .. " on " .. tostring(tbl[k]))
            net[ftype](tbl[k])
        end
    end)
end

print("Got to before concommands were added")

concommand.Add("artery_dbpasswordkey",function()
    if CLIENT then return end

end, nil, "Sets the encryption key for the mysql database password")

if CLIENT then
    print("Got to before setup command")

    concommand.Add("artery_setup", function(ply,cmd,args)
        print("setup called")
        if SERVER then return end
        print("Got past server gaurd")
        local width = ScrW()
        local height = ScrH()
        local configpanel = vgui.Create( "DFrame" )
        configpanel:SetPos( 0, height/8 )
        configpanel:SetSize( width/4, (height/4)*3 )
        configpanel:SetTitle( "Artery Settings" )
        configpanel:SetDraggable( true )
        configpanel:MakePopup()
        local scrollpanel = vgui.Create( "DScrollPanel", configpanel )
        scrollpanel:Dock(FILL)
        local entries = {}
        for k,v in pairs(valuesneeded) do
            local settinglabel = vgui.Create( "DLabel", scrollpanel )
            settinglabel:Dock(TOP)
            settinglabel:SetText( k )
            scrollpanel:AddItem(settinglabel)
            local settingentry
            if v == "String" then
                settingentry = vgui.Create( "DTextEntry", scrollpanel )
                settingentry:SetSize(width/10,18)
                settingentry:Dock(TOP)
            elseif v == "Bool" then
                settingentry = vgui.Create( "DCheckBox", holder)
                settingentry:Dock(TOP)
                --settingentry:SetSize(18,18)
            end
            scrollpanel:AddItem(settingentry)
            entries[k] = settingentry
        end
        local savebutton = vgui.Create( "DButton",scrollpanel )
        savebutton.DoClick = function()
            net.Start( "ART_CONFIG_WRITE")
            for k,v in pairs(valuesneeded) do
                local nfunc = "Write"..v
                local value = nil
                if v == "String" then value = entries[k]:GetValue()
                elseif v == "Bool" then value = entries[k]:GetChecked() end
                assert(value ~= nil, "Didn't know setting type:" .. v .. " for " .. k)
                print("Doing " .. nfunc .. " on " .. tostring(value))
                net[nfunc](value)
            end
            net.SendToServer()
        end
        savebutton:SetText("Save config")
        savebutton:Dock(TOP)
        scrollpanel:AddItem(savebutton)
    end)
end