iup-nft 0.1__py3-none-any.whl → 0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
iup/main_iupd.py ADDED
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ import argparse
4
+ from os import path
5
+ import re
6
+ import paramiko
7
+ import iup
8
+ from iup.config import Config
9
+ import ipaddress
10
+
11
+
12
+ HOSTNAME_PATTERN = r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$'
13
+ CMD_TEMPLATE = 'iupd {}'
14
+ CFG_FILE = 'config_iupd.ini'
15
+
16
+ def is_domain_name(string: str):
17
+ if re.match(HOSTNAME_PATTERN, string):
18
+ return True
19
+ else:
20
+ return False
21
+
22
+ def is_ip_address(ip_str: str):
23
+ try:
24
+ ip = ipaddress.ip_address(ip_str)
25
+ return ip is not None
26
+ except ValueError:
27
+ return False
28
+
29
+ def parse_host_name(hostname_str: str):
30
+ if is_domain_name(hostname_str) or is_ip_address(hostname_str):
31
+ return hostname_str
32
+ return None
33
+
34
+ def parse_port(port: str):
35
+ if port.isdigit():
36
+ port_num = int(port)
37
+ if port_num > 1 and port_num < 65535:
38
+ return port
39
+ return None
40
+
41
+
42
+ def parse_yes_or_no(msg: str):
43
+ msg = msg.upper()
44
+ if msg in ['Y', 'YES', 'T', 'TRUE']:
45
+ return True
46
+ if msg in ['N', 'NO', 'F', 'FALSE']:
47
+ return False
48
+ return None
49
+
50
+
51
+ def read_hostname_from_file(path: str):
52
+ hostnames = []
53
+ with open(path, 'r') as f:
54
+ for line in f:
55
+ host_name_str = line.strip()
56
+ if is_domain_name(host_name_str):
57
+ hostnames.append(host_name_str)
58
+ hostnames = list(dict.fromkeys(hostnames))
59
+ return hostnames
60
+
61
+ def load_and_overlay_config() -> Config:
62
+ parser = argparse.ArgumentParser(description='iupd参数说明')
63
+ parser.add_argument("sources", nargs='+', help='域名或者域名文件路径')
64
+ parser.add_argument(
65
+ "--version",
66
+ action="version",
67
+ version=f"%(prog)s version {iup.__version__} installed in {path.dirname(__file__)}",
68
+ )
69
+ args = parser.parse_args()
70
+ config_path = path.join(path.dirname(__file__), CFG_FILE)
71
+ if not path.exists(config_path):
72
+ gen_config()
73
+ print('当前配置文件路径为:', config_path)
74
+ config = Config(config_path)
75
+ return args.sources, config
76
+
77
+ def update_rule_by_ssh(hostnames: list, config: Config):
78
+ ssh = paramiko.SSHClient()
79
+ ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
80
+ ssh.connect(config.host, port=config.port, username=config.username, password=config.password)
81
+ for hostname in hostnames:
82
+ cmd = CMD_TEMPLATE.format(hostname)
83
+ _, stdout, stderr = ssh.exec_command(cmd)
84
+ msg = stdout.readlines()
85
+ err = stderr.readlines()
86
+ print('msg', msg) if msg else print()
87
+ print('err', err) if err else print()
88
+ ssh.close()
89
+
90
+ def gen_hostnames_by_sources(sources):
91
+ hostnames = []
92
+ for source in sources:
93
+ if not path.exists(source):
94
+ if is_domain_name(source):
95
+ hostnames.append(source)
96
+ elif path.isfile(source):
97
+ hostnames += read_hostname_from_file(source)
98
+ return list(dict.fromkeys(hostnames))
99
+
100
+ def main():
101
+ sources, config = load_and_overlay_config()
102
+ print('sources:', sources)
103
+ hostnames = gen_hostnames_by_sources(sources)
104
+ if hostnames:
105
+ update_rule_by_ssh(hostnames, config)
106
+ else:
107
+ print('没有读取到有效域名,请检查!!!')
108
+
109
+ def read_info_from_input(prompt, parser=None):
110
+ while True:
111
+ info = input(prompt)
112
+ if parser:
113
+ info = parser(info)
114
+ if not info is None:
115
+ break
116
+ else:
117
+ print('输入有误,请重新输入!')
118
+ continue
119
+ break
120
+ return info
121
+
122
+ def gen_config():
123
+ print('配置向导,请按提示输入信息...')
124
+ host = read_info_from_input('请输入有效主机名:\n', parse_host_name)
125
+ port = int(read_info_from_input('请输入有效端口:\n', parse_port))
126
+ username = read_info_from_input('请输入openwrt用户名:\n')
127
+ password = read_info_from_input('请输入openwrt密码:\n')
128
+ config = Config()
129
+ config.update(host, port, username, password)
130
+ config_path = path.join(path.dirname(__file__), CFG_FILE)
131
+ config.save_to(config_path)
132
+ print('已生成配置文件写入到{}'.format(config_path))
133
+
134
+
135
+ if __name__ == '__main__':
136
+ gen_config()
137
+
138
+
139
+
140
+
141
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: iup-nft
3
- Version: 0.1
3
+ Version: 0.2
4
4
  Summary: ipset updater for openwrt
