Skip to content

Commit 9870227

Browse files
committed
first commit
0 parents  commit 9870227

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode/
2+
bin/
3+
deno.jsonc

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# webshare
2+
3+
foo

mod.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Webshare from "./webshare.ts"
2+
3+
export default Webshare

test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Webshare from "./mod.ts"
2+
3+
const webshare = new Webshare(process.env.WebshareToken)
4+

types.ts

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Auth
2+
declare interface Authorization {
3+
headers: {
4+
Authorization: string
5+
}
6+
}
7+
8+
// User
9+
declare interface User {
10+
email: string
11+
full_name: string
12+
date_joined: string
13+
last_login: string
14+
}
15+
16+
declare type GetUser = () => Promise<User>
17+
18+
// Subscription
19+
declare type ProxyType = "free" | "cloud" | "premiumcloud" | "dedicated"
20+
21+
declare type CountryCodes = "AF" | "AX" | "AL" | "DZ" | "AS" | "AD" | "AO" | "AI" | "AQ" | "AG" | "AR" | "AM" | "AW" | "AU" | "AT" | "AZ" | "BS" | "BH" | "BD" | "BB" | "BY" | "BE" | "BZ" | "BJ" | "BM" | "BT" | "BO" | "BA" | "BW" | "BV" | "BR" | "IO" | "BN" | "BG" | "BF" | "BI" | "KH" | "CM" | "CA" | "CV" | "KY" | "CF" | "TD" | "CL" | "CN" | "CX" | "CC" | "CO" | "KM" | "CG" | "CD" | "CK" | "CR" | "CI" | "HR" | "CU" | "CY" | "CZ" | "DK" | "DJ" | "DM" | "DO" | "EC" | "EG" | "SV" | "GQ" | "ER" | "EE" | "ET" | "FK" | "FO" | "FJ" | "FI" | "FR" | "GF" | "PF" | "TF" | "GA" | "GM" | "GE" | "DE" | "GH" | "GI" | "GR" | "GL" | "GD" | "GP" | "GU" | "GT" | "GG" | "GN" | "GW" | "GY" | "HT" | "HM" | "VA" | "HN" | "HK" | "HU" | "IS" | "IN" | "ID" | "IR" | "IQ" | "IE" | "IM" | "IL" | "IT" | "JM" | "JP" | "JE" | "JO" | "KZ" | "KE" | "KI" | "KR" | "KP" | "KW" | "KG" | "LA" | "LV" | "LB" | "LS" | "LR" | "LY" | "LI" | "LT" | "LU" | "MO" | "MK" | "MG" | "MW" | "MY" | "MV" | "ML" | "MT" | "MH" | "MQ" | "MR" | "MU" | "YT" | "MX" | "FM" | "MD" | "MC" | "MN" | "ME" | "MS" | "MA" | "MZ" | "MM" | "NA" | "NR" | "NP" | "NL" | "AN" | "NC" | "NZ" | "NI" | "NE" | "NG" | "NU" | "NF" | "MP" | "NO" | "OM" | "PK" | "PW" | "PS" | "PA" | "PG" | "PY" | "PE" | "PH" | "PN" | "PL" | "PT" | "PR" | "QA" | "RE" | "RO" | "RU" | "RW" | "BL" | "SH" | "KN" | "LC" | "MF" | "PM" | "VC" | "WS" | "SM" | "ST" | "SA" | "SN" | "RS" | "SC" | "SL" | "SG" | "SK" | "SI" | "SB" | "SO" | "ZA" | "GS" | "ES" | "LK" | "SD" | "SR" | "SJ" | "SZ" | "SE" | "CH" | "SY" | "TW" | "TJ" | "TZ" | "TH" | "TL" | "TG" | "TK" | "TO" | "TT" | "TN" | "TR" | "TM" | "TC" | "TV" | "UG" | "UA" | "AE" | "GB" | "US" | "UM" | "UY" | "UZ" | "VU" | "VE" | "VN" | "VG" | "VI" | "WF" | "EH" | "YE" | "ZM" | "ZW"
22+
23+
declare type Countries = { [key in CountryCodes]: number }
24+
25+
declare interface Subscription {
26+
proxy_type: ProxyType
27+
proxy_count: number
28+
countries: Countries
29+
bandwidth_limit: number
30+
31+
automatic_refresh_frequency: number
32+
ondemand_refreshes_total: number
33+
proxy_replacements_total: number
34+
35+
unlimited_ip_authorizations: boolean
36+
unlimited_burst_threads: boolean
37+
38+
start_date: string
39+
end_date: string
40+
renewals_paid: number
41+
42+
monthly_price: number
43+
yearly_price: number
44+
free_credits: number
45+
46+
subuser_count: number
47+
}
48+
49+
declare type GetSubscription = () => Promise<Subscription>
50+
51+
// Proxy
52+
declare interface ProxyConfig {
53+
countries: Countries
54+
username: string
55+
password: string
56+
authorized_ips: Array<string>
57+
download_links: {
58+
http_password_backbone: string
59+
http_password_direct: string
60+
http_ip_backbone: string
61+
http_ip_direct: string
62+
socks5_password_backbone: string
63+
socks5_password_direct: string
64+
socks5_ip_backbone: string
65+
socks5_ip_direct: string
66+
}
67+
}
68+
69+
declare type GetProxyConfig = () => Promise<ProxyConfig>
70+
declare type SetProxyConfig = (authorized_ips: Array<string>) => Promise<ProxyConfig>
71+
72+
declare interface ProxyPorts {
73+
http: number
74+
socks5: number
75+
}
76+
77+
declare interface ProxyInfo {
78+
username: string
79+
password: string
80+
proxy_address: string
81+
ports: {http:8167, socks5:8168},
82+
valid: boolean
83+
last_verification: string
84+
country_code: CountryCodes,
85+
country_code_confidence: number,
86+
city_name: string
87+
}
88+
89+
declare interface ProxyList {
90+
count: number
91+
next?: string
92+
previous?: string
93+
94+
results: Array<ProxyInfo>
95+
}
96+
97+
declare type StatusCodes = {
98+
[key: string]: number
99+
}
100+
101+
declare interface ProxyStats {
102+
"bandwidth_used": number
103+
"bandwidth_remaining": number
104+
"bandwidth_limit": number
105+
"bandwidth_projected": number
106+
107+
"requests_total": number
108+
"requests_countries": Countries,
109+
"requests_successful": number
110+
"requests_failed": number
111+
"request_failure_status_code": StatusCodes
112+
}

