-
Notifications
You must be signed in to change notification settings - Fork 0
/
preheat.go
71 lines (62 loc) · 1.45 KB
/
preheat.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
package kurma
import (
"crypto/sha512"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"time"
)
const (
tokenLen = 142
)
func PreHeat(authURI, targetURI string) net.Conn {
nonce := fetchNonce(authURI)
fmt.Printf("nonce: %s\n", nonce)
return preHeater(nonce, targetURI)
}
func fetchNonce(uri string) string {
client := &http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest("GET", uri, nil)
token := os.Getenv("TOKEN")
if token == "" {
log.Println("[!] No TOKEN set!!!")
ts := time.Now().Unix()
hsh := fmt.Sprintf("%x", sha512.Sum512([]byte(string(ts))))
token = (hsh + hsh)[:tokenLen]
}
req.Header.Set(secretHeader, token)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
return resp.Header.Get(secretHeader)
}
func preHeater(nonce, uri string) net.Conn {
req, err := http.NewRequest("GET", "/", nil)
req.Header.Set(secretHeader, nonce)
if err != nil {
panic(err)
}
log.Println("Dialing:", uri)
dial, err := net.Dial("tcp", uri)
if err != nil {
panic(err)
}
log.Println("Client: create TLS connection")
// TODO can get the server name from uri, or as an optional parameter.
tls_conn := tls.Client(dial, &tls.Config{ServerName: "localhost"})
conn := httputil.NewClientConn(tls_conn, nil)
_, err = conn.Do(req)
if err != httputil.ErrPersistEOF && err != nil {
panic(err)
}
log.Println("Client: hijacking https connection")
connection, _ := conn.Hijack()
return connection
}