Repository navigation
Expand file tree
/
Copy pathfoundry.py
More file actions
157 lines (140 loc) · 5.46 KB
/
Copy pathfoundry.py
File metadata and controls
157 lines (140 loc) · 5.46 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
import json
import os
import platform
import shutil
import tempfile
import urllib.error
import urllib.request
from pathlib import Path
from sys import exit
hard_traits_path = Path.home() / ".ethos" / "traits" / "hard_traits"
hard_traits_path.mkdir(parents=True, exist_ok=True)
def detect_platform():
system = platform.system().lower()
if system == "darwin":
os_name = "macos"
elif system == "windows":
os_name = "windows"
elif system == "linux":
os_name = "linux"
elif system=="android":
os_name="termux"
else:
os_name = system
machine = platform.machine().lower()
if machine in ["x86_64", "amd64"]:
arch = "x86_64"
elif machine in ["arm64", "aarch64"]:
arch = "aarch64"
elif machine in ["armv7l", "armv8l","arm"]:
arch = "armv7"
else:
arch = machine
if os_name == "windows" and "arm" in machine:
arch = "arm64"
return f"{os_name}-{arch}"
def fetch_api_with_fallback(endpoint):
primary_url = "https://foundry-ethos.pages.dev/" + endpoint
fallback_url = "https://amancode22.github.io/ethos-foundry/" + endpoint
req_primary = urllib.request.Request(
primary_url,
headers={
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36"
},
)
try:
with urllib.request.urlopen(req_primary, timeout=20) as response:
code = response.getcode()
return response.read().decode("utf-8"), code
except urllib.error.HTTPError as e:
error_body = e.read().decode("utf-8")
return error_body, e.code
except (urllib.error.URLError, TimeoutError) as e:
print(f"Primary URL of foundry failed: {e}. Switching to fallback...")
try:
with urllib.request.urlopen(fallback_url, timeout=20) as response:
code = response.getcode()
return response.read().decode("utf-8"), code
except urllib.error.HTTPError as e:
error_body = e.read().decode("utf-8")
return error_body, e.code
except Exception as fallback_error:
return f"Both URLs failed. Last error: {fallback_error}"
def search_trait_foundry(args):
trait_name = args.query
try:
index, code = fetch_api_with_fallback("index.json")
if code != 200:
raise Exception(
f"Result code {code} is not valid, requiring code 200 check your network else report issue."
)
index = json.loads(index)
except Exception as e:
print(
f"From foundry got invalid results, error while converting api output to json: {e}"
)
exit(1)
findings = {}
count = 0
for i in index["traits"]:
if not index["traits"][i]["verified"]:
continue
if trait_name in i:
count += 1
print(
f"[{count}] {i}\n\tAuthor: {index['traits'][i]['author']}\n\tDescription: {index['traits'][i]['description']}"
)
elif trait_name in index["traits"][i]["tags"]:
count += 1
print(
f"[{count}] {i}\n\tAuthor: {index['traits'][i]['author']}\n\tDescription: {index['traits'][i]['description']}"
)
if count == 0:
print("[-] No results found in Ethos Foundry.")
else:
print(f"Total Number of Results: {count}")
def install_trait_foundry(args):
trait_name = args.hardtrait_name
resp, code = fetch_api_with_fallback("traits/greetc/manifest.json")
if code == 404:
print(f"[-] Trait {trait_name} is not found on foundry.")
exit(1)
if code == 200:
try:
result_json = json.loads(resp)
except Exception as e:
print(
f"Got error while converting foundry response to json check your internet or report issue if a bug, error is: {e}"
)
exit(1)
platform = detect_platform()
if platform not in result_json["platforms"]:
print(f"[-] This trait is not built for your platform {platform}.")
exit(1)
url = result_json["platforms"][platform]["url"]
checksum = result_json["platforms"][platform]["checksum"]
print(f"[*] Found url: {url} for hard trait binary, trying to download it.....")
with tempfile.TemporaryDirectory() as temp_dir:
temp_file = Path(temp_dir) / url.split("/")[-1]
urllib.request.urlretrieve(url, temp_file)
print(
"[*] Downloaded hard trait binary. Making foundry manifest.json compatible with ethos..."
)
manifest_final = {}
manifest_final["name"] = result_json["name"]
manifest_final["functions"] = result_json["functions"]
manifest_final["binary"] = url.split("/")[-1]
print(
"[*] Created ethos compatible manifest. Saving manifest.json and hard trait binary also......."
)
target_folder = hard_traits_path / trait_name
target_folder.mkdir(parents=True, exist_ok=True)
shutil.move(str(temp_file), str(target_folder))
with open(target_folder / "manifest.json", "w") as f:
json.dump(manifest_final, f)
print(f"[+] Hard trait {trait_name} installed succesfully.")
else:
print(
f"[-] Recieved invalid http code {code}, check your internet or report issue."
)
exit(1)