Browse Source

CIDR 지원 추가

https://github.com/openNAMU/openNAMU/issues/1463
잉여개발기 (SPDV) 2 years ago
parent
commit
a3dc688d3a
9 changed files with 51 additions and 21 deletions
  1. 1 0
      app.py
  2. 2 2
      lang/en-US.json
  3. 2 2
      lang/ko-KR.json
  4. 18 7
      route/give_user_ban.py
  5. 0 1
      route/main_tool_admin.py
  6. 0 8
      route/main_tool_redirect.py
  7. 8 0
      route/recent_block.py
  8. 19 0
      route/tool/func.py
  9. 1 1
      version.json

+ 1 - 0
app.py

@@ -445,6 +445,7 @@ app.route('/auth/give/<name>', methods = ['POST', 'GET'])(give_auth)
 
 app.route('/auth/give/ban', methods = ['POST', 'GET'])(give_user_ban)
 app.route('/auth/give/ban/<everything:name>', methods = ['POST', 'GET'])(give_user_ban)
+app.route('/auth/give/ban_cidr/<everything:name>', methods = ['POST', 'GET'], defaults = { 'ban_type' : 'cidr' })(give_user_ban)
 app.route('/auth/give/ban_regex/<everything:name>', methods = ['POST', 'GET'], defaults = { 'ban_type' : 'regex' })(give_user_ban)
 app.route('/auth/give/ban_multiple', methods = ['POST', 'GET'], defaults = { 'ban_type' : 'multiple' })(give_user_ban)
 

+ 2 - 2
lang/en-US.json

@@ -184,7 +184,7 @@
         "encryption_method" : "Encryption method",
         "check_key" : "Check authentication key",
         "reset_user_ok" : "Check Success",
-        "name_or_ip_or_regex" : "Username or IP or Regex",
+        "name_or_ip_or_regex_or_cidr" : "Username or IP or Regex or CIDR",
         "ban_period" : "Period to block",
         "not_sure" : "Not sure",
         "use_push_alarm" : "Enable Push Notification",
@@ -586,7 +586,7 @@
         "default_edit_help" : "Describe it here",
         "markup_enabled" : "Markup enabled",
         "many_delete_help" : "Please write down the documents name one by one on the line.",
-        "name_or_ip_or_regex_multiple" : "Please write down the username or IP or Regex one by one on the line.",
+        "name_or_ip_or_regex_or_cidr_multiple" : "Please write down the username or IP or Regex or CIDR one by one on the line.",
         "sqlite_only" : "SQLite only",
         "linux_only" : "Linux OS only",
         "approval_question_visible_only_when_approval_on" : "Approval questions are visible only when approval requirement is on",

+ 2 - 2
lang/ko-KR.json

@@ -183,7 +183,7 @@
     "adsense_setting": "애드센스 설정",
     "smtp_username": "SMTP 아이디",
     "previous": "이전",
-    "name_or_ip_or_regex": "사용자 이름 혹은 IP 주소 혹은 정규표현식",
+    "name_or_ip_or_regex_or_cidr": "사용자 이름 혹은 IP 주소 혹은 정규표현식 혹은 CIDR",
     "editor": "편집자",
     "wiki_secret_key": "위키 비밀 키",
     "band_ban": "대역 차단",
@@ -457,7 +457,7 @@
     "set_wiki_access_password": "위키 접속 비밀번호",
     "set_history_recording_off": "역사 기록 중지",
     "multiple_ban": "다중 차단",
-    "name_or_ip_or_regex_multiple": "한 줄에 IP나 정규식을 한 개씩 적어주세요.",
+    "name_or_ip_or_regex_or_cidr_multiple": "한 줄에 IP나 정규표현이나 CIDR을 한 개씩 적어주세요.",
     "dont_move": "이동하지 않음",
     "stiemap_exclude_domain": "도메인 제외",
     "stiemap_exclude_user_page": "사용자 문서 제외",

+ 18 - 7
route/give_user_ban.py

@@ -20,7 +20,7 @@ def give_user_ban(name = None, ban_type = ''):
             else:
                 end = '0'
             
-            regex_get = flask.request.form.get('regex', None)
+            regex_get = flask.request.form.get('do_ban_type', '')
             why = flask.request.form.get('why', '')
 
             release = ''
@@ -43,18 +43,25 @@ def give_user_ban(name = None, ban_type = ''):
                     all_user = [flask.request.form.get('name', 'test')]
 
             for name in all_user:
