如何在使用golang http包时限制客户端IP地址

如何在使用golang http包时限制客户端IP地址,第1张

概述我正在使用golang http包.服务器如何限制客户端IP地址? func (s *Worker) Run(c chan error) { apiMux := http.NewServeMux() apiMux.HandleFunc("/test", s.test) apiMux.HandleFunc("/block/create", s.CreateBlock) 我正在使用golang Http包.服务器如何限制客户端IP地址?

func (s *Worker) Run(c chan error) {    APIMux := http.NewServeMux()    APIMux.HandleFunc("/test",s.test)    APIMux.HandleFunc("/block/create",s.CreateBlock)    APIMux.HandleFunc("/block/delete",s.DeleteBlock)    APIServer := &http.Server{        Addr:    "0.0.0.0:" + strconv.Itoa(s.ListenPort),Handler: APIMux,}    go func() {        log.Println("Worker Listening on " + APIServer.Addr)        c <- APIServer.ListenAndServe()    }()}
解决方法 您需要做两件事:一个是使用中间件处理程序包装您的多路复用器,该处理程序预处理您的请求并验证IP.另一个是获取用户的真实IP,如果您位于防火墙或负载均衡器后面(导致地址始终是LB的地址),或者如果您的用户位于代理后面,这一点非常重要.

至于包装你的多路复用器,它很简单:

APIServer := &http.Server{    Addr:    "0.0.0.0:8080",Handler: http.HandlerFunc( func(w http.ResponseWriter,req *http.Request) {        // get the real IP of the user,see below        addr := getRealAddr(req)       // the actual vaildation - replace with whatever you want       if (addr != "1.2.3.4") {            http.Error(w,"Blocked",401)            return        }        // pass the request to the mux        APIMux.Servehttp(w,req)    }),}

我正在附加getRealAddr函数,该函数来自我做过类似事情的实际项目:

func getRealAddr(r *http.Request)  string {    remoteIP := ""    // the default is the originating ip. but we try to find better options because this is almost    // never the right IP    if parts := strings.Split(r.RemoteAddr,":"); len(parts) == 2 {        remoteIP = parts[0]    }    // If we have a forwarded-for header,take the address from there    if xff := strings.Trim(r.header.Get("X-Forwarded-For"),","); len(xff) > 0 {        addrs := strings.Split(xff,")        lastFwd := addrs[len(addrs)-1]        if ip := net.ParseIP(lastFwd); ip != nil {            remoteIP = ip.String()        }    // parse X-Real-Ip header    } else if xri := r.header.Get("X-Real-Ip"); len(xri) > 0 {        if ip := net.ParseIP(xri); ip != nil {            remoteIP = ip.String()        }    }    return remoteIP}

至于过滤,它可以基于一组ips或CIDR范围,当然,这取决于你.

如果您有兴趣,上面的代码来自我编写的一个API构建工具包,名为Vertex,它内置了这个:https://github.com/EverythingMe/vertex

总结

以上是内存溢出为你收集整理的如何在使用golang http包时限制客户端IP地址全部内容,希望文章能够帮你解决如何在使用golang http包时限制客户端IP地址所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1265599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存