在以下代码中:
l, err := net.Listen("tcp", ":"+port)
assert(err)
c, err := l.Accept()
assert(err)
cert, err := tls.LoadX509KeyPair(TLS_CERT, TLS_PKEY)
assert(err)
TLSconfig := &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.VerifyClientCertIfGiven,
ServerName: DOMAIN_NAME,
}
tlsConn := tls.Server(c, TLSconfig)
tlsConn.Handshake()
c = net.Conn(tlsConn)
我想创建一个http响应编写器,例如:
w := CreateResponseWriter(c) //<-- how to do this?
w.Header().Set(...)
if !Authorized(c.RemoteAddr()) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
data, _ := os.Open("/path/to/content")
defer data.Close()
io.Copy(w, data)
目的是我在Golang中编写一个http代理,我需要检查远程连接是否被授权,如果没有,我将显示登录页面。感谢。