@@ -13,14 +13,14 @@ import (
1313 "log/slog"
1414 "net"
1515 "net/http"
16+ "net/http/httputil"
1617 "os"
1718 "os/signal"
1819 "strings"
1920 "syscall"
2021 "time"
2122
2223 "github.com/josexy/mitmpgo"
23- "github.com/josexy/mitmpgo/buf"
2424 "github.com/josexy/mitmpgo/metadata"
2525)
2626
@@ -37,6 +37,9 @@ type bodyDecoder struct {
3737}
3838
3939func newBodyDecoder (r io.ReadCloser , encoding string , chunkType int ) (io.ReadCloser , error ) {
40+ if r == http .NoBody { // no body and no need to replace it
41+ return r , nil
42+ }
4043 if encoding == "" {
4144 return newChunkBodyReader (r , CHUNK_SIZE , chunkType ), nil
4245 }
@@ -102,7 +105,7 @@ func (r *chunkBodyReader) Read(p []byte) (n int, err error) {
102105 // fmt.Printf("--> hex dump(chunk size/data size: %d/%d):\n%s\n", r.N, n, hex.Dump(p[:n]))
103106 }
104107 if err == io .EOF {
105- fmt .Printf ("<<-- [%d]full data dump (%d bytes):\n %s \n " , r .chunkType , r .buf .Len (), r . buf . Bytes ())
108+ fmt .Printf ("<<-- [%d]full data dump (%d bytes):\n " , r .chunkType , r .buf .Len ())
106109 }
107110 return
108111}
@@ -147,6 +150,7 @@ func main() {
147150 // mitmpgo.WithDisableProxy(),
148151 // mitmpgo.WithDisableHTTP2(),
149152 // mitmpgo.WithSkipVerifySSLFromServer(),
153+ // mitmpgo.WithMaxWebsocketFramesPerForward(4096),
150154 )
151155 if err != nil {
152156 panic (err )
@@ -207,7 +211,6 @@ func httpInterceptor(ctx context.Context, req *http.Request, invoker mitmpgo.HTT
207211 slog .String ("host" , req .Host ),
208212 slog .String ("proto" , req .Proto ),
209213 slog .String ("method" , req .Method ),
210- slog .Bool ("tls" , req .TLS != nil ),
211214 slog .String ("url" , req .URL .String ()),
212215 slog .Any ("headers" , map [string ][]string (req .Header )),
213216 )
@@ -257,17 +260,28 @@ func httpInterceptor(ctx context.Context, req *http.Request, invoker mitmpgo.HTT
257260 return rsp , err
258261}
259262
260- func websocketInterceptor (ctx context.Context , dir mitmpgo. WSDirection , msgType int , b * buf. Buffer , req * http.Request , wdi mitmpgo.WebsocketDelegatedInvoker ) error {
263+ func websocketInterceptor (ctx context.Context , req * http. Request , rsp * http.Response , fw mitmpgo.WebsocketFramesWatcher ) {
261264 _md , _ := metadata .FromContext (ctx )
262265 md := _md .MD ()
263- slog .Debug ("websocket" ,
266+ slog .Debug ("request" ,
267+ slog .Bool ("stream_body" , md .StreamBody ),
264268 slog .String ("source" , md .SourceAddr .String ()),
265269 slog .String ("destination" , md .DestinationAddr .String ()),
266270 slog .String ("hostport" , md .RequestHostport ),
267- slog .String ("uri" , req .URL .String ()),
268- slog .String ("direction" , dir .String ()),
269- slog .Int ("msg_type" , msgType ),
271+ slog .String ("host" , req .Host ),
272+ slog .String ("proto" , req .Proto ),
273+ slog .String ("method" , req .Method ),
274+ slog .String ("url" , req .URL .String ()),
275+ slog .Int ("status_code" , rsp .StatusCode ),
276+ slog .Any ("request_headers" , map [string ][]string (req .Header )),
277+ slog .Any ("response_headers" , map [string ][]string (rsp .Header )),
270278 )
279+
280+ data , _ := httputil .DumpRequest (req , false )
281+ fmt .Printf ("%s\n " , string (data ))
282+ data , _ = httputil .DumpResponse (rsp , false )
283+ fmt .Printf ("%s\n " , string (data ))
284+
271285 if md .TLSState != nil {
272286 slog .Debug ("tls state" ,
273287 slog .String ("server_name" , md .TLSState .ServerName ),
@@ -292,10 +306,25 @@ func websocketInterceptor(ctx context.Context, dir mitmpgo.WSDirection, msgType
292306 slog .Any ("ip" , md .ServerCertificate .IPAddresses ),
293307 )
294308 }
295- // if md.Direction == metadata.Receive {
296- // b.WriteString(time.Now().String())
297- // }
298- return wdi .Invoke (msgType , b )
309+
310+ for {
311+ select {
312+ case <- ctx .Done ():
313+ return
314+ case frame , ok := <- fw .GetFrame ():
315+ if ! ok {
316+ return
317+ }
318+ dir := frame .Direction ()
319+ msgType := frame .MessageType ()
320+ dataBuf := frame .DataBuffer ()
321+ fmt .Printf ("---> %s %d %s\n " , dir , msgType , dataBuf .String ())
322+ if err := frame .Invoke (); err != nil {
323+ slog .Error ("frame invoke error" , slog .String ("error" , err .Error ()))
324+ }
325+ frame .Release ()
326+ }
327+ }
299328}
300329
301330func getDecodedReader (r io.Reader , encoding string ) (io.ReadCloser , error ) {
0 commit comments