Go Pentester - HTTP Servers(3)
Building Middleware with Negroni
Reasons use middleware, including logging requests, authenticating and authorizing users, and mapping resources.
Idiomatic HTTP Middleware for Golang. https://github.com/urfave/negroni
Install the negroni package.
go get github.com/urfave/negroni
PS: How to solve the go get can not work in China. Following is the best solution so far. https://github.com/goproxy/goproxy.cn
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct
Negroni example
package main import (
"github.com/gorilla/mux"
"github.com/urfave/negroni"
"net/http"
) func main() {
r := mux.NewRouter()
n := negroni.Classic()
n.UseHandler(r)
http.ListenAndServe(":8000",n)
}
Build and execute this program.

Create trivial middleware that prints a message and passes execution to the next middleware in the chain:
package main import (
"fmt"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
"net/http"
) type trivial struct {
} func (t *trivial) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
fmt.Println("Executing trivial middleware")
next(w, r)
} func main() {
r := mux.NewRouter()
n := negroni.Classic()
n.UseHandler(r)
n.Use(&trivial{})
http.ListenAndServe(":8000",n)
}
Build and test this new program.

Adding Authentication with Negroni
Use of context, which can easily pass variables between functions.
package main import (
"context"
"fmt"
"net/http" "github.com/gorilla/mux"
"github.com/urfave/negroni"
) type badAuth struct {
Username string
Password string
} func (b *badAuth) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
username := r.URL.Query().Get("username")
password := r.URL.Query().Get("password")
if username != b.Username && password !=b.Password {
http.Error(w, "Unauthorized", 401)
return
}
ctx := context.WithValue(r.Context(), "username", username)
r = r.WithContext(ctx)
next(w, r)
} func hello(w http.ResponseWriter, r * http.Request) {
username := r.Context().Value("username").(string)
fmt.Fprintf(w, "Hi %s\n", username)
} func main() {
r := mux.NewRouter()
r.HandleFunc("/hello",hello).Methods("GET")
n := negroni.Classic()
n.Use(&badAuth{
Username: "admin",
Password: "password",
})
n.UseHandler(r)
http.ListenAndServe(":8000", n) }
Build and excute this program. Then test it by sending a few requests to the server.
curl -i http://localhost:8000/hello
curl -i 'http://localhost:8000/hello?username=admin&password=password'

Logs on the server-side.

Go Pentester - HTTP Servers(3)的更多相关文章
- Go Pentester - HTTP Servers(2)
Routing with the gorilla/mux Package A powerful HTTP router and URL matcher for building Go web serv ...
- Go Pentester - HTTP Servers(1)
HTTP Server Basics Use net/http package and useful third-party packages by building simple servers. ...
- Coping with the TCP TIME-WAIT state on busy Linux servers
Coping with the TCP TIME-WAIT state on busy Linux servers 文章源自于:https://vincent.bernat.im/en/blog/20 ...
- How To Restart timer service on all servers in farm
[array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...
- eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat
eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- Servers
Servers¶ Server interface. class novaclient.v1_1.servers.Server(manager, info, loaded=False) Bases: ...
- 使用servers 启动项目时 ,一直处于启动中, 最后出现无法的问题。
使用eclipse 中的servers 配置了一个server 来启动项目, 发现无法启动 排除法: 去掉项目配置,单独启动该server ,发现可以启动, 说明是项目出现问题 但是项目并没有报错, ...
- servers中添加server时,看不到运行环境的选择。
servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.
随机推荐
- SQL去掉重复数据
SELECT vc_your_email,vc_our_ref_or_code INTO #tmp FROM( SELECT vc_your_email,vc_our_ref_or_code,ROW_ ...
- Centos中使用virtualenvwrapper
Centos中使用virtualenvwrapper python特有的一种软件环境,创建多个python环境,各个环境之间完全隔离,互不影响.它可以用来解决Python项目开发和运行过程中的依赖项和 ...
- MonoBehaviour.StartCoroutine开启协同程序
StartCoroutine协同程序 StartCoroutine(IEnumerator) StartCoroutine(string methodName) StartCoroutine(stri ...
- Merge,Rebase,Cherry-Pick 一文解惑
代码合并在日常开发中是较为常见的场景,采用合适的合并方式,可以起到事半功倍的效果.对应在 Git 中合并的方式主要有三个,Merge,Rebase,Cherry-Pick. 开始部分会首先介绍一下这三 ...
- 推荐一种通过刷leetcode来增强技术功底的方法
背景 如果前人认为这个一种学习提高或者检验能力的成功实践.而自己目前又没有更好的方法,那就不妨试一试. 而不管作为面试官还是被面试者,编码题最近越来越流行.而两种角色都需要思考的问题是希望考察什么能力 ...
- 扫描U盘
编辑器加载中...int CSendUDiskDlg::SearchUDisk(void) { int nCount, i; char szDriver[3]; nCount = 0; szDrive ...
- Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析
一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...
- PHP实现邮箱验证码验证功能
*文章来源:https://blog.egsec.cn/archives/623 (我的主站) *本文将主要说明:PHP实现邮箱验证码验证功能,通过注册或登录向用户发送身份确认验证码,并通过判断输入 ...
- H5禁止微信内置浏览器调整字体大小
微信webview内置了调整字体大小的功能,用户可以根据实际情况进行调节.但是这也会导致字体大小改变以后,出现页面布局错乱的情况. 1.iOS的解决方案是覆盖掉微信的样式: body { /* IOS ...
- 在具体的前端工作中通常HTML页面乱码怎么解决?
HTML文件乱码一般是因为编码格式不匹配造成的,比如:不同编码内容混杂.浏览器不能自动检测网页编码等等:但无论是哪种情况造成乱码,在HTML文件头中设置网页编码,匹配好编码格式就可. 下面是一个中文乱 ...