first versiom

This commit is contained in:
2022-02-23 10:58:31 +08:00
parent f95b46d8a4
commit bb8a233a40
11 changed files with 304 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

9
.idea/gh-proxy.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,8 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="GoCommentLeadingSpace" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="GoCommentStart" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="HttpUrlsUsage" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
</profile>
</component>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/gh-proxy.iml" filepath="$PROJECT_DIR$/.idea/gh-proxy.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

29
.idea/watcherTasks.xml generated Normal file
View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<TaskOptions isEnabled="true">
<option name="arguments" value="fmt $FilePath$" />
<option name="checkSyntaxErrors" value="true" />
<option name="description" />
<option name="exitCodeBehavior" value="ERROR" />
<option name="fileExtension" value="go" />
<option name="immediateSync" value="false" />
<option name="name" value="go fmt" />
<option name="output" value="$FilePath$" />
<option name="outputFilters">
<array />
</option>
<option name="outputFromStdout" value="false" />
<option name="program" value="$GoExecPath$" />
<option name="runOnExternalChanges" value="false" />
<option name="scopeName" value="Project Files" />
<option name="trackOnlyRoot" value="true" />
<option name="workingDir" value="$ProjectFileDir$" />
<envs>
<env name="GOROOT" value="$GOROOT$" />
<env name="GOPATH" value="$GOPATH$" />
<env name="PATH" value="$GoBinDirs$" />
</envs>
</TaskOptions>
</component>
</project>

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module gh-proxy
go 1.17

136
main.go Normal file
View File

@@ -0,0 +1,136 @@
package main
import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
)
const (
PREFIX = "https://gh.ncc.cx/"
exp1 = `^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$`
exp2 = `^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob|raw)\/.*$`
exp3 = `^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$`
exp4 = `^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$`
exp5 = `^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$`
exp6 = `^(?:https?:\/\/)?github\.com\/.+?\/.+?\/tags.*$`
)
func checkUrl(u string) bool {
for _, exp := range []string{exp1, exp2, exp3, exp4, exp5, exp6} {
if ok, e := regexp.MatchString(exp, u); ok {
return true
} else if e != nil {
fmt.Println(e)
}
}
return false
}
func fetchHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if !strings.Contains(path, "/") {
w.WriteHeader(http.StatusNotFound)
return
}
path = strings.TrimPrefix(path, "/") // 获取到的URL
path = strings.Replace(path, "https:/", "https://", 1)
u, _ := url.Parse(path)
match1, _ := regexp.MatchString(exp1, u.String())
match2, _ := regexp.MatchString(exp2, u.String())
match3, _ := regexp.MatchString(exp3, u.String())
match4, _ := regexp.MatchString(exp4, u.String())
match5, _ := regexp.MatchString(exp5, u.String())
match6, _ := regexp.MatchString(exp6, u.String())
if match1 || match5 || match6 || match3 || match4 {
httpHandler(w, r, path)
return
} else if match2 {
path = strings.ReplaceAll(u.Path, "/blob/", "/raw/")
httpHandler(w, r, path)
return
}
http.Error(w, fmt.Sprintf("%s is not support", path), http.StatusOK)
}
func httpHandler(w http.ResponseWriter, r *http.Request, pathname string) {
reqHdrRaw := r.Header
if r.Method == http.MethodOptions && reqHdrRaw.Get("access-control-request-headers") != "" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, TRACE, DELETE, HEAD, OPTIONS")
w.Header().Set("Access-Control-Max-Age", "1728000")
w.WriteHeader(http.StatusNoContent)
return
}
if strings.HasPrefix(pathname, "github") {
pathname = "https://" + pathname
}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
proxy(w, r, client, pathname)
}
func proxy(w http.ResponseWriter, r *http.Request, client *http.Client, pathname string) {
r.URL, _ = url.Parse(pathname)
req, _ := http.NewRequest(r.Method, r.URL.String(), r.Body)
for k, v := range r.Header {
req.Header.Set(k, v[0])
}
for _, value := range r.Cookies() {
w.Header().Add(value.Name, value.Value)
}
res, e := client.Do(req)
if e != nil {
http.Error(w, e.Error(), http.StatusOK)
return
}
if res.Header.Get("location") != "" {
location := res.Header.Get("location")
if checkUrl(location) {
res.Header.Set("location", PREFIX+location)
} else {
newReq, _ := http.NewRequest(r.Method, location, res.Body)
proxy(w, newReq, http.DefaultClient, location)
return
}
}
res.Header.Set("Access-Control-Expose-Headers", "*")
res.Header.Set("Access-Control-Allow-Origin", "*")
res.Header.Del("Content-Security-Policy")
res.Header.Del("Content-Security-Policy-Report-Only")
res.Header.Del("Clear-Site-Data")
w.WriteHeader(res.StatusCode)
for k, v := range res.Header {
w.Header().Set(k, strings.Join(v, ","))
}
_ = res.Write(w)
}
func main() {
http.HandleFunc("/", fetchHandler)
err := http.ListenAndServe(fmt.Sprintf(":%d", 8091), nil)
if err != nil {
fmt.Println("run server error: ", err)
return
}
}

49
regexp_test.go Normal file
View File

@@ -0,0 +1,49 @@
package main
import "testing"
func TestExp(t *testing.T) {
arr := []struct {
Url string
Match bool
}{
{
Url: "https://github.com/hunshcn/project/archive/master.zip",
Match: true,
},
{
Url: "https://github.com/hunshcn/project/archive/v0.1.0.tar.gz",
Match: true,
},
{
Url: "https://github.com/hunshcn/project/releases/download/v0.1.0/example.zip",
Match: true,
},
{
Url: "https://github.com/hunshcn/project/blob/master/filename",
Match: true,
},
{
Url: "https://github.com/hunshcn/project/blob/1111111111111111111111111111/filename",
Match: true,
},
{
Url: "https://raw.githubusercontent.com/stilleshan/ServerStatus/master/Dockerfile",
Match: true,
},
{
Url: "https://baidu.com",
Match: false,
},
{
Url: "https://git.aweoo.com/fghwett/notepad.git",
Match: false,
},
}
for _, v := range arr {
if v.Match != checkUrl(v.Url) {
t.Errorf("%s is not match, except: %t", v.Url, v.Match)
}
}
}

21
server.crt Normal file
View File

@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDeDCCAmACCQCf38ooVlcvWjANBgkqhkiG9w0BAQsFADB+MQswCQYDVQQGEwJD
TjEPMA0GA1UECAwGRnVqaWFuMQ8wDQYDVQQHDAZYaWFtZW4xDTALBgNVBAoMBFRl
c3QxDTALBgNVBAsMBFRlc3QxFzAVBgNVBAMMDjEyNy4wLjAuMTo4MDkxMRYwFAYJ
KoZIhvcNAQkBFgdpQGNjLmJpMB4XDTIyMDIyMzAyNTMwNloXDTIzMDIyMzAyNTMw
NlowfjELMAkGA1UEBhMCQ04xDzANBgNVBAgMBkZ1amlhbjEPMA0GA1UEBwwGWGlh
bWVuMQ0wCwYDVQQKDARUZXN0MQ0wCwYDVQQLDARUZX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