go - 给定一个端口,在 Golang 中返回相应的服务(例如 http、ssh、...)
问题描述
net
我在 go中玩弄包裹。虽然我看到了许多有用的 Lookup* 函数,但似乎它们中的任何一个都没有返回给定端口使用的服务。
例如(这纯粹是编造的):
service, err := net.LookUpService(23)
// service is ssh
我看到这实际上可以在节点中实现,dns.lookupService
其中实际提供的回调被提供服务。go 中有类似的可能吗?我发现在给定服务和网络(例如 tcp、udp)的情况下,它会返回端口,但我有点想要相反的结果。我希望这是有道理的。
解决方案
在包netdb
中查看https://godoc.org/honnef.co/go/netdb
包 netdb 为 netdb.h 中定义的 protoent 和 servent 结构提供 Go 接口
通过解析 /etc/protocols 和 /etc/services 使用纯 Go 实现
特别是:
函数 GetServByPort
func GetServByPort(port int, 协议 *Protoent) *Servent
GetServByPort 返回给定端口号和协议的 Servent。如果协议为 nil,则返回与端口号匹配的第一个服务。
推荐阅读
- javascript - 如何从 karate-config.js 初始化和访问变量?
- javascript - 如何在Javascript中的每个元素之后通过逗号分隔将迭代元素推入数组
- node.js - npm 抱怨模块外的导入。这意味着什么?
- reactjs - @apollo/client (react) 将对象数组添加到突变中
- java - Spring在运行时从属性中选择bean实现
- styled-components - 如何在 switch 语句中将两种不同的颜色分配给不同样式的组件?
- django - 如何创建优化涉及 2 个表和 ManyToManyField 字段的查询的索引?
- c# - 有没有一种简单的方法可以使用 Razor Pages 和 .NET 填充设计的网站?
- windows - 在 Windows 10 上将特定文件从一个文件夹移动到另一个文件夹的脚本
- kotlin - 带有 Exposed ORM 的“GraphQL 不支持泛型类型”