Skip to content

Commit

Permalink
feat(github): support github proxy (AlistGo#7979 close AlistGo#7963)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirCute authored Feb 16, 2025
1 parent f25be15 commit 36b4204
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
17 changes: 14 additions & 3 deletions drivers/github/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ func (d *Github) Init(ctx context.Context) error {
}
d.client = base.NewRestyClient().
SetHeader("Accept", "application/vnd.github.object+json").
SetHeader("Authorization", "Bearer "+d.Token).
SetHeader("X-GitHub-Api-Version", "2022-11-28").
SetLogger(log.StandardLogger()).
SetDebug(false)
token := strings.TrimSpace(d.Token)
if token != "" {
d.client = d.client.SetHeader("Authorization", "Bearer "+token)
}
if d.Ref == "" {
repo, err := d.getRepo()
if err != nil {
Expand Down Expand Up @@ -149,8 +152,13 @@ func (d *Github) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
if obj.Type == "submodule" {
return nil, errors.New("cannot download a submodule")
}
url := obj.DownloadURL
ghProxy := strings.TrimSpace(d.Addition.GitHubProxy)
if ghProxy != "" {
url = strings.Replace(url, "https://raw.githubusercontent.com", ghProxy, 1)
}
return &model.Link{
URL: obj.DownloadURL,
URL: url,
}, nil
}

Expand Down Expand Up @@ -679,8 +687,11 @@ func (d *Github) putBlob(ctx context.Context, s model.FileStreamer, up driver.Up
return "", err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", "Bearer "+d.Token)
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
token := strings.TrimSpace(d.Token)
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
req.ContentLength = length

res, err := base.HttpClient.Do(req)
Expand Down
3 changes: 2 additions & 1 deletion drivers/github/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (

type Addition struct {
driver.RootPath
Token string `json:"token" type:"string" required:"true"`
Token string `json:"token" type:"string"`
Owner string `json:"owner" type:"string" required:"true"`
Repo string `json:"repo" type:"string" required:"true"`
Ref string `json:"ref" type:"string" help:"A branch, a tag or a commit SHA, main branch by default."`
GitHubProxy string `json:"gh_proxy" type:"string" help:"GitHub proxy, e.g. https://ghproxy.net/raw.githubusercontent.com or https://gh-proxy.com/raw.githubusercontent.com"`
CommitterName string `json:"committer_name" type:"string"`
CommitterEmail string `json:"committer_email" type:"string"`
AuthorName string `json:"author_name" type:"string"`
Expand Down

0 comments on commit 36b4204

Please sign in to comment.