正文

云vscode搭建之使用容器化部署的方法(vscode roslaunch)居然可以这样

文章摘要

这篇文章介绍了一个使用Go语言实现的端口扫描工具。该工具通过注册Go语言命令在启动时启动端口监听功能,并每隔一秒检查是否有新的端口被启用。当发现新端口时,会在浏览器中打开提示对话框,询问用户是否在浏览器中打开该端口,以便代理服务器能够访问容器的指定端口。文章的核心内容是该工具的启动、端口扫描和用户交互功能。

FROM golang WORKDIR /workspace RUN cp /usr/local/go/bin function activate(context) { // Use the console to output diagnostic information (console.log) and errors (console.error) // This line of code will only be executed once when your extension is activated // The command has been defined in the package.json file // Now provide the implementation of the command with registerCommand // The commandId parameter must match the command field in package.json let disposable=vscode.commands.registerCommand(‘port-finder.helloWorld’, function () { // The code you place here will be executed every time your command is executed // Display a message box to the user vscode.window.showInformationMessage(‘Hello World from port_finder!’); }); context.subscriptions.push(disposable); initGetPorts() } var s=new Set() function initGetPorts() { getListeningPorts(function(ports) { ports.forEach(p=> { s.add(p) }) setInterval(function() { // 设置定时器,每隔一秒获取一次 listenPortChange() }, 1000) }) } function listenPortChange() { // 获取处于LISTEN状态的端口 getListeningPorts(function(ports) { var tmpSet=new Set() ports.forEach(p=> { if (!s.has(p)) { // 发现新的端口被监听就提醒用户是否在浏览器中打开 vscode.window.showInformationMessage(“发现新开启的端口:” + p + “,是否在浏览器中访问?”, “是”, “否”, “不再提示”) .then(result=> { if (result===”是”) { // 在浏览器中打开来访问代理服务器,后面带上端口信息,以便代理服务器知道访问容器的哪个端口 vscode.env.openExternal(vscode.Uri.parse(`http://192.168.44.100/proxy/` + p)) } }) } tmpSet.add(p) }) s=tmpSet }) } function getListeningPorts(callback) { var exec=require(‘child_process’).exec; exec(‘netstat -nlt’, function(error, stdout, stderr){ if(error) { console.error(‘error: ‘ + error); return; } var ports=parsePort(stdout) callback(ports) }) } function parsePort(msg) { var idx=msg.indexOf(“tcp”) msg=msg.slice(idx, msg.length) var colums=msg.split(“\n”) var ret=new Array() colums=colums.slice(0, colums.length – 1) colums.forEach(element=> { var port=findPort(element) if (port !=-1) { ret.push(port) } }); return ret; } function findPort(colum) { var idx=colum.indexOf(‘:’) var first=colum.slice(0, idx) while (colum[idx]==’:’) { idx++ } var second=colum.slice(idx, colum.length) var fidx=first.lastIndexOf(‘ ‘) var sidx=second.indexOf(‘ ‘) var ip=first.slice(fidx + 1, first.length) var port=second.slice(0, sidx) if (ip==”127.0.0.1″) { return -1 } else { return Number(port) } } // this method is called when your extension is deactivated function deactivate() {} module.exports={ activate, deactivate }