Skip to content

Commit

Permalink
code refactor/restructure and nicer console output
Browse files Browse the repository at this point in the history
  • Loading branch information
unique1o1 committed Aug 24, 2021
1 parent 8abb51e commit 949408b
Show file tree
Hide file tree
Showing 15 changed files with 464 additions and 382 deletions.
236 changes: 0 additions & 236 deletions client.go

This file was deleted.

41 changes: 41 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package client

import (
"github.com/gofrs/uuid"
"github.com/unique1o1/jprq/client/model"
"github.com/unique1o1/jprq/pkg/socket"
"gopkg.in/mgo.v2/bson"
)

type JPRQClient struct {
DstUrl string
DstWSUrl string
Host string
Token string
Conn *socket.Socket
SocketTracker map[uuid.UUID]chan *model.ResponseMessage
}

func (t *JPRQClient) ReadMessage() (*model.ResponseMessage, error) {
resp := new(model.ResponseMessage)
_, message, err := t.Conn.ReadMessage()

if err != nil {
return nil, err
}
err = bson.Unmarshal(message, resp)
if err != nil {
return nil, err
}
return resp, nil
}

func (t *JPRQClient) WriteMessage(messageType int, data *model.RequestMessage) error {
reqMessage, err := bson.Marshal(data)
if err != nil {
return err
}
return t.Conn.WriteMessage(messageType, reqMessage)
}

//
55 changes: 55 additions & 0 deletions client/httpHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package client

import (
"bytes"
"fmt"
"github.com/gorilla/websocket"
"github.com/unique1o1/jprq/client/model"
"github.com/unique1o1/jprq/pkg/utils"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
)

func (t *JPRQClient) HTTPRequestListener(message *model.ResponseMessage) {
website, _ := url.Parse(utils.JoinURL(t.DstUrl, message.URL))
jar, _ := cookiejar.New(&cookiejar.Options{})
jar.SetCookies(
website,
message.Cookie,
)
client := http.Client{
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error { //stop following redirects
return http.ErrUseLastResponse
},
}
req, _ := http.NewRequest(message.Method, utils.JoinURL(t.DstUrl, message.URL), bytes.NewBuffer(message.Body))
req.Host = t.Host
req.Header = message.Header
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}

{
utils.PrettyPrintRequest(resp.StatusCode, message.Method, website.Path)
}
reqMessage := &model.RequestMessage{
RequestId: message.ID,
Token: t.Token,
Body: body,
Status: resp.StatusCode,
Cookie: client.Jar.Cookies(website),
Header: resp.Header,
}
t.WriteMessage(websocket.BinaryMessage, reqMessage)

}
16 changes: 16 additions & 0 deletions client/model/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package model

import (
"github.com/gofrs/uuid"
"net/http"
)

type RequestMessage struct {
RequestId uuid.UUID `bson:"request_id,omitempty"`
SocketMsgType int `bson:"socket_msg_type,omitempty"`
Token string `bson:"token,omitempty"`
Body []byte `bson:"body,omitempty"`
Status int `bson:"status,omitempty"`
Header http.Header `bson:"header,omitempty"`
Cookie []*http.Cookie `bson:"cookie,omitempty"`
}
21 changes: 21 additions & 0 deletions client/model/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model

import (
"github.com/gofrs/uuid"
"net/http"
)

type HandshakeResponseMessage struct {
Host string `bson:"host"`
Token string `bson:"token"`
}
type ResponseMessage struct {
Status int `bson:"status,omitempty"`
SocketMsgType int `bson:"socket_msg_type,omitempty"`
ID uuid.UUID `bson:"id,omitempty"`
Method string `bson:"method,omitempty"`
URL string `bson:"url,omitempty"`
Body []byte `bson:"body,omitempty"`
Header http.Header `bson:"header,omitempty"`
Cookie []*http.Cookie `bson:"cookie,omitempty"`
}
Loading

0 comments on commit 949408b

Please sign in to comment.