Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
hoochanlon committed Dec 7, 2024
1 parent a3059b1 commit 0ea0792
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 124 deletions.
39 changes: 39 additions & 0 deletions IP地址规划计算器v1.3.1.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- mode: python ; coding: utf-8 -*-


a = Analysis(
['C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\main.py'],
pathex=[],
binaries=[],
datas=[('C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\docs', 'docs'), ('C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\images', 'images')],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
name='IP地址规划计算器v1.3.1',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=['C:\\Users\\胡成龙\\Desktop\\Network-Calculator\\images\\logo.ico'],
)
4 changes: 2 additions & 2 deletions core/ip_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
print(sys.path)
import ipaddress
import re
from network_class_utils import get_network_class_by_cidr
from core.network_class_utils import get_network_class_by_cidr

# 根据IP地址和子网掩码计算网络和IP信息
def calculate_ip_info(ip_address, subnet_mask):
Expand Down Expand Up @@ -55,7 +55,7 @@ def calculate_ip_info(ip_address, subnet_mask):
reverse_subnet_mask = str(ipaddress.IPv4Address(int(ipaddress.IPv4Address(subnet_mask)) ^ 0xFFFFFFFF))

# 网络类别
network_class = get_network_class_by_cidr(cidr)
network_class = get_network_class_by_cidr(f"{ip_address}{cidr}")

# 总IP数量的计算方法
total_ips = 2 ** (32 - network.prefixlen)
Expand Down
79 changes: 51 additions & 28 deletions core/network_class_utils.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
# network_class_utils.py
import re
import ipaddress

# 根据CIDR位数判断网络类别
# 根据CIDR位数和地址判断网络类别
def get_network_class_by_cidr(cidr):
print(cidr)
# 验证CIDR格式
validation_result = validate_cidr(cidr)
print(f"CIDR验证结果: {validation_result}") # 打印验证结果
if validation_result != "valid":
return validation_result

# 如果CIDR只给了位数(如 /24),则假设一个默认IP地址
if not cidr.startswith('192.168.1.0'):
cidr = f"192.168.1.0{cidr}"

# 提取CIDR中的子网掩码部分(即位数)
cidr_bits = int(cidr.split('/')[1])

# 判断网络类别
if 0 <= cidr_bits <= 7:
return "Any" # /7以下
elif 8 <= cidr_bits <= 15:
return "A类" # /8 到 /15
elif 16 <= cidr_bits <= 23:
return "B类" # /16 到 /23
elif 24 <= cidr_bits <= 32:
return "C类" # /24 到 /32
# 处理CIDR格式,确保IP是网络地址而不是主机地址
if '/' in cidr:
ip, prefix = cidr.split('/')
print(f"IP地址: {ip}, 子网掩码位数: {prefix}") # 打印拆分后的IP地址和子网掩码位数
network = ipaddress.IPv4Network(f"{ip}/{prefix}", strict=False)
else:
return "无效的CIDR"
print(f"没有子网掩码,默认使用 /16 位数")
network = ipaddress.IPv4Network(f"{cidr}/16", strict=False) # 默认 /16

# 输出网络地址信息
print(f"网络地址: {network.network_address}, 网络范围: {network}") # 打印网络地址

# 判断IP地址所在类别
ip = str(network.network_address)
print(f"判断的IP: {ip}")

if ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("10.0.0.0/8"):
return "A类(私有)"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("172.16.0.0/12"):
return "B类(私有)"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("192.168.0.0/16"):
return "C类(私有)"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("127.0.0.0/8"):
return "Localhost"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("169.254.0.0/16"):
return "ZeroConf (Apipa/Bonjour)"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("100.64.0.0/10"):
return "Internal routing (RFC 6598)"

# 判断A、B、C、D、E类
if ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("0.0.0.0/8"):
return "A类"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("128.0.0.0/8"):
return "B类"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("192.0.0.0/8"):
return "C类"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("224.0.0.0/4"):
return "D类"
elif ipaddress.IPv4Address(ip) in ipaddress.IPv4Network("240.0.0.0/4"):
return "E类"

return "无效的CIDR范围"

# 验证CIDR格式
def validate_cidr(cidr):
# 检查CIDR是否符合格式,例如:10.0.0.0/8 或 /24
cidr_pattern = re.compile(r"^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$|^/(\d{1,2})$")
# 改进的正则表达式,确保匹配完整的IP地址(0-255)
cidr_pattern = re.compile(r"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/([0-9]|[1-2][0-9]|3[0-2])$")
if not cidr_pattern.match(cidr):
return "无效的CIDR格式"

# 如果是完整的CIDR,验证IP和CIDR位数是否合法
if cidr.startswith('192.168.1.0'):
cidr_bits = int(cidr.split('/')[1])
if '/' in cidr:
ip, cidr_bits = cidr.split('/')
cidr_bits = int(cidr_bits)
if not (0 <= cidr_bits <= 32):
return "无效的CIDR位数"

return "valid"





11 changes: 2 additions & 9 deletions core/subnet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import ipaddress
import math
import re
from network_class_utils import get_network_class_by_cidr

# 根据主机数计算子网掩码相关信息
def hosts_to_subnet_mask_and_hosts(hosts):
Expand Down Expand Up @@ -37,8 +36,6 @@ def hosts_to_subnet_mask_and_hosts(hosts):
# 反掩码的十六进制表示
inverted_mask_hex = ''.join(f"{int(octet):02X}" for octet in inverted_mask.split('.'))

# 网络类别
network_class = get_network_class_by_cidr(cidr)

return (
hosts,
Expand All @@ -49,8 +46,7 @@ def hosts_to_subnet_mask_and_hosts(hosts):
subnet_mask_binary,
inverted_mask_binary,
subnet_mask_hex,
inverted_mask_hex,
network_class
inverted_mask_hex
)


Expand Down Expand Up @@ -88,8 +84,6 @@ def cidr_or_subnet_mask_to_info(cidr_or_mask):
# 反掩码的十六进制表示
inverted_mask_hex = '.'.join(f"{int(octet):02X}" for octet in inverted_mask.split('.'))

# 网络类别
network_class = get_network_class_by_cidr(cidr)

# 多行返回
return (
Expand All @@ -101,8 +95,7 @@ def cidr_or_subnet_mask_to_info(cidr_or_mask):
subnet_mask_binary,
inverted_mask_binary,
subnet_mask_hex,
inverted_mask_hex,
network_class
inverted_mask_hex
)

except ValueError:
Expand Down
Binary file added docs/cheat_sheet_ipv4_en.pdf
Binary file not shown.
27 changes: 14 additions & 13 deletions gui/gui_main_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from gui.gui_ip_calc import ip_calculator
from gui.gui_subnet_calc import subnet_calculator
from gui.gui_base_converter import ip_base_converter
from gui.gui_menu import show_word, show_about
from gui.gui_menu import show_word, show_about,get_cheat_sheet_ipv4_en_pdf
from gui.gui_calc_history_manager import show_history

# 打开网页链接
Expand All @@ -18,7 +18,7 @@ def open_webpage(url):
# 创建主界面
def create_gui():
root = tk.Tk()
root.title("IP地址规划计算器v1.3.1")
root.title("IP地址规划计算器v1.3.2")

# 设置窗口大小为固定大小
root.geometry("420x560")
Expand All @@ -38,19 +38,20 @@ def create_gui():
info_menu.add_command(label="关于", command=show_about)

# 知识库菜单
ol_knowledge_base_menu = Menu(menu, tearoff=0)
knowledge_base_menu = Menu(menu, tearoff=0)
# 知识文章添加到知识库菜单
ol_post_menu = Menu(ol_knowledge_base_menu, tearoff=0)
ol_knowledge_base_menu.add_cascade(label="查看精选IP与子网掩码解读文章", menu=ol_post_menu)
ol_post_menu.add_command(label="hoochanlon - 一篇全文让你彻底理清IP地址、子网掩码、网关", command=lambda: open_webpage("https://hoochanlon.github.io/helpdesk-guide/enhance/net/neta.html"))
ol_post_menu.add_command(label="freecodecamp.org - 子网检查单", command=lambda: open_webpage("https://www.freecodecamp.org/chinese/news/subnet-cheat-sheet-24-subnet-mask-30-26-27-29-and-other-ip-address-cidr-network-references"))
ol_post_menu.add_command(label="MEP - 这五个计算题,彻底弄懂ip地址与子网掩码(转载)", command=lambda: open_webpage("http://mepconsultants.net/nd.jsp?id=4456"))
post_menu = Menu(knowledge_base_menu, tearoff=0)
knowledge_base_menu.add_cascade(label="查看精选IP与子网掩码解读文章", menu=post_menu)
post_menu.add_command(label="hoochanlon - 一篇全文让你彻底理清IP地址、子网掩码、网关", command=lambda: open_webpage("https://hoochanlon.github.io/helpdesk-guide/enhance/net/neta.html"))
post_menu.add_command(label="freecodecamp.org - 子网检查单", command=lambda: open_webpage("https://www.freecodecamp.org/chinese/news/subnet-cheat-sheet-24-subnet-mask-30-26-27-29-and-other-ip-address-cidr-network-references"))
post_menu.add_command(label="MEP - 这五个计算题,彻底弄懂ip地址与子网掩码(转载)", command=lambda: open_webpage("http://mepconsultants.net/nd.jsp?id=4456"))
# 添加在线工具
menu.add_cascade(label="知识库", menu=ol_knowledge_base_menu)
ol_knowledge_base_menu.add_command(label="清华大学交叉信息研究院 - ip地址在线计算器", command=lambda: open_webpage("https://iiis.tsinghua.edu.cn/ip/"))
ol_knowledge_base_menu.add_command(label="calculator.io - 子网计算器", command=lambda: open_webpage("https://www.calculator.io/zh/%E5%AD%90%E7%BD%91%E8%AE%A1%E7%AE%97%E5%99%A8/"))
ol_knowledge_base_menu.add_command(label="webdemo.myscript.com - 手写数学公式算结果", command=lambda: open_webpage("https://webdemo.myscript.com/"))
ol_knowledge_base_menu.add_command(label="tool.browser.qq.com - 腾讯在线工具箱", command=lambda: open_webpage("https://tool.browser.qq.com/"))
menu.add_cascade(label="知识库", menu=knowledge_base_menu)
knowledge_base_menu.add_command(label="PAESSLER - IPv4 子网速查表",command=lambda: open_webpage(f"file://{get_cheat_sheet_ipv4_en_pdf()}"))
knowledge_base_menu.add_command(label="清华大学交叉信息研究院 - ip地址在线计算器", command=lambda: open_webpage("https://iiis.tsinghua.edu.cn/ip/"))
knowledge_base_menu.add_command(label="calculator.io - 子网计算器", command=lambda: open_webpage("https://www.calculator.io/zh/%E5%AD%90%E7%BD%91%E8%AE%A1%E7%AE%97%E5%99%A8/"))
knowledge_base_menu.add_command(label="webdemo.myscript.com - 手写数学公式算结果", command=lambda: open_webpage("https://webdemo.myscript.com/"))
knowledge_base_menu.add_command(label="tool.browser.qq.com - 腾讯在线工具箱", command=lambda: open_webpage("https://tool.browser.qq.com/"))

