Skip to content

Commit

Permalink
inital upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexInABox committed Oct 7, 2023
1 parent 2632256 commit 3d35e14
Show file tree
Hide file tree
Showing 7 changed files with 1,783 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
.vscode
.git
.gitignore
.github
config.json
LICENSE
README.md
39 changes: 39 additions & 0 deletions .github/workflows/docker-image-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Docker Image CI

on:
workflow_dispatch:
push:
branches: [ "main" ]


jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GH_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: |
alexinabox/hoyolab-auto-claimer-js:latest
ghcr.io/alexinabox/hoyolab-auto-claimer-js:latest
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use a small Node.js image as the base
FROM node:alpine

# Set the timezone to UTC+8 (Asia/Shanghai)
ENV TZ=Asia/Shanghai

# Set the working directory to /app
WORKDIR /app

# Copy all files to the container
COPY . .

# Install dependencies
RUN npm install

# Expose the port that the app will run on
EXPOSE 80

# Start the app using the start script from package.json
CMD ["npm", "start"]
163 changes: 163 additions & 0 deletions hac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
const config = require('./config.json');
const fetch = require('node-fetch');

//run the main function every day at set SERVER_UTC time
//this is to ensure that the daily reward is claimed at the same time every day

startUp();

function startUp() {
//Just in case try the main loop once at start up
main();

//Schedule the main loop to run every day
scheduleDailyClaim();
}

function scheduleDailyClaim() {
// Calculate a random minute within the first hour (0-59)
const randomMinute = Math.floor(Math.random() * 60);

// Calculate the milliseconds until the next day
const now = new Date();
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
tomorrow.setHours(0, randomMinute, 0, 0);

// Calculate the delay in milliseconds until the next run
const delay = tomorrow - now;

console.log(`Next run: ${tomorrow.toLocaleString()}`);
console.log(`Current time: ${now.toLocaleString()}`);
console.log('Timezone: ' + Intl.DateTimeFormat().resolvedOptions().timeZone);


// Schedule the next run
setTimeout(() => {
main();
// Re-run this function at the next natural interval
scheduleDailyClaim();
}, delay);
}


async function main() {
var COOKIES = config.COOKIES;
for (var i = 0; i < COOKIES.length; i++) {
try {
var cookie_owner_name = String(COOKIES[i].name);
var cookie_owner_value = String(COOKIES[i].cookie);

var claimResultNumber = await claimDailyReward(cookie_owner_value);
//claimResultNumber can have 3 values: 0, 1, 2
//-1: claim failed (no apparent reason)
// 0: already claimed
// 1: claim success

switch (claimResultNumber) {
case 0:
console.log(cookie_owner_name + " has already claimed the reward for today");
break;
case 1:
console.log(cookie_owner_name + " claim success");
break;
case -1:
console.log(cookie_owner_name + " claim failed");
break;
default:
console.log(cookie_owner_name + " claim failed");
break;
}

} catch (error) {
console.log(error);
}
}
}

async function claimDailyReward(cookie) {
var alreadyClaimed = await alreadyClaimedDaily(cookie);
if (alreadyClaimed == -1) {
console.log("cookie or account seems to be invalid");
return -1;
} else if (alreadyClaimed) {
return 0;
}

const dailyRewardURL = "https://sg-hk4e-api.hoyolab.com/event/sol/sign?act_id=" + config.ACT_ID;
const dailyRewardHeaders = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,vi;q=0.8',
'Content-Length': '29',
'Content-Type': 'application/json;charset=UTF-8',
'Cookie': cookie,
'Origin': 'https://act.hoyolab.com',
'Referer': 'https://act.hoyolab.com/',
'Sec-Ch-Ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Opera GX";v="102"',
'Sec-Ch-Ua-Mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0S'
}


const response = await fetch(dailyRewardURL, { headers: dailyRewardHeaders, method: 'POST' });
const responseData = await response.json();

if (responseData.message == "OK") {
console.log("claim success");
return 1;
}
else {
console.log("claim failed with message: " + responseData.message);
return -1;
}
}

async function alreadyClaimedDaily(cookie) {
try {

const dailyRewardURL = "https://sg-hk4e-api.hoyolab.com/event/sol/resign_info?act_id=" + config.ACT_ID;
const dailyRewardHeaders = {
'Cookie': cookie
}

const response = await fetch(dailyRewardURL, { headers: dailyRewardHeaders });
const responseData = await response.json();
console.log("already claimed: " + responseData.data.signed);

return responseData.data.signed;
} catch (error) {
console.log(error);
return -1;
}
}

async function claimMakeUpMissions(cookie) {
const makeUpMissionsURL = "https://sg-hk4e-api.hoyolab.com/event/sol/task/award?lang=en-us";
const myHeaders = {
'Cookie': cookie,
'Content-Type': 'application/json;charset=UTF-8'
}

for (var i = 1; i <= 3; i++) {

var raw = JSON.stringify({
"act_id": "e202102251931481",
"lang": "en-us",
"id": i
});

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow',
waitUntil: 'networkidle0'
};

await fetch(makeUpMissionsURL, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
}
Empty file removed index.js
Empty file.
Loading

0 comments on commit 3d35e14

Please sign in to comment.