-                if regex_get or ban_type == 'regex':
-                    type_d = 'regex' if regex_get else ban_type
+                if regex_get == 'regex':
+                    type_d = 'regex'
 
                     try:
                         re.compile(name)
                     except:
                         return re_error(conn, '/error/23')
+                elif regex_get == 'cidr':
+                    type_d = 'cidr'
+
+                    try:
+                        ipaddress.IPv4Network(name, False)
+                    except:
+                        return re_error(conn, '/error/45')
                 else:
                     type_d = None
 
                 if type_d:
-                    if admin_check(conn, None, 'ban' + (' ' + type_d if type_d else '') + ' (' + name + ')') != 1:
+                    if admin_check(conn, None, 'ban ' + type_d + ' (' + name + ')') != 1:
                         return re_error(conn, '/error/3')
                 else:
                     if name == ip:
@@ -78,10 +85,10 @@ def give_user_ban(name = None, ban_type = ''):
         else:
             if ban_type == 'multiple':
                 main_name = get_lang(conn, 'multiple_ban')
-                n_name = '<textarea class="opennamu_textarea_500" placeholder="' + get_lang(conn, 'name_or_ip_or_regex_multiple') + '" name="name"></textarea><hr class="main_hr">'
+                n_name = '<textarea class="opennamu_textarea_500" placeholder="' + get_lang(conn, 'name_or_ip_or_regex_or_cidr_multiple') + '" name="name"></textarea><hr class="main_hr">'
             else:
                 main_name = get_lang(conn, 'ban')
-                n_name = '<input placeholder="' + get_lang(conn, 'name_or_ip_or_regex') + '" value="' + (name if name else '') + '" name="name"><hr class="main_hr">'
+                n_name = '<input placeholder="' + get_lang(conn, 'name_or_ip_or_regex_or_cidr') + '" value="' + (name if name else '') + '" name="name"><hr class="main_hr">'
 
             now = 0
             
@@ -108,7 +115,11 @@ def give_user_ban(name = None, ban_type = ''):
                 data = info_data + '''
                     <form method="post" ''' + action + '''>
                         ''' + n_name + '''
-                        <input type="checkbox" name="regex" ''' + ('checked' if ban_type == 'regex' else '') + '> ' + get_lang(conn, 'regex') + '''
+                        <select name="do_ban_type">
+                            <option value="normal">''' + get_lang(conn, 'normal') + '''</option>
+                            <option value="regex" ''' + ('selected' if ban_type == 'regex' else '') + '>' + get_lang(conn, 'regex') + '''</option>
+                            <option value="cidr" ''' + ('selected' if ban_type == 'cidr' else '') + '>' + get_lang(conn, 'cidr') + '''</option>
+                        </select>
                         <hr class="main_hr">
                         <input type="date" value="''' + date_value + '''" name="date" pattern="\\d{4}-\\d{2}-\\d{2}">
                         <hr class="main_hr">

+ 0 - 1
route/main_tool_admin.py

@@ -11,7 +11,6 @@ def main_tool_admin():
                     <li><a href="/manager/3">''' + get_lang(conn, 'check_user') + '''</a></li>
                     <li><a href="/auth/give/ban">''' + get_lang(conn, 'ban') + '''</a></li>
                     <li><a href="/auth/give/ban_multiple">''' + get_lang(conn, 'multiple_ban') + '''</a></li>
-                    <li><a href="/manager/17">''' + get_lang(conn, 'release') + '''</a></li>
                     <li><a href="/manager/5">''' + get_lang(conn, 'authorize') + '''</a></li>
                 </ul>
                 <h2>''' + get_lang(conn, 'owner') + '''</h2>

+ 0 - 8
route/main_tool_redirect.py

@@ -19,7 +19,6 @@ def main_tool_redirect(num = 1, add_2 = ''):
             12 : [get_lang(conn, 'compare_target'), '/list/user/check', get_lang(conn, 'compare_target')],
             13 : [get_lang(conn, 'document_name'), '/edit', get_lang(conn, 'load')],
             14 : [get_lang(conn, 'document_name'), '/star_doc', get_lang(conn, 'add_star_doc')],
-            15 : [get_lang(conn, 'name_or_ip_or_regex'), '/auth/give/ban', get_lang(conn, 'release')],
             16 : [0, '/auth/give/fix', get_lang(conn, 'user_fix')],
         }
         
@@ -36,8 +35,6 @@ def main_tool_redirect(num = 1, add_2 = ''):
                         return redirect(conn, '/edit_from/' + url_pas(add_2))
                     else:
                         return redirect(conn, title_list[num][1] + '/' + url_pas(add_2) + '/normal/1/' + url_pas(add_1))
-                elif flask.request.form.get('regex', '') != '':
-                    return redirect(conn, '/auth/give/ban_regex/' + url_pas(add_1))
                 else:
                     return redirect(conn, title_list[num][1] + '/' + url_pas(add_1))
             else:
@@ -46,10 +43,6 @@ def main_tool_redirect(num = 1, add_2 = ''):
                 else:
                     placeholder = title_list[num][0]
 
-                plus = ''
-                if num == 15:
-                    plus = '<input type="checkbox" name="regex"> ' + get_lang(conn, 'regex') + '<hr class="main_hr">'
-
                 top_plus = ''
                 if num == 13:
                     curs.execute(db_change("select html, plus from html_filter where kind = 'template'"))
@@ -67,7 +60,6 @@ def main_tool_redirect(num = 1, add_2 = ''):
                             ''' + top_plus + '''
                             <input placeholder="''' + placeholder + '''" id="data_field" name="name" type="text">
                             <hr class="main_hr">
-                            ''' + plus + '''
                             <button type="submit">''' + get_lang(conn, 'go') + '''</button>
                         </form>
                     ''',

