sarlink-portal/actions/omada-actions.ts

182 lines
4.7 KiB
TypeScript
Raw Normal View History

interface IpAddress {
ip: string;
mask: number;
}
interface Ipv6Address {
ip: string;
prefix: number;
}
interface MacAddress {
ruleId?: number;
name: string;
macAddress: string;
}
interface GroupProfile {
groupId: string;
site?: string;
name: string;
buildIn?: boolean;
ipList?: IpAddress[];
ipv6List?: Ipv6Address[];
macAddressList?: MacAddress[];
count: number;
type: number;
resource: number;
}
interface OmadaResponse {
errorCode: number;
msg: string;
result: {
data: GroupProfile[];
};
}
async function fetchOmadaGroupProfiles(
omadacId: string,
siteId: string,
): Promise<OmadaResponse> {
if (!omadacId || !siteId) {
throw new Error("omadacId and siteId are required parameters");
}
const timestamp: number = Date.now();
const baseUrl: string = "https://omada.sarlink.link";
const url: string = `${baseUrl}/${omadacId}/api/v2/sites/${siteId}/setting/profiles/groups?_t=${timestamp}`;
const headers: HeadersInit = {
accept: "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
cookie: "TPOMADA_SESSIONID=f30bf82348784089ba90740e59e4aa99",
"csrf-token": "fedc6283e9604372b402f82fdaeba0db",
priority: "u=1, i",
referer: baseUrl,
refresh: "manual",
"sec-ch-ua": '"Brave";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Linux"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"sec-gpc": "1",
"user-agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
};
try {
const response: Response = await fetch(url, {
method: "GET",
headers: headers,
credentials: "include",
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data: OmadaResponse = await response.json();
return data;
} catch (error) {
console.error("Error fetching Omada group profiles:", error);
throw error instanceof Error ? error : new Error("Unknown error occurred");
}
}
export { fetchOmadaGroupProfiles, type MacAddress };
export async function addDevicesToGroup({
omadacId,
siteId,
groupId,
newDevices,
}: {
omadacId?: string;
siteId?: string;
groupId?: string;
newDevices: MacAddress[];
}): Promise<void> {
if (!omadacId || !siteId || !groupId) {
throw new Error("omadacId, siteId, and groupId are required parameters");
}
try {
// Fetch the existing group profiles
const groupProfiles: OmadaResponse = await fetchOmadaGroupProfiles(
omadacId,
siteId,
);
// Find the group profile with the specified groupId
const groupProfile: GroupProfile | undefined =
groupProfiles.result.data.find((profile) => profile.groupId === groupId);
if (!groupProfile) {
throw new Error(`Group with ID ${groupId} not found`);
}
// Create a new array with the existing and new devices
const updatedMacAddressList: MacAddress[] = [
...(groupProfile.macAddressList || []),
...newDevices,
];
// Prepare the request payload
const requestBody = {
name: groupProfile.name,
type: groupProfile.type,
resource: groupProfile.resource,
ipList: groupProfile.ipList,
ipv6List: groupProfile.ipv6List,
macAddressList: updatedMacAddressList,
portList: null,
countryList: null,
portType: null,
portMaskList: null,
domainNamePort: null,
};
const timestamp: number = Date.now();
const baseUrl: string = "https://omada.sarlink.link";
const url: string = `${baseUrl}/${omadacId}/api/v2/sites/${siteId}/setting/profiles/groups/2/${groupId}?_t=${timestamp}`;
const headers: HeadersInit = {
accept: "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/json;charset=UTF-8",
cookie: "TPOMADA_SESSIONID=f30bf82348784089ba90740e59e4aa99",
"csrf-token": "fedc6283e9604372b402f82fdaeba0db",
origin: baseUrl,
priority: "u=1, i",
referer: baseUrl,
refresh: "manual",
"sec-ch-ua": '"Brave";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Linux"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"sec-gpc": "1",
"user-agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
};
const response: Response = await fetch(url, {
method: "PATCH",
headers: headers,
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
console.error("Error adding devices to group:", error);
throw error instanceof Error ? error : new Error("Unknown error occurred");
}
}