linux – 只接受Go中Localhost的HTTP连接?

我在Golang中有一个简单的HTTP服务器:

h := http.NewServeMux()
h.Handle("/somepath",MyHandler)

s := &http.Server{
    Addr:    "1234",Handler: h,}   

s.ListenAndServe();

在调用者不是localhost的情况下删除连接的最佳方法是什么?目前我正在考虑检查底层连接信息并确保IP地址为127.0.0.1,但这会浪费大量资源(并运行一大堆Go代码),最终会丢弃连接.理想情况下,我可以检测Golang服务器根据IP地址丢弃初始TCP SYN数据包,而不是根本不创建TCP连接(或显示此端口正在侦听).

这里最干净的道路是什么?

解决方法

VonC的评论转换为答案.

您可以通过在http.Server.Addr或http.ListenAndServe中设置host:port来绑定主机.

他们在内部使用net.Listen.

net.Listen开始:

For TCP and UDP,the syntax of laddr is “host:port”,like “127.0.0.1:8080”. If host is omitted,as in “:8080”,Listen listens on all available interfaces instead of just the interface with the given host address.

相关文章

/etc/sysctl.conf这个目录主要是配置一些系统信息,/etc/sys...
1.作用 useradd或adduser命令用来建立用户帐号和创建用户的起...
它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅...
不管是我们在安装软件还是监测软件的使用性能,我们都要随时...
装好Tomcat7后,发现除了本机能访问外界访问不了,岂有此理。...
修改防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,...