diff options
| author | ruki <[email protected]> | 2022-04-30 12:48:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-30 12:48:38 +0800 |
| commit | 1546206045844f3b78244a55fa0771c30d10387d (patch) | |
| tree | be072874039c903094ce2ffddae6851b8c64f1ad | |
| parent | f07c1a3340266179d5289ad5a1509d33f9522856 (diff) | |
add and remove user
| -rw-r--r-- | xmake/actions/service/main.lua | 8 | ||||
| -rw-r--r-- | xmake/actions/service/xmake.lua | 6 | ||||
| -rw-r--r-- | xmake/modules/private/service/add_user.lua | 55 | ||||
| -rw-r--r-- | xmake/modules/private/service/client.lua | 13 | ||||
| -rw-r--r-- | xmake/modules/private/service/config.lua | 18 | ||||
| -rw-r--r-- | xmake/modules/private/service/import_config.lua | 6 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/client.lua | 5 | ||||
| -rw-r--r-- | xmake/modules/private/service/rm_user.lua | 58 | ||||
| -rw-r--r-- | xmake/modules/private/service/server.lua | 10 |
9 files changed, 153 insertions, 26 deletions
diff --git a/xmake/actions/service/main.lua b/xmake/actions/service/main.lua index d06c2f585..9080ff324 100644 --- a/xmake/actions/service/main.lua +++ b/xmake/actions/service/main.lua @@ -30,6 +30,8 @@ import("private.service.disconnect_service") import("private.service.clean_files") import("private.service.sync_files") import("private.service.import_config") +import("private.service.add_user") +import("private.service.rm_user") import("private.service.show_logs") import("private.service.show_status") @@ -52,7 +54,11 @@ function main() elseif option.get("sync") then sync_files() elseif option.get("config") then - import_config() + import_config(option.get("config")) + elseif option.get("add-user") then + add_user(option.get("add-user")) + elseif option.get("rm-user") then + rm_user(option.get("rm-user")) elseif option.get("logs") then show_logs() elseif option.get("status") then diff --git a/xmake/actions/service/xmake.lua b/xmake/actions/service/xmake.lua index 7b5de6263..74ffd313d 100644 --- a/xmake/actions/service/xmake.lua +++ b/xmake/actions/service/xmake.lua @@ -36,6 +36,12 @@ task("service") {nil, "config", "kv", nil, "Import the configuration file. (default: ~/.xmake/service.conf)", "e.g.", " - xmake service --config=/tmp/config.lua" }, + {nil, "add-user", "kv", nil, "Add user in the server.", + "e.g.", + " - xmake service --add-user=root" }, + {nil, "rm-user", "kv", nil, "Remove user in the server.", + "e.g.", + " - xmake service --rm-user=root" }, {nil, "logs", "k", nil, "Show service logs if the daemon service has been started." }, {nil, "status", "k", nil, "Show service status if the daemon service has been started." } }} diff --git a/xmake/modules/private/service/add_user.lua b/xmake/modules/private/service/add_user.lua new file mode 100644 index 000000000..99a1c9546 --- /dev/null +++ b/xmake/modules/private/service/add_user.lua @@ -0,0 +1,55 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file add_user.lua +-- + +-- imports +import("core.base.option") +import("core.base.base64") +import("core.base.bytes") +import("core.base.hashset") +import("private.service.service") +import("private.service.config") + +function main(user) + assert(user, "empty user name!") + + -- get user password + cprint("Please input user ${bright}%s${clear} password:", user) + io.flush() + local pass = (io.read() or ""):trim() + assert(pass ~= "", "password is empty!") + + -- compute user authorization + local auth = base64.encode(user .. ":" .. pass) + auth = hash.sha256(bytes(auth)) + + -- save to configs + local configs = assert(config.configs(), "configs not found!") + configs.server = configs.server or {} + configs.server.auths = configs.server.auths or {} + if not hashset.from(configs.server.auths):has(auth) then + table.insert(configs.server.auths, auth) + else + cprint("User ${bright}%s${clear} has been added!", user) + return + end + config.save(configs) + cprint("Add user ${bright}%s${clear} ok!", user) +end + diff --git a/xmake/modules/private/service/client.lua b/xmake/modules/private/service/client.lua index 4392ca19b..5a91ab1e8 100644 --- a/xmake/modules/private/service/client.lua +++ b/xmake/modules/private/service/client.lua @@ -40,6 +40,14 @@ function client:address_set(address) self._ADDR = "127.0.0.1" self._PORT = splitinfo[1] end + local addr = self._ADDR + if addr and addr:find("@", 1, true) then + splitinfo = addr:split('@', {plain = true}) + if #splitinfo == 2 then + self._USER = splitinfo[1] + self._ADDR = splitinfo[2] + end + end assert(self._ADDR and self._PORT, "invalid client address!") end @@ -48,11 +56,6 @@ function client:user() return self._USER end --- set user name -function client:user_set(user) - self._USER = user -end - -- get the ip address function client:addr() return self._ADDR diff --git a/xmake/modules/private/service/config.lua b/xmake/modules/private/service/config.lua index e803a79d9..b944a1801 100644 --- a/xmake/modules/private/service/config.lua +++ b/xmake/modules/private/service/config.lua @@ -30,10 +30,10 @@ function _generate_configfile() logfile = path.join(servicedir, "logs.txt"), server = { known_hosts = { - "127.0.0.1" + -- "127.0.0.1" }, auths = { - "root:123456" + -- "a7b198c3ddf355c59e9221d13305314b4b05130a2967dc76f66a59759bcc3250" } }, remote_build = { @@ -42,12 +42,12 @@ function _generate_configfile() workdir = path.join(servicedir, "remote_build"), }, client = { - connect = "127.0.0.1:9691", - user = "root" + -- with authorization: "[email protected]:9691" + connect = "127.0.0.1:9691" } } } - io.save(filepath, configs, {orderkeys = true}) + save(configs) end -- get config file path @@ -74,7 +74,7 @@ function get(name) return value end --- load config +-- load configs function load() assert(not _g.configs, "config has been loaded!") local filepath = configfile() @@ -84,3 +84,9 @@ function load() assert(os.isfile(filepath), "%s not found!", filepath) _g.configs = io.load(filepath) end + +-- save configs +function save(configs) + local filepath = configfile() + io.save(filepath, configs, {orderkeys = true}) +end diff --git a/xmake/modules/private/service/import_config.lua b/xmake/modules/private/service/import_config.lua index 666d6c436..2a4fcebef 100644 --- a/xmake/modules/private/service/import_config.lua +++ b/xmake/modules/private/service/import_config.lua @@ -21,8 +21,10 @@ -- imports import("core.base.option") import("private.service.service") +import("private.service.config") -function main() - -- TODO +function main(filepath) + os.cp(filepath, config.configfile()) + print("import %s ok!", filepath) end diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index fa4bfd4bf..c6106b78b 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -46,10 +46,6 @@ function remote_build_client:init() local address = assert(config.get("remote_build.client.connect"), "config(remote_build.client.connect): not found!") super.address_set(self, address) - -- init user - local user = config.get("remote_build.client.user") - super.user_set(self, user) - -- get project directory local projectdir = os.projectdir() local projectfile = os.projectfile() @@ -94,6 +90,7 @@ function remote_build_client:connect() -- compute user authorization auth = base64.encode(self:user() .. ":" .. pass) + auth = hash.sha256(bytes(auth)) end -- do connect diff --git a/xmake/modules/private/service/rm_user.lua b/xmake/modules/private/service/rm_user.lua new file mode 100644 index 000000000..793d7df1e --- /dev/null +++ b/xmake/modules/private/service/rm_user.lua @@ -0,0 +1,58 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file rm_user.lua +-- + +-- imports +import("core.base.option") +import("core.base.base64") +import("core.base.bytes") +import("core.base.hashset") +import("private.service.service") +import("private.service.config") + +function main(user) + assert(user, "empty user name!") + + -- get user password + cprint("Please input user ${bright}%s${clear} password:", user) + io.flush() + local pass = (io.read() or ""):trim() + assert(pass ~= "", "password is empty!") + + -- compute user authorization + local auth = base64.encode(user .. ":" .. pass) + auth = hash.sha256(bytes(auth)) + + -- save to configs + local configs = assert(config.configs(), "configs not found!") + configs.server = configs.server or {} + configs.server.auths = configs.server.auths or {} + local authset = hashset.from(configs.server.auths) + if authset:has(auth) then + authset:remove(auth) + configs.server.auths = authset:to_array() + else + cprint("User ${bright}%s${clear} not found!", user) + return + end + config.save(configs) + cprint("Remove user ${bright}%s${clear} ok!", user) +end + + diff --git a/xmake/modules/private/service/server.lua b/xmake/modules/private/service/server.lua index a2ec2580d..d926b9ccb 100644 --- a/xmake/modules/private/service/server.lua +++ b/xmake/modules/private/service/server.lua @@ -81,18 +81,12 @@ end -- set authorizations function server:auths_set(auths) - if auths then - local result = hashset.new() - for _, auth in ipairs(auths) do - result:insert(base64.encode(auth)) - end - self._AUTHS = result - end + self._AUTHS = auths and hashset.from(auths) or hashset.new() end -- we need verify user function server:need_verfiy() - return self:auths() ~= nil + return not self:auths():empty() end -- verify user |