+ 8 - 0
route/recent_block.py

@@ -66,6 +66,14 @@ def recent_block(name = 'Test', tool = 'all', num = 1):
                     ip += ' <a href="/auth/give/ban_regex/' + url_pas(data[1]) + '">(' + get_lang(conn, 'ban') + ')</a>'
 
                 ip += ' (' + get_lang(conn, 'regex') + ')'
+            elif data[5] == 'cidr':
+                ip = data[1]
+                if data[6] == '1':
+                    ip = '<s>' + ip + '</s> <a href="/auth/give/ban_cidr/' + url_pas(data[1]) + '">(' + get_lang(conn, 'release') + ')</a>'
+                else:
+                    ip += ' <a href="/auth/give/ban_cidr/' + url_pas(data[1]) + '">(' + get_lang(conn, 'ban') + ')</a>'
+
+                ip += ' (' + get_lang(conn, 'cidr') + ')'
             else:
                 ip = all_ip[data[1]]
 

+ 19 - 0
route/tool/func.py

@@ -2135,6 +2135,7 @@ def ban_check(conn, ip = None, tool = ''):
     regex_d = curs.fetchall()
     for test_r in regex_d:
         ban_type = ban_type_check(test_r[0])
+
         g_regex = re.compile(test_r[1])
         if g_regex.search(ip):
             if tool == 'login':
@@ -2145,6 +2146,22 @@ def ban_check(conn, ip = None, tool = ''):
                     return [1, 'a' + ban_type]
             else:
                 return [1, 'a' + ban_type]
+            
+    curs.execute(db_change("select login, block from rb where band = 'cidr' and ongoing = '1'"))
+    regex_d = curs.fetchall()
+    for test_r in regex_d:
+        ban_type = ban_type_check(test_r[0])
+        
+        cidr_list = [str(ip) for ip in ipaddress.IPv4Network(test_r[1], False)]
+        if ip in cidr_list:
+            if tool == 'login':
+                if ban_type != '1':
+                    return [1, 'b' + ban_type]
+            elif tool == 'edit_request':
+                if ban_type != '2':
+                    return [1, 'b' + ban_type]
+            else:
+                return [1, 'b' + ban_type]
 
     curs.execute(db_change("select login from rb where block = ? and band = '' and ongoing = '1'"), [ip])
     ban_d = curs.fetchall()
@@ -2709,6 +2726,8 @@ def re_error(conn, data):
             db_data = curs.fetchall()
             db_data = '' if not db_data else db_data[0][0]
             data = get_lang(conn, 'error_content_length_too_long') + db_data
+        elif num == 45:
+            data = get_lang(conn, 'cidr_error')
         else:
             data = '???'
 

+ 1 - 1
version.json

@@ -1,6 +1,6 @@
 {
     "beta" : {
-        "r_ver" : "v3.5.0-dev94",
+        "r_ver" : "v3.5.0-dev95",
         "c_ver" : "3500379",
         "s_ver" : "3500113"
     }