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
3 changes: 3 additions & 0 deletions aries/src/service/relay_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ async fn check_nodes_online(context: Context) {
let nodelist: Vec<ztm_node::Model> =
storage.get_all_node().await.unwrap().into_iter().collect();
for mut node in nodelist {
if !node.online {
continue;
}
//check online
let from_timestamp = Duration::from_millis(node.last_online_time as u64);
let now = SystemTime::now();
Expand Down
9 changes: 8 additions & 1 deletion jupiter/src/storage/ztm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ impl ZTMStorage {
}

pub async fn update_node(&self, node: ztm_node::Model) -> Result<ztm_node::Model, MegaError> {
Ok(ztm_node::Entity::update(node.into_active_model())
let mut active_model: ztm_node::ActiveModel = node.clone().into_active_model();
active_model.hub = Set(node.hub);
active_model.agent_name = Set(node.agent_name);
active_model.service_name = Set(node.service_name);
active_model.r#type = Set(node.r#type);
active_model.online = Set(node.online);
active_model.last_online_time = Set(node.last_online_time);
Ok(ztm_node::Entity::update(active_model)
.exec(self.get_connection())
.await
.unwrap())
Expand Down
98 changes: 13 additions & 85 deletions neptune/mega/tunnel/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function ({ app, mesh, punch }) {
export default function ({ app, mesh }) {
var currentListens = []
var currentTargets = {}

Expand Down Expand Up @@ -39,12 +39,9 @@ export default function ({ app, mesh, punch }) {
function getInbound(ep, protocol, name) {
if (ep === app.endpoint.id) {
return getLocalConfig().then(
config => {
var inbound = config.inbound.find(
i => i.protocol === protocol && i.name === name
) || null
var hole = punch.findHole(ep)
}
config => config.inbound.find(
i => i.protocol === protocol && i.name === name
) || null
)
} else {
return requestPeer(ep, new Message(
Expand Down Expand Up @@ -173,33 +170,6 @@ export default function ({ app, mesh, punch }) {
}
}

function createHole(ep, role) {
var h = punch.findHole(ep)
if(h) return h

if(role === 'server') {
return punch.createOutboundHole(ep)
} else if(role === 'client') {
return punch.createInboundHole(ep)
}
}

function updateHoleInfo(ep, ip, port, cert) {
checkIP(ip)
checkPort(Number.parseInt(port))
punch.updateHoleInfo(ep, ip, port, cert)
}

function syncPunch(ep) {
var hole = punch.findHole(ep)
if(!hole) throw `Invalid Hole State for ${ep}`
hole.punch()
}

function deleteHole(ep, remote) {
punch.deleteHole(ep, remote)
}

function getLocalConfig() {
return mesh.read('/local/config.json').then(
data => data ? JSON.decode(data) : { inbound: [], outbound: [] }
Expand Down Expand Up @@ -227,34 +197,23 @@ export default function ({ app, mesh, punch }) {
currentListens = []
currentTargets = {}

console.info(`Applying config: ${JSON.encode(config)}`)

config.inbound.forEach(i => {
var protocol = i.protocol
var name = i.name
var listens = i.listens
var $selectedEP

var $selectedEP
var connectPeer = pipeline($=>$
.connectHTTPTunnel(
new Message({
method: 'CONNECT',
path: `/api/outbound/${protocol}/${name}`,
})
).to($=>$.pipe(() => {
punch.createInboundHole($selectedEP)
var hole = punch.findHole($selectedEP)
if(hole && hole.ready()) {
console.info("Using direct session")
return hole.directSession()
}
console.info("Using hub forwarded session")
return pipeline($=>$
.muxHTTP().to($=>$
.pipe(mesh.connect($selectedEP))
)
).to($=>$
.muxHTTP().to($=>$
.pipe(() => mesh.connect($selectedEP))
)
}))
)
.onEnd(() => app.log(`Disconnected from ep ${$selectedEP} for ${protocol}/${name}`))
)

Expand Down Expand Up @@ -326,24 +285,14 @@ export default function ({ app, mesh, punch }) {
var $response
return pipeline($=>$
.onStart(req)
.pipe(() => {
var h = punch.findHole(ep)
if(h && h.ready()) {
return h.directSession()
}
return pipeline($=>$
.muxHTTP().to($=>$
.pipe(mesh.connect(ep))
))
})
.muxHTTP().to($=>$
.pipe(mesh.connect(ep))
)
.replaceMessage(res => {
$response = res
return new StreamEnd
})
.onEnd(() => {
console.info('Answers in api: ', $response)
return $response
})
.onEnd(() => $response)
).spawn()
}

Expand Down Expand Up @@ -394,22 +343,6 @@ export default function ({ app, mesh, punch }) {
)
)

var tunnelHole = null
var makeRespTunnel = pipeline($=>$
.onStart(ctx => {
console.info("Making resp tunnel: ", ctx)
var ep = ctx.peer.id
tunnelHole = punch.findHole(ep)
if(!tunnelHole) throw `Invalid Hole State for ${ep}`
return new Data
})
.pipe(() => {
var p = tunnelHole.makeRespTunnel()
tunnelHole = null
return p
}, () => tunnelHole)
)

getLocalConfig().then(applyLocalConfig)

return {
Expand All @@ -422,11 +355,6 @@ export default function ({ app, mesh, punch }) {
setOutbound,
deleteInbound,
deleteOutbound,
createHole,
updateHoleInfo,
makeRespTunnel,
syncPunch,
deleteHole,
servePeerInbound,
}
}
Expand Down
73 changes: 1 addition & 72 deletions neptune/mega/tunnel/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import initHole from './punch.js'
import initAPI from './api.js'
import initCLI from './cli.js'

export default function ({ app, mesh, utils }) {
var punch = initHole({ app, mesh })
var api = initAPI({ app, mesh, punch })
var api = initAPI({ app, mesh })
var cli = initCLI({ app, mesh, utils, api })

var $ctx
Expand Down Expand Up @@ -84,17 +82,6 @@ export default function ({ app, mesh, utils }) {
}),
},

// '/api/punch/{destEp}': {
// 'GET': responder(({destEp}) => {
// return api.createHole(destEp)
// }),

// 'DELETE': responder(({destEp}) => {
// api.deleteHole(destEp)
// return response(204)
// })
// },

'*': {
'GET': responder((_, req) => {
return Promise.resolve(gui.serve(req) || response(404))
Expand Down Expand Up @@ -150,64 +137,6 @@ export default function ({ app, mesh, utils }) {

'CONNECT': api.servePeerInbound,
},

'/api/ping': {
'GET': responder(() => Promise.resolve(response(200)))
},

'/api/punch/{action}': {
'GET': responder(({action}) => {
var ep = $ctx.peer.id
var ip = $ctx.peer.ip
var port = $ctx.peer.port

console.log(`Punch Event: ${action} from ${ep} ${ip} ${port}`)
switch(action) {
case 'leave':
api.deleteHole(ep, true)
break
default:
return Promise.resolve(response(500, "Unknown punch action"))
}
return Promise.resolve(response(200))
}),

'POST': responder(({action}, req) => {
var obj = JSON.decode(req.body)
var ep = $ctx.peer.id
var ip = $ctx.peer.ip
var port = $ctx.peer.port

console.log(`Punch Event: ${action} from ${ep} ${ip} ${port}`)
console.log("Punch req: ", obj)
switch(action) {
case 'request':
api.createHole(ep, 'server')
api.updateHoleInfo(ep, ip, port, obj.cert)
api.syncPunch(ep)
// var certs = hole.signPeerCert(new crypto.PublicKey(obj.pkey))
// return Promise.resolve(response(200, cert))
break
case 'accept':
api.updateHoleInfo(ep, ip, port, obj.cert)
api.syncPunch(ep)
break
default:
return Promise.resolve(response(500, "Unknown punch action"))
}
return Promise.resolve(response(200))
}),

'CONNECT': pipeline($=>$.pipe(api.makeRespTunnel, () => $ctx))
},
})

punch.setService((ctx) => {
// Tricky callback to set ctx,
// expecting everything in hole works
// just like it's coming from hub.
$ctx = ctx
return servePeer
})

return pipeline($=>$
Expand Down
Loading