Browse Source

Merge branch 'dev'

dev
Dnomd343 2 years ago
parent
commit
e446344e92
  1. 52
      assets/china-ip.py
  2. 5
      assets/chinalist.py
  3. 5
      assets/gfwlist.py
  4. 2
      src/to-json/src/ffi.rs
  5. 9
      src/to-json/src/json.rs
  6. 2
      src/to-json/src/lib.rs
  7. 2
      src/to-json/src/tests.rs

52
assets/china-ip.py

@ -4,49 +4,27 @@
import os import os
from IPy import IP, IPSet from IPy import IP, IPSet
operators = ['china', 'cmcc', 'chinanet', 'unicom', 'tietong', 'cernet', 'cstnet', 'drpeng', 'googlecn']
operators += ['%s6' % x for x in operators] # add `...6` suffix
source = [ source = [
'curl -sL https://github.com/misakaio/chnroutes2/raw/master/chnroutes.txt | sed \'/^#/d\'', 'curl -sL https://github.com/misakaio/chnroutes2/raw/master/chnroutes.txt | sed \'/^#/d\'',
'curl -sL https://github.com/metowolf/iplist/raw/master/data/special/china.txt', 'curl -sL https://github.com/metowolf/iplist/raw/master/data/special/china.txt',
'curl -sL https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt', 'curl -sL https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/cernet.txt', ] + ['curl -sL https://gaoyifan.github.io/china-operator-ip/%s.txt' % x for x in operators]
'curl -sL https://gaoyifan.github.io/china-operator-ip/china.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/chinanet.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/cmcc.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/cstnet.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/drpeng.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/googlecn.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/tietong.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/unicom.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/cernet6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/china6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/chinanet6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/cmcc6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/cstnet6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/drpeng6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/googlecn6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/tietong6.txt',
'curl -sL https://gaoyifan.github.io/china-operator-ip/unicom6.txt',
]
ips = set() ipv4 = IPSet()
for script in source: ipv6 = IPSet()
ipAddrs = set()
for script in source: # traverse fetch commands
raw = os.popen(script).read().split('\n') raw = os.popen(script).read().split('\n')
ips.update(filter(None, raw)) ipAddrs.update(filter(None, raw))
for ipAddr in ipAddrs:
v4 = IPSet()
v6 = IPSet()
for ip in ips:
try: try:
ipAddr = IP(ip) ip = IP(ipAddr) # ip format check
(v4 if ipAddr.version() == 4 else v6).add(ipAddr) (ipv4 if ip.version() == 4 else ipv6).add(ip)
except: except: pass
pass
ips = []
for ip in v4:
ips.append(str(ip) + ('' if '/' in str(ip) else '/32'))
for ip in v6:
ips.append(str(ip) + ('' if '/' in str(ip) else '/128'))
release = [('%s' if '/' in str(ip) else '%s/32') % str(ip) for ip in ipv4] # format into CIDR
release += [('%s' if '/' in str(ip) else '%s/128') % str(ip) for ip in ipv6]
with open('china-ip.txt', 'w') as fileObj: with open('china-ip.txt', 'w') as fileObj:
fileObj.write('\n'.join(ips) + '\n') fileObj.write('\n'.join(release) + '\n')

5
assets/chinalist.py

@ -14,11 +14,10 @@ source = [
] ]
domains = set() domains = set()
for script in source: for script in source: # traverse fetch commands
raw = os.popen(script).read().split('\n') raw = os.popen(script).read().split('\n')
domains.update(filter(None, raw)) domains.update(filter(None, raw))
regex = r'^(?=^.{3,255}$)[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}(.[a-zA-Z0-9][a-zA-Z0-9\-]{0,62})+$' regex = r'^(?=^.{3,255}$)[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}(.[a-zA-Z0-9][a-zA-Z0-9\-]{0,62})+$'
domains = {x for x in domains if re.search(regex, str(x)) is not None} domains = {x for x in domains if re.search(regex, str(x)) is not None} # filter invalid domains
with open('chinalist.txt', 'w') as fileObj: with open('chinalist.txt', 'w') as fileObj:
fileObj.write('\n'.join(sorted(domains)) + '\n') fileObj.write('\n'.join(sorted(domains)) + '\n')

5
assets/gfwlist.py

@ -19,11 +19,10 @@ source = [
] ]
domains = set() domains = set()
for script in source: for script in source: # traverse fetch commands
raw = os.popen(script).read().split('\n') raw = os.popen(script).read().split('\n')
domains.update(filter(None, raw)) domains.update(filter(None, raw))
regex = r'^(?=^.{3,255}$)[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}(.[a-zA-Z0-9][a-zA-Z0-9\-]{0,62})+$' regex = r'^(?=^.{3,255}$)[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}(.[a-zA-Z0-9][a-zA-Z0-9\-]{0,62})+$'
domains = {x for x in domains if re.search(regex, str(x)) is not None} domains = {x for x in domains if re.search(regex, str(x)) is not None} # filter invalid domains
with open('gfwlist.txt', 'w') as fileObj: with open('gfwlist.txt', 'w') as fileObj:
fileObj.write('\n'.join(sorted(domains)) + '\n') fileObj.write('\n'.join(sorted(domains)) + '\n')

2
src/to-json/src/ffi.rs

@ -1,4 +1,4 @@
use crate::convert::to_json; use crate::json::to_json;
use std::ffi::{c_char, CStr, CString}; use std::ffi::{c_char, CStr, CString};
fn to_c_string(string: String) -> *const c_char { // fetch c-style ptr of string fn to_c_string(string: String) -> *const c_char { // fetch c-style ptr of string

9
src/to-json/src/convert.rs → src/to-json/src/json.rs

@ -1,10 +1,11 @@
use serde_json as json;
use crate::parser::{parser, Value}; use crate::parser::{parser, Value};
fn json_convert(content: &str) -> Result<String, String> { // convert to JSON format fn json_convert(content: &str) -> Result<String, String> { // convert to JSON format
let data = match parser(content)? { let data = match parser(content)? {
Value::JSON(json) => serde_json::to_string(&json), Value::JSON(_json) => json::to_string(&_json),
Value::YAML(yaml) => serde_json::to_string(&yaml), Value::YAML(_yaml) => json::to_string(&_yaml),
Value::TOML(toml) => serde_json::to_string(&toml), Value::TOML(_toml) => json::to_string(&_toml),
}; };
match data { match data {
Ok(data) => Ok(data), Ok(data) => Ok(data),
@ -15,6 +16,6 @@ fn json_convert(content: &str) -> Result<String, String> { // convert to JSON fo
pub fn to_json(content: &str) -> Option<String> { // to JSON string pub fn to_json(content: &str) -> Option<String> { // to JSON string
match json_convert(content) { match json_convert(content) {
Ok(data) => Some(data), Ok(data) => Some(data),
Err(_) => None, Err(_) => None, // convert failed
} }
} }

2
src/to-json/src/lib.rs

@ -1,4 +1,4 @@
mod ffi; mod ffi;
mod json;
mod tests; mod tests;
mod parser; mod parser;
mod convert;

2
src/to-json/src/tests.rs

@ -1,4 +1,4 @@
use crate::convert::to_json; use crate::json::to_json;
#[allow(dead_code)] #[allow(dead_code)]
const JSON_TEST_CONTENT: &str = r#" const JSON_TEST_CONTENT: &str = r#"

Loading…
Cancel
Save