Skip to content

Commit

Permalink
feat: allow any url
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotiaWang committed Apr 7, 2024
1 parent 041a77a commit 34f1c2a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# 使用官方 Go 基础镜像,这里可以指定 Go 版本
FROM golang:1.22-alpine AS builder

# 设置工作目录
Expand Down Expand Up @@ -27,4 +26,4 @@ COPY --from=builder /app/main .
EXPOSE 80

# 运行可执行文件
CMD ["./main"]
ENTRYPOINT ["./main"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ A simple Go proxy for GitHub API, but MUCH lighter. Ported & modified from [huns
### Docker

```bash
# 直接运行,只允许代理 GitHub 链接
docker run -d -p 80:80 --name gh-proxy-go anotia/gh-proxy-go
# 允许代理任意链接
docker run -d -p 80:80 --name gh-proxy-go anotia/gh-proxy-go --allow-any-url
```

### 命令行运行
Expand All @@ -24,4 +27,7 @@ docker run -d -p 80:80 --name gh-proxy-go anotia/gh-proxy-go

# 修改端口 / 地址
./gh-proxy-go --port 8080 --host 127.0.0.1

# 允许代理任意链接
./gh-proxy-go --allow-any-url
```
7 changes: 3 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@
<h1 style="margin-bottom: 50px">GitHub 文件加速</h1>
<form action="./" method="get" style="padding-bottom: 40px" target="_blank" class="flex" onsubmit="toSubmit(event)">
<label class="block" style="width: fit-content">
<input class="block url" name="q" type="text" placeholder="输入 GitHub 文件链接"
pattern="^((https|http):\/\/)?(github\.com\/.+?\/.+?\/(?:releases|archive|blob|raw|suites)|((?:raw|gist)\.(?:githubusercontent|github)\.com))\/.+$"
required>
<input class="block url" name="q" type="text" placeholder="输入 GitHub 文件链接" required>
<div class="bar"></div>
</label>
<input class="block btn" type="submit" value="下载">
Expand All @@ -147,7 +145,8 @@ <h1 style="margin-bottom: 50px">GitHub 文件加速</h1>
</div>
</form>
<p style="position: sticky;top: calc(100% - 2.5em);">项目使用 Go 编写,开源于 <a style="color: #3294ea"
href="https://github.com/AnotiaWang/gh-proxy-go">GitHub</a>,改编自 <a style="color: #3294ea" href="https://github.com/hunshcn/gh-proxy">hunshcn/gh-proxy</a>
href="https://github.com/AnotiaWang/gh-proxy-go">GitHub</a>,改编自 <a style="color: #3294ea"
href="https://github.com/hunshcn/gh-proxy">hunshcn/gh-proxy</a>
</p>
</body>

Expand Down
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ var (
jsdelivr = 0
// whiteList = []string{}
// blackList = []string{}
passList = []string{}
exp1 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:releases|archive)/.*$`)
exp2 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:blob|raw)/.*$`)
exp3 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:info|git-).*$`)
exp4 = regexp.MustCompile(`^(?:https?://)?raw\.(?:githubusercontent|github)\.com/(?P<author>.+?)/(?P<repo>.+?)/.+?/.+$`)
exp5 = regexp.MustCompile(`^(?:https?://)?gist\.(?:githubusercontent|github)\.com/(?P<author>.+?)/.+?/.+$`)
exp6 = regexp.MustCompile(`(\.com/.*?/.+?)/(.+?/)`)
passList = []string{}
exp1 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:releases|archive)/.*$`)
exp2 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:blob|raw)/.*$`)
exp3 = regexp.MustCompile(`^(?:https?://)?github\.com/(?P<author>.+?)/(?P<repo>.+?)/(?:info|git-).*$`)
exp4 = regexp.MustCompile(`^(?:https?://)?raw\.(?:githubusercontent|github)\.com/(?P<author>.+?)/(?P<repo>.+?)/.+?/.+$`)
exp5 = regexp.MustCompile(`^(?:https?://)?gist\.(?:githubusercontent|github)\.com/(?P<author>.+?)/.+?/.+$`)
exp6 = regexp.MustCompile(`(\.com/.*?/.+?)/(.+?/)`)
allowAnyUrl bool
)

func main() {
Expand All @@ -35,6 +36,7 @@ func main() {

flag.StringVar(&host, "host", "0.0.0.0", "Host address")
flag.StringVar(&port, "port", "80", "Port number")
flag.BoolVar(&allowAnyUrl, "allow-any-url", false, "Do not check if the URL is a GitHub URL")
flag.Parse()

hostRegExp := regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+$`)
Expand Down Expand Up @@ -80,6 +82,11 @@ func main() {
handler(c, path)
})

if allowAnyUrl {
log.Println("Mode: Allow any URL")
} else {
log.Println("Mode: GitHub URL only")
}
log.Println("Starting server at " + host + ":" + port)

err := router.Run(host + ":" + port)
Expand Down Expand Up @@ -107,7 +114,7 @@ func handler(c *gin.Context, u string) {
break
}
}
} else {
} else if !allowAnyUrl {
c.String(http.StatusForbidden, "Invalid input.")
return
}
Expand Down

0 comments on commit 34f1c2a

Please sign in to comment.