-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathapp.py
More file actions
1244 lines (1049 loc) · 43.4 KB
/
app.py
File metadata and controls
1244 lines (1049 loc) · 43.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, request, jsonify, send_from_directory, Response, render_template, redirect, url_for
from database import Database
from ansible_manager import AnsibleManager
import json
import os
from functools import wraps
import secrets
from flask_sock import Sock
import paramiko
import threading
import stat
from werkzeug.utils import secure_filename
import time
import hmac
import hashlib
import jwt
import datetime
import logging
from crypto_utils import CryptoUtils, set_crypto_keys, derive_key_from_credentials
# 新增获取客户端真实IP的函数
def get_client_ip():
"""获取客户端真实IP地址
优先从代理转发的头信息中获取真实IP,如不存在则返回直连IP
"""
# 尝试从常见的代理头中获取
if request.headers.get('X-Forwarded-For'):
# 取列表中第一个IP(通常是原始客户端)
return request.headers.get('X-Forwarded-For').split(',')[0].strip()
elif request.headers.get('X-Real-IP'):
return request.headers.get('X-Real-IP')
# 如果没有代理头,则使用直接IP
return request.remote_addr
app = Flask(__name__, static_folder='public', static_url_path='')
app.secret_key = secrets.token_hex(32)
# 设置令牌过期时间为5小时
JWT_EXPIRATION = 5 * 60 * 60 # 5小时,以秒为单位
JWT_SECRET = app.secret_key
db = Database()
ansible = AnsibleManager(db)
crypto = CryptoUtils()
# 账号密码变量
ADMIN_USERNAME = os.getenv('ADMIN_USERNAME')
ADMIN_PASSWORD = os.getenv('ADMIN_PASSWORD')
# 检查必要的环境变量
if not ADMIN_USERNAME or not ADMIN_PASSWORD:
app.logger.warning("未设置管理员凭证环境变量(ADMIN_USERNAME/ADMIN_PASSWORD),请设置这些环境变量以确保系统安全")
# 配置WebSocket
sock = Sock(app)
sock.init_app(app)
UPLOAD_FOLDER = '/tmp/ansible_uploads'
# 确保上传目录存在
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# 简化allowed_file函数
def allowed_file(filename):
"""检查文件是否允许上传,当前策略是允许所有文件"""
return True
def handle_error(f):
"""错误处理装饰器"""
@wraps(f)
def decorated_function(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
app.logger.error(f"Error in {f.__name__}: {str(e)}")
return jsonify({'error': str(e)}), 500
return decorated_function
def auth_required(f):
"""JWT认证要求装饰器"""
@wraps(f)
def decorated_function(*args, **kwargs):
# 获取Authorization头部
auth_header = request.headers.get('Authorization')
token = None
# 从header中提取token
if auth_header and auth_header.startswith('Bearer '):
token = auth_header.split(' ')[1]
# 如果token不在header中,尝试从cookies获取
if not token:
token = request.cookies.get('token')
# 如果token不在cookies中,尝试从查询参数获取(用于兼容某些场景)
if not token:
token = request.args.get('token')
if not token:
return jsonify({'error': 'Unauthorized'}), 401
user = decode_token(token)
if not user:
return jsonify({'error': 'Invalid or expired token'}), 401
# 在每次API调用时,如果没有设置加密密钥,则从用户凭证派生
# 这里从crypto_utils导入全局变量
from crypto_utils import CRYPTO_KEY, CRYPTO_SALT
# 检查密钥是否有效或需要重新派生
if (CRYPTO_KEY is None or
isinstance(CRYPTO_KEY, bytes) and (len(CRYPTO_KEY) != 32 or CRYPTO_KEY == os.urandom(32))) and ADMIN_USERNAME and ADMIN_PASSWORD:
# 只有在设置了环境变量时才尝试派生密钥
app.logger.info("API调用中检测到加密密钥未设置或无效,尝试从用户凭证派生")
try:
key, salt = derive_key_from_credentials(ADMIN_USERNAME, ADMIN_PASSWORD)
set_crypto_keys(key, salt)
app.logger.info("密钥派生成功,长度为: %d 字节", len(key))
except Exception as e:
app.logger.error(f"密钥派生失败: {str(e)}")
return jsonify({'error': '系统加密配置错误,请联系管理员'}), 500
# 将用户信息添加到request中,以便视图函数使用
request.user = user
return f(*args, **kwargs)
return decorated_function
@app.before_request
def before_request():
app.logger.info(f"处理请求: {request.path}")
if request.method == 'OPTIONS':
return None
if request.path.startswith('/ws/'):
return None
if request.path.startswith('/terminal'):
return None
if request.path == '/login':
return None
if request.path.startswith('/api/') and request.path != '/api/login':
auth_header = request.headers.get('Authorization')
token = None
if auth_header and auth_header.startswith('Bearer '):
token = auth_header.split(' ')[1]
if not token:
token = request.cookies.get('token')
if not token:
token = request.args.get('token')
if not token:
return jsonify({'error': 'Unauthorized'}), 401
user = decode_token(token)
if not user:
return jsonify({'error': 'Invalid or expired token'}), 401
request.user = user
else:
pass
@app.after_request
def after_request(response):
# 记录API请求
if request.path.startswith("/api/"):
status = 'success' if response.status_code < 400 else 'failed'
db.add_access_log(
get_client_ip(),
request.path,
status,
response.status_code
)
return response
@app.route('/api/login', methods=['POST'])
def login():
"""用户登录"""
data = request.json
username = data.get('username')
password = data.get('password')
# 确保环境变量已设置
if not ADMIN_USERNAME or not ADMIN_PASSWORD:
app.logger.error("系统未配置管理员凭证")
return jsonify({'success': False, 'message': '系统配置错误'}), 500
if username == ADMIN_USERNAME and password == ADMIN_PASSWORD:
# 从用户凭证派生加密密钥
try:
key, salt = derive_key_from_credentials(username, password)
# 设置全局加密密钥
set_crypto_keys(key, salt)
app.logger.info(f"已从用户凭证成功派生加密密钥,长度为: {len(key)} 字节")
# 生成JWT令牌
token = generate_token('admin')
# 创建包含token的响应
response_data = {'success': True, 'message': '登录成功', 'token': token}
response = jsonify(response_data)
# 将token也存在cookie中,方便前端获取
# secure=True表示只在HTTPS连接中发送
# httponly=True表示JavaScript不能访问cookie,增加安全性
# samesite='Lax'防止CSRF攻击
response.set_cookie(
'token',
token,
max_age=JWT_EXPIRATION,
# secure=True, # 生产环境建议开启
httponly=True,
samesite='Lax'
)
return response
except Exception as e:
app.logger.error(f"密钥派生失败: {str(e)}")
return jsonify({'success': False, 'message': '登录失败,系统加密配置错误'}), 500
else:
app.logger.warning(f"登录失败,用户名或密码不正确: {username}")
return jsonify({'success': False, 'message': '用户名或密码不正确'}), 401
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve_react_app(path):
"""处理前端路由 - 所有路由都交给React处理,除非是静态文件"""
app.logger.info(f"serve_react_app 处理 路径: '{path}'")
# 显式处理终端路径(同时处理有斜杠和无斜杠的情况)
if path.startswith('terminal'):
app.logger.info(f"明确处理终端路径: {path}")
return send_from_directory(app.static_folder, 'index.html')
# 如果是API请求或WebSocket路由,不处理(已有专门的处理器)
if path.startswith('api/') or path.startswith('ws/'):
app.logger.info(f"API或WebSocket路径,返回404: {path}")
return jsonify({'error': 'Not found'}), 404
# 检查请求的路径是否对应 public 目录下的一个实际存在的文件
static_file_path = os.path.join(app.static_folder, path)
app.logger.info(f"尝试查找静态文件: {static_file_path}")
if path != "" and os.path.exists(static_file_path) and not os.path.isdir(static_file_path):
app.logger.info(f"找到静态文件,返回: {static_file_path}")
# 如果是实际文件(如 CSS, JS, 图片),则直接提供该文件
return send_from_directory(app.static_folder, path)
else:
app.logger.info(f"未找到静态文件,返回index.html用于前端路由: {path}")
# 否则,提供 public/index.html,让 React Router 处理路由
return send_from_directory(app.static_folder, 'index.html')
@app.route('/api/hosts', methods=['GET'])
@handle_error
@auth_required
def get_hosts():
"""获取所有主机列表"""
hosts = db.get_hosts()
for host in hosts:
# 不返回明文密码到前端,但保留加密形式用于识别
# 根据认证方式调整 is_password_encrypted 的含义
if host['auth_method'] == 'password':
host['is_password_encrypted'] = crypto.is_encrypted(host['encrypted_password'])
else:
host['is_password_encrypted'] = False # 密钥认证,没有加密密码
host['password'] = '********' # 始终不返回明文密码
# 删除不需要返回的字段
if 'encrypted_password' in host:
del host['encrypted_password']
return jsonify(hosts)
@app.route('/api/hosts/<int:host_id>', methods=['GET'])
@handle_error
@auth_required
def get_host(host_id):
"""获取单个主机信息"""
host = db.get_host(host_id)
if host:
# 根据认证方式调整 is_password_encrypted 的含义
if host['auth_method'] == 'password':
host['is_password_encrypted'] = crypto.is_encrypted(host['encrypted_password'])
else:
host['is_password_encrypted'] = False # 密钥认证,没有加密密码
host['password'] = '********' # 始终不返回明文密码
# 删除不需要返回的字段
if 'encrypted_password' in host:
del host['encrypted_password']
return jsonify(host)
return jsonify({'error': 'Host not found'}), 404
@app.route('/api/hosts', methods=['POST'])
@handle_error
@auth_required
def add_host():
"""添加单个主机"""
host_data = request.json
auth_method = host_data.get('auth_method', 'password') # 默认为密码认证
required_fields = ['comment', 'address', 'username', 'port']
if auth_method == 'password':
required_fields.append('password')
if not all(field in host_data for field in required_fields):
return jsonify({'error': 'Missing required fields'}), 400
host_id = db.add_host(host_data)
return jsonify({
'message': 'Host added successfully',
'host_id': host_id
}), 201
@app.route('/api/hosts/batch', methods=['POST'])
@handle_error
@auth_required
def add_hosts_batch():
"""批量添加主机"""
hosts_data = request.json
if not isinstance(hosts_data, list):
return jsonify({'error': 'Invalid data format'}), 400
for host in hosts_data:
auth_method = host.get('auth_method', 'password')
required_fields = ['comment', 'address', 'username', 'port']
if auth_method == 'password':
required_fields.append('password')
if not all(field in host for field in required_fields):
return jsonify({'error': f'Missing required fields in host data: {host}'}), 400
count = db.add_hosts_batch(hosts_data)
return jsonify({
'message': f'Successfully added {count} hosts',
'count': count
})
@app.route('/api/hosts/<int:host_id>', methods=['PUT'])
@handle_error
@auth_required
def update_host(host_id):
"""更新主机信息"""
host_data = request.json
auth_method = host_data.get('auth_method', 'password') # 默认为密码认证
required_fields = ['comment', 'address', 'username', 'port']
if auth_method == 'password' and 'password' in host_data and host_data['password'] != '':
# 如果是密码认证且提供了新密码,则密码是必需的
pass # 密码会在db.update_host中处理
elif auth_method == 'password' and ('password' not in host_data or host_data['password'] == ''):
# 如果是密码认证但未提供新密码,则不更新密码字段,保持原有密码
pass
elif auth_method == 'key':
# 如果是密钥认证,密码字段不强制要求
pass
if not all(field in host_data for field in required_fields):
return jsonify({'error': 'Missing required fields'}), 400
# 检查主机是否存在
if not db.get_host(host_id):
return jsonify({'error': 'Host not found'}), 404
db.update_host(host_id, host_data)
return jsonify({'message': 'Host updated successfully'})
@app.route('/api/hosts/<int:host_id>', methods=['DELETE'])
@handle_error
@auth_required
def delete_host(host_id):
"""删除主机"""
# 检查主机是否存在
if not db.get_host(host_id):
return jsonify({'error': 'Host not found'}), 404
db.delete_host(host_id)
return jsonify({'message': 'Host deleted successfully'})
@app.route('/api/execute', methods=['POST'])
@handle_error
@auth_required
def execute_command():
"""执行命令"""
data = request.json
command = data.get('command')
host_ids = data.get('hosts')
if not command:
return jsonify({'error': 'Command is required'}), 400
# 确定目标主机
if host_ids == 'all':
target_hosts = db.get_hosts()
else:
if not isinstance(host_ids, list):
return jsonify({'error': 'Invalid hosts format'}), 400
target_hosts = []
for host_id in host_ids:
host = db.get_host(host_id)
if host:
target_hosts.append(host)
else:
return jsonify({'error': f'Host not found: {host_id}'}), 404
if not target_hosts:
return jsonify({'error': 'No valid target hosts'}), 400
# 执行命令并获取结果
results = ansible.execute_command(command, target_hosts)
return jsonify(results)
@app.route('/api/logs', methods=['GET'])
@handle_error
@auth_required
def get_logs():
"""获取命令执行日志"""
limit = request.args.get('limit', default=100, type=int)
logs = db.get_command_logs(limit)
return jsonify(logs)
@app.route('/api/hosts/<int:host_id>/facts', methods=['GET'])
@handle_error
@auth_required
def get_host_facts(host_id):
"""获取主机详细信息"""
facts = ansible.get_host_facts(host_id)
if facts:
return jsonify(facts)
return jsonify({'error': 'Failed to get host facts'}), 404
@app.route('/api/hosts/<int:host_id>/ping', methods=['GET'])
@handle_error
@auth_required
def ping_host(host_id):
"""检查主机连通性"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
# 使用 Ansible 执行 ping 模块
results = ansible.execute_ping([host])
# 解析结果
host_address = host['address']
if host_address in results['success']:
return jsonify({'status': 'success', 'message': '连接正常'})
elif host_address in results['unreachable']:
return jsonify({'status': 'unreachable', 'message': '无法连接'})
else:
return jsonify({'status': 'failed', 'message': '失败'})
@sock.route('/ws/terminal/<int:host_id>')
def terminal_ws(ws, host_id):
"""处理终端 WebSocket 连接"""
app.logger.info(f"处理WebSocket连接请求: host_id={host_id}")
# 检查授权令牌
token = request.args.get('token')
if not token:
app.logger.error(f"终端WebSocket错误: 未提供令牌")
ws.send(json.dumps({"error": "Authorization required"}))
return
# 验证令牌是否有效
try:
# 令牌格式:host_id:timestamp:签名
parts = token.split(':')
if len(parts) != 3 or parts[0] != str(host_id):
raise ValueError("Invalid token format")
# 检查时间戳是否在有效期内(5分钟)
token_timestamp = int(parts[1])
current_time = int(time.time())
if current_time - token_timestamp > 300: # 5分钟有效期
raise ValueError("Token expired")
# 验证签名
message = f"{host_id}:{token_timestamp}"
expected_signature = hmac.new(
app.secret_key.encode(),
message.encode(),
hashlib.sha256
).hexdigest()
if parts[2] != expected_signature:
raise ValueError("Invalid token signature")
except Exception as e:
app.logger.error(f"终端WebSocket令牌验证失败")
ws.send(json.dumps({"error": "Invalid or expired token"}))
return
host = db.get_host(host_id)
if not host:
app.logger.error(f"终端WebSocket错误: 主机ID不存在")
ws.send(json.dumps({"error": "Host not found"}))
return
app.logger.info(f"找到主机信息: id={host_id}")
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
app.logger.info(f"正在连接SSH")
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username'],
'timeout': 10
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
# else: paramiko will try to use SSH agent or default keys (/root/.ssh/id_ed25519)
ssh.connect(**connect_args)
# 默认终端大小
term_width = 100
term_height = 30
app.logger.info(f"SSH连接成功,创建终端会话")
channel = ssh.invoke_shell(term='xterm-256color', width=term_width, height=term_height)
def send_data():
while True:
try:
if channel.recv_ready():
data = channel.recv(1024).decode('utf-8', errors='ignore')
if data:
ws.send(data)
else:
time.sleep(0.1)
except Exception as e:
app.logger.error(f"数据发送错误")
break
thread = threading.Thread(target=send_data)
thread.daemon = True
thread.start()
app.logger.info(f"WebSocket连接已建立,后台线程已启动")
# 发送初始欢迎信息
welcome_msg = f"\r\n\x1b[1;32m*** 已连接到主机 ***\x1b[0m\r\n"
ws.send(welcome_msg)
while True:
try:
message = ws.receive()
if message is None:
app.logger.info(f"WebSocket连接已关闭")
break
data = json.loads(message)
if data['type'] == 'input':
channel.send(data['data'])
elif data['type'] == 'resize':
new_size = data['data']
channel.resize_pty(
width=new_size['cols'],
height=new_size['rows']
)
except json.JSONDecodeError as e:
app.logger.error(f"JSON解析错误")
continue
except Exception as e:
app.logger.error(f"WebSocket接收错误")
break
except paramiko.AuthenticationException:
app.logger.error(f"SSH认证失败")
ws.send(f'\r\n\x1b[1;31m*** SSH认证失败 ***\x1b[0m\r\n')
except paramiko.SSHException as e:
app.logger.error(f"SSH连接错误")
ws.send(f'\r\n\x1b[1;31m*** SSH连接错误 ***\x1b[0m\r\n')
except Exception as e:
app.logger.error(f"终端连接错误")
ws.send(f'\r\n\x1b[1;31m*** 连接错误 ***\x1b[0m\r\n')
finally:
app.logger.info(f"关闭终端连接")
if 'channel' in locals():
channel.close()
if 'ssh' in locals():
ssh.close()
@app.route('/api/sftp/<int:host_id>/list')
@handle_error
@auth_required
def sftp_list(host_id):
"""获取 SFTP 文件列表"""
path = request.args.get('path', '/')
host = db.get_host(host_id)
try:
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
file_list = []
for entry in sftp.listdir_attr(path):
file_list.append({
'name': entry.filename,
'type': 'directory' if stat.S_ISDIR(entry.st_mode) else 'file',
'size': entry.st_size,
'mtime': entry.st_mtime
})
return jsonify(file_list)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/mkdir', methods=['POST'])
@handle_error
@auth_required
def sftp_mkdir(host_id):
"""创建文件夹"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
try:
data = request.json
path = data.get('path')
if not path:
return jsonify({'error': 'Path is required'}), 400
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
try:
sftp.stat(path)
return jsonify({'error': 'Directory already exists'}), 400
except IOError:
sftp.mkdir(path)
return jsonify({'success': True})
except Exception as e:
app.logger.error(f"SFTP mkdir error: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/upload', methods=['POST'])
@handle_error
@auth_required
def sftp_upload(host_id):
"""处理文件上传"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
try:
path = request.form.get('path', '/')
if not request.files:
return jsonify({'error': 'No files provided'}), 400
files = request.files.getlist('files[]')
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
for file in files:
if file.filename:
filename = secure_filename(file.filename)
remote_path = os.path.join(path, filename).replace('\\', '/')
temp_path = os.path.join('/tmp', filename)
file.save(temp_path)
try:
sftp.put(temp_path, remote_path)
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
return jsonify({'success': True})
except Exception as e:
app.logger.error(f"SFTP upload error: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/rename', methods=['POST'])
@handle_error
@auth_required
def sftp_rename(host_id):
"""重命名文件或文件夹"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
try:
data = request.json
old_path = data.get('old_path')
new_path = data.get('new_path')
if not old_path or not new_path:
return jsonify({'error': 'Both old_path and new_path are required'}), 400
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
try:
sftp.stat(new_path)
return jsonify({'error': 'Destination already exists'}), 400
except IOError:
sftp.rename(old_path, new_path)
return jsonify({'success': True})
except Exception as e:
app.logger.error(f"SFTP rename error: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/touch', methods=['POST'])
@handle_error
@auth_required
def sftp_touch(host_id):
"""创建空文件"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
try:
data = request.json
path = data.get('path')
if not path:
return jsonify({'error': 'Path is required'}), 400
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
try:
sftp.stat(path)
return jsonify({'error': 'File already exists'}), 400
except IOError:
with sftp.file(path, 'w') as f:
f.write('')
return jsonify({'success': True})
except Exception as e:
app.logger.error(f"SFTP touch error: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/read')
@handle_error
@auth_required
def sftp_read(host_id):
"""读取文件内容"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
path = request.args.get('path')
try:
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
with sftp.file(path, 'r') as f:
content = f.read().decode('utf-8', errors='replace')
return jsonify({'content': content})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/write', methods=['POST'])
@handle_error
@auth_required
def sftp_write(host_id):
"""写入文件内容"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
try:
data = request.json
path = data.get('path')
content = data.get('content', '')
if not path:
return jsonify({'error': 'Path is required'}), 400
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
with sftp.file(path, 'w') as f:
f.write(content)
return jsonify({'success': True})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/delete', methods=['POST'])
@handle_error
@auth_required
def sftp_delete(host_id):
"""删除文件或文件夹"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
try:
data = request.json
path = data.get('path')
is_directory = data.get('is_directory', False)
if not path:
return jsonify({'error': 'Path is required'}), 400
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
if is_directory:
# 检查目录是否为空
if sftp.listdir(path):
return jsonify({'error': 'Directory is not empty'}), 400
sftp.rmdir(path)
else:
sftp.remove(path)
return jsonify({'success': True})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/sftp/<int:host_id>/download')
@handle_error
@auth_required
def sftp_download(host_id):
"""下载文件"""
host = db.get_host(host_id)
if not host:
return jsonify({'error': 'Host not found'}), 404
path = request.args.get('path')
if not path:
return jsonify({'error': 'Path is required'}), 400
try:
filename = os.path.basename(path)
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connect_args = {
'hostname': host['address'],
'port': host['port'],
'username': host['username']
}
if host['auth_method'] == 'password':
connect_args['password'] = host['password']
ssh.connect(**connect_args)
with ssh.open_sftp() as sftp:
# 检查文件状态
file_attr = sftp.stat(path)
if stat.S_ISDIR(file_attr.st_mode):
return jsonify({'error': 'Cannot download a directory'}), 400
# 为防止路径遍历漏洞,只处理文件名
temp_path = os.path.join('/tmp', secure_filename(filename))
sftp.get(path, temp_path)
try:
with open(temp_path, 'rb') as f:
content = f.read()
# 创建响应对象
response = Response(content)
response.headers['Content-Type'] = 'application/octet-stream'
response.headers['Content-Disposition'] = f'attachment; filename="{filename}"'
return response
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
except Exception as e:
app.logger.error(f"SFTP download error: {str(e)}")
return jsonify({'error': str(e)}), 500
@app.errorhandler(404)
def not_found_error(error):
"""处理404错误"""
app.logger.error(f"404错误: 路径={request.path}, IP={request.remote_addr}, 方法={request.method}")
# 如果是API或WebSocket请求,返回JSON错误
if request.path.startswith('/api/') or request.path.startswith('/ws/'):
return jsonify({'error': 'Not found'}), 404
# 其他所有路径交给前端路由处理,与serve_react_app一致
return send_from_directory(app.static_folder, 'index.html')
@app.errorhandler(500)
def internal_error(error):
"""处理500错误"""
return jsonify({'error': 'Internal server error'}), 500
@app.route('/api/access-logs', methods=['GET'])
@handle_error
@auth_required
def get_access_logs():
"""获取访问日志"""
logs = db.get_access_logs()
return jsonify(logs)
@app.route('/api/access-logs/cleanup', methods=['POST'])
@handle_error
@auth_required
def cleanup_logs():
"""清理旧日志"""
db.cleanup_old_logs()
return jsonify({'message': '已清理7天前的日志'})
def create_required_directories():
"""创建必要的目录"""
directories = ['logs', 'data']
for directory in directories:
os.makedirs(directory, exist_ok=True)
@app.route('/api/upload', methods=['POST'])
@handle_error
@auth_required
def api_upload():
"""API版本的文件上传处理,适配前端发送的格式,支持部分成功场景"""
if 'file' not in request.files:
return jsonify({'error': '没有文件被上传'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': '没有选择文件'}), 400