Skip to content

Commit

Permalink
yakit 新增了保存远程登录页面,端口扫描界面得到了优化
Browse files Browse the repository at this point in the history
  • Loading branch information
v1ll4n committed Oct 26, 2021
1 parent ee76add commit 1d1372c
Show file tree
Hide file tree
Showing 18 changed files with 551 additions and 171 deletions.
16 changes: 16 additions & 0 deletions app/main/handlers/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,20 @@ module.exports = (win, getClient) => {
ipcMain.handle("QueryPorts", async (e, params) => {
return await asyncQueryPorts(params)
})

// asyncDeletePorts wrapper
const asyncDeletePorts = (params) => {
return new Promise((resolve, reject) => {
getClient().DeletePorts(params, (err, data) => {
if (err) {
reject(err)
return
}
resolve(data)
})
})
}
ipcMain.handle("DeletePorts", async (e, params) => {
return await asyncDeletePorts(params)
})
};
130 changes: 72 additions & 58 deletions app/main/handlers/yakLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const childProcess = require("child_process");
const process = require("process");
const psList = require("ps-list");
const treeKill = require("tree-kill");
const sudoPrompt = require("sudo-prompt");

function getRandomInt(max) {
return Math.floor(Math.random() * max);
Expand All @@ -12,17 +13,9 @@ function notification(msg) {
new Notification({title: msg}).show()
}

let yakProcess;
module.exports = {
clearing: () => {
if (!yakProcess) {
return
}
try {
yakProcess.kill()
} catch (e) {
console.info("KILL yakProcessError: " + `${e}`)
}

},
register: (win, getClient) => {
// asyncPsList wrapper
Expand Down Expand Up @@ -50,64 +43,85 @@ module.exports = {
});
return ls;
});
// asyncPsList wrapper
ipcMain.handle("kill-yak-grpc", async (e, pid) => {
return treeKill(pid, "SIGKILL")
});

ipcMain.handle("kill-local-yak-grpc-server", async (e) => {
console.info("start to kill / clean yak local grpc process")
if (yakProcess) {
try {
yakProcess.kill()
} catch (e) {
console.info("KILL yakProcessError: " + `${e}`)
// asyncKillYakGRPC wrapper
const asyncKillYakGRPC = (pid) => {
return new Promise((resolve, reject) => {
if (process.platform === 'win32') {
childProcess.exec(`taskkill /F /PID ${pid}`, error => {
if (!error) {
resolve(true)
} else {
sudoPrompt.exec(`taskkill /F /PID ${pid}`, {
"name": `taskkill F PID ${pid}`,
}, err => {
if (!error) {
resolve(true)
} else {
reject(`${err}`)
}
})
}
})
} else {
childProcess.exec(`kill -9 ${pid}`, error => {
if (!error) {
resolve(true)
} else {
sudoPrompt.exec(`kill -9 ${pid}`, {
name: `kill SIGKILL PID ${pid}`
}, err => {
console.info(err)
if (!error) {
resolve(true)
} else {
reject(`${err}`)
}
})
}
})
}
})
}
ipcMain.handle("kill-yak-grpc", async (e, pid) => {
try {
return await asyncKillYakGRPC(pid)
} catch (e) {
return ""
}
yakProcess = null;
});

let randPort;
ipcMain.handle("start-local-yak-grpc-server", async (e, sudo) => {
if (yakProcess) {
console.info("u have started local yak grpc...")
return randPort;
}

if ((!`${process.env.PATH}`.includes("/usr/local/bin")) && (!`${process.env.PATH}`.includes("/usr/local/bin/"))) {
process.env.PATH += ":/usr/local/bin"
}
})

randPort = 50000 + getRandomInt(10000);
const cmd = `yak grpc --port ${randPort}`;
// asyncStartLocalYakGRPCServer wrapper
const asyncStartLocalYakGRPCServer = (params) => {
return new Promise((resolve, reject) => {
const {sudo} = params;

let buffer = ""
const child = childProcess.exec(cmd, (err, stdout, stderr) => {
buffer = buffer + stdout + stderr
})
yakProcess = child;
child.on("error", (err) => {
notification(`Yak local gRPC start error: ${err}`)
let randPort = 50000 + getRandomInt(10000);
const cmd = `yak grpc --port ${randPort}`;
try {
if (win) win.webContents.send("client-yak-local-grpc-error", `${err}`)
} catch (e) {
}
})
child.on("data", data => {
console.info(data)
})
child.on("close", async (code, sig) => {
const msg = `Yak local gRPC 本地进程已退出:CODE: ${code} SIG: ${sig} OUTPUT: ${buffer}`;
notification(msg)
randPort = null;
try {
if (win) win.webContents.send("client-yak-local-grpc-close", msg)
if (win) win.webContents.send("client-start-local-grpc-failed")
if (sudo) {
sudoPrompt.exec(cmd, {
name: `yak grpc port ${randPort}`,
},
function (error, stdout, stderr) {
}
)
} else {
childProcess.exec(cmd, err => {
})
}
} catch (e) {

return -1
}
return randPort
})
return randPort;
}
ipcMain.handle("start-local-yak-grpc-server", async (e, params) => {
try {
return await asyncStartLocalYakGRPCServer(params)
} catch (e) {
return -1
}
})
},
}
9 changes: 0 additions & 9 deletions app/main/handlers/yakUpdate.js

This file was deleted.

94 changes: 94 additions & 0 deletions app/main/handlers/yakUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const {ipcMain, Notification} = require("electron");
const childProcess = require("child_process");
const process = require("process");
const path = require("path");
const os = require("os");
const fs = require("fs");
const psList = require("ps-list");
const treeKill = require("tree-kill");

const homeDir = path.join(os.homedir(), "yakit-projects");
const secretDir = path.join(homeDir, "auth");
fs.mkdirSync(secretDir, {recursive: true})
const secretFile = path.join(secretDir, "yakit-remote.json");
const authMeta = [];

const loadSecrets = () => {
authMeta.splice(0, authMeta.length)
try {
const data = fs.readFileSync(path.join(secretDir, "yakit-remote.json"));
JSON.parse(data).forEach(i => {
if (!(i["host"] && i["port"])) {
return
}

authMeta.push({
name: i["name"] || `${i["host"]}:${i["port"]}`,
host: i["host"],
port: i["port"],
tls: i["tls"] | false,
password: i["password"] || "",
caPem: i["caPem"] || "",
})
})
} catch (e) {
console.info(e)
}
};

function saveSecret(name, host, port, tls, password, caPem) {
if (!host || !port) {
throw new Error("empty host or port")
}

authMeta.push({
host, port, tls, password, caPem,
name: name || `${host}:${port}`,
})
saveAllSecret([...authMeta])
};

const saveAllSecret = (authInfos) => {
try {
fs.unlinkSync(secretFile)
} catch (e) {

}


const authFileStr = JSON.stringify(
[...authInfos.filter((v, i, arr) => {
return arr.findIndex(origin => origin.name === v.name) === i
})]
);
fs.writeFileSync(secretFile, new Buffer(authFileStr, "utf8"))
};

loadSecrets()

module.exports = {
register: (win, getClient) => {
ipcMain.handle("save-yakit-remote-auth", async (e, params) => {
let {name, host, port, tls, caPem, password} = params;
name = name || `${host}:${port}`
saveAllSecret([...authMeta.filter(i => {
return i.name !== name
})]);
loadSecrets()
saveSecret(name, host, port, tls, caPem, password)
})
ipcMain.handle("remove-yakit-remote-auth", async (e, name) => {
saveAllSecret([...authMeta.filter(i => {
return i.name !== name
})]);
loadSecrets();
})
ipcMain.handle("get-yakit-remote-auth-all", async (e, name) => {
loadSecrets()
return authMeta;
})
ipcMain.handle("get-yakit-remote-auth-dir", async (e, name) => {
return secretDir;
})
},
}
2 changes: 1 addition & 1 deletion app/main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ module.exports = {
require("./handlers/menu")(win, getClient);

// 管理 yak 引擎版本 / 升级等
require("./handlers/yakUpdate")(win, getClient);
require("./handlers/yakUtil").register(win, getClient);
}
}
4 changes: 4 additions & 0 deletions app/protos/grpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,16 @@ message PortScanRequest {

// 保存已经关闭的端口
bool SaveClosedPorts = 9;

// 上传扫描目标为文件
string TargetsFile = 10;
}

message DeletePortsRequest {
string Hosts = 1;
string Ports = 2;
repeated int64 Id = 3;
bool All = 4;
}

message QueryPortsRequest {
Expand Down
5 changes: 3 additions & 2 deletions app/renderer/src/main/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function App() {
return () => {
ipcRenderer.removeAllListeners("client-yak-local-grpc-error");
ipcRenderer.removeAllListeners("client-yak-local-grpc-close");
ipcRenderer.invoke("kill-local-yak-grpc-server")
}
}, [mode])

Expand Down Expand Up @@ -136,7 +135,9 @@ function App() {
onErrorConfirmed={() => {
setConnected(false)
}}
/> : <Spin spinning={loading} tip={"Yakit 正在检测 Yak gRPC 核心引擎环境..."}>
/> : <Spin spinning={loading} tip={"Yakit 正在检测 Yak gRPC 核心引擎环境..."} style={{
marginBottom: 100,
}}>
<YakEnvironment
setMode={setMode}
onConnected={() => {
Expand Down
11 changes: 6 additions & 5 deletions app/renderer/src/main/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// <React.StrictMode>
<App/>,
// </React.StrictMode>,
document.getElementById('root')
)
;

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
Expand Down
Loading

0 comments on commit 1d1372c

Please sign in to comment.