-
Notifications
You must be signed in to change notification settings - Fork 6
/
net.go
78 lines (73 loc) · 1.78 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package snippets
import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/http/httptrace"
"time"
"github.com/julienschmidt/httprouter"
"golang.org/x/crypto/acme/autocert"
)
func encryptListen() error {
var certPem, keyPem []byte
cert, err := tls.X509KeyPair(certPem, keyPem)
if err != nil {
return err
}
cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
l, err := tls.Listen("tcp", ":2000", cfg)
if err != nil {
return err
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
return err
}
go func(c net.Conn) {
io.Copy(c, c)
c.Close()
}(conn)
}
}
func newService() {
manager := &autocert.Manager{
Cache: autocert.DirCache("cache-dir"),
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("example.come", "www.example.come"),
}
router := httprouter.New()
var userHandler = func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var _ = "process"
}
var withMiddleware = func(h httprouter.Handle) httprouter.Handle {
var _ = "intercept"
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
h(w, r, params)
}
}
router.GET("/user/:id", withMiddleware(userHandler))
server := &http.Server{
Addr: ":https",
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
TLSConfig: manager.TLSConfig(),
Handler: router,
}
server.ListenAndServeTLS("", "")
}
func withTrace(req *http.Request) *http.Request {
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Printf("Got Conn: %+v\n", connInfo)
},
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
fmt.Printf("DNS Info: %+v\n", dnsInfo)
},
}
return req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
}