Skip to content

Commit

Permalink
add relay length vary 0-32
Browse files Browse the repository at this point in the history
  • Loading branch information
woodlyer committed Aug 19, 2023
1 parent 0f56827 commit b218c3d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
59 changes: 59 additions & 0 deletions go-gost/relay/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
FeatureUserAuth FeatureType = 0x01
FeatureAddr FeatureType = 0x02
FeatureTunnel FeatureType = 0x03
FeatureRandom FeatureType = 0xf0 //wood
)

var (
Expand Down Expand Up @@ -54,6 +55,8 @@ func NewFeature(t FeatureType, data []byte) (f Feature, err error) {
f = new(AddrFeature)
case FeatureTunnel:
f = new(TunnelFeature)
case FeatureRandom:
f = new(RandomFeature) //wood
default:
return nil, errors.New("unknown feature")
}
Expand Down Expand Up @@ -428,3 +431,59 @@ func (f *TunnelFeature) Decode(b []byte) error {
copy(f.ID[:], b)
return nil
}

// wood
// RandomFeature is a relay feature,
// it is used to random the length of packet.
//
// Protocol spec:
//
// +------+----------+
// | ULEN | RANDOM |
// +------+----------+
// | 1 | 0 to 255 |
// +------+----------+
//
// ULEN - length of RANDOM field, 1 byte.
// RANDOM - random, variable length, 0 to 255 bytes.

// random string to change the packet length.
type RandomFeature struct {
RandomStr string
}

func (f *RandomFeature) Type() FeatureType {
return FeatureRandom
}

func (f *RandomFeature) Encode() ([]byte, error) {
var buf bytes.Buffer
f.RandomStr = RandString() // random string have 0-32 string length
f.RandomStr = "RAND" + f.RandomStr
ulen := len(f.RandomStr)
if ulen > 0xFF {
return nil, errors.New("random maximum length exceeded")
}
buf.WriteByte(uint8(ulen))
buf.WriteString(f.RandomStr)

return buf.Bytes(), nil
}

func (f *RandomFeature) Decode(b []byte) error {
//return nil

if len(b) < 1 {
return ErrShortBuffer
}

pos := 0
ulen := int(b[pos])

pos++
if len(b) < pos+ulen {
return ErrShortBuffer
}
f.RandomStr = string(b[pos : pos+ulen])
return nil
}
23 changes: 23 additions & 0 deletions go-gost/relay/randstring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package relay

import (
"math/rand"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

// short than 250
// not too long, length vary from 0 to 32
func RandString() string {
strLen := rand.Intn(32) // [0-32)
buf := make([]rune, strLen)
for i := range buf {
buf[i] = letters[rand.Intn(len(letters))]
}
return string(buf)
}
4 changes: 4 additions & 0 deletions go-gost/x/connector/relay/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (c *relayConnector) Connect(ctx context.Context, conn net.Conn, network, ad
Version: relay.Version1,
Cmd: relay.CmdConnect,
}

// wood
req.Features = append(req.Features, &relay.RandomFeature{})

if network == "udp" || network == "udp4" || network == "udp6" {
req.Cmd |= relay.FUDP

Expand Down
8 changes: 5 additions & 3 deletions mybuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ win()

swager()
{
go install https://github.com/go-swagger/go-swagger/cmd
swagger generate spec -o swagger.yaml
cp swagger.yaml go-gost/x/api/
# go install https://github.com/go-swagger/go-swagger/cmd
wget https://github.com/go-swagger/go-swagger/releases/download/v0.30.5/swagger_linux_amd64
mv swagger_linux_amd64 swagger
chmod +x swagger
./swagger generate spec -o go-gost/x/api/swagger.yaml
}


Expand Down

0 comments on commit b218c3d

Please sign in to comment.