-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(plugin): moved ipInfo plugin in a separate file
- Loading branch information
Showing
9 changed files
with
65 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { IPinfo } from "node-ipinfo"; | ||
import { IPinfoWrapper, ApiLimitError } from "node-ipinfo"; | ||
import net from "node:net"; | ||
import fsDriver from "unstorage/drivers/fs"; | ||
import { createStorage } from "unstorage"; | ||
import type { SlipAuthCore } from "../core"; | ||
|
||
export const setupIpInfoAddOn = (auth: SlipAuthCore, ipInfoToken: string) => { | ||
const ipinfoWrapper = new IPinfoWrapper(ipInfoToken); | ||
const cache = createStorage<IPinfo>({ | ||
driver: fsDriver({ | ||
base: "./.data/cache/ipinfo", | ||
}), | ||
}); | ||
|
||
auth.hooks.hook("sessions:create", async (session) => { | ||
session.ip = "92.168.1.58"; | ||
if (!session.ip) { | ||
return; | ||
} | ||
const ipType = net.isIP(session.ip); | ||
|
||
if (ipType === 0) { | ||
// valid cases are 4 and 6 (ipv) | ||
return; | ||
} | ||
|
||
const cachedValue = await cache.getItem(session.ip); | ||
if (cachedValue !== null) { | ||
return; | ||
} | ||
|
||
try { | ||
const response = await ipinfoWrapper.lookupIp(session.ip); | ||
cache.setItem(session.ip, response); | ||
} | ||
catch (error) { | ||
console.log(error); | ||
if (error instanceof ApiLimitError) { | ||
// handle api limit exceed error | ||
} | ||
else { | ||
// handle other errors | ||
} | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...time/database/lib/sqlite/schema.sqlite.ts → src/runtime/database/sqlite/schema.sqlite.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters