-
Notifications
You must be signed in to change notification settings - Fork 8
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
20 changed files
with
273 additions
and
461 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
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
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,73 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
"github.com/nothub/mrpack-install/buildinfo" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"runtime/debug" | ||
"time" | ||
) | ||
|
||
type Client struct { | ||
c http.Client | ||
ua string | ||
} | ||
|
||
var DefaultClient = newHTTPClient() | ||
|
||
func newHTTPClient() *Client { | ||
c := &Client{ | ||
c: http.Client{}, | ||
} | ||
|
||
c.c.Transport = newTransport() | ||
|
||
c.ua = fmt.Sprintf("%s/%s", "mrpack-install", buildinfo.Version) | ||
info, ok := debug.ReadBuildInfo() | ||
if ok && info.Main.Path != "" { | ||
c.ua = fmt.Sprintf("%s (+https://%s)", c.ua, info.Main.Path) | ||
} | ||
|
||
return c | ||
} | ||
|
||
func newTransport() *http.Transport { | ||
return &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
}.DialContext, | ||
ForceAttemptHTTP2: true, | ||
MaxIdleConns: 100, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 20 * time.Second, | ||
ResponseHeaderTimeout: 25 * time.Second, | ||
ExpectContinueTimeout: 10 * time.Second, | ||
} | ||
} | ||
|
||
func (c *Client) SetProxy(CustomProxy string) error { | ||
proxy, err := url.Parse(CustomProxy) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
transport := newTransport() | ||
transport.Proxy = http.ProxyURL(proxy) | ||
c.c.Transport = transport | ||
|
||
// Test proxy | ||
httpUrl := "https://api.modrinth.com/" | ||
response, err := c.c.Get(httpUrl) | ||
if err != nil { | ||
return err | ||
} | ||
if response.StatusCode != http.StatusOK { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,66 @@ | ||
package download | ||
|
||
import ( | ||
"crypto" | ||
"github.com/nothub/hashutils/chksum" | ||
"github.com/nothub/hashutils/encoding" | ||
"github.com/nothub/mrpack-install/http" | ||
modrinth "github.com/nothub/mrpack-install/modrinth/api" | ||
"log" | ||
"path" | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
type Download struct { | ||
Path string | ||
Urls []string | ||
Hashes modrinth.Hashes | ||
} | ||
|
||
type Downloader struct { | ||
Downloads []*Download | ||
Threads int | ||
Retries int | ||
} | ||
|
||
func (g *Downloader) Download(baseDir string) { | ||
var wg sync.WaitGroup | ||
for i := range g.Downloads { | ||
wg.Add(1) | ||
dl := g.Downloads[i] | ||
go func() { | ||
defer wg.Done() | ||
absPath, _ := filepath.Abs(path.Join(baseDir, dl.Path)) | ||
success := false | ||
for _, link := range dl.Urls { | ||
// retry when download failed | ||
for retries := 0; retries < g.Retries; retries++ { | ||
// try download | ||
f, err := http.DefaultClient.DownloadFile(link, path.Dir(absPath), path.Base(absPath)) | ||
if err != nil { | ||
log.Printf("Download failed for %s (attempt %v), because: %s\n", dl.Path, retries+1, err.Error()) | ||
continue | ||
} | ||
// check hashcode | ||
_, err = chksum.VerifyFile(f, dl.Hashes.Sha512, crypto.SHA512.New(), encoding.Hex) | ||
if err != nil { | ||
log.Printf("Hash check failed for %s (attempt %v), because: %s\n", dl.Path, retries+1, err.Error()) | ||
continue | ||
} | ||
// success yay | ||
log.Printf("Downloaded: %s\n", f) | ||
success = true | ||
break | ||
} | ||
if success { | ||
break | ||
} | ||
} | ||
if !success { | ||
log.Printf("Downloaded failed: %s\n", dl.Path) | ||
} | ||
}() | ||
} | ||
wg.Wait() | ||
} |
Oops, something went wrong.