-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code refactor/restructure and nicer console output
- Loading branch information
Showing
15 changed files
with
464 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
Oops, something went wrong.