liuyuqi-dellpc 1 year ago
parent
commit
0d09e0ac28
4 changed files with 46 additions and 4 deletions
  1. 23 0
      docs/Develop.md
  2. 5 3
      fgh/api/home.py
  3. 6 1
      fgh/utils/proxy.py
  4. 12 0
      fgh/utils/regex_util.py

+ 23 - 0
docs/Develop.md

@@ -0,0 +1,23 @@
+# Develop
+
+## Introduction
+
+基于flask,提供如下接口:
+
+```
+@home.route('/<path:u>', methods=['GET', 'POST'])
+def handler(u):
+```
+对于任意的url,都会调用handler函数,参数u为url的path部分。如访问: https://localhost:8080/https://github.com/xx/yy.git 时,u为 https://github.com/xx/yy.git
+
+### 三种模式:
+
+白名单:只有在白名单中的url才会被处理,其他的url会返回404
+黑名单:只有不在黑名单中的url才会被处理
+pass_py: 替换为 cdn 链接,不处理
+
+
+
+
+
+

+ 5 - 3
fgh/api/home.py

@@ -40,10 +40,12 @@ def index():
 
 @home.route('/<path:u>', methods=['GET', 'POST'])
 def handler(u):
-    # 确保它是以 'http://' 或 'https://' 开头
+    # 自动加上https://
     u = u if u.startswith('http') else 'https://' + u
     if u.rfind('://', 3, 9) == -1:
-        u = u.replace('s:/', 's://', 1)  # uwsgi会将//传递为/
+        u = u.replace('s:/', 's://', 1)  # uwsgi 会将//传递为/
+    # save the original url to db
+    
     pass_by = False
     m = check_url(u)
     if m:
@@ -52,7 +54,7 @@ def handler(u):
             for i in white_list:
                 if m[:len(i)] == i or i[0] == '*' and len(m) == 2 and m[1] == i[1]:
                     break
-            else:
+            else:  # 如果 for 循环完成而没有break被执行,那么会执行else
                 return Response('Forbidden by white list.', status=403)
         for i in black_list:
             if m[:len(i)] == i or i[0] == '*' and len(m) == 2 and m[1] == i[1]:

+ 6 - 1
fgh/utils/proxy.py

@@ -26,6 +26,10 @@ CHUNK_SIZE = 1024 * 10
 
 
 def check_url(u):
+    """ 检测URL是否GitHub链接
+    :param u: URL
+    :return: 匹配到返回 match object,否则返回 False
+    """
     for exp in (exp1, exp2, exp3, exp4, exp5):
         m = exp.match(u)
         if m:
@@ -92,7 +96,8 @@ def proxy(u, allow_redirects=False):
         url = u + request.url.replace(request.base_url, '', 1)
         if url.startswith('https:/') and not url.startswith('https://'):
             url = 'https://' + url[7:]
-        r = requests.request(method=request.method, url=url, data=request.data, headers=r_headers, stream=True, allow_redirects=allow_redirects)
+        r = requests.request(method=request.method, url=url, data=request.data, 
+                             headers=r_headers, stream=True, allow_redirects=allow_redirects)
         headers = dict(r.headers)
         
         # 是否超过设定的 size_limit,如果超过则重定向到原始URL

+ 12 - 0
fgh/utils/regex_util.py

@@ -20,3 +20,15 @@ exp4 = re.compile(r'^(?:https?://)?raw\.(?:githubusercontent|github)\.com/(?P<au
 exp5 = re.compile(r'^(?:https?://)?gist\.(?:githubusercontent|github)\.com/(?P<author>.+?)/.+?/.+$')
 
 
+if __name__=='__main__':
+    github_str = {
+        "str0": r'http://github.com/jianboy/test1/releases/tag/v1.0',
+        "str1": r'https://github.com/jianboy/test1/releases/a.zip',
+        "str2": r'https://github.com/author/repo/blob'
+    }
+
+    for k, v in github_str.items():
+        i=1
+        for exp in [exp1, exp2, exp3, exp4, exp5]:
+            print(k + ", exp"+ str(i), exp.match(v))
+            i+=1