Skip to content

Commit eb983f7

Browse files
committed
feat: 完成github代理核心功能
1 parent 11b61ba commit eb983f7

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
### Example user template template
2-
### Example user template
3-
41
# IntelliJ project files
52
.idea
63
*.iml
74
out
85
gen
6+
97
### Go template
108
# Binaries for programs and plugins
119
*.exe

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module gh
2+
3+
go 1.17
4+
5+
require github.com/creack/pty v1.1.17

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
2+
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=

main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/creack/pty"
6+
"io"
7+
"os"
8+
"os/exec"
9+
"runtime"
10+
"strings"
11+
)
12+
13+
func isWindows() bool {
14+
flag := runtime.GOOS == "windows"
15+
return flag
16+
}
17+
18+
func execCommand(cmd string) {
19+
args := [2]string{
20+
"bash", "-c",
21+
}
22+
if isWindows() {
23+
args[0] = "cmd"
24+
args[1] = "/C"
25+
}
26+
c := exec.Command(args[0], args[1], cmd)
27+
f, err := pty.Start(c)
28+
if err != nil {
29+
panic(err)
30+
}
31+
_, err = io.Copy(os.Stdout, f)
32+
if err != nil {
33+
return
34+
}
35+
}
36+
37+
func githubProxy() {
38+
var cmds = os.Args[1:]
39+
for i := 1; i < len(cmds); i++ {
40+
cmd := cmds[i]
41+
if strings.Contains(cmd, "https://github.com") {
42+
cmds[i] = strings.ReplaceAll(cmd, "https://github.com", "https://ghproxy.com/https://github.com")
43+
}
44+
45+
if strings.Contains(cmd, "https://raw.githubusercontent.com") {
46+
cmds[i] = strings.ReplaceAll(cmd, "https://raw.githubusercontent.com", "https://ghproxy.com/https://raw.githubusercontent.com")
47+
}
48+
49+
if strings.Contains(cmd, "https://gist.github.com") {
50+
cmds[i] = strings.ReplaceAll(cmd, "https://gist.github.com", "https://ghproxy.com/https://gist.github.com")
51+
}
52+
}
53+
54+
cmd := strings.Join(cmds, " ")
55+
fmt.Printf("👉 : %v\n", cmd)
56+
execCommand(cmd)
57+
}
58+
59+
func main() {
60+
githubProxy()
61+
}

0 commit comments

Comments
 (0)