Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion aries/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod service;

#[tokio::main]
async fn main() {

ctrlc::set_handler(move || {
tracing::info!("Received Ctrl-C signal, exiting...");
std::process::exit(0);
Expand Down
14 changes: 8 additions & 6 deletions gemini/src/ztm/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Agent {
pub struct ZTMEndPoint {
pub id: String,
pub username: String,
pub name: String,
pub online: bool,
#[serde(rename = "isLocal")]
pub is_local: bool,
Expand Down Expand Up @@ -216,7 +217,7 @@ impl ZTMAgent for LocalZTMAgent {
match self.get_ztm_endpoints().await {
Ok(ep_list) => {
for ele in ep_list {
if ele.online && ele.username == peer_id {
if ele.online && ele.name == peer_id {
return Ok(ele);
}
}
Expand Down Expand Up @@ -434,24 +435,25 @@ pub async fn run_ztm_client(
agent: LocalZTMAgent,
http_port: u16,
) {
let name = peer_id.clone();
let url = format!("{bootstrap_node}/api/v1/certificate?name={name}");
//TODO ztm permit control?
// let name = peer_id.clone();
let url = format!("{bootstrap_node}/api/v1/certificate?name=root");
let request_result = reqwest::get(url).await;
let response_text = match handle_response(request_result).await {
Ok(s) => s,
Err(s) => {
tracing::error!("GET {bootstrap_node}/api/v1/certificate?name={name} failed,{s}");
tracing::error!("GET {bootstrap_node}/api/v1/certificate?name=root failed,{s}");
return;
}
};
let permit: ZTMUserPermit = match serde_json::from_slice(response_text.as_bytes()) {
let mut permit: ZTMUserPermit = match serde_json::from_slice(response_text.as_bytes()) {
Ok(p) => p,
Err(e) => {
tracing::error!("{}", e);
return;
}
};

permit.agent.name = peer_id.clone();
// 2. join ztm mesh
let mesh = match agent.connect_ztm_hub(permit.clone()).await {
Ok(m) => m,
Expand Down
28 changes: 16 additions & 12 deletions neptune/hub/cmdline.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ export default function (argv, { commands, notes, help, fallback }) {
var opts = parseOptions(cmd.options, rest)

} catch (err) {
var command = pattern.slice(0, best).join(' ')
throw `${err}. Type '${program} help ${command}' for help info.`
var command = ['help', ...pattern.slice(0, best)].join(' ')
throw `${err}. Type '${program} ${command}' for help info.`
}

return cmd.action({ ...args, ...opts })
}

function matchCommand(pattern, argv) {
var i = argv.findIndex(
(arg, i) => arg.startsWith('-') || arg !== pattern[i]
(arg, i) => (arg.length > 1 && arg.startsWith('-')) || arg !== pattern[i]
)
return i < 0 ? argv.length : i
}
Expand All @@ -101,7 +101,7 @@ function parseCommand(pattern, argv, rest) {
var values = {}
var i = argv.findIndex((arg, i) => {
var tok = pattern[i]
if (arg.startsWith('-')) return true
if (arg.length > 1 && arg.startsWith('-')) return true
if (!tok) throw `Excessive positional argument: ${arg}`
if (tok.startsWith('[') || tok.startsWith('<')) {
values[tok] = arg
Expand Down Expand Up @@ -131,7 +131,8 @@ function parseOptions(format, argv) {
if (t.endsWith(',')) t = t.substring(0, t.length - 1)
aliases.push(t)
} else {
if (t.endsWith('...]') || t.endsWith('...>')) type = 'array'
if (t === '...') type = 'remainder array'
else if (t.endsWith('...]') || t.endsWith('...>')) type = 'array'
else if (t.startsWith('[')) type = 'optional string'
else if (t.startsWith('<')) type = 'string'
else type = 'boolean'
Expand All @@ -147,7 +148,11 @@ function parseOptions(format, argv) {

argv.forEach(arg => {
if (currentOption) {
if (arg.startsWith('-')) {
if (options[currentOption]?.type === 'remainder array') {
addOption(currentOption, arg)
return
}
if (arg.length > 1 && arg.startsWith('-')) {
endOption(currentOption)
currentOption = undefined
} else {
Expand All @@ -157,7 +162,7 @@ function parseOptions(format, argv) {
}
if (arg.startsWith('--')) {
currentOption = arg
} else if (arg.startsWith('-')) {
} else if (arg.length > 1 && arg.startsWith('-')) {
if (arg.length === 2) {
currentOption = arg
} else {
Expand All @@ -182,6 +187,7 @@ function parseOptions(format, argv) {
option.value = value
break
case 'array':
case 'remainder array':
option.value ??= []
option.value.push(value)
break
Expand Down Expand Up @@ -220,25 +226,23 @@ function parseOptions(format, argv) {
function tokenize(str) {
var tokens = str.split(' ').reduce(
(a, b) => {
if (typeof a === 'string') a = [a]
var last = a.pop()
if (last.startsWith('<')) {
a.push(`${last} ${b}`)
} else if (last.startsWith('[')) {
if (last && (last.startsWith('<') || last.startsWith('['))) {
a.push(`${last} ${b}`)
} else {
a.push(last, b)
}
if (b.endsWith('>') || b.endsWith(']')) a.push('')
return a
}
}, []
)
return tokens instanceof Array ? tokens.filter(t => t) : [tokens]
}

function stripIndentation(s, indent) {
var lines = s.split('\n')
if (lines[0].trim() === '') lines.shift()
if (lines[lines.length - 1].trim() === '') lines.pop()
var depth = lines[0].length - lines[0].trimStart().length
var padding = ' '.repeat(indent || 0)
return lines.map(l => padding + l.substring(depth)).join('\n')
Expand Down
62 changes: 62 additions & 0 deletions neptune/hub/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ function open(pathname) {
data TEXT NOT NULL
)
`)

db.exec(`
CREATE TABLE IF NOT EXISTS files (
path TEXT PRIMARY KEY,
hash TEXT NOT NULL,
size INTEGER NOT NULL,
time REAL NOT NULL,
since REAL NOT NULL
)
`)
}

function getCert(name) {
Expand Down Expand Up @@ -70,6 +80,55 @@ function delKey(name) {
.exec()
}

function recordToFile(rec) {
return {
pathname: rec.path,
hash: rec.hash,
size: +rec.size,
time: rec.time,
since: rec.since,
}
}

function allFiles() {
return (
db.sql('SELECT * FROM files')
.exec()
.map(recordToFile)
)
}

function getFile(pathname) {
return (
db.sql('SELECT * FROM files WHERE path = ?')
.bind(1, pathname)
.exec()
.map(recordToFile)[0]
)
}

function setFile(pathname, file) {
var obj = getFile(pathname)
if (obj) {
Object.assign(obj, file)
db.sql('UPDATE files SET hash = ?, size = ?, time = ?, since = ? WHERE path = ?')
.bind(1, obj.hash)
.bind(2, obj.size)
.bind(3, obj.time)
.bind(4, obj.since)
.bind(5, pathname)
.exec()
} else {
db.sql('INSERT INTO files(path, hash, size, time, since) VALUES(?, ?, ?, ?, ?)')
.bind(1, pathname)
.bind(2, file.hash)
.bind(3, file.size)
.bind(4, file.time)
.bind(5, file.since)
.exec()
}
}

export default {
open,
getCert,
Expand All @@ -78,4 +137,7 @@ export default {
getKey,
setKey,
delKey,
allFiles,
getFile,
setFile,
}
Loading