Skip to content

Commit

Permalink
feat: v0.2.5 - RELEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
IllTamer committed Mar 19, 2024
1 parent 2187785 commit 051345d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
10 changes: 6 additions & 4 deletions cmd/perpe/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ func EnableAgent() {
continue
}
gopool.Go(func() {
err := handle.TryPostHttp(httpPost.Url, httpPost.Secret)
if err != nil {
log.Infof("Attempt to establish http post connection failed: %v", err)
for {
err := handle.TryPostHttp(httpPost.Url, httpPost.Secret)
if err != nil {
log.Infof("Attempt to establish http post connection failed: %v", err)
}
time.Sleep(1 * time.Second)
}
time.Sleep(1 * time.Second)
})
}
})
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/mattn/go-colorable v0.1.13
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.3
github.com/theodesp/blockingQueues v0.0.0-20171230192932-26531ad66e7c
github.com/valyala/bytebufferpool v1.0.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -30,7 +33,6 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
Expand All @@ -41,10 +43,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/theodesp/blockingQueues v0.0.0-20171230192932-26531ad66e7c // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
Expand All @@ -53,5 +53,4 @@ require (
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -116,8 +114,6 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
Expand Down
10 changes: 7 additions & 3 deletions internal/handle/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ConfigureRWWClientHandler(c *ReadWithWriteClient) {
}

// ConfigureRAWClientHandler configure and link handler to connection
func ConfigureRAWClientHandler(c *ReadAndWriteClient) {
func ConfigureRAWClientHandler(c *ReadThenWriteClient) {
configureClientHandlerFunc(c, func() {
// write to client
// perp -> client
Expand Down Expand Up @@ -66,7 +66,8 @@ func configureClientHandlerFunc(c Client, rwFunc func()) {
handler.AddWait()
err := doHeartbeat(c)
if err != nil {
log.Debugf("[Client] Error occurred when heartbeat on %s: %v", url, err)
log.Infof("[Client] Error occurred when heartbeat on %s: %v", url, err)
handler.WaitExitAll()
}
})

Expand All @@ -92,7 +93,7 @@ func writeAndReadClientLoop(c *ReadWithWriteClient) {
}
log.Debugf("[<->Client] Try to send message to client(id-%s, name-%s)", handler.id, handler.name)
handler.Lock.Lock()
message, err := c.writeAndReadFunc(data)
message, err := c.writeWithReadFunc(data)
handler.Lock.Unlock()
addEchoThenServe("[<->Client]", c, func() ([]byte, error) {
return message, err
Expand Down Expand Up @@ -204,6 +205,9 @@ func addEchoThenServe(prefix string, c Client, messageSupplier func() ([]byte, e
}
log.Debugf("%s Received message with url-%s: %s", prefix, c.getUrl(), string(message))

if len(message) == 0 {
return
}
var msgData global.MsgData
err = json.Unmarshal(message, &msgData)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions internal/handle/client_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
global "github.com/IUnlimit/perpetua/internal"
"github.com/IUnlimit/perpetua/internal/utils"
log "github.com/sirupsen/logrus"
Expand All @@ -31,7 +32,7 @@ func NewReadWithWriteClient(url string, secret string, handler *Handler) *ReadWi
}
}

func (rww *ReadWithWriteClient) writeAndReadFunc(data global.MsgData) ([]byte, error) {
func (rww *ReadWithWriteClient) writeWithReadFunc(data global.MsgData) ([]byte, error) {
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
Expand All @@ -51,6 +52,10 @@ func (rww *ReadWithWriteClient) writeAndReadFunc(data global.MsgData) ([]byte, e
return nil, err
}
defer resp.Body.Close()
if !(200 <= resp.StatusCode && resp.StatusCode <= 299) {
return nil, errors.New(fmt.Sprintf("error response status: %d", resp.StatusCode))
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand All @@ -59,7 +64,7 @@ func (rww *ReadWithWriteClient) writeAndReadFunc(data global.MsgData) ([]byte, e
}

func (rww *ReadWithWriteClient) writeFunc(data global.MsgData) error {
_, err := rww.writeAndReadFunc(data)
_, err := rww.writeWithReadFunc(data)
return err
}

Expand Down
28 changes: 14 additions & 14 deletions internal/handle/client_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@ import (
"time"
)

// ReadAndWriteClient read loop and write loop
type ReadAndWriteClient struct {
// ReadThenWriteClient read loop and write loop
type ReadThenWriteClient struct {
addr string
conn *websocket.Conn
handler *Handler
}

func NewReadAndWriteClient(conn *websocket.Conn, handler *Handler) *ReadAndWriteClient {
func NewReadThenWriteClient(conn *websocket.Conn, handler *Handler) *ReadThenWriteClient {
addr := conn.LocalAddr().String()
return &ReadAndWriteClient{
return &ReadThenWriteClient{
addr: addr,
conn: conn,
handler: handler,
}
}

func (raw *ReadAndWriteClient) writeFunc(data global.MsgData) error {
return raw.conn.WriteJSON(data)
func (rtw *ReadThenWriteClient) writeFunc(data global.MsgData) error {
return rtw.conn.WriteJSON(data)
}

func (raw *ReadAndWriteClient) readFunc() ([]byte, error) {
_, bytes, err := raw.conn.ReadMessage()
func (rtw *ReadThenWriteClient) readFunc() ([]byte, error) {
_, bytes, err := rtw.conn.ReadMessage()
return bytes, err
}

func (raw *ReadAndWriteClient) getHandler() *Handler {
return raw.handler
func (rtw *ReadThenWriteClient) getHandler() *Handler {
return rtw.handler
}

func (raw *ReadAndWriteClient) getUrl() string {
return raw.addr
func (rtw *ReadThenWriteClient) getUrl() string {
return rtw.addr
}

var upgrader websocket.Upgrader
Expand Down Expand Up @@ -79,7 +79,7 @@ func TryReverseWebsocket(wsUrl string, accessToken string) error {
defer conn.Close()

handler := NewHandler(context.Background())
client := NewReadAndWriteClient(conn, handler)
client := NewReadThenWriteClient(conn, handler)
ConfigureRAWClientHandler(client)
return nil
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func CreateWSInstance(port int) {

start = true
handler = NewHandler(ctx)
client := NewReadAndWriteClient(conn, handler)
client := NewReadThenWriteClient(conn, handler)
ConfigureRAWClientHandler(client)
_ = server.Shutdown(ctx)
}
Expand Down

0 comments on commit 051345d

Please sign in to comment.