webshare.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import {
2+
Authorization
3+
} from "./types.ts"
4+
5+
/* Rate Limits */
6+
// General: 180/min
7+
// Proxy List: 60/min
8+
// Proxy List Download Links: 20/min
9+
10+
const webshareURL = "https://proxy.webshare.io/api"
11+
12+
export default class Webshare {
13+
private auth!: Authorization
14+
15+
constructor(token: string) {
16+
this.token = `Token ${token}`
17+
}
18+
19+
private get token() {
20+
return this.auth.Authorization
21+
}
22+
23+
private set token(token: string) {
24+
this.auth = {
25+
headers: {
26+
Authorization: token
27+
}
28+
}
29+
}
30+
31+
public async GetUserInfo() {
32+
const response = await fetch(`${webshareURL}/profile/`, this.auth ).catch(e => console.log(`Error fetching UserProfile: ${e}`)) as Response
33+
34+
return <User> await response.json()
35+
}
36+
37+
public async GetSubscriptionInfo() {
38+
const response = await fetch(`${webshareURL}/subscription/`, this.auth).catch(e => console.log(`Error fetching Subscription: ${e}`)) as Response
39+
40+
return <Subscription> await response.json()
41+
}
42+
43+
public async GetProxyConfig() {
44+
const response = await fetch(`${webshareURL}/proxy/config/`, this.auth).catch(e => console.log(`Error fetching ProxyConfig: ${e}`)) as Response
45+
46+
return <ProxyConfig> await response.json()
47+
}
48+
49+
public async SetProxyConfig(authorized_ips: Array<string>) {
50+
const response = await fetch(`${webshareURL}/proxy/config/`, Object.assign({ method: "POST", body: JSON.stringify(authorized_ips) }, this.auth)).catch(e => console.log(`Error setting ProxyConfig: ${e}`)) as Response
51+
52+
return <ProxyConfig> await response.json()
53+
}
54+
55+
public async ResetProxyPassword() {
56+
const response = await fetch(`${webshareURL}/proxy/config/reset_password/`, Object.assign({ method: "POST" }, this.auth)).catch(e => console.log(`Error resetting Proxy password: ${e}`)) as Response
57+
58+
return <ProxyConfig> await response.json()
59+
}
60+
61+
public async GetProxyList(...countries: Array<CountryCodes>) {
62+
const parsedCountries = countries.reduce((acc, cur, index) => acc += `${index >= 1 ? "-" : ""}${cur}`, "")
63+
const proxyList: Array<ProxyInfo> = []
64+
65+
let page = 1
66+
67+
while (true) {
68+
const response = await fetch(`${webshareURL}/proxy/list/?page=${page}${parsedCountries ? `&countries=${parsedCountries}` : ""}`, this.auth).catch(e => console.log(`Error fetching ProxyList: ${e}`)) as Response
69+
const json: ProxyList = await response.json()
70+
71+
proxyList.push(...json.results)
72+
73+
if (!json.next) break
74+
75+
page++
76+
}
77+
78+
return proxyList
79+
}
80+
81+
public async GetProxyStats() {
82+
const response = await fetch(`${webshareURL}/proxy/stats/`, this.auth).catch(e => console.log(`Error fetching ProxyStats: ${e}`)) as Response
83+
84+
return <ProxyStats> await response.json()
85+
}
86+
}

0 commit comments

Comments
 (0)