-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
208 additions
and
44 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,97 @@ | ||
package message | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"github.com/yezzey-gp/yproxy/pkg/settings" | ||
) | ||
|
||
type CatMessageV2 struct { | ||
Decrypt bool | ||
Name string | ||
StartOffset uint64 | ||
|
||
Settings []settings.StorageSettings | ||
} | ||
|
||
var _ ProtoMessage = &CatMessage{} | ||
|
||
func NewCatMessageV2(name string, decrypt bool, StartOffset uint64) *CatMessageV2 { | ||
return &CatMessageV2{ | ||
Name: name, | ||
Decrypt: decrypt, | ||
StartOffset: StartOffset, | ||
} | ||
} | ||
|
||
func (c *CatMessageV2) Encode() []byte { | ||
bt := []byte{ | ||
byte(MessageTypeCat), | ||
0, | ||
0, | ||
0, | ||
} | ||
|
||
if c.Decrypt { | ||
bt[1] = byte(DecryptMessage) | ||
} else { | ||
bt[1] = byte(NoDecryptMessage) | ||
} | ||
|
||
if c.StartOffset != 0 { | ||
bt[2] = byte(ExtendedMesssage) | ||
} | ||
|
||
bt = append(bt, []byte(c.Name)...) | ||
bt = append(bt, 0) | ||
if c.StartOffset != 0 { | ||
bt = binary.BigEndian.AppendUint64(bt, c.StartOffset) | ||
} | ||
|
||
slen := make([]byte, 8) | ||
binary.BigEndian.PutUint64(slen, uint64(len(c.Settings))) | ||
bt = append(bt, slen...) | ||
|
||
for _, s := range c.Settings { | ||
|
||
bt = append(bt, []byte(s.Name)...) | ||
bt = append(bt, 0) | ||
|
||
bt = append(bt, []byte(s.Value)...) | ||
bt = append(bt, 0) | ||
} | ||
|
||
ln := len(bt) + 8 | ||
|
||
bs := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bs, uint64(ln)) | ||
return append(bs, bt...) | ||
} | ||
|
||
func (c *CatMessageV2) Decode(body []byte) { | ||
var off uint64 | ||
c.Name, off = GetCstring(body[4:]) | ||
if body[1] == byte(DecryptMessage) { | ||
c.Decrypt = true | ||
} | ||
if body[2] == byte(ExtendedMesssage) { | ||
c.StartOffset = binary.BigEndian.Uint64(body[4+len(c.Name)+1:]) | ||
} | ||
|
||
settLen := binary.BigEndian.Uint64(body[4+off : 4+off+8]) | ||
|
||
totalOff := 4 + off + 8 | ||
|
||
c.Settings = make([]settings.StorageSettings, settLen) | ||
|
||
for i := 0; i < int(settLen); i++ { | ||
|
||
var currOff uint64 | ||
|
||
c.Settings[i].Name, currOff = GetCstring(body[totalOff:]) | ||
totalOff += currOff | ||
|
||
c.Settings[i].Value, currOff = GetCstring(body[totalOff:]) | ||
totalOff += currOff | ||
} | ||
} |
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
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
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,18 @@ | ||
package message | ||
|
||
import "bytes" | ||
|
||
func GetCstring(b []byte) (string, uint64) { | ||
offset := uint64(0) | ||
buff := bytes.NewBufferString("") | ||
|
||
for i := 0; i < len(b); i++ { | ||
offset++ | ||
if b[i] == 0 { | ||
break | ||
} | ||
buff.WriteByte(b[i]) | ||
} | ||
|
||
return buff.String(), offset | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,6 @@ | ||
package settings | ||
|
||
type StorageSettings struct { | ||
Name string | ||
Value string | ||
} |
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
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
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
Oops, something went wrong.