-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(@artusx/utils): split utils
- Loading branch information
Showing
6 changed files
with
91 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { md5 } from './crypto'; | ||
import { getEnv } from './env'; | ||
|
||
export const avatar = (email: string) => { | ||
if (!email) { | ||
return ''; | ||
} | ||
const hash = md5(email); | ||
return `https://s.gravatar.com/avatar/${hash}`; | ||
}; | ||
|
||
export const getApiId = () => { | ||
const apiID = getEnv<number>('API_ID', 'number'); | ||
|
||
if (!apiID) { | ||
return; | ||
} | ||
|
||
return apiID; | ||
}; | ||
|
||
export const getProxy = () => { | ||
const ip = getEnv<string>('PROXY_IP'); | ||
const port = getEnv<number>('PROXY_PORT', 'number'); | ||
const socksType = getEnv<number>('PROXY_SOCKET_TYPE', 'number'); | ||
|
||
if (!ip || !port || !socksType) { | ||
return; | ||
} | ||
|
||
const protocol = socksType === 5 ? 'socks5' : 'socks4'; | ||
const proxyString = `${protocol}://${ip}:${port}`; | ||
|
||
return { | ||
ip, | ||
port, | ||
socksType, | ||
proxyString, | ||
}; | ||
}; |
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,13 @@ | ||
import * as crypto from 'crypto'; | ||
|
||
export const md5 = (signature: string) => { | ||
return crypto.createHash('md5').update(signature).digest('hex'); | ||
}; | ||
|
||
export const hmac = (password: string, salt: string) => { | ||
return crypto.createHmac('sha256', salt).update(password).digest('hex'); | ||
}; | ||
|
||
export const slat = () => { | ||
return crypto.randomBytes(16).toString('hex'); | ||
}; |
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,33 @@ | ||
export const getBooleanFromEnv = (key: string, defaultValue: boolean = false): boolean => { | ||
const value = (process.env[key] || '').toLowerCase(); | ||
|
||
if (!value) { | ||
return defaultValue; | ||
} | ||
|
||
if (value === 'true' || value === '1') { | ||
return true; | ||
} | ||
|
||
if (value === 'false' || value === '0') { | ||
return true; | ||
} | ||
|
||
return defaultValue; | ||
}; | ||
|
||
export const getEnv = <T>(key: string, type?: string): T => { | ||
const value = process.env[key] || ''; | ||
|
||
let target: unknown = value; | ||
|
||
if (type === 'boolean') { | ||
target = getBooleanFromEnv(key); | ||
} | ||
|
||
if (type === 'number') { | ||
target = parseInt(value); | ||
} | ||
|
||
return target as T; | ||
}; |
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,3 @@ | ||
export * from './app'; | ||
export * from './env'; | ||
export * from './crypto'; |