forked from pagpeter/TrackMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
executable file
·73 lines (65 loc) · 1.89 KB
/
router.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"time"
)
func Log(msg string) {
t := time.Now()
formatted := t.Format("2006-01-02 15:04:05")
fmt.Printf("[%v] %v\n", formatted, msg)
}
// WriteLog append msg to log_file in a new line. pass input as json
func WriteLog(msg string, log_file string) error {
// Open the file in append mode. If it doesn't exist, create it with permissions 0666.
file, err := os.OpenFile(log_file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()
// Write the text to the file
msg += "\n\n"
_, err = io.WriteString(file, msg)
if err != nil {
return err
}
return nil
}
func cleanIP(ip string) string {
return strings.Replace(strings.Replace(ip, "]", "", -1), "[", "", -1)
}
// Router returns bytes and content type that should be sent to the client
func Router(path string, res Response) ([]byte, string) {
res.TCPIP = TCPFingerprints[cleanIP(res.IP)]
res.TLS.JA4 = CalculateJa4(res.TLS)
// res.Donate = "Please consider donating to keep this API running."
Log(fmt.Sprintf("%v %v %v %v %v", cleanIP(res.IP), res.Method, res.HTTPVersion, res.Path, res.TLS.JA3Hash))
// if GetUserAgent(res) == "" {
// return []byte("{\"error\": \"No user-agent\"}"), "text/html"
// }
if c.LogToDB && res.Path != "/favicon.ico" {
SaveRequest(res)
}
if c.LogFile != "" && res.Path != "/favicon.ico" {
data, err := json.Marshal(res)
if err != nil {
log.Fatalf("Error occurred during marshaling. Error: %s", err.Error())
} else {
WriteLog(string(data), c.LogFile)
}
}
u, _ := url.Parse("https://tls.peet.ws" + path)
m, _ := url.ParseQuery(u.RawQuery)
paths := getAllPaths()
if val, ok := paths[u.Path]; ok {
return val(res, m)
}
// 404
b, _ := ReadFile("static/404.html")
return []byte(strings.ReplaceAll(string(b), "/*DATA*/", fmt.Sprintf("%v", GetTotalRequestCount()))), "text/html"
}