Browse Source

修复任务无结果

liuyuqi-cnb 1 day ago
parent
commit
72002fb0ad
3 changed files with 55 additions and 16 deletions
  1. 26 5
      gui.py
  2. 8 2
      searchdomain/db.py
  3. 21 9
      searchdomain/searchdomain.py

+ 26 - 5
gui.py

@@ -291,14 +291,25 @@ class SearchDomainGUI:
             messagebox.showerror("错误", f"输入文件不存在: {input_file}")
             return
             
-        # 准备参数
+        # 准备输出文件路径
         if not os.path.isabs(output_file):
             output_file = os.path.join(self.app_path, output_file)
+        
+        # 确保输出目录存在
+        output_dir = os.path.dirname(output_file)
+        if output_dir and not os.path.exists(output_dir):
+            try:
+                os.makedirs(output_dir, exist_ok=True)
+            except Exception as e:
+                messagebox.showerror("错误", f"无法创建输出目录: {output_dir}\n{str(e)}")
+                return
             
+        # 准备参数
+        # app_path 应该设置为输出文件的目录,因为 saveRes 使用 app_path + output
         params = OrderedDict({
-            "input": os.path.basename(input_file),
-            "output": os.path.basename(output_file),
-            "app_path": os.path.dirname(input_file)
+            "input": input_file,  # 使用完整路径
+            "output": os.path.basename(output_file),  # 只使用文件名
+            "app_path": os.path.dirname(output_file)  # 输出文件的目录
         })
         
         # 在后台线程中运行
@@ -315,6 +326,7 @@ class SearchDomainGUI:
         try:
             self.log(f"开始搜索域名...")
             self.log(f"输入文件: {params['input']}")
+            self.log(f"输出目录: {params['app_path']}")
             self.log(f"输出文件: {params['output']}")
             self.log(f"导出所有: {export_all}")
             
@@ -324,7 +336,16 @@ class SearchDomainGUI:
             self.log("域名搜索完成!")
             output_path = os.path.join(params["app_path"], params["output"])
             self.log(f"结果已保存到: {output_path}")
-            messagebox.showinfo("成功", f"域名搜索完成!\n结果已保存到: {output_path}")
+            
+            # 验证文件是否真的存在
+            if os.path.exists(output_path):
+                file_size = os.path.getsize(output_path)
+                self.log(f"文件大小: {file_size} 字节")
+                messagebox.showinfo("成功", f"域名搜索完成!\n结果已保存到: {output_path}\n文件大小: {file_size} 字节")
+            else:
+                error_msg = f"警告:输出文件不存在: {output_path}"
+                self.log(error_msg)
+                messagebox.showwarning("警告", error_msg)
             
         except Exception as e:
             error_msg = f"搜索域名时出错: {str(e)}"

+ 8 - 2
searchdomain/db.py

@@ -14,8 +14,14 @@ class File(Db):
 
     def save(self, filePath: str, res: str):
         # super().save(res)
-        with open(filePath,'a+',encoding='utf-8') as file:
-            file.writelines(res+"\n")
+        # 确保目录存在
+        dir_path = os.path.dirname(filePath)
+        if dir_path and not os.path.exists(dir_path):
+            os.makedirs(dir_path, exist_ok=True)
+        # 使用追加模式写入,如果文件不存在会自动创建
+        with open(filePath, 'a+', encoding='utf-8') as file:
+            file.write(res + "\n")
+            file.flush()  # 确保立即写入磁盘
 
 class Mysql(Db):
     '''mysql数据库保存数据库'''

+ 21 - 9
searchdomain/searchdomain.py

@@ -33,12 +33,14 @@ class SearchDomain(object):
             res = False
             logging.info(str(index) + ": searching domain:"+ domain + " is unavaliable.")
         except Exception as e:
-            if(str(e).index("No match") == 0):
+            error_str = str(e)
+            # 检查是否是域名未注册的错误
+            if "No match" in error_str or "No match for" in error_str:
                 res = True
                 logging.info(str(index) + ": searching domain:"+ domain +" is avaliable.")
             else:
                 res = False
-                logging.error(e)
+                logging.error(f"Error checking {domain}: {error_str}")
         if self.export_all:
             self.saveRes(domain, res)
         else:
@@ -48,16 +50,26 @@ class SearchDomain(object):
     def saveRes(self, domain: str, res: bool):
         """ save result to file """
         # db.Mysql().save()
-        db.File().save(os.path.join(self.params["app_path"], self.output), domain + "    " + str(res))
+        output_path = os.path.join(self.params["app_path"], self.output)
+        db.File().save(output_path, domain + "    " + str(res))
 
     def run(self):
         '''begin search domain'''
-        with open(os.path.join(self.params["app_path"], self.input), "r", encoding="utf8", errors="ignore") as file:
-            pool = ThreadPoolExecutor(max_workers=5)
-            index = 0
-            for line in file.readlines():
-                index = index + 1
-                pool.submit(self.crawl, line.strip(), index)
+        # 支持 input 为完整路径或相对路径
+        input_path = self.input if os.path.isabs(self.input) else os.path.join(self.params["app_path"], self.input)
+        with open(input_path, "r", encoding="utf8", errors="ignore") as file:
+            with ThreadPoolExecutor(max_workers=5) as pool:
+                index = 0
+                futures = []
+                for line in file.readlines():
+                    domain = line.strip()
+                    if domain:  # 跳过空行
+                        index = index + 1
+                        future = pool.submit(self.crawl, domain, index)
+                        futures.append(future)
+                # 等待所有任务完成
+                for future in futures:
+                    future.result()
 
 if __name__ == '__main__':
     sd = SearchDomain()