Skip to content
Open
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
12 changes: 12 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
Expand All @@ -24,6 +25,7 @@ import (

var dependencies = Dependencies{}
var keychainPassphrase string
var proxyUrl string

type Dependencies struct {
Logger log.Logger
Expand Down Expand Up @@ -107,11 +109,21 @@ func initWithCommand(cmd *cobra.Command) {
dependencies.Machine = machine.New(machine.Args{OS: dependencies.OS})
dependencies.CookieJar = newCookieJar(dependencies.Machine)
dependencies.Keychain = newKeychain(dependencies.Machine, dependencies.Logger, interactive)
var proxyUrlParsed *url.URL
if proxyUrl != "" {
parsedUrl, err := url.Parse(proxyUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "error: invalid proxy url: %v\n", proxyUrl)
os.Exit(1)
}
proxyUrlParsed = parsedUrl
}
dependencies.AppStore = appstore.NewAppStore(appstore.Args{
CookieJar: dependencies.CookieJar,
OperatingSystem: dependencies.OS,
Keychain: dependencies.Keychain,
Machine: dependencies.Machine,
ProxyUrl: proxyUrlParsed,
})

util.Must("", createConfigDirectory(dependencies.OS, dependencies.Machine))
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func rootCmd() *cobra.Command {
cmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "enables verbose logs")
cmd.PersistentFlags().BoolVarP(&nonInteractive, "non-interactive", "", false, "run in non-interactive session")
cmd.PersistentFlags().StringVar(&keychainPassphrase, "keychain-passphrase", "", "passphrase for unlocking keychain")
cmd.PersistentFlags().StringVar(&proxyUrl, "proxy", "", "url of a HTTP proxy to be used")

cmd.AddCommand(authCmd())
cmd.AddCommand(downloadCmd())
Expand Down
5 changes: 4 additions & 1 deletion pkg/appstore/appstore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package appstore

import (
"net/url"

"github.com/majd/ipatool/v2/pkg/http"
"github.com/majd/ipatool/v2/pkg/keychain"
"github.com/majd/ipatool/v2/pkg/util/machine"
Expand Down Expand Up @@ -47,13 +49,14 @@ type Args struct {
CookieJar http.CookieJar
OperatingSystem operatingsystem.OperatingSystem
Machine machine.Machine
ProxyUrl *url.URL
}

func NewAppStore(args Args) AppStore {
clientArgs := http.Args{
CookieJar: args.CookieJar,
ProxyUrl: args.ProxyUrl,
}

return &appstore{
keychain: args.Keychain,
loginClient: http.NewClient[loginResult](clientArgs),
Expand Down
10 changes: 9 additions & 1 deletion pkg/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"

"howett.net/plist"
Expand All @@ -29,6 +30,7 @@ type client[R interface{}] struct {

type Args struct {
CookieJar CookieJar
ProxyUrl *url.URL
}

type AddHeaderTransport struct {
Expand All @@ -49,6 +51,12 @@ func (t *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error
}

func NewClient[R interface{}](args Args) Client[R] {
var proxy http.RoundTripper
if args.ProxyUrl != nil {
proxy = &http.Transport{Proxy: http.ProxyURL(args.ProxyUrl)}
} else {
proxy = http.DefaultTransport
}
return &client[R]{
internalClient: http.Client{
Timeout: 0,
Expand All @@ -60,7 +68,7 @@ func NewClient[R interface{}](args Args) Client[R] {

return nil
},
Transport: &AddHeaderTransport{http.DefaultTransport},
Transport: &AddHeaderTransport{proxy},
},
cookieJar: args.CookieJar,
}
Expand Down