We only cesu mirror rather than source

This commit is contained in:
Aoran Zeng 2023-09-04 09:01:33 +08:00
parent 9c729ccfba
commit 6b46892825
2 changed files with 84 additions and 193 deletions

228
chsrc.c
View File

@ -61,14 +61,14 @@ does_the_mirror_exist (source_info* sources, size_t size, char* target, char* in
}
bool exist_b = false;
for (int i=1; i<size; i++)
for (int i=0; i<size; i++)
{
source = sources[i];
if (xy_streql(source.mirror->code, input)) {
idx = i;
exist_b = true;
break;
}
source = sources[i];
}
if (!exist_b) {
xy_error (xy_strjoin(3, "chsrc: 镜像站 ", input, " 不存在"));
@ -101,7 +101,7 @@ to_human_readable_speed (double speed)
* @return -1
*/
double
test_speed (char* url)
test_speed (const char* url)
{
// 我们用 —L因为Ruby China源会跳转到其他地方
char* curl_cmd = xy_strjoin(4, "curl -qsL -o ", xy_os_devnull, " -w \"%{http_code} %{speed_download}\" -m8 -A chsrc/" Chsrc_Version
@ -135,19 +135,24 @@ test_speed (char* url)
}
/**
* @param[in] place url后url
*/
#define common_cesu(s) common_cesu_(s##_sources, s##_sources_n)
int
common_cesu (source_info* sources, size_t size, char* place)
common_cesu_ (source_info* sources, size_t size)
{
double speeds[size];
double speed = 0.0;
for (int i=0;i<size;i++)
{
source_info src = sources[i];
const char* baseurl = src.url;
char* testurl = xy_2strjoin(baseurl, place);
double speed = test_speed (testurl);
// const char* baseurl = src.url;
// char* url = xy_2strjoin(baseurl, place);
const char* url = src.mirror->__bigfile_url;
if (NULL==url) {
xy_warn ("chsrc: 跳过该站点");
speed = 0;
} else {
speed = test_speed (url);
}
speeds[i] = speed;
}
int fastidx = dblary_maxidx (speeds, size);
@ -158,21 +163,6 @@ common_cesu (source_info* sources, size_t size, char* place)
/***************************************** 换源 *********************************************/
/* Ruby源 @maintainer ccmywish */
/**
* https://mirrors.bfsu.edu.cn/rubygems/gems/nokogiri-1.15.0-java.gem 大小为9.9MB
*
* @return
*/
int
pl_ruby_cesu (char* option)
{
size_t size = pl_ruby_sources_n;
source_info* sources = pl_ruby_sources;
return common_cesu (sources, size, "gems/nokogiri-1.15.0-java.gem");
}
void
pl_ruby_getsrc (char* option)
{
@ -205,7 +195,7 @@ pl_ruby_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_ruby_sources, pl_ruby_sources_n, "ruby", option);
} else {
index = pl_ruby_cesu ("");
index = common_cesu(pl_ruby);
}
source_info source = pl_ruby_sources[index];
@ -268,18 +258,6 @@ _pl_python_check_cmd (char** prog)
}
/**
* TODO: Python
*/
int
pl_python_cesu (char* option)
{
return common_cesu (pl_python_sources, pl_python_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
void
pl_python_getsrc (char* option)
{
@ -304,7 +282,7 @@ pl_python_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_python_sources, pl_python_sources_n, "python", option);
} else {
index = pl_python_cesu ("");
index = common_cesu (pl_python);
}
source_info source = pl_python_sources[index];
@ -331,15 +309,6 @@ _pl_nodejs_check_cmd ()
}
}
/**
* TODO:
*/
int
pl_nodejs_cesu (char* option)
{
return common_cesu (pl_nodejs_sources, pl_nodejs_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
void
pl_nodejs_getsrc (char* option)
@ -362,7 +331,7 @@ pl_nodejs_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_nodejs_sources, pl_nodejs_sources_n, "nodejs", option);
} else {
index = pl_nodejs_cesu ("");
index = common_cesu (pl_nodejs);
}
source_info source = pl_nodejs_sources[index];
@ -391,15 +360,6 @@ _pl_perl_check_cmd ()
}
}
/**
* TODO:
*/
int
pl_perl_cesu (char* option)
{
return common_cesu (pl_perl_sources, pl_perl_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
@ -422,7 +382,7 @@ pl_perl_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_perl_sources, pl_perl_sources_n, "perl", option);
} else {
index = pl_perl_cesu ("");
index = common_cesu (pl_perl);
}
source_info source = pl_perl_sources[index];
@ -454,17 +414,6 @@ _pl_php_check_cmd()
}
}
/**
* TODO:
*/
int
pl_php_cesu (char* option)
{
xy_warn ("chsrc: Need Help: 烦请PHP用户帮助寻找Packgist.org中较大的包的链接");
return 0;
return common_cesu (pl_php_sources, pl_php_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/**
* Windows上测试通过PHP用户确认
@ -491,7 +440,7 @@ pl_php_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_php_sources, pl_php_sources_n, "php", option);
} else {
index = pl_php_cesu ("");
index = common_cesu (pl_php);
}
source_info source = pl_php_sources[index];
@ -521,15 +470,6 @@ _pl_go_check_cmd ()
}
/**
* TODO:
*/
int
pl_go_cesu (char* option)
{
return common_cesu (pl_go_sources, pl_go_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
@ -553,7 +493,7 @@ pl_go_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_go_sources, pl_go_sources_n, "go", option);
} else {
index = pl_go_cesu ("");
index = common_cesu (pl_go);
}
source_info source = pl_go_sources[index];
@ -569,17 +509,6 @@ pl_go_setsrc (char* option)
/**
* TODO:
*/
int
pl_rust_cesu (char* option)
{
return common_cesu (pl_rust_sources, pl_rust_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_rust_getsrc (char* option)
@ -601,7 +530,7 @@ pl_rust_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_rust_sources, pl_rust_sources_n, "rust", option);
} else {
index = pl_rust_cesu ("");
index = common_cesu (pl_rust);
}
source_info source = pl_rust_sources[index];
@ -627,14 +556,6 @@ pl_rust_setsrc (char* option)
void
pl_dotnet_cesu (char* option)
{
xy_error ("chsrc: 暂时无法为NuGet测速若有需求请您提交issue");
}
void
pl_dotnet_getsrc (char* option)
{
@ -654,17 +575,6 @@ pl_dotnet_setsrc (char* option)
/**
* TODO:
*/
int
pl_java_cesu (char* option)
{
return common_cesu (pl_java_sources, pl_java_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_java_getsrc (char* option)
@ -699,7 +609,7 @@ pl_java_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_java_sources, pl_java_sources_n, "java", option);
} else {
index = pl_java_cesu ("");
index = common_cesu (pl_java);
}
source_info source = pl_java_sources[index];
@ -736,17 +646,6 @@ pl_java_setsrc (char* option)
}
/**
* TODO:
*/
int
pl_r_cesu (char* option)
{
return common_cesu (pl_r_sources, pl_r_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_r_getsrc (char* option)
@ -766,7 +665,7 @@ pl_r_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_r_sources, pl_r_sources_n, "r", option);
} else {
index = pl_r_cesu ("");
index = common_cesu (pl_r);
}
source_info source = pl_r_sources[index];
@ -788,17 +687,6 @@ pl_r_setsrc (char* option)
/**
* TODO:
*/
int
pl_julia_cesu (char* option)
{
return common_cesu (pl_r_sources, pl_r_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_julia_getsrc (char* option)
@ -818,7 +706,7 @@ pl_julia_setsrc (char* option)
if (NULL!=option) {
index = does_the_mirror_exist (pl_julia_sources, pl_julia_sources_n, "julia", option);
} else {
index = pl_julia_cesu ("");
index = common_cesu (pl_julia);
}
source_info source = pl_julia_sources[index];
@ -1053,18 +941,18 @@ os_mysys2_setsrc(char* option)
/************************************** Begin Target Matrix ****************************************/
def_target_info(pl_ruby);
def_target_info(pl_python);
target_info
pl_python_target = {pl_python_setsrc, NULL, NULL, pl_python_sources, 5},
pl_nodejs_target = {pl_nodejs_setsrc, NULL, NULL, pl_nodejs_sources, 2},
pl_perl_target = {pl_perl_setsrc, NULL, NULL, pl_perl_sources, 5},
pl_rust_target = {pl_rust_setsrc, NULL, NULL, pl_rust_sources, 5},
pl_go_target = {pl_go_setsrc, NULL, NULL, pl_go_sources, 3},
pl_dotnet_target = {pl_dotnet_setsrc, NULL, NULL, pl_dotnet_sources, 1},
pl_java_target = {pl_java_setsrc, NULL, NULL, pl_java_sources, 1},
pl_php_target = {pl_php_setsrc, pl_php_getsrc, pl_php_cesu, pl_php_sources, pl_php_sources_n},
pl_r_target = {pl_r_setsrc, NULL, NULL, pl_r_sources, 5},
pl_julia_target = {pl_julia_setsrc, NULL, NULL, pl_julia_sources, 3};
pl_nodejs_target = {pl_nodejs_setsrc, NULL, pl_nodejs_sources, 2},
pl_perl_target = {pl_perl_setsrc, NULL, pl_perl_sources, 5},
pl_rust_target = {pl_rust_setsrc, NULL, pl_rust_sources, 5},
pl_go_target = {pl_go_setsrc, NULL, pl_go_sources, 3},
pl_dotnet_target = {pl_dotnet_setsrc, NULL, pl_dotnet_sources, 1},
pl_java_target = {pl_java_setsrc, NULL, pl_java_sources, 1},
pl_php_target = {pl_php_setsrc, pl_php_getsrc, pl_php_sources, pl_php_sources_n},
pl_r_target = {pl_r_setsrc, NULL, pl_r_sources, 5},
pl_julia_target = {pl_julia_setsrc, NULL, pl_julia_sources, 3};
#define targetinfo(t) (const char const*)t
@ -1089,12 +977,12 @@ static const char const
target_info
os_ubuntu_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7},
os_debian_target = {os_debian_setsrc, NULL, NULL, os_debian_sources, 7},
os_fedora_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7},
os_kali_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7},
os_openbsd_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7},
os_mysys2_target = {os_ubuntu_setsrc, NULL, NULL, os_ubuntu_sources, 7};
os_ubuntu_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_debian_target = {os_debian_setsrc, NULL, os_debian_sources, 7},
os_fedora_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_kali_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_openbsd_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_mysys2_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7};
static const char const
*os_ubuntu [] = {"ubuntu", NULL, targetinfo(&os_ubuntu_target)},
*os_debian [] = {"debian", NULL, targetinfo(&os_debian_target)},
@ -1109,9 +997,9 @@ static const char const
target_info
wr_anaconda_target = {NULL, NULL, NULL, NULL, 0},
wr_emacs_target = {NULL, NULL, NULL, NULL, 0},
wr_tex_target = {NULL, NULL, NULL, NULL, 0};
wr_anaconda_target = {NULL, NULL, NULL, 0},
wr_emacs_target = {NULL, NULL, NULL, 0},
wr_tex_target = {NULL, NULL, NULL, 0};
static const char const
*wr_anaconda[] = {"conda", "anaconda", NULL, targetinfo(&wr_anaconda_target)},
@ -1306,31 +1194,33 @@ get_target (const char* input, int code, char* option)
target_info* target = (target_info*) *target_tmp;
if (Target_Set_Source==code) {
if (Target_Set_Source==code)
{
if (target->setfn) target->setfn(option);
else xy_error (xy_strjoin(3, "chsrc: 暂未对", input, "实现set功能欢迎贡献"));
}
else if (Target_Get_Source==code) {
else if (Target_Get_Source==code)
{
if (target->getfn) target->getfn("");
else xy_error (xy_strjoin(3, "chsrc: 暂未对", input, "实现get功能欢迎贡献"));
}
else if (Target_List_Source==code) {
else if (Target_List_Source==code)
{
xy_info (xy_strjoin(3,"chsrc: 对", input ,"支持以下镜像站,荣耀均归属于这些站点,以及它们的开发/维护者们"));
xy_warn (xy_strjoin(3, "chsrc: 下方code列可用于指定使用某源请使用 chsrc set ", input, " <code>"));
printf ("%-14s%-35s%-45s ", "code", "服务商缩写", "服务源URL"); puts("服务商名称");
puts ("--------------------------------------------------------------------------------------------------------");
print_supported_sources_for_target (target->sources);
}
else if (Target_Cesu_Source==code) {
if (!target->cesufn)
xy_error (xy_strjoin(3, "chsrc: 暂未对", input, "实现cesu功能欢迎贡献"));
else {
char* check_cmd = xy_str_to_quietcmd("curl --version");
bool exist_b = does_the_program_exist (check_cmd, "curl");
if (!exist_b) xy_error ("chsrc: 没有curl命令无法测速");
else target->cesufn("");
return true;
}
else if (Target_Cesu_Source==code)
{
char* check_cmd = xy_str_to_quietcmd("curl --version");
bool exist_b = does_the_program_exist (check_cmd, "curl");
if (!exist_b) xy_error ("chsrc: 没有curl命令无法测速");
// TODO: 通用测速
common_cesu_ (target->sources, target->sources_n);
return true;
}
return true;
}

49
chsrc.h
View File

@ -16,44 +16,46 @@ typedef struct {
const char* abbr;
const char* name;
const char* site;
const char* __bigfile_url; // 用于对该镜像站测速
} mirror_info;
// 教育网
// 我们目前根据 https://github.com/mirrorz-org/oh-my-mirrorz 挑选速度前10位
//
mirror_info
MirrorZ = {"mirrorz", "MirrorZ", "MirrorZ校园网镜像站", "https://mirrors.cernet.edu.cn/"},
Tuna = {"tuna", "TUNA", "清华大学开源软件镜像站", "https://mirrors.tuna.tsinghua.edu.cn/"},
Sjtug_Zhiyuan = {"sjtu", "SJTUG-zhiyuan", "上海交通大学致远镜像站", "https://mirrors.sjtug.sjtu.edu.cn/"},
Zju = {"zju", "ZJU", "浙江大学开源软件镜像站", "https://mirrors.zju.edu.cn/"},
Lzuoss = {"lzu", "LZUOSS", "兰州大学开源社区镜像站", "https://mirror.lzu.edu.cn/"},
Jlu = {"jlu", "JLU", "吉林大学开源镜像站", "https://mirrors.jlu.edu.cn/"},
Bfsu = {"bfsu", "BFSU", "北京外国语大学开源软件镜像站","https://mirrors.bfsu.edu.cn/"},
Pku = {"pku", "PKU", "北京大学开源镜像站", "https://mirrors.pku.edu.cn/"},
Bjtu = {"bjtu", "BJTU", "北京交通大学自由与开源软件镜像站", "https://mirror.bjtu.edu.cn/"},
Sustech = {"sustech", "SUSTech", "南方科技大学开源软件镜像站", "https://mirrors.sustech.edu.cn/"},
Ustc = {"ustc", "USTC", "中国科学技术大学开源镜像站", "https://mirrors.ustc.edu.cn/"},
MirrorZ = {"mirrorz", "MirrorZ", "MirrorZ校园网镜像站", "https://mirrors.cernet.edu.cn/", NULL},
Tuna = {"tuna", "TUNA", "清华大学开源软件镜像站", "https://mirrors.tuna.tsinghua.edu.cn/", NULL},
Sjtug_Zhiyuan = {"sjtu", "SJTUG-zhiyuan", "上海交通大学致远镜像站", "https://mirrors.sjtug.sjtu.edu.cn/", NULL},
Zju = {"zju", "ZJU", "浙江大学开源软件镜像站", "https://mirrors.zju.edu.cn/", NULL},
Lzuoss = {"lzu", "LZUOSS", "兰州大学开源社区镜像站", "https://mirror.lzu.edu.cn/", NULL},
Jlu = {"jlu", "JLU", "吉林大学开源镜像站", "https://mirrors.jlu.edu.cn/", NULL},
Bfsu = {"bfsu", "BFSU", "北京外国语大学开源软件镜像站","https://mirrors.bfsu.edu.cn/", NULL},
Pku = {"pku", "PKU", "北京大学开源镜像站", "https://mirrors.pku.edu.cn/", NULL},
Bjtu = {"bjtu", "BJTU", "北京交通大学自由与开源软件镜像站", "https://mirror.bjtu.edu.cn/", NULL},
Sustech = {"sustech", "SUSTech", "南方科技大学开源软件镜像站", "https://mirrors.sustech.edu.cn/", NULL},
Ustc = {"ustc", "USTC", "中国科学技术大学开源镜像站", "https://mirrors.ustc.edu.cn/", NULL},
// 速度暂时处于10位以后但是目前可用的源
Nju = {"nju", "NJU", "南京大学开源镜像站", "https://mirrors.nju.edu.cn/"},
Cqu = {"cqu", "CQU", "重庆大学开源软件镜像站", "https://mirrors.cqu.edu.cn/"};
Nju = {"nju", "NJU", "南京大学开源镜像站", "https://mirrors.nju.edu.cn/", NULL},
Cqu = {"cqu", "CQU", "重庆大学开源软件镜像站", "https://mirrors.cqu.edu.cn/", NULL};
// 大型公司
// 注意,腾讯软件源中,有很多链接都已失效,请仔细检查
mirror_info
Ali = {"ali", "Ali OPSX", "阿里巴巴开源镜像站", "https://developer.aliyun.com/mirror/"},
Tencent = {"tencent", "Tencent", "腾讯软件源", "https://mirrors.tencent.com/"},
Netease = {"netease", "Netease", "网易开源镜像站", "https://mirrors.163.com/"},
Sohu = {"sohu", "SOHU", "搜狐开源镜像站", "https://mirrors.sohu.com/"};
Ali = {"ali", "Ali OPSX", "阿里巴巴开源镜像站", "https://developer.aliyun.com/mirror/", NULL},
Tencent = {"tencent", "Tencent", "腾讯软件源", "https://mirrors.tencent.com/", NULL},
Netease = {"netease", "Netease", "网易开源镜像站", "https://mirrors.163.com/", NULL},
Sohu = {"sohu", "SOHU", "搜狐开源镜像站", "https://mirrors.sohu.com/", NULL};
// 开源社区
mirror_info
RubyChina = {"rubychina", "RubyChina", "Ruby China 社区", "https://gems.ruby-china.com/"},
GoProxyCN = {"goproxy.cn", "Goproxy.cn", "七牛云 Goproxy.cn", "https://goproxy.cn/"},
GoProxyIO = {"goproxy.io", "GOPROXY.IO", "GOPROXY.IO", "https://goproxy.io/"},
NugetOrg = {"nuget.org", "NuGet Org", "Nuget Organization", "https://www.nuget.org/"};
RubyChina = {"rubychina", "RubyChina", "Ruby China 社区", "https://gems.ruby-china.com/",
"https://gems.ruby-china.com/rubygems/gems/nokogiri-1.15.0-java.gem"}, // 9.9 MB
GoProxyCN = {"goproxy.cn", "Goproxy.cn", "七牛云 Goproxy.cn", "https://goproxy.cn/", NULL},
GoProxyIO = {"goproxy.io", "GOPROXY.IO", "GOPROXY.IO", "https://goproxy.io/", NULL},
NugetOrg = {"nuget.org", "NuGet Org", "Nuget Organization", "https://www.nuget.org/", NULL};
mirror_info*
@ -107,7 +109,7 @@ pl_python_sources[] = {
{&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/pypi/web/simple"},
{&Tuna, "https://pypi.tuna.tsinghua.edu.cn/simple"},
{&Lzuoss, "https://mirror.lzu.edu.cn/pypi/web/simple"},
{&Jlu, "https://mirrors.jlu.edu.cn/pypi//web/simple"},
{&Jlu, "https://mirrors.jlu.edu.cn/pypi/web/simple"},
{&Bfsu, "https://mirrors.bfsu.edu.cn/pypi/web/simple"}
},
@ -379,9 +381,8 @@ def_target_sources_n(os_kali); def_target_sources_n(os_openbsd); def_target_so
typedef struct {
void (*setfn)(char* option);
void (*getfn)(char* option);
int (*cesufn)(char* option);
source_info* sources;
size_t sources_n;
} target_info;
#define def_target_info(t) target_info t##_target = {t##_setsrc, t##_getsrc, t##_cesu, t##_sources, t##_sources_n}
#define def_target_info(t) target_info t##_target = {t##_setsrc, t##_getsrc, t##_sources, t##_sources_n}