Skip to content

Commit

Permalink
Merge pull request #1 from Chikage0o0/add-proxy
Browse files Browse the repository at this point in the history
Add proxy option and refactor http package
  • Loading branch information
nothub authored Sep 6, 2022
2 parents 9651191 + 22997e1 commit 62e801a
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 22 deletions.
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {
rootCmd.Flags().String("host", "api.modrinth.com", "Labrinth host")
rootCmd.Flags().String("server-dir", "mc", "Server directory path")
rootCmd.Flags().String("server-file", "", "Server jar file name")
rootCmd.Flags().String("proxy", "", "Use a proxy to download")
}

var rootCmd = &cobra.Command{
Expand All @@ -41,6 +42,17 @@ var rootCmd = &cobra.Command{
log.Fatalln(err)
}

proxy, err := cmd.Flags().GetString("proxy")
if err != nil {
log.Fatalln(err)
}
if proxy != "" {
err := http.Instance.SetProxy(proxy)
if err != nil {
log.Fatalln(err)
}
}

input := args[0]
version := ""
if len(args) > 1 {
Expand Down
25 changes: 3 additions & 22 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,14 @@ import (
"os"
"path"
"regexp"
"runtime/debug"
"strconv"
)

type ErrorModel interface {
String() string
}

type Client struct {
UserAgent string
HTTPClient *http.Client
}

// TODO: global lookup map host -> ratelimit hits left and sleep wait strategy

var Instance *Client = nil

func init() {
Instance = &Client{
UserAgent: "mrpack-install",
HTTPClient: &http.Client{},
}
info, ok := debug.ReadBuildInfo()
if ok && info.Main.Path != "" {
Instance.UserAgent = info.Main.Path + "/" + info.Main.Version
}
}
var Instance = NewHTTPClient()

func (client *Client) GetJson(url string, respModel interface{}, errModel ErrorModel) error {
request, err := http.NewRequest(http.MethodGet, url, nil)
Expand All @@ -49,7 +30,7 @@ func (client *Client) GetJson(url string, respModel interface{}, errModel ErrorM

request.Close = true

response, err := client.HTTPClient.Do(request)
response, err := client.Do(request)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,7 +67,7 @@ func (client *Client) DownloadFile(url string, downloadDir string, fileName stri
request.Header.Set("User-Agent", client.UserAgent)
request.Close = true

response, err := client.HTTPClient.Do(request)
response, err := client.Do(request)
if err != nil {
return "", err
}
Expand Down
115 changes: 115 additions & 0 deletions http/http_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package http

import (
"crypto/tls"
"net/http"
"net/http/cookiejar"
"net/url"
"runtime/debug"
"time"
)

type Client struct {
http.Client
insecureSkipVerify bool
UserAgent string
transport *http.Transport
}

// TODO: global lookup map host -> ratelimit hits left and sleep wait strategy

func NewHTTPClient() *Client {
client := &Client{
Client: http.Client{},
UserAgent: "mrpack-install",
}
client.Client.Jar, _ = cookiejar.New(nil)
info, ok := debug.ReadBuildInfo()
if ok && info.Main.Path != "" {
client.UserAgent = info.Main.Path + "/" + info.Main.Version
}
return client
}

func (client *Client) lazyInit() {
if client.transport == nil {
client.transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
TLSHandshakeTimeout: 20 * time.Second,
DisableKeepAlives: false,
DisableCompression: false, // gzip
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
ResponseHeaderTimeout: 25 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
}
client.Client.Transport = client.transport
}
}

func (client *Client) SetUserAgent(ua string) {
client.UserAgent = ua
}

func (client *Client) SetCookiejar(jar http.CookieJar) {
client.Client.Jar = jar
}

func (client *Client) ResetCookiejar() {
client.Jar, _ = cookiejar.New(nil)
}

func (client *Client) SetProxy(CustomProxy string) error {
client.lazyInit()
proxy, err := url.Parse(CustomProxy)
if err != nil {
return err
}

client.transport.Proxy = http.ProxyURL(proxy)

// Test proxy
httpUrl := "https://api.modrinth.com/"
response, err := client.Get(httpUrl)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return err
}
return nil
}

func (client *Client) SetInsecureSkipVerify(b bool) {
client.lazyInit()
client.transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: b,
}
}

func (client *Client) SetKeepAlive(b bool) {
client.lazyInit()
client.transport.DisableKeepAlives = !b
}

func (client *Client) SetGzip(b bool) {
client.lazyInit()
client.transport.DisableCompression = !b
}

func (client *Client) SetResponseHeaderTimeout(t time.Duration) {
client.lazyInit()
client.transport.ResponseHeaderTimeout = t
}

func (client *Client) SetTLSHandshakeTimeout(t time.Duration) {
client.lazyInit()
client.transport.TLSHandshakeTimeout = t
}

func (client *Client) SetTimeout(t time.Duration) {
client.Client.Timeout = t
}

0 comments on commit 62e801a

Please sign in to comment.