Skip to content

Commit

Permalink
Merge pull request #638 from adotkhan/bytes
Browse files Browse the repository at this point in the history
Byte generation with goregen
  • Loading branch information
rod-hynes committed May 11, 2023
2 parents 75fa033 + b4e6c8a commit f925cd4
Show file tree
Hide file tree
Showing 22 changed files with 1,377 additions and 233 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
go test -v -race ./psiphon/common/accesscontrol
go test -v -race ./psiphon/common/crypto/ssh
go test -v -race ./psiphon/common/fragmentor
go test -v -race ./psiphon/common/regen
go test -v -race ./psiphon/common/monotime
go test -v -race ./psiphon/common/obfuscator
go test -v -race ./psiphon/common/osl
Expand Down Expand Up @@ -111,6 +112,7 @@ jobs:
go test -v -covermode=count -coverprofile=accesscontrol.coverprofile ./psiphon/common/accesscontrol
go test -v -covermode=count -coverprofile=ssh.coverprofile ./psiphon/common/crypto/ssh
go test -v -covermode=count -coverprofile=fragmentor.coverprofile ./psiphon/common/fragmentor
go test -v -covermode=count -coverprofile=regen.coverprofile ./psiphon/common/regen
go test -v -covermode=count -coverprofile=monotime.coverprofile ./psiphon/common/monotime
go test -v -covermode=count -coverprofile=obfuscator.coverprofile ./psiphon/common/obfuscator
go test -v -covermode=count -coverprofile=osl.coverprofile ./psiphon/common/osl
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ require (
github.com/stretchr/testify v1.7.1
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8
github.com/wader/filtertransport v0.0.0-20200316221534-bdd9e61eee78
github.com/zach-klippenstein/goregen v0.0.0-20160303162051-795b5e3961ea
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a
golang.org/x/net v0.7.0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
Expand All @@ -70,7 +69,6 @@ require (
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3-0.20210916003710-5d5e8c018a13 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gxui v0.0.0-20151028112939-f85e0a97b3a4 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/josharian/native v1.0.0 // indirect
github.com/kardianos/osext v0.0.0-20170510131534-ae77be60afb1 // indirect
Expand All @@ -83,7 +81,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/sergeyfrolov/bsbuffer v0.0.0-20180903213811-94e85abb8507 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
gitlab.com/yawning/obfs4.git v0.0.0-20190120164510-816cff15f425 // indirect
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
Expand Down
84 changes: 0 additions & 84 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions psiphon/common/parameters/frontingSpec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
regen "github.com/zach-klippenstein/goregen"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/regen"
)

// FrontingSpecs is a list of domain fronting specs.
Expand Down Expand Up @@ -73,7 +73,7 @@ func (specs FrontingSpecs) SelectParameters() (
return "", "", "", "", nil, "", errors.TraceNew("missing fronting address")
}

frontingDialAddr, err := regen.Generate(
frontingDialAddr, err := regen.GenerateString(
spec.Addresses[prng.Intn(len(spec.Addresses))])
if err != nil {
return "", "", "", "", nil, "", errors.Trace(err)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

/*
* Copyright (c) 2023, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package regen

import (
"fmt"
"math"
)

// CharClass represents a regular expression character class as a list of ranges.
Expand Down Expand Up @@ -57,7 +77,7 @@ e.g.
func parseCharClass(runes []rune) *tCharClass {
var totalSize int32
numRanges := len(runes) / 2
ranges := make([]tCharClassRange, numRanges, numRanges)
ranges := make([]tCharClassRange, numRanges)

for i := 0; i < numRanges; i++ {
start := runes[i*2]
Expand All @@ -79,6 +99,38 @@ func parseCharClass(runes []rune) *tCharClass {
return &tCharClass{ranges, totalSize}
}

// parseByteClass parses character classes only for byte values (0-255).
// Returns nil if runes does not contain any byte values.
//
// Note:
// If an end range is greater than 255, it is truncated to 255.
func parseByteClass(runes []rune) *tCharClass {
var totalSize int32

var ranges []tCharClassRange
for i := 0; i < len(runes)-1; i += 2 {
start := runes[i]
end := runes[i+1]

var r tCharClassRange

if start <= math.MaxUint8 {
if end > math.MaxUint8 {
end = math.MaxUint8
}
r = newCharClassRange(start, end)
ranges = append(ranges, r)
totalSize += r.Size
}
}

if len(ranges) == 0 {
return nil
}

return &tCharClass{ranges, totalSize}
}

// GetRuneAt gets a rune from CharClass as a contiguous array of runes.
func (class *tCharClass) GetRuneAt(i int32) rune {
for _, r := range class.Ranges {
Expand All @@ -95,10 +147,6 @@ func (class *tCharClass) String() string {
}

func newCharClassRange(start rune, end rune) tCharClassRange {
if start < 1 {
panic("char class range cannot contain runes less than 1")
}

size := end - start + 1

if size < 1 {
Expand All @@ -113,8 +161,8 @@ func newCharClassRange(start rune, end rune) tCharClassRange {

func (r tCharClassRange) String() string {
if r.Size == 1 {
return fmt.Sprintf("%s:1", runesToString(r.Start))
return fmt.Sprintf("%s:1", runesToUTF8(r.Start))
}
return fmt.Sprintf("%s-%s:%d", runesToString(r.Start), runesToString(r.Start+rune(r.Size-1)), r.Size)
return fmt.Sprintf("%s-%s:%d", runesToUTF8(r.Start), runesToUTF8(r.Start+rune(r.Size-1)), r.Size)

}
File renamed without changes.
45 changes: 45 additions & 0 deletions psiphon/common/regen/generator_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2014 Zachary Klippenstein
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package regen

import (
"errors"
"testing"
)

func TestGeneratorError(t *testing.T) {

t.Run("Handles nil cause", func(t *testing.T) {
err := generatorError(nil, "msg")
if err == nil {
t.Fatal("Expected error, got nil")
}
if err.Error() != "msg" {
t.Fatalf("Expected error message 'msg', got '%s'", err.Error())
}
})

t.Run("Formats", func(t *testing.T) {
err := generatorError(errors.New("cause"), "msg %s", "arg")
if err == nil {
t.Fatal("Expected error, got nil")
}
if err.Error() != "msg arg\ncaused by cause" {
t.Fatalf("Expected error message 'msg arg\ncaused by cause', got '%s'", err.Error())
}
})
}
Loading

0 comments on commit f925cd4

Please sign in to comment.