Skip to content

Commit

Permalink
feat(): add timeout flag
Browse files Browse the repository at this point in the history
  • Loading branch information
SalehBorhani committed Jan 10, 2025
1 parent af49131 commit f70cb83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/dns/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func URLValidator(URL string) bool {

func CheckWithURL(c *cli.Context) error {
fileToDownload := c.Args().First()
timeout := c.Int("timeout")

dnsList, err := check.ReadDNSFromFile("config/dns.conf")
if err != nil {
fmt.Println("Error reading DNS list:", err)
Expand All @@ -39,18 +41,22 @@ func CheckWithURL(c *cli.Context) error {

// Map to store the total size downloaded by each DNS
dnsSizeMap := make(map[string]int64)
// Create a context with a timeout of 15 seconds

fmt.Println("Timeout:", timeout)
fmt.Println("URL: ", fileToDownload)

tempDir := time.Now().UnixMilli()

for _, dns := range dnsList {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
// Create a custom HTTP client with the specified DNS
clientWithCustomDNS := check.ChangeDNS(dns)
client := grab.NewClient()
client.HTTPClient = clientWithCustomDNS

// Create a new download request
req, err := grab.NewRequest(".", fileToDownload)
req, err := grab.NewRequest(fmt.Sprintf("/tmp/%v", tempDir), fileToDownload)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating request for DNS %s: %v\n", dns, err)
}
Expand Down
16 changes: 16 additions & 0 deletions internal/unlockercli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func Run() {
Name: "docker",
Aliases: []string{"d"},
Usage: "Finds the fastest docker registries for an specific docker image",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "timeout",
Usage: "Sets timeout",
Value: 10,
Aliases: []string{"t"},
},
},
Action: func(cCtx *cli.Context) error {
if docker.DockerImageValidator(cCtx.Args().First()) {
return docker.CheckWithDockerImage(cCtx)
Expand All @@ -46,6 +54,14 @@ func Run() {
{
Name: "dns",
Usage: "Finds the fastest DNS SNI-Proxy for downloading an specific URL",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "timeout",
Usage: "Sets timeout",
Value: 10,
Aliases: []string{"t"},
},
},
Action: func(cCtx *cli.Context) error {
if dns.URLValidator(cCtx.Args().First()) {
return dns.CheckWithURL(cCtx)
Expand Down

0 comments on commit f70cb83

Please sign in to comment.