# 添加标题标签到主窗口的顶部
title_label = tk.Label(root, text="网络和IP地址计算器", font=("仿宋", 14), pady=10)
Expand Down
18 changes: 15 additions & 3 deletions gui/gui_menu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gui_menu.py
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
import webbrowser
import os
import sys
Expand Down Expand Up @@ -47,8 +48,6 @@ def show_word():
print(f"文档加载失败: {e}")
messagebox.showerror("错误", f"无法加载文档: {e}")



def show_about():
# 创建新的Toplevel窗口
about_window = tk.Toplevel()
Expand Down Expand Up @@ -122,7 +121,7 @@ def show_about():
avatar_label.pack(pady=20) # 在顶部居中显示头像

# ---------------- 关于部分 ----------------
about_text = "IP地址规划计算器 v1.3"
about_text = "IP地址规划计算器 v1.3.2"
author_text = "Author: Hoochanlon"
desc_text = (
"这是一个功能强大的工具,帮助用户进行IP地址规划和计算。\n\n"
Expand Down Expand Up @@ -216,3 +215,16 @@ def show_wechat_qr():
wechat_image_label.bind("<Button-1>", lambda event: show_wechat_qr()) # 绑定点击事件
wechat_image_label.grid(row=0, column=2, padx=5) # 放在第0行,第2列,左右间距5



def get_cheat_sheet_ipv4_en_pdf():
# 获取资源文件路径
if hasattr(sys, '_MEIPASS'): # 如果是打包后的环境
current_dir = sys._MEIPASS
else:
Desktop_dir = os.path.join(os.path.expanduser("~"), "Desktop")
current_dir = os.path.join(Desktop_dir, "Network-Calculator")

doc_path = os.path.join(current_dir, "docs", "cheat_sheet_ipv4_en.pdf")

return doc_path
8 changes: 2 additions & 6 deletions gui/gui_subnet_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import tkinter as tk
from core.calc_history_manager import save_to_history
from subnet_utils import hosts_to_subnet_mask_and_hosts, cidr_or_subnet_mask_to_info, validate_subnet_mask, validate_cidr
from core.subnet_utils import hosts_to_subnet_mask_and_hosts, cidr_or_subnet_mask_to_info, validate_subnet_mask, validate_cidr
from gui.gui_right_menu import attach_result_right_click_menu
import re

Expand Down Expand Up @@ -68,8 +68,7 @@ def calculate_info():
subnet_mask_binary,
inverted_mask_binary,
subnet_mask_hex,
inverted_mask_hex,
network_class
inverted_mask_hex
) = hosts_to_subnet_mask_and_hosts(hosts)

if isinstance(subnet_mask, str) and "主机数" in subnet_mask:
Expand All @@ -80,7 +79,6 @@ def calculate_info():
f"输入的主机数: {hosts}\n"
f"推算得出子网掩码: {subnet_mask}{cidr}\n"
f"基于子网掩码的实际可用主机数: {usable_hosts}\n"
f"网络类别: {network_class}\n"
f"反掩码: {inverted_mask}\n\n"
f"子网掩码的二进制:\n{subnet_mask_binary}\n"
f"子网掩码的十六进制: {subnet_mask_hex}\n"
Expand Down Expand Up @@ -127,13 +125,11 @@ def calculate_info():
inverted_mask_binary,
subnet_mask_hex,
inverted_mask_hex,
network_class
) = cidr_or_subnet_mask_to_info(subnet_input)

output = (
f"输入的子网掩码或CIDR: {cidr_or_mask}\n"
f"可用主机数: {usable_hosts}\n"
f"网络类别: {network_class}\n"
f"通过常规掩码算出的CIDR: {cidr}\n"
f"通过CIDR算出的子网掩码: {subnet_mask}\n"
f"反掩码: {inverted_mask}\n\n"
Expand Down
Loading

0 comments on commit 0ea0792

Please sign in to comment.