diff options
| author | ruki <[email protected]> | 2022-05-01 13:45:18 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-01 13:45:18 +0800 |
| commit | 91d3f55bf5400ecf07c094c5fa3f05e910c5f51c (patch) | |
| tree | 055ff77c9c974b4cd721fe0e32c764145ee1e0f4 | |
| parent | 2d3313ba1a2aa40350be90d52082f2e0f2cfedb4 (diff) | |
add token mode
| -rw-r--r-- | xmake/actions/service/main.lua | 3 | ||||
| -rw-r--r-- | xmake/actions/service/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/private/service/add_user.lua | 10 | ||||
| -rw-r--r-- | xmake/modules/private/service/config.lua | 23 | ||||
| -rw-r--r-- | xmake/modules/private/service/gen_token.lua | 52 | ||||
| -rw-r--r-- | xmake/modules/private/service/message.lua | 18 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/client.lua | 32 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/server.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/private/service/rm_user.lua | 14 | ||||
| -rw-r--r-- | xmake/modules/private/service/server.lua | 26 |
10 files changed, 123 insertions, 60 deletions
diff --git a/xmake/actions/service/main.lua b/xmake/actions/service/main.lua index 9080ff324..36fa3f368 100644 --- a/xmake/actions/service/main.lua +++ b/xmake/actions/service/main.lua @@ -32,6 +32,7 @@ import("private.service.sync_files") import("private.service.import_config") import("private.service.add_user") import("private.service.rm_user") +import("private.service.gen_token") import("private.service.show_logs") import("private.service.show_status") @@ -55,6 +56,8 @@ function main() sync_files() elseif option.get("config") then import_config(option.get("config")) + elseif option.get("gen-token") then + gen_token() elseif option.get("add-user") then add_user(option.get("add-user")) elseif option.get("rm-user") then diff --git a/xmake/actions/service/xmake.lua b/xmake/actions/service/xmake.lua index 74ffd313d..ad5689048 100644 --- a/xmake/actions/service/xmake.lua +++ b/xmake/actions/service/xmake.lua @@ -42,6 +42,9 @@ task("service") {nil, "rm-user", "kv", nil, "Remove user in the server.", "e.g.", " - xmake service --rm-user=root" }, + {nil, "gen-token", "k", nil, "Generate a new token in the server.", + "e.g.", + " - xmake service --gen-token" }, {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 index 8474de089..8e5512b5d 100644 --- a/xmake/modules/private/service/add_user.lua +++ b/xmake/modules/private/service/add_user.lua @@ -36,15 +36,15 @@ function main(user) assert(pass ~= "", "password is empty!") -- compute user authorization - local auth = base64.encode(user .. ":" .. pass) - auth = hash.md5(bytes(auth)) + local token = base64.encode(user .. ":" .. pass) + token = hash.md5(bytes(token)) -- 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) + configs.server.tokens = configs.server.tokens or {} + if not hashset.from(configs.server.tokens):has(token) then + table.insert(configs.server.tokens, token) else cprint("User ${bright}%s${clear} has been added!", user) return diff --git a/xmake/modules/private/service/config.lua b/xmake/modules/private/service/config.lua index 10cb472e7..e2b19bdef 100644 --- a/xmake/modules/private/service/config.lua +++ b/xmake/modules/private/service/config.lua @@ -23,24 +23,27 @@ import("core.base.global") import("core.base.base64") import("core.base.bytes") +-- generate a token +function _generate_token() + return hash.md5(bytes(base64.encode(hash.uuid()))) +end + -- generate a default config file function _generate_configfile() local filepath = configfile() assert(not _g.configs and not os.isfile(filepath)) local servicedir = path.join(global.directory(), "service") - local initauth = base64.encode("root:000000") - initauth = hash.md5(bytes(initauth)) - print("generating the default config file to %s ..", filepath) - print("the default user(root) and password(000000) are generated!") + local token = _generate_token() + print("generating the config file to %s ..", filepath) + cprint("an token(${bright yellow}%s${clear}) is generated, we can use this token to connect service.", token) local configs = { logfile = path.join(servicedir, "logs.txt"), server = { known_hosts = { -- "127.0.0.1" }, - auths = { - -- initialized user and password, root:000000 - initauth + tokens = { + token } }, remote_build = { @@ -50,8 +53,10 @@ function _generate_configfile() }, client = { -- without authorization: "127.0.0.1:9691" - -- with authorization: "[email protected]:9691" - connect = "[email protected]:9691" + -- with user authorization: "[email protected]:9691" + connect = "127.0.0.1:9691", + -- with token authorization + token = token } } } diff --git a/xmake/modules/private/service/gen_token.lua b/xmake/modules/private/service/gen_token.lua new file mode 100644 index 000000000..6da8ea33f --- /dev/null +++ b/xmake/modules/private/service/gen_token.lua @@ -0,0 +1,52 @@ +--!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 gen_token.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") + +-- generate a token +function _generate_token() + return hash.md5(bytes(base64.encode(hash.uuid()))) +end + +function main() + + -- generate token + local token = _generate_token() + + -- save to configs + local configs = assert(config.configs(), "configs not found!") + configs.server = configs.server or {} + configs.server.tokens = configs.server.tokens or {} + if not hashset.from(configs.server.tokens):has(token) then + table.insert(configs.server.tokens, token) + else + cprint("Token ${yellow bright}%s${clear} has been added!", token) + return + end + config.save(configs) + cprint("New token ${yellow bright}%s${clear} is generated!", token) +end + diff --git a/xmake/modules/private/service/message.lua b/xmake/modules/private/service/message.lua index cbd7e54f7..ee190cc2f 100644 --- a/xmake/modules/private/service/message.lua +++ b/xmake/modules/private/service/message.lua @@ -90,8 +90,8 @@ function message:is_data() end -- get user authorization -function message:auth() - return self:body().auth +function message:token() + return self:body().token end -- is success? @@ -143,7 +143,7 @@ function new_connect(session_id, opt) return _new({ code = message.CODE_CONNECT, session_id = session_id, - auth = opt.auth, + token = opt.token, xmakever = xmake.version():shortstr() }) end @@ -154,7 +154,7 @@ function new_disconnect(session_id, opt) return _new({ code = message.CODE_DISCONNECT, session_id = session_id, - auth = opt.auth + token = opt.token }) end @@ -164,7 +164,7 @@ function new_diff(session_id, manifest, opt) return _new({ code = message.CODE_DIFF, session_id = session_id, - auth = opt.auth, + token = opt.token, manifest = manifest }) end @@ -175,7 +175,7 @@ function new_sync(session_id, manifest, opt) return _new({ code = message.CODE_SYNC, session_id = session_id, - auth = opt.auth, + token = opt.token, manifest = manifest }) end @@ -186,7 +186,7 @@ function new_clean(session_id, opt) return _new({ code = message.CODE_CLEAN, session_id = session_id, - auth = opt.auth + token = opt.token }) end @@ -196,7 +196,7 @@ function new_runcmd(session_id, program, argv, opt) return _new({ code = message.CODE_RUNCMD, session_id = session_id, - auth = opt.auth, + token = opt.token, program = program, argv = argv }) @@ -209,7 +209,7 @@ function new_data(session_id, size, opt) code = message.CODE_DATA, size = size, session_id = session_id, - auth = opt.auth + token = opt.token }) end diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index a53e265df..f5672d2a4 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -79,8 +79,8 @@ function remote_build_client:connect() end -- we need user authorization? - local auth - if self:user() then + local token = config.get("remote_build.client.token") + if not token and self:user() then -- get user password cprint("Please input user ${bright}%s${clear} password:", self:user()) @@ -89,8 +89,8 @@ function remote_build_client:connect() assert(pass ~= "", "password is empty!") -- compute user authorization - auth = base64.encode(self:user() .. ":" .. pass) - auth = hash.md5(bytes(auth)) + token = base64.encode(self:user() .. ":" .. pass) + token = hash.md5(bytes(token)) end -- do connect @@ -103,7 +103,7 @@ function remote_build_client:connect() print("%s: connect %s:%d ..", self, addr, port) if sock then local stream = socket_stream(sock) - if stream:send_msg(message.new_connect(session_id, {auth = auth})) and stream:flush() then + if stream:send_msg(message.new_connect(session_id, {token = token})) and stream:flush() then local msg = stream:recv_msg() if msg then vprint(msg:body()) @@ -125,7 +125,7 @@ function remote_build_client:connect() local status = self:status() status.addr = addr status.port = port - status.auth = auth + status.token = token status.connected = ok status.session_id = session_id self:status_save() @@ -151,7 +151,7 @@ function remote_build_client:disconnect() print("%s: disconnect %s:%d ..", self, addr, port) if sock then local stream = socket_stream(sock) - if stream:send_msg(message.new_disconnect(session_id, {auth = self:auth()})) and stream:flush() then + if stream:send_msg(message.new_disconnect(session_id, {token = self:token()})) and stream:flush() then local msg = stream:recv_msg() if msg then vprint(msg:body()) @@ -175,7 +175,7 @@ function remote_build_client:disconnect() -- update status local status = self:status() - status.auth = nil + status.token = nil status.connected = not ok self:status_save() end @@ -215,7 +215,7 @@ function remote_build_client:sync() -- do sync cprint("Uploading files with ${bright}%d${clear} bytes ..", os.filesize(archive_diff_file)) local send_ok = false - if stream:send_msg(message.new_sync(session_id, diff_files, {auth = self:auth()})) and stream:flush() then + if stream:send_msg(message.new_sync(session_id, diff_files, {token = self:token()})) and stream:flush() then if stream:send_file(archive_diff_file) and stream:flush() then send_ok = true end @@ -256,7 +256,7 @@ function remote_build_client:clean() local ok = false print("%s: clean files in %s:%d ..", self, addr, port) local stream = socket_stream(sock) - if stream:send_msg(message.new_clean(session_id, {auth = self:auth()})) and stream:flush() then + if stream:send_msg(message.new_clean(session_id, {token = self:token()})) and stream:flush() then local msg = stream:recv_msg() if msg then vprint(msg:body()) @@ -288,7 +288,7 @@ function remote_build_client:runcmd(program, argv) local leftstr = "" cprint("%s: run ${bright}%s${clear} in %s:%d ..", self, command, addr, port) local stream = socket_stream(sock) - if stream:send_msg(message.new_runcmd(session_id, program, argv, {auth = self:auth()})) and stream:flush() then + if stream:send_msg(message.new_runcmd(session_id, program, argv, {token = self:token()})) and stream:flush() then local stdin_opt = {stop = false} scheduler.co_start(self._read_stdin, self, stream, stdin_opt) while true do @@ -371,9 +371,9 @@ function remote_build_client:workdir() return self._WORKDIR end --- get user auth -function remote_build_client:auth() - return self:status().auth +-- get user token +function remote_build_client:token() + return self:status().token end -- get the session id, only for unique project @@ -396,7 +396,7 @@ function remote_build_client:_diff_files(stream) local count = 0 local result, errors cprint("Comparing ${bright}%d${clear} files ..", filecount) - if stream:send_msg(message.new_diff(session_id, manifest, {auth = self:auth()})) and stream:flush() then + if stream:send_msg(message.new_diff(session_id, manifest, {token = self:token()})) and stream:flush() then local msg = stream:recv_msg() if msg and msg:success() then result = msg:body().manifest @@ -460,7 +460,7 @@ function remote_build_client:_read_stdin(stream, opt) if line and #line > 0 then local ok = false local data = bytes(line) - if stream:send_msg(message.new_data(0, data:size(), {auth = self:auth()})) then + if stream:send_msg(message.new_data(0, data:size(), {token = self:token()})) then if stream:send(data) and stream:flush() then ok = true end diff --git a/xmake/modules/private/service/remote_build/server.lua b/xmake/modules/private/service/remote_build/server.lua index 3b29c5fee..965121ed9 100644 --- a/xmake/modules/private/service/remote_build/server.lua +++ b/xmake/modules/private/service/remote_build/server.lua @@ -66,7 +66,7 @@ function remote_build_server:_on_handle(stream, msg) { function() if self:need_verfiy() then - local ok, errors = self:verify_user(msg:auth(), stream:sock():peeraddr()) + local ok, errors = self:verify_user(msg:token(), stream:sock():peeraddr()) if not ok then session_errs = errors return false diff --git a/xmake/modules/private/service/rm_user.lua b/xmake/modules/private/service/rm_user.lua index 4dfe86a22..c1ed7a7fb 100644 --- a/xmake/modules/private/service/rm_user.lua +++ b/xmake/modules/private/service/rm_user.lua @@ -36,17 +36,17 @@ function main(user) assert(pass ~= "", "password is empty!") -- compute user authorization - local auth = base64.encode(user .. ":" .. pass) - auth = hash.md5(bytes(auth)) + local token = base64.encode(user .. ":" .. pass) + token = hash.md5(bytes(token)) -- 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() + configs.server.tokens = configs.server.tokens or {} + local tokenset = hashset.from(configs.server.tokens) + if tokenset:has(token) then + tokenset:remove(token) + configs.server.tokens = tokenset:to_array() else cprint("User ${bright}%s${clear} not found!", user) return diff --git a/xmake/modules/private/service/server.lua b/xmake/modules/private/service/server.lua index d3cb0055a..3365effc2 100644 --- a/xmake/modules/private/service/server.lua +++ b/xmake/modules/private/service/server.lua @@ -37,8 +37,8 @@ function server:init(daemon) self._DAEMON = daemon -- init authorizations - local auths = config.get("server.auths") - self:auths_set(auths) + local tokens = config.get("server.tokens") + self:tokens_set(tokens) -- init known hosts local known_hosts = config.get("server.known_hosts") @@ -78,14 +78,14 @@ function server:port() return self._PORT end --- get authorizations -function server:auths() - return self._AUTHS +-- get tokens +function server:tokens() + return self._TOKENS end --- set authorizations -function server:auths_set(auths) - self._AUTHS = auths and hashset.from(auths) or hashset.new() +-- set tokens +function server:tokens_set(tokens) + self._TOKENS = tokens and hashset.from(tokens) or hashset.new() end -- get known hosts @@ -100,17 +100,17 @@ end -- we need verify user function server:need_verfiy() - return not self:auths():empty() + return not self:tokens():empty() end -- verify user -function server:verify_user(auth, peeraddr) - if not auth then - return false, "client has no authorization, we need add user name to `remote_build.client.connect`!" +function server:verify_user(token, peeraddr) + if not token then + return false, "client has no authorization, we need add username to connect address or token to `remote_build.client.token`!" end -- check authorization - if not self:auths():has(auth) then + if not self:tokens():has(token) then return false, "user and password are incorrect!" end |
