Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
srvpro2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
srvpro2
Commits
55af5b5a
Commit
55af5b5a
authored
Feb 12, 2026
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
buggy
parent
16dfabaa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
161 additions
and
0 deletions
+161
-0
src/services/chnroute.ts
src/services/chnroute.ts
+137
-0
src/services/http-client.ts
src/services/http-client.ts
+11
-0
src/utility/use-proxy.ts
src/utility/use-proxy.ts
+13
-0
No files found.
src/services/chnroute.ts
0 → 100644
View file @
55af5b5a
import
{
Context
}
from
'
../app
'
;
import
*
as
ipaddr
from
'
ipaddr.js
'
;
export
class
Chnroute
{
private
logger
=
this
.
ctx
.
createLogger
(
'
Chnroute
'
);
private
misakaUrl
=
'
https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt
'
;
private
chinaIpRanges
:
[
ipaddr
.
IPv4
,
number
][]
=
[];
private
isUpdating
=
false
;
private
retryCount
=
0
;
constructor
(
private
ctx
:
Context
)
{}
private
async
updateChnroute
()
{
if
(
this
.
isUpdating
)
return
;
this
.
isUpdating
=
true
;
try
{
const
{
data
}
=
await
this
.
ctx
.
http
.
get
<
string
>
(
this
.
misakaUrl
,
{
responseType
:
'
text
'
,
timeout
:
3000
,
// 3 seconds timeout
});
const
ranges
=
data
.
trim
()
.
split
(
'
\n
'
)
.
map
((
line
)
=>
line
.
trim
())
.
filter
((
line
)
=>
line
&&
!
line
.
startsWith
(
'
#
'
));
this
.
chinaIpRanges
=
ranges
.
map
(
(
range
)
=>
ipaddr
.
parseCIDR
(
range
)
as
[
ipaddr
.
IPv4
,
number
],
);
this
.
logger
.
info
(
{
count
:
this
.
chinaIpRanges
.
length
},
'
Update chnroute success
'
,
);
this
.
retryCount
=
0
;
this
.
isUpdating
=
false
;
}
catch
(
e
:
any
)
{
this
.
logger
.
warn
({
err
:
e
.
message
},
'
Update chnroute failed
'
);
this
.
isUpdating
=
false
;
// Exponential backoff: 1s, 2s, 4s, 8s, 16s, 32s, 64s, max 64s
const
delay
=
Math
.
min
(
Math
.
pow
(
2
,
this
.
retryCount
)
*
1000
,
64000
);
this
.
retryCount
++
;
this
.
logger
.
info
(
{
retryCount
:
this
.
retryCount
,
delayMs
:
delay
},
'
Scheduling chnroute retry
'
,
);
setTimeout
(()
=>
{
this
.
updateChnroute
();
},
delay
);
}
}
/**
* Check if an IP is in China mainland (excluding HK, MO, TW)
*/
isChina
(
ip
:
string
):
boolean
{
if
(
!
this
.
chinaIpRanges
.
length
)
{
return
false
;
}
try
{
// Remove IPv6 prefix if present
if
(
ip
.
startsWith
(
'
::ffff:
'
))
{
ip
=
ip
.
slice
(
7
);
}
const
addr
=
ipaddr
.
parse
(
ip
);
// Only check IPv4 addresses
if
(
addr
.
kind
()
!==
'
ipv4
'
)
{
return
false
;
}
// Check if IP matches any China IP range
return
this
.
chinaIpRanges
.
some
(([
range
,
mask
])
=>
{
return
(
addr
as
ipaddr
.
IPv4
).
match
(
range
,
mask
);
});
}
catch
{
return
false
;
}
}
/**
* Check if an IP is private/local
*/
isPrivate
(
ip
:
string
):
boolean
{
try
{
// Remove IPv6 prefix if present
if
(
ip
.
startsWith
(
'
::ffff:
'
))
{
ip
=
ip
.
slice
(
7
);
}
const
addr
=
ipaddr
.
parse
(
ip
);
// Check for loopback
if
(
addr
.
toString
()
===
'
::1
'
||
addr
.
toString
()
===
'
127.0.0.1
'
)
{
return
true
;
}
// Check if it's in private range
const
range
=
addr
.
range
();
return
range
===
'
private
'
||
range
===
'
loopback
'
;
}
catch
{
return
false
;
}
}
/**
* Get locale based on IP address
* Returns 'zh-CN' for China mainland IPs and private IPs
* Returns 'en-US' for all other IPs
*/
getLocale
(
ip
:
string
):
'
zh-CN
'
|
'
en-US
'
{
// Private/local IPs are considered zh-CN
if
(
this
.
isPrivate
(
ip
))
{
return
'
zh-CN
'
;
}
// China mainland IPs are zh-CN
if
(
this
.
isChina
(
ip
))
{
return
'
zh-CN
'
;
}
// All other IPs are en-US
return
'
en-US
'
;
}
async
init
()
{
this
.
updateChnroute
().
then
();
}
}
src/services/http-client.ts
0 → 100644
View file @
55af5b5a
import
axios
from
'
axios
'
;
import
{
useProxy
}
from
'
../utility/use-proxy
'
;
import
{
AppContext
}
from
'
nfkit
'
;
import
{
ConfigService
}
from
'
./config
'
;
export
class
HttpClient
{
constructor
(
private
ctx
:
AppContext
)
{}
http
=
axios
.
create
({
...
useProxy
(
this
.
ctx
.
get
(
ConfigService
).
getConfig
(
'
USE_PROXY
'
,
''
)),
});
}
src/utility/use-proxy.ts
0 → 100644
View file @
55af5b5a
import
{
HttpsProxyAgent
}
from
'
https-proxy-agent
'
;
import
{
AxiosRequestConfig
}
from
'
axios
'
;
import
{
HttpProxyAgent
}
from
'
http-proxy-agent
'
;
export
function
useProxy
(
proxy
:
string
):
AxiosRequestConfig
{
if
(
!
proxy
)
{
return
{};
}
return
{
httpAgent
:
new
HttpProxyAgent
(
proxy
),
httpsAgent
:
new
HttpsProxyAgent
(
proxy
),
};
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment