美文网首页
pprof接口隐藏/认证

pprof接口隐藏/认证

作者: 写个代码容易么 | 来源:发表于2020-10-24 09:52 被阅读0次

pprof是golang提供的性能分析工具,这里就不过多介绍了。

使用方式很简单,导入pprof包即可

import  _ "net/http/pprof"

pprof.go源文件init函数会初始化性能监控接口

func init() {
    http.HandleFunc("/debug/pprof/", Index)
    http.HandleFunc("/debug/pprof/cmdline", Cmdline)
    http.HandleFunc("/debug/pprof/profile", Profile)
    http.HandleFunc("/debug/pprof/symbol", Symbol)
    http.HandleFunc("/debug/pprof/trace", Trace)
}

但是这种简单的方式使用会导致一个大问题,就是/debug/pprof接口会随着我们的应用暴露到公网

1. net/http

可通过http多路复用将pprof接口和业务接口隔离

业务接口使用8080端口,pprof接口使用内网的8081端口

package main

import (
    "fmt"
    "log"
    "net/http"
    _ "net/http/pprof"
)

func main() {
    go func() {
        //内网可访问的pprof地址
        log.Fatal(http.ListenAndServe("127.0.0.1:8081", nil))
    }()

    mux := http.NewServeMux()
    mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
        fmt.Fprintf(writer, "hello")
    })
    //外网可访问的接口地址
    log.Fatal(http.ListenAndServe(":8080", mux))
}

2. gin

如果使用gin框架,可通过gin-contrib/pprof包实现隔离/认证

import "github.com/gin-contrib/pprof"
package main

import (
    "encoding/base64"
    "fmt"
    "github.com/gin-contrib/pprof"
    "github.com/gin-gonic/gin"
    "net/http"
)

func main() {
    router := gin.Default()
    //自定义路由
    //pprof.Register(router,"dev/pprof")
    //注册默认路由 /debug/pprof
    //pprof.Register(router)

    //注册组,需要认证
    authStr := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte("admin:123456")))
    pprofGroup := router.Group("/admin", func(c *gin.Context) {
        auth := c.Request.Header.Get("Authorization")
        if auth != authStr {
            c.Header("www-Authenticate", "Basic")
            c.AbortWithStatus(http.StatusUnauthorized)
            return
        }
        c.Next()
    })
    pprof.RouteRegister(pprofGroup, "pprof")
    router.Run(":8080")
}

将pprof接口注册到admin路由组,如果账号密码错误响应头设置www-Authenticate: Basic,表示使用http基本认证(弹出登录框),并返回状态码401(未授权)

http基本认证会将(账号+冒号+密码)Base64后并添加Basic标识

如账号密码为admin/123456
base64(admin:123456) = YWRtaW46MTIzNDU2

最后请求头会添加
Authorization: Basic YWRtaW46MTIzNDU2

访问http://localhost:8080/admin/pprof/
后会弹出登录框,输入账号密码 admin/123456后就能访问pprof界面了

相关文章

  • pprof接口隐藏/认证

    pprof是golang提供的性能分析工具,这里就不过多介绍了。 使用方式很简单,导入pprof包即可 pprof...

  • go语言性能分析工具pprof

    pprof支持两种方式记录性能 1、runtime/pprof 2、net/http/pprof ,两种方式均可分...

  • golang性能优化之pprof及其火焰图

    1 pprof简介 golang代码的性能监控使用pprof包来做。pprof有两个包: runtime/ppro...

  • go 性能剖析 PProf

    PProf是什么 pprof 是用于可视化和分析性能分析数据的工具 pprof 以 profile.proto 读...

  • TSS对接api

    认证 接口列表 认证接口详情 login 请求方法 POST 请求参数 返回结果 register 请求方法 PO...

  • Lesson-40 Go 的 pprof 的一些使用

    Pprof 获取 pprof 状态 读取 CPU 状态 查看状态 内存状态 GC 状态

  • golang内存分析工具

    go tool pprof http://localhost:6060/debug/pprof/heap 安装好g...

  • AuthenticationManager

    AuthenticationManager(接口)是认证相关的核心接口,也是发起认证的出发点,因为在实际需求中,我...

  • Go 调试

    Go的pprof使用 web服务器 import _"net/http/pprof" go func() { ...

  • go 跟踪剖析 trace

    但单单使用 PProf 有时候不一定足够完整,因为在真实的程序中还包含许多的隐藏动作,例如 Goroutine 在...

网友评论

      本文标题:pprof接口隐藏/认证

      本文链接:https://www.haomeiwen.com/subject/axdimktx.html