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目录中的配置文件格式不对.
随机推荐
- cb46a_c++_STL_算法_逆转和旋转reverse_rotate函数advance
cb46a_c++_STL_算法_逆转和旋转reverse_rotateSTL算法--变序性算法reverse() 逆转reverse_copy()一边复制一般逆转rotate()旋转,某个位置开始前 ...
- Linux 查看日志的时候常用命令总结
Linux ~ 查看日志的常用命令总结 1.tail -n <行数>,显示文件的尾部n行内容. -f 循环读取,常用于查阅正在改变的日志文件. ① tail -f test.log ...
- 小师妹学JVM之:java的字节码byte code简介
目录 简介 Byte Code的作用 查看Byte Code字节码 java Byte Code是怎么工作的 总结 简介 Byte Code也叫做字节码,是连接java源代码和JVM的桥梁,源代码编译 ...
- Java 将PDF/XPS转为Word/html /SVG/PS/PCL/PNG、PDF和XPS互转(基于Spire.Cloud.SDK for Java)
Spire.Cloud.SDK for Java提供了接口PdfConvertApi通过convert()方法将PDF文档以及XPS文档转为指定文档格式,如转PDF为Word(支持Docx.Doc). ...
- Nacos学习笔记
Nacos简介 Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服务元数据及流量管理.Nacos 帮助更敏捷和容易地构建.交付和管理微服务平台. Nacos 是构建以“服 ...
- MySQL高级用法
-- 关联查询-- select * from Goods_BomItems s,Goods_Bom t where t.GoodsBomId = s.GoodsBomId and t.GoodsBo ...
- 重学 Java 设计模式:实战中介者模式「按照Mybaits原理手写ORM框架,给JDBC方式操作数据库增加中介者场景」
作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 同龄人的差距是从什么时候拉开的 同样的幼儿园.同样的小学.一样 ...
- Python干货整理之数据结构篇
1 stack的实现 实现接口: init() 用于初始化stack,数据类型为list size() 用于获得stack的大小 push() 用于往栈中添加元素,添加的元素类型可以是int或者lis ...
- python+opencv实现图像缩放
x, y = img_.shape[0:2] img_ = cv2.resize(img_, (int(y/2), int(x/2))) 实现图像长宽缩小为原来的一半
- js语法基础入门(1)
1.基础入门 1.1.hello world 1.1.1.JavaScript是什么? JavaScript是一门跨平台.面向对象的轻量级脚本语言,在web开发中被广泛应用 1.1.2.JavaScr ...