-
Notifications
You must be signed in to change notification settings - Fork 2
/
mitm.go
159 lines (144 loc) · 3.71 KB
/
mitm.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 (
"bytes"
"context"
"encoding/base64"
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/gorilla/websocket"
)
const (
gameServer = "http://localhost:4567"
terminalListen = "localhost:2333"
gameServerAuthUsername = ""
gameServerAuthPassword = ""
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(*http.Request) bool {
return true
},
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func wsMitm(writer http.ResponseWriter, request *http.Request) {
var serverHeader http.Header
if gameServerAuthUsername != "" {
serverHeader = make(http.Header)
serverHeader.Add("Authorization", "Basic " + basicAuth(gameServerAuthUsername, gameServerAuthPassword))
}
server, _, err := websocket.DefaultDialer.Dial("ws"+gameServer[4:], serverHeader)
if err != nil {
log.Print("server:", err)
return
}
defer server.Close()
client, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Print("client:", err)
return
}
defer client.Close()
ctx, cancel := context.WithCancel(request.Context())
go func() {
defer cancel()
for {
mt, message, err := client.ReadMessage()
if err != nil {
log.Println("read client:", err)
return
}
//log.Printf("recv client: %s", message)
err = server.WriteMessage(mt, message)
if err != nil {
log.Println("write server:", err)
return
}
}
}()
go func() {
defer cancel()
recvServer(client, server)
}()
go func() {
ticker := time.NewTicker(time.Second * 15)
defer ticker.Stop()
for _ = range ticker.C {
deadline := time.Now().Add(time.Second * 5)
err := client.WriteControl(websocket.PingMessage, nil, deadline)
if err != nil {
log.Println("ping client:", err)
return
}
}
}()
<-ctx.Done()
}
func main() {
gameServerUri, _ := url.ParseRequestURI(gameServer)
proxy := httputil.NewSingleHostReverseProxy(gameServerUri)
oldDirector := proxy.Director
proxy.Director = func(request *http.Request) {
oldDirector(request)
if (gameServerAuthUsername != "") {
request.SetBasicAuth(gameServerAuthUsername, gameServerAuthPassword)
}
}
proxy.ModifyResponse = func(response *http.Response) (err error) {
switch response.Request.URL.Path {
case "/main.js":
err = responseModifier(response, func(data []byte) []byte {
return bytes.Replace(data, []byte("const RES_"), []byte("let RES_"), 2)
})
case "/visuals.js":
err = responseModifier(response, func(data []byte) []byte {
data = bytes.Replace(data,
[]byte(`(globals.state.state.entities.player.x - (RES_W/2))`),
[]byte(`this.viewportX`),
1)
data = bytes.Replace(data,
[]byte(`(globals.state.state.entities.player.y - (RES_H/2))`),
[]byte(`this.viewportY`),
1)
return data
})
case "/":
err = responseModifier(response, func(data []byte) []byte {
index := bytes.Index(data, []byte("</head>"))
if index < 0 {
return data
}
var buf bytes.Buffer
buf.Write(data[:index])
buf.WriteString(`<script src="/hack.js"></script><link rel="stylesheet" href="/hack.css">`)
buf.Write(data[index:])
return buf.Bytes()
})
}
return
}
log.Fatal(http.ListenAndServe("localhost:12450",
http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if len(request.Header.Values("Upgrade")) > 0 {
if request.URL.Path == "/aux" {
aux(writer, request)
} else {
wsMitm(writer, request)
}
return
}
switch request.URL.Path {
case "/hack.js":
http.ServeFile(writer, request, "hack.js")
case "/hack.css":
http.ServeFile(writer, request, "hack.css")
default:
proxy.ServeHTTP(writer, request)
}
})),
)
}