-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
160 lines (131 loc) · 3.48 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
)
var httpClient = &http.Client{}
type MapData struct {
Version int `json:"version"`
Sources []string `json:"sources"`
SourcesContent []string `json:"sourcesContent"`
}
func ValidateMapURL(siteURL string) ([]string, bool) {
// Check if the URL is likely a JavaScript file
if !(strings.HasSuffix(siteURL, ".js") || strings.Contains(siteURL, ".js?")) {
return nil, false
}
// If URL ends with ".js?", strip everything after "?" and add ".map" to it
if strings.Contains(siteURL, ".js?") {
siteURL = strings.Split(siteURL, "?")[0] + ".map"
} else {
siteURL += ".map"
}
mapURL := siteURL
validatedURLs := make([]string, 0)
request, err := http.NewRequest("GET", mapURL, nil)
if err != nil {
return validatedURLs, false
}
request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109) Gecko/20100101 Firefox/112.0")
response, err := httpClient.Do(request)
if err != nil {
return validatedURLs, false
}
defer response.Body.Close()
if response.StatusCode == 200 {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return validatedURLs, false
}
if strings.Contains(string(body), "\"mappings\"") {
validatedURLs = append(validatedURLs, mapURL)
}
}
return validatedURLs, true
}
func ExtractMap(mapURL string) (data MapData, err error) {
request, err := http.NewRequest("GET", mapURL, nil)
if err != nil {
return
}
request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109) Gecko/20100101 Firefox/112.0")
response, err := httpClient.Do(request)
if err != nil {
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}
err = json.Unmarshal(body, &data)
return
}
func WriteToFile(path string, content string) error {
path = filepath.Clean(path)
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(path), 0700)
if err != nil {
return err
}
}
return ioutil.WriteFile(path, []byte(content), 0600)
}
func main() {
inputFile := flag.String("f", "", "File with URLs")
outputDir := flag.String("o", "", "Directory to output files")
flag.Parse()
var reader io.Reader
if *inputFile == "" {
reader = os.Stdin
} else {
file, err := os.Open(*inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file %s", file)
os.Exit(1)
}
defer file.Close()
reader = file
}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
siteURL := scanner.Text()
validatedURLs, isValid := ValidateMapURL(siteURL)
if !isValid {
continue
}
for _, mapURL := range validatedURLs {
mapData, err := ExtractMap(mapURL)
if err != nil {
fmt.Println("Error in extracting map: ", err)
continue
}
for i, sourceURL := range mapData.Sources {
sourceContent := mapData.SourcesContent[i]
path := sourceURL
if strings.HasPrefix(path, "webpack:///") {
path = strings.TrimPrefix(path, "webpack:///")
}
path = strings.ReplaceAll(path, "/", string(os.PathSeparator))
siteDirName := strings.ReplaceAll(siteURL, "/", "_")
if *outputDir != "" {
path = filepath.Join(*outputDir, siteDirName, path)
} else {
path = filepath.Join(siteDirName, path)
}
err = WriteToFile(path, sourceContent)
if err != nil {
fmt.Println("Error in writing file: ", err)
}
}
}
}
}