Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use regex to extract correct ip from user addr #41

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions disperser/batcher/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func (b *Batcher) HandleSingleBatch(ctx context.Context) (uint64, error) {
if meta.BlobStatus == disperser.Failed {
log.Info("[batcher] disperse batch reach max retries", "key", metadata.GetBlobKey())
b.EncodingStreamer.RemoveEncodedBlob(metadata)
b.Queue.RemoveBlob(ctx, metadata)
}
}
}
Expand Down Expand Up @@ -394,6 +395,7 @@ func (b *Batcher) HandleSignedBatch(ctx context.Context) error {
log.Info("[batcher] submit aggregateSignatures reach max retries", "key", metadata.GetBlobKey())
b.EncodingStreamer.RemoveEncodedBlob(metadata)
b.sliceSigner.RemoveSignedBlob(ts[idx])
b.Queue.RemoveBlob(ctx, metadata)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions disperser/batcher/slice_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ func (s *SliceSigner) aggregateSignature(ctx context.Context, signInfo *SignInfo
if meta.BlobStatus == disperser.Failed {
s.logger.Info("[signer] signing blob reach max retries", "key", metadata.GetBlobKey())
s.EncodingStreamer.RemoveEncodedBlob(metadata)
s.blobStore.RemoveBlob(ctx, metadata)
}
}
}
Expand Down
50 changes: 43 additions & 7 deletions disperser/signer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"regexp"
"strings"
"time"

Expand All @@ -17,22 +18,57 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

const ipv4WithPortPattern = `\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?::\d{1,5})\b`
const ipv4Pattern = `\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b`
const portPattern = `\b(\d{1,5})\b`

type client struct {
timeout time.Duration
timeout time.Duration
ipv4Regex *regexp.Regexp
}

func NewSignerClient(timeout time.Duration) (disperser.SignerClient, error) {
regex := regexp.MustCompile(ipv4WithPortPattern)

return client{
timeout: timeout,
timeout: timeout,
ipv4Regex: regex,
}, nil
}

func (c client) BatchSign(ctx context.Context, addr string, data []*pb.SignRequest, log common.Logger) ([]*core.Signature, error) {
// Check if the lowercase URL starts with "http://"
prefix := "http://"
if strings.HasPrefix(strings.ToLower(addr), prefix) {
// Remove the prefix from the original URL
addr = addr[len(prefix):]
matches := c.ipv4Regex.FindAllString(addr, -1)
if len(matches) != 1 {
formattedAddr := ""
prefix := "http://"
if strings.HasPrefix(strings.ToLower(addr), prefix) {
addr = addr[len(prefix):]
}

idx := strings.Index(addr, ":")
if idx != -1 {
ipv4Reg := regexp.MustCompile(ipv4Pattern)
matches := ipv4Reg.FindAllString(addr[:idx], -1)
if len(matches) == 1 {
formattedAddr = matches[0]

portReg := regexp.MustCompile(portPattern)
matches := portReg.FindAllString(addr[idx+1:], -1)
if len(matches) == 1 {
formattedAddr += ":" + matches[0]
} else {
formattedAddr = ""
}
}
}

if formattedAddr == "" {
return nil, fmt.Errorf("signer addr is not correct: %v", addr)
}

addr = formattedAddr
} else {
addr = matches[0]
}

ctxWithTimeout, cancel := context.WithTimeout(ctx, c.timeout)
Expand Down
Loading