5
5
  Author: nobitaqaq
6
6
  Author-email: xiaoleigs@gmail.com
@@ -0,0 +1,9 @@
1
+ iup/__init__.py,sha256=WXPOI9BwESIVzJ-XhG7bIFhsRg98u_v5gp16nY0bcbI,108
2
+ iup/config.py,sha256=Ecg9m83D_Cp_inYrltt--oPcupVvCw9oo8m5ukKyWLA,2862
3
+ iup/main.py,sha256=j7-6cFrI6_RFkWR-Y4_LChs3fBVb1JGKQ8aW_w_9kMk,5508
4
+ iup/main_iupd.py,sha256=M1FFWLte7ZwiKL5OmsI3wAHG6GzyiNeBG3ku36X1QbY,4209
5
+ iup_nft-0.2.dist-info/METADATA,sha256=L2R4XmsVsMyJiaG7WG8U0EkAlajVVJL8BGtoFkLQvPM,208
6
+ iup_nft-0.2.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
7
+ iup_nft-0.2.dist-info/entry_points.txt,sha256=__DnLePT02PMCliwlVR7zwdJVJXmIWIwa5jeAb-U9rM,94
8
+ iup_nft-0.2.dist-info/top_level.txt,sha256=abec3YOzJRti4IxzOLIn-uDOPTtYUE-Uv65cAv1v8JY,4
9
+ iup_nft-0.2.dist-info/RECORD,,
@@ -1,3 +1,4 @@
1
1
  [console_scripts]
2
2
  iup = iup.main:main
3
3
  iup-cfg = iup.main:gen_config
4
+ iupd = iup.main_iupd:main
@@ -1,8 +0,0 @@
1
- iup/__init__.py,sha256=WXPOI9BwESIVzJ-XhG7bIFhsRg98u_v5gp16nY0bcbI,108
2
- iup/config.py,sha256=Ecg9m83D_Cp_inYrltt--oPcupVvCw9oo8m5ukKyWLA,2862
3
- iup/main.py,sha256=j7-6cFrI6_RFkWR-Y4_LChs3fBVb1JGKQ8aW_w_9kMk,5508
4
- iup_nft-0.1.dist-info/METADATA,sha256=SiGliRq46XFLR0k3q-mV62-0tnU8QI-p9vke0ftFqVw,208
5
- iup_nft-0.1.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
6
- iup_nft-0.1.dist-info/entry_points.txt,sha256=ER1eUBmB6bXwudHBT51NoOtDSlnUgBi4T6J9_kA4pwc,68
7
- iup_nft-0.1.dist-info/top_level.txt,sha256=abec3YOzJRti4IxzOLIn-uDOPTtYUE-Uv65cAv1v8JY,4
8
- iup_nft-0.1.dist-info/RECORD,,
